Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix await identifiers in property names #249

Merged
merged 3 commits into from May 14, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

fixes

  • Loading branch information...
Protryon committed May 2, 2019
commit e1386e0a3ac0fb3f8306574b36c78d5365399e8c
@@ -2204,12 +2204,24 @@ protected BindingBindingWithDefault targetToBindingPossiblyWithDefault(@Nonnull
if (propName instanceof StaticPropertyName) {
StaticPropertyName staticPropertyName = (StaticPropertyName) propName;
if (this.eat(TokenType.ASSIGN)) {
if (this.allowYieldExpression && token.type == TokenType.YIELD) {
throw this.createError(ErrorMessages.INVALID_TOKEN_CONTEXT, "yield");
}
if (this.allowAwaitExpression && token.type == TokenType.AWAIT) {
throw this.createError(ErrorMessages.INVALID_TOKEN_CONTEXT, "await");
}
Expression init = this.isolateCoverGrammar(this::parseAssignmentExpression).left().fromJust();
this.firstExprError = this.createErrorWithLocation(startLocation, ErrorMessages.ILLEGAL_PROPERTY);
AssignmentTargetPropertyIdentifier toReturn = new AssignmentTargetPropertyIdentifier(this.transformDestructuring(staticPropertyName), Maybe.of(init));
return Either.right(this.finishNode(startState, toReturn));
}
if (!this.match(TokenType.COLON)) {
if (this.allowYieldExpression && token.type == TokenType.YIELD) {
throw this.createError(ErrorMessages.INVALID_TOKEN_CONTEXT, "yield");
}
if (this.allowAwaitExpression && token.type == TokenType.AWAIT) {
throw this.createError(ErrorMessages.INVALID_TOKEN_CONTEXT, "await");
}
if (token.type != TokenType.IDENTIFIER && token.type != TokenType.YIELD && token.type != TokenType.LET && token.type != TokenType.ASYNC && token.type != TokenType.AWAIT) {
throw this.createUnexpected(token);
}
@@ -2311,8 +2323,7 @@ protected BindingBindingWithDefault targetToBindingPossiblyWithDefault(@Nonnull
if (isGenerator && this.match(TokenType.COLON)) {
throw this.createUnexpected(this.lookahead);
}

return Either.left(name);
return Either.left(name);
}

protected boolean lookaheadPropertyName() {
@@ -2366,12 +2377,6 @@ protected boolean lookaheadPropertyName() {
@Nonnull
protected String parseIdentifierName() throws JsError {
if (isIdentifierName(this.lookahead.type.klass)) {
if (this.allowYieldExpression && this.match(TokenType.YIELD)) {
throw this.createError(ErrorMessages.INVALID_TOKEN_CONTEXT, "yield");
}
if (this.allowAwaitExpression && this.match(TokenType.AWAIT)) {
throw this.createError(ErrorMessages.INVALID_TOKEN_CONTEXT, "await");
}
return this.lex().toString();
} else {
throw this.createUnexpected(this.lookahead);
@@ -89,7 +89,7 @@ public void testGeneratorDeclarationTest() throws JsError {

testScriptFailure("label: function* a(){}", 15, "Unexpected token \"*\"");
testScriptFailure("function*g(yield){}", 11, "\"yield\" may not be used as an identifier in this context");
testScriptFailure("function*g({yield}){}", 12, "\"yield\" may not be used as an identifier in this context");
testScriptFailure("function*g({yield}){}", 17, "Unexpected token \"yield\"");
testScriptFailure("function*g([yield]){}", 12, "\"yield\" may not be used as an identifier in this context");
testScriptFailure("function*g({a: yield}){}", 15, "\"yield\" may not be used as an identifier in this context");
testScriptFailure("function*g(yield = 0){}", 11, "\"yield\" may not be used as an identifier in this context");
@@ -183,6 +183,6 @@ public void testObjectExpression() throws JsError {
new BindingIdentifier("b"), Maybe.of(new LiteralNumericExpression(0.0))))))), Maybe.empty()),
new FunctionBody(ImmutableList.empty(), ImmutableList.empty())))));

testScriptFailure("(function* (){return {yield = 5} = {}})", 22, "\"yield\" may not be used as an identifier in this context");
testScriptFailure("(function* (){return {yield = 5} = {}})", 30, "\"yield\" may not be used as an identifier in this context");
}
}
@@ -30,6 +30,6 @@ public void testYieldGeneratorExpression() throws JsError {

testScriptFailure("function *a(){yield\n*a}", 2, 0, 20, "Unexpected token \"*\"");
testScriptFailure("function *a(){yield*}", 20, "Unexpected token \"}\"");
testScriptFailure("function *a(){({yield} = {})}", 16, "\"yield\" may not be used as an identifier in this context");
testScriptFailure("function *a(){({yield} = {})}", 21, "\"yield\" may not be used as an identifier in this context");
}
}
@@ -132,8 +132,8 @@ public void testAsyncFailure() {
testScriptFailure("async (a = aw\\u{61}it => {}) => {}", 11, "Async arrow parameters may not contain \"await\"");
testScriptFailure("async (a = (b = await (0)) => {}) => {}", 16, "Async arrow parameters may not contain \"await\"");
testScriptFailure("async ({await}) => 1", 13,"Async arrow parameters may not contain \"await\"");
This conversation was marked as resolved by bakkot

This comment has been minimized.

Copy link
@bakkot

bakkot May 3, 2019

Contributor

Should add similar tests for yield, since this PR is adding those cases.

This comment has been minimized.

Copy link
@bakkot

bakkot May 3, 2019

Contributor

I guess the only missing ones are function *a({yield}){} and function *a({yield = 0}){}.

testScriptFailure("async function x({await}) { return 1 }", 18,"\"await\" may not be used as an identifier in this context");
testScriptFailure("async function f() { return {await}; }", 29,"\"await\" may not be used as an identifier in this context");
testScriptFailure("async function f() { return {await = 0} = {}; }", 29,"\"await\" may not be used as an identifier in this context");
testScriptFailure("async function x({await}) { return 1 }", 23,"Unexpected token \"}\"");
testScriptFailure("async function f() { return {await}; }", 34,"\"await\" may not be used as an identifier in this context");
testScriptFailure("async function f() { return {await = 0} = {}; }", 37,"\"await\" may not be used as an identifier in this context");
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.