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 invalid setter parse #12076

Merged
merged 2 commits into from Sep 19, 2020
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
12 changes: 10 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1795,12 +1795,20 @@ export default class ExpressionParser extends LValParser {
return method.kind === "get" ? 0 : 1;
}

// This exists so we can override within the ESTree plugin
getObjectOrClassMethodParams(method: N.ObjectMethod | N.ClassMethod) {
return method.params;
}

// get methods aren't allowed to have any parameters
// set methods must have exactly 1 parameter which is not a rest parameter
checkGetterSetterParams(method: N.ObjectMethod | N.ClassMethod): void {
const paramCount = this.getGetterSetterExpectedParamCount(method);
const params = this.getObjectOrClassMethodParams(method);

const start = method.start;
if (method.params.length !== paramCount) {

if (params.length !== paramCount) {
if (method.kind === "get") {
this.raise(start, Errors.BadGetterArity);
} else {
Expand All @@ -1810,7 +1818,7 @@ export default class ExpressionParser extends LValParser {

if (
method.kind === "set" &&
method.params[method.params.length - 1].type === "RestElement"
params[params.length - 1]?.type === "RestElement"
) {
this.raise(start, Errors.BadSetterRestParameter);
}
Expand Down
19 changes: 3 additions & 16 deletions packages/babel-parser/src/plugins/estree.js
Expand Up @@ -105,22 +105,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

checkGetterSetterParams(method: N.ObjectMethod | N.ClassMethod): void {
const prop = ((method: any): N.EstreeProperty | N.EstreeMethodDefinition);
const paramCount = prop.kind === "get" ? 0 : 1;
const start = prop.start;
if (prop.value.params.length !== paramCount) {
if (method.kind === "get") {
this.raise(start, Errors.BadGetterArity);
} else {
this.raise(start, Errors.BadSetterArity);
}
} else if (
prop.kind === "set" &&
prop.value.params[0].type === "RestElement"
) {
this.raise(start, Errors.BadSetterRestParameter);
}
getObjectOrClassMethodParams(method: N.ObjectMethod | N.ClassMethod) {
return ((method: any): N.EstreeProperty | N.EstreeMethodDefinition).value
.params;
}

checkLVal(
Expand Down
@@ -0,0 +1 @@
({ set x(){} })
@@ -0,0 +1,52 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"errors": [
"SyntaxError: setter must have exactly one formal parameter (1:3)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but I think we can improve this error message:

A setter must have exactly one parameter.

Or Typescript's:

A 'set' accessor must have exactly one parameter.

],
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"expression": {
"type": "ObjectExpression",
"start":1,"end":14,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":14}},
"properties": [
{
"type": "ObjectMethod",
"start":3,"end":12,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":12}},
"method": false,
"key": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"},
"name": "x"
},
"computed": false,
"kind": "set",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12}},
"body": [],
"directives": []
}
}
],
"extra": {
"parenthesized": true,
"parenStart": 0
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
({ set x(){} })
@@ -0,0 +1,56 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"errors": [
"SyntaxError: setter must have exactly one formal parameter (1:3)"
],
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"expression": {
"type": "ObjectExpression",
"start":1,"end":14,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":14}},
"properties": [
{
"type": "Property",
"start":3,"end":12,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":12}},
"method": false,
"key": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"},
"name": "x"
},
"computed": false,
"kind": "set",
"value": {
"type": "FunctionExpression",
"start":8,"end":12,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":12}},
"id": null,
"generator": false,
"async": false,
"expression": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12}},
"body": []
}
},
"shorthand": false
}
],
"extra": {
"parenthesized": true,
"parenStart": 0
}
}
}
]
}
}