Skip to content

Commit

Permalink
Raise error for rest parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Apr 11, 2021
1 parent f81ff17 commit 6a0c387
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ const TSErrors = makeErrorTemplates(
"'readonly' modifier can only appear on a property declaration or index signature.",
SetAccesorCannotHaveOptionalParameter:
"A 'set' accessor cannot have an optional parameter.",
SetAccesorCannotHaveRestParameter:
"A 'set' accessor cannot have rest parameter.",
SetAccesorCannotHaveReturnType:
"A 'set' accessor cannot have a return type annotation.",
TypeAnnotationAfterAssign:
Expand Down Expand Up @@ -647,6 +649,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
TSErrors.SetAccesorCannotHaveOptionalParameter,
);
}
if (firstParameter.type === "RestElement") {
this.raise(
this.state.pos,
TSErrors.SetAccesorCannotHaveRestParameter,
);
}
}
if (method.typeAnnotation) {
this.raise(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Foo {
set bar(...v);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"type": "File",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: A 'set' accessor cannot have rest parameter. (3:1)"
],
"program": {
"type": "Program",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":34,"loc":{"start":{"line":1,"column":14},"end":{"line":3,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":16}},
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"kind": "set",
"parameters": [
{
"type": "RestElement",
"start":26,"end":30,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":14}},
"argument": {
"type": "Identifier",
"start":29,"end":30,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":14},"identifierName":"v"},
"name": "v"
}
}
]
}
]
}
}
],
"directives": []
}
}

0 comments on commit 6a0c387

Please sign in to comment.