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

Disallow duplicate params in methods #9599

Merged
merged 2 commits into from Feb 27, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1740,7 +1740,7 @@ export default class ExpressionParser extends LValParser {
);
this.parseFunctionParams((node: any), allowModifiers);
this.checkYieldAwaitInDefaultParams();
this.parseFunctionBodyAndFinish(node, type);
this.parseFunctionBodyAndFinish(node, type, true);

this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
Expand Down Expand Up @@ -1804,14 +1804,19 @@ export default class ExpressionParser extends LValParser {
parseFunctionBodyAndFinish(
node: N.BodilessFunctionOrMethodBase,
type: string,
isMethod?: boolean = false,
): void {
// $FlowIgnore (node is not bodiless if we get here)
this.parseFunctionBody(node);
this.parseFunctionBody(node, false, isMethod);
this.finishNode(node, type);
}

// Parse function body and check parameters.
parseFunctionBody(node: N.Function, allowExpression: ?boolean): void {
parseFunctionBody(
node: N.Function,
allowExpression: ?boolean,
isMethod?: boolean = false,
): void {
const isExpression = allowExpression && !this.match(tt.braceL);
const oldStrict = this.state.strict;
let useStrict = false;
Expand Down Expand Up @@ -1853,7 +1858,7 @@ export default class ExpressionParser extends LValParser {
// if a let/const declaration in the function clashes with one of the params.
this.checkParams(
node,
!oldStrict && !useStrict && !allowExpression && !nonSimple,
!oldStrict && !useStrict && !allowExpression && !isMethod && !nonSimple,
allowExpression,
);
node.body = this.parseBlock(true, false);
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-parser/src/plugins/estree.js
Expand Up @@ -259,8 +259,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return node;
}

parseFunctionBody(node: N.Function, allowExpression: ?boolean): void {
super.parseFunctionBody(node, allowExpression);
parseFunctionBody(
node: N.Function,
allowExpression: ?boolean,
isMethod?: boolean = false,
): void {
super.parseFunctionBody(node, allowExpression, isMethod);
node.expression = node.body.type !== "BlockStatement";
}

Expand Down
13 changes: 9 additions & 4 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -1544,19 +1544,24 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// Overrides
// ==================================

parseFunctionBody(node: N.Function, allowExpressionBody: ?boolean): void {
parseFunctionBody(
node: N.Function,
allowExpressionBody: ?boolean,
isMethod?: boolean = false,
): void {
if (allowExpressionBody) {
return this.forwardNoArrowParamsConversionAt(node, () =>
super.parseFunctionBody(node, true),
super.parseFunctionBody(node, true, isMethod),
);
}

return super.parseFunctionBody(node, false);
return super.parseFunctionBody(node, false, isMethod);
}

parseFunctionBodyAndFinish(
node: N.BodilessFunctionOrMethodBase,
type: string,
isMethod?: boolean = false,
): void {
if (this.match(tt.colon)) {
const typeNode = this.startNode();
Expand All @@ -1573,7 +1578,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
: null;
}

super.parseFunctionBodyAndFinish(node, type);
super.parseFunctionBodyAndFinish(node, type, isMethod);
}

// interfaces
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-parser/src/plugins/typescript.js
Expand Up @@ -1472,6 +1472,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
parseFunctionBodyAndFinish(
node: N.BodilessFunctionOrMethodBase,
type: string,
isMethod?: boolean = false,
): void {
if (this.match(tt.colon)) {
node.returnType = this.tsParseTypeOrTypePredicateAnnotation(tt.colon);
Expand All @@ -1488,7 +1489,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return;
}

super.parseFunctionBodyAndFinish(node, type);
super.parseFunctionBodyAndFinish(node, type, isMethod);
}

parseSubscript(
Expand Down
@@ -0,0 +1,3 @@
class Foo {
bar(a, a) {}
}
@@ -0,0 +1,3 @@
{
"throws": "Argument name clash (2:11)"
}
@@ -0,0 +1,3 @@
({
bar(a, a) {}
})
@@ -0,0 +1,3 @@
{
"throws": "Argument name clash (2:11)"
}
2 changes: 0 additions & 2 deletions scripts/tests/test262/test262_whitelist.txt
Expand Up @@ -406,10 +406,8 @@ language/expressions/class/elements/syntax/early-errors/invalid-names/method-out
language/expressions/class/elements/syntax/early-errors/invalid-names/method-outter-member-expression-this.js(strict mode)
language/expressions/class/elements/syntax/early-errors/super-private-access-invalid.js(default)
language/expressions/class/elements/syntax/early-errors/super-private-access-invalid.js(strict mode)
language/expressions/object/method-definition/early-errors-object-async-method-duplicate-parameters.js(default)
language/expressions/object/method-definition/early-errors-object-method-async-lineterminator.js(default)
language/expressions/object/method-definition/early-errors-object-method-async-lineterminator.js(strict mode)
language/expressions/object/method-definition/early-errors-object-method-duplicate-parameters.js(default)
language/expressions/object/method-definition/private-name-early-error-async-fn-inside-class.js(default)
language/expressions/object/method-definition/private-name-early-error-async-fn-inside-class.js(strict mode)
language/expressions/object/method-definition/private-name-early-error-async-gen-inside-class.js(default)
Expand Down