Fix invalid setter parse (#12076)
* Fix invalid `setter` parse * estree Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
parent
ae18f9c0d9
commit
18d13d0032
@ -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 {
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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(
|
||||
|
||||
1
packages/babel-parser/test/fixtures/core/object/invalid-setter/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/core/object/invalid-setter/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
({ set x(){} })
|
||||
52
packages/babel-parser/test/fixtures/core/object/invalid-setter/output.json
vendored
Normal file
52
packages/babel-parser/test/fixtures/core/object/invalid-setter/output.json
vendored
Normal file
@ -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)"
|
||||
],
|
||||
"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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
({ set x(){} })
|
||||
56
packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json
vendored
Normal file
56
packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json
vendored
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user