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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"prettier"
],
"rules": {
"prettier/prettier": ["error", { "trailingComma": "all" }]
"prettier/prettier": ["error", { "trailingComma": "all" }],
"no-case-declarations": "error"
},
"env": {
"node": true
Expand Down
84 changes: 52 additions & 32 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,7 @@ export default class ExpressionParser extends LValParser {
noCalls,
);
} else if (this.match(tt.questionDot)) {
if (!this.hasPlugin("optionalChaining")) {
this.raise(
startPos,
"You can only use optional-chaining when the 'optionalChaining' plugin is enabled.",
);
}
this.expectPlugin("optionalChaining");

if (noCalls && this.lookahead().type == tt.parenL) {
state.stop = true;
Expand Down Expand Up @@ -660,11 +655,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?

}

if (!this.hasPlugin("dynamicImport")) this.unexpected();
this.expectPlugin("dynamicImport");

node = this.startNode();
this.next();
Expand All @@ -681,7 +676,7 @@ export default class ExpressionParser extends LValParser {
case tt._yield:
if (this.state.inGenerator) this.unexpected();

case tt.name:
case tt.name: {
node = this.startNode();
const allowAwait = this.state.value === "await" && this.state.inAsync;
const allowYield = this.shouldAllowYieldIdentifier();
Expand Down Expand Up @@ -710,27 +705,29 @@ export default class ExpressionParser extends LValParser {
}

return id;
}

case tt._do:
if (this.hasPlugin("doExpressions")) {
const node = this.startNode();
this.next();
const oldInFunction = this.state.inFunction;
const oldLabels = this.state.labels;
this.state.labels = [];
this.state.inFunction = false;
node.body = this.parseBlock(false);
this.state.inFunction = oldInFunction;
this.state.labels = oldLabels;
return this.finishNode(node, "DoExpression");
}
case tt._do: {
this.expectPlugin("doExpressions");
const node = this.startNode();
this.next();
const oldInFunction = this.state.inFunction;
const oldLabels = this.state.labels;
this.state.labels = [];
this.state.inFunction = false;
node.body = this.parseBlock(false);
this.state.inFunction = oldInFunction;
this.state.labels = oldLabels;
return this.finishNode(node, "DoExpression");
}

case tt.regexp:
case tt.regexp: {
const value = this.state.value;
node = this.parseLiteral(value.value, "RegExpLiteral");
node.pattern = value.pattern;
node.flags = value.flags;
return node;
}

case tt.num:
return this.parseLiteral(this.state.value, "NumericLiteral");
Expand Down Expand Up @@ -784,7 +781,7 @@ export default class ExpressionParser extends LValParser {
case tt.backQuote:
return this.parseTemplate(false);

case tt.doubleColon:
case tt.doubleColon: {
node = this.startNode();
this.next();
node.object = null;
Expand All @@ -797,6 +794,7 @@ export default class ExpressionParser extends LValParser {
"Binding should be performed on object property.",
);
}
}

default:
throw this.unexpected();
Expand Down Expand Up @@ -825,11 +823,7 @@ export default class ExpressionParser extends LValParser {
parseFunctionExpression(): N.FunctionExpression | N.MetaProperty {
const node = this.startNode();
const meta = this.parseIdentifier(true);
if (
this.state.inGenerator &&
this.hasPlugin("functionSent") &&
this.eat(tt.dot)
) {
if (this.state.inGenerator && this.eat(tt.dot)) {
return this.parseMetaProperty(node, meta, "sent");
}
return this.parseFunction(node, false);
Expand All @@ -841,6 +835,16 @@ export default class ExpressionParser extends LValParser {
propertyName: string,
): N.MetaProperty {
node.meta = meta;

if (meta.name === "function" && propertyName === "sent") {
if (this.isContextual(propertyName)) {
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.

}
}

node.property = this.parseIdentifier(true);

if (node.property.name !== propertyName) {
Expand All @@ -857,6 +861,18 @@ export default class ExpressionParser extends LValParser {
const node = this.startNode();
const id = this.parseIdentifier(true);
this.expect(tt.dot);

if (id.name === "import") {
if (this.isContextual("meta")) {
this.expectPlugin("importMeta");
} else if (!this.hasPlugin("importMeta")) {
this.raise(
id.start,
`Dynamic imports require a parameter: import('a.js').then`,
);
}
}

if (!this.inModule) {
this.raise(
id.start,
Expand Down Expand Up @@ -1141,7 +1157,8 @@ export default class ExpressionParser extends LValParser {
decorators = [];
}

if (this.hasPlugin("objectRestSpread") && this.match(tt.ellipsis)) {
if (this.match(tt.ellipsis)) {
this.expectPlugin("objectRestSpread");
prop = this.parseSpread(isPattern ? { start: 0 } : undefined);
if (isPattern) {
this.toAssignable(prop, true, "object pattern");
Expand Down Expand Up @@ -1199,8 +1216,11 @@ export default class ExpressionParser extends LValParser {
prop.computed = false;
} else {
isAsync = true;
if (this.hasPlugin("asyncGenerators"))
isGenerator = this.eat(tt.star);
if (this.match(tt.star)) {
this.expectPlugin("asyncGenerators");
this.next();
isGenerator = true;
}
this.parsePropertyName(prop);
}
} else {
Expand Down
9 changes: 8 additions & 1 deletion src/parser/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import CommentsParser from "./comments";
// message.

export default class LocationParser extends CommentsParser {
raise(pos: number, message: string): empty {
raise(
pos: number,
message: string,
missingPluginNames?: Array<string>,
): empty {
const loc = getLineInfo(this.input, pos);
message += ` (${loc.line}:${loc.column})`;
// $FlowIgnore
Expand All @@ -19,6 +23,9 @@ export default class LocationParser extends CommentsParser {
);
err.pos = pos;
err.loc = loc;
if (missingPluginNames) {
err.missingPlugin = missingPluginNames;
}
throw err;
}
}
6 changes: 4 additions & 2 deletions src/parser/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ export default class LValParser extends NodeUtils {
this.toAssignable(node.value, isBinding, contextDescription);
break;

case "SpreadElement":
case "SpreadElement": {
this.checkToRestConversion(node);

node.type = "RestElement";
const arg = node.argument;
this.toAssignable(arg, isBinding, contextDescription);
break;
}

case "ArrayExpression":
node.type = "ArrayPattern";
Expand Down Expand Up @@ -211,11 +212,12 @@ export default class LValParser extends NodeUtils {
case tt.name:
return this.parseBindingIdentifier();

case tt.bracketL:
case tt.bracketL: {
const node = this.startNode();
this.next();
node.elements = this.parseBindingList(tt.bracketR, true);
return this.finishNode(node, "ArrayPattern");
}

case tt.braceL:
return this.parseObj(true);
Expand Down
44 changes: 21 additions & 23 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ export default class StatementParser extends ExpressionParser {
}

parseDecorator(): N.Decorator {
if (!(this.hasPlugin("decorators") || this.hasPlugin("decorators2"))) {
this.unexpected();
}
this.expectOnePlugin(["decorators", "decorators2"]);

const node = this.startNode();
this.next();
Expand Down Expand Up @@ -340,11 +338,8 @@ export default class StatementParser extends ExpressionParser {
this.state.labels.push(loopLabel);

let forAwait = false;
if (
this.hasPlugin("asyncGenerators") &&
this.state.inAsync &&
this.isContextual("await")
) {
if (this.state.inAsync && this.isContextual("await")) {
this.expectPlugin("asyncGenerators");
forAwait = true;
this.next();
}
Expand Down Expand Up @@ -489,12 +484,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?

clause.param = this.parseBindingAtom();
this.checkLVal(clause.param, true, Object.create(null), "catch clause");
this.expect(tt.parenR);
} else {
this.expectPlugin("optionalCatchBinding");
clause.param = null;
}
clause.body = this.parseBlock();
Expand Down Expand Up @@ -782,12 +778,11 @@ export default class StatementParser extends ExpressionParser {
this.initFunction(node, isAsync);

if (this.match(tt.star)) {
if (node.async && !this.hasPlugin("asyncGenerators")) {
this.unexpected();
} else {
node.generator = true;
this.next();
if (node.async) {
this.expectPlugin("asyncGenerators");
}
node.generator = true;
this.next();
}

if (
Expand Down Expand Up @@ -960,8 +955,9 @@ export default class StatementParser extends ExpressionParser {
isStatic = true;
}

if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) {
if (this.match(tt.hash)) {
// Private property
this.expectPlugin("classPrivateProperties");
this.next();
const privateProp: N.ClassPrivateProperty = memberAny;
privateProp.key = this.parseIdentifier(true);
Expand Down Expand Up @@ -1047,8 +1043,12 @@ export default class StatementParser extends ExpressionParser {
this.pushClassProperty(classBody, prop);
} else if (isSimple && key.name === "async" && !this.isLineTerminator()) {
// an async method
const isGenerator =
this.hasPlugin("asyncGenerators") && this.eat(tt.star);
let isGenerator = false;
if (this.match(tt.star)) {
this.expectPlugin("asyncGenerators");
this.next();
isGenerator = true;
}
method.kind = "method";
this.parsePropertyName(method);
if (this.isNonstaticConstructor(method)) {
Expand Down Expand Up @@ -1151,24 +1151,22 @@ export default class StatementParser extends ExpressionParser {
}

parseClassProperty(node: N.ClassProperty): N.ClassProperty {
const noPluginMsg =
"You can only use Class Properties when the 'classProperties' plugin is enabled.";
if (!node.typeAnnotation && !this.hasPlugin("classProperties")) {
this.raise(node.start, noPluginMsg);
if (!node.typeAnnotation) {
this.expectPlugin("classProperties");
}

this.state.inClassProperty = true;

if (this.match(tt.eq)) {
if (!this.hasPlugin("classProperties"))
this.raise(this.state.start, noPluginMsg);
this.expectPlugin("classProperties");
this.next();
node.value = this.parseMaybeAssign();
} else {
node.value = null;
}
this.semicolon();
this.state.inClassProperty = false;

return this.finishNode(node, "ClassProperty");
}

Expand Down
22 changes: 22 additions & 0 deletions src/parser/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,26 @@ export default class UtilParser extends Tokenizer {
}
throw this.raise(pos != null ? pos : this.state.start, messageOrType);
}

expectPlugin(name: string): void {
if (!this.hasPlugin(name)) {
throw this.raise(
this.state.start,
`This experimental syntax requires enabling the parser plugin: '${name}'`,
[name],
);
}
}

expectOnePlugin(names: Array<string>): void {
if (!names.some(n => this.hasPlugin(n))) {
throw this.raise(
this.state.start,
`This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(
", ",
)}'`,
names,
);
}
}
}
Loading