Skip to content
This repository was archived by the owner on May 19, 2018. It is now read-only.

Syntax Error: add message with the plugin that should be enabled #658

Merged
merged 7 commits into from
Aug 28, 2017
Merged

Conversation

hzoo
Copy link
Member

@hzoo hzoo commented Aug 3, 2017

Fixes #169
Closes #178
Rebased/rewrite of #178

Add a new method: this.expectPlugin instead of this.hasPlugin which throws if the plugin isn't enabled.

screen shot 2017-08-03 at 5 00 49 pm

This also means something better than "unexpected token" when it's a config issue. This also isn't tied to Babel but just babylon's plugins, so other tools can catch the error like Babel will to produce their own specific error messages.

screen shot 2017-08-03 at 5 01 51 pm

  • non-block: make sure this covers all the relevant uses of hasPlugin (can be in later PRs)
    • bigInt, classPrivateProperties, exportExtensions, numericSeparator
  • document the need to write a test without a plugin to get test coverage for each plugin added in the future

@@ -491,12 +486,13 @@ export default class StatementParser extends ExpressionParser {
if (this.match(tt._catch)) {
const clause = this.startNode();
this.next();
if (this.match(tt.parenL) || !this.hasPlugin("optionalCatchBinding")) {
if (this.match(tt.parenL)) {
this.expect(tt.parenL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this became redundant now?

@@ -627,11 +622,11 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "Super");

case tt._import:
if (this.hasPlugin("importMeta") && this.lookahead().type === tt.dot) {
if (this.lookahead().type === tt.dot) {
return this.parseImportMetaProperty();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you drop a this.expectPlugin("importMeta") here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's called by the parseImportMetaProperty function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this problem was brought up by @motiz88, @danez in https://github.com/babel/babylon/pull/178/files#r87556780

If we make the error here I guess it's a different error if they just have import. vs import.asdf but maybe we shouldn't make a big deal out of it?

Copy link
Member

@danez danez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besides the if(true) {} looks good

@@ -679,7 +674,9 @@ export default class ExpressionParser extends LValParser {
return id;

case tt._do:
if (this.hasPlugin("doExpressions")) {
// TODO
if (true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this ? :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a bug somewhere if I don't have it if you want to look into that 😄

Copy link
Member

@Jessidhia Jessidhia Aug 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this if necessary at all? The _do context can only be legally entered if the plugin is enabled to begin with.

You do need a block here because you have lexical declarations inside the case, but it should just be regular braces.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it's not yeah I just need a block if that works

this.expectPlugin("functionSent");
} else if (!this.hasPlugin("functionSent")) {
// They didn't actually say `function.sent`, just `function.`, so a simple error would be less confusing.
this.unexpected();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncovered. I add a test.

@danez danez force-pushed the expect branch 2 times, most recently from e0d3d59 to 5f58d89 Compare August 18, 2017 23:44
@danez
Copy link
Member

danez commented Aug 19, 2017

I enabled the no-case-declarations. This should prevent this problems.

@danez
Copy link
Member

danez commented Aug 19, 2017

Okay I also rebased the branch.

}

this.state.inClassProperty = true;

if (this.match(tt.eq)) {
if (!this.hasPlugin("classProperties"))
this.raise(this.state.start, noPluginMsg);
this.expectOnePlugin(["classProperties"]);
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo Aug 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can use exceptPlugin

Also here

if (!node.typeAnnotation && !this.hasPlugin("classProperties")) {
this.raise(node.start, noPluginMsg);
if (!node.typeAnnotation) {
this.expectOnePlugin(["classProperties"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can use exceptPlugin

@@ -0,0 +1,4 @@
{
"throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'classProperties, typescript' (2:6)",
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo Aug 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this affected by #666?

Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@xtuc xtuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 👍

Just a question about the name expectPlugin sounds like it's part of the lookahead process. What about assertPlugin? shouldHavePlugin? checkPlugin?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants