Disallow duplicate params in methods (#9599)

* Disallow duplicate params in methods

* Fix plugins
This commit is contained in:
Daniel Tschinder
2019-02-27 15:54:07 -08:00
committed by GitHub
parent 5cb280f986
commit 208195f425
9 changed files with 38 additions and 13 deletions

View File

@@ -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;
@@ -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;
@@ -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);

View File

@@ -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";
}

View File

@@ -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();
@@ -1573,7 +1578,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
: null;
}
super.parseFunctionBodyAndFinish(node, type);
super.parseFunctionBodyAndFinish(node, type, isMethod);
}
// interfaces

View File

@@ -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);
@@ -1488,7 +1489,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return;
}
super.parseFunctionBodyAndFinish(node, type);
super.parseFunctionBodyAndFinish(node, type, isMethod);
}
parseSubscript(

View File

@@ -0,0 +1,3 @@
class Foo {
bar(a, a) {}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Argument name clash (2:11)"
}

View File

@@ -0,0 +1,3 @@
({
bar(a, a) {}
})

View File

@@ -0,0 +1,3 @@
{
"throws": "Argument name clash (2:11)"
}