diff --git a/src/com/google/javascript/jscomp/parsing/parser/Parser.java b/src/com/google/javascript/jscomp/parsing/parser/Parser.java index 90d60d9c1d2..9a62f7785ea 100644 --- a/src/com/google/javascript/jscomp/parsing/parser/Parser.java +++ b/src/com/google/javascript/jscomp/parsing/parser/Parser.java @@ -2633,14 +2633,24 @@ private ParseTree parseCoverParenthesizedExpressionAndArrowParameterList() { // Case ( ) if (peek(TokenType.CLOSE_PAREN)) { eat(TokenType.CLOSE_PAREN); - return new FormalParameterListTree(getTreeLocation(start), ImmutableList.of()); + if (peek(TokenType.ARROW)) { + return new FormalParameterListTree(getTreeLocation(start), ImmutableList.of()); + } else { + reportError("invalid parenthesized expression"); + return new MissingPrimaryExpressionTree(getTreeLocation(start)); + } } // Case ( ... BindingIdentifier ) if (peek(TokenType.SPREAD)) { - ParseTree result = new FormalParameterListTree( - getTreeLocation(start), ImmutableList.of(parseParameter(ParamContext.IMPLEMENTATION))); + ImmutableList params = ImmutableList.of( + parseParameter(ParamContext.IMPLEMENTATION)); eat(TokenType.CLOSE_PAREN); - return result; + if (peek(TokenType.ARROW)) { + return new FormalParameterListTree(getTreeLocation(start), params); + } else { + reportError("invalid parenthesized expression"); + return new MissingPrimaryExpressionTree(getTreeLocation(start)); + } } // For either of the two remaining cases: // ( Expression ) @@ -2796,9 +2806,6 @@ private ParseTree parseAssignment(Expression expressionIn) { if (peek(TokenType.ARROW)) { return completeAssignmentExpressionParseAtArrow(left, expressionIn); } - if (left.type == ParseTreeType.FORMAL_PARAMETER_LIST) { - reportError("invalid paren expression"); - } if (peekAssignmentOperator()) { left = transformLeftHandSideExpression(left); diff --git a/test/com/google/javascript/jscomp/parsing/ParserTest.java b/test/com/google/javascript/jscomp/parsing/ParserTest.java index 3460662d9f4..70700146b54 100644 --- a/test/com/google/javascript/jscomp/parsing/ParserTest.java +++ b/test/com/google/javascript/jscomp/parsing/ParserTest.java @@ -2634,15 +2634,34 @@ public void testRestParameters_ES5() { getRequiresEs6Message(Feature.REST_PARAMETERS)); } - public void testExpressionsThatLookLikeParameters() { + public void testExpressionsThatLookLikeParameters1() { mode = LanguageMode.ECMASCRIPT6; - parseError("();", "invalid paren expression"); + parseError("();", "invalid parenthesized expression"); expectFeatures(Feature.REST_PARAMETERS); - parseError("(...xs);", "invalid paren expression"); + parseError("(...xs);", "invalid parenthesized expression"); parseError("(x, ...xs);", "A rest parameter must be in a parameter list."); parseError("(a, b, c, ...xs);", "A rest parameter must be in a parameter list."); } + public void testExpressionsThatLookLikeParameters2() { + mode = LanguageMode.ECMASCRIPT6; + parseError("!()", "invalid parenthesized expression"); + parseError("().method", "invalid parenthesized expression"); + parseError("() || a", "invalid parenthesized expression"); + parseError("() && a", "invalid parenthesized expression"); + parseError("x = ()", "invalid parenthesized expression"); + } + + public void testExpressionsThatLookLikeParameters3() { + expectFeatures(Feature.REST_PARAMETERS); + mode = LanguageMode.ECMASCRIPT6; + parseError("!(...x)", "invalid parenthesized expression"); + parseError("(...x).method", "invalid parenthesized expression"); + parseError("(...x) || a", "invalid parenthesized expression"); + parseError("(...x) && a", "invalid parenthesized expression"); + parseError("x = (...x)", "invalid parenthesized expression"); + } + public void testDefaultParametersWithRestParameters() { mode = LanguageMode.ECMASCRIPT6; expectFeatures(Feature.DEFAULT_PARAMETERS, Feature.REST_PARAMETERS); @@ -2778,7 +2797,7 @@ public void testArrow1() { getRequiresEs6Message(Feature.ARROW_FUNCTIONS)); } - public void testArrowInvalid() { + public void testArrowInvalid1() { mode = LanguageMode.ECMASCRIPT6; parseError("*()=>1;", "primary expression expected"); expectFeatures(Feature.ARROW_FUNCTIONS);