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

Throw a syntax error for a parameter properties in not constructor #12061

Merged
merged 2 commits into from Sep 14, 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
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/statement.js
Expand Up @@ -1083,7 +1083,7 @@ export default class StatementParser extends ExpressionParser {
node.id = this.parseFunctionId();
}
this.parseFunctionParams(node);
this.parseFunctionParams(node, /* allowModifiers */ false);
// For the smartPipelines plugin: Disable topic references from outer
// contexts within the function body. They are permitted in function
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -90,6 +90,8 @@ const TSErrors = Object.freeze({
"Template literal types cannot have any substitution",
TypeAnnotationAfterAssign:
"Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`",
UnexpectedParameterModifier:
"A parameter property is only allowed in a constructor implementation.",
UnexpectedReadonly:
"'readonly' type modifier is only permitted on array and tuple literal types.",
UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
Expand Down Expand Up @@ -1709,9 +1711,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>

let accessibility: ?N.Accessibility;
let readonly = false;
if (allowModifiers) {
if (allowModifiers !== undefined) {
accessibility = this.parseAccessModifier();
readonly = !!this.tsParseModifier(["readonly"]);
if (allowModifiers === false && (accessibility || readonly)) {
this.raise(startPos, TSErrors.UnexpectedParameterModifier);
}
}

const left = this.parseMaybeDefault();
Expand Down
@@ -0,0 +1,11 @@
class C {
not_constructor(
readonly r,
public pu: number,
protected po?,
private pi?: number,
public readonly pur,
// Also works on AssignmentPattern
readonly x = 0,
public y?: number = 0) {}
}
@@ -0,0 +1,199 @@
{
"type": "File",
"start":0,"end":261,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":1}},
"errors": [
"SyntaxError: A parameter property is only allowed in a constructor implementation. (3:8)",
"SyntaxError: A parameter property is only allowed in a constructor implementation. (4:8)",
"SyntaxError: A parameter property is only allowed in a constructor implementation. (5:8)",
"SyntaxError: A parameter property is only allowed in a constructor implementation. (6:8)",
"SyntaxError: A parameter property is only allowed in a constructor implementation. (7:8)",
"SyntaxError: A parameter property is only allowed in a constructor implementation. (9:8)",
"SyntaxError: A parameter property is only allowed in a constructor implementation. (10:8)"
],
"program": {
"type": "Program",
"start":0,"end":261,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":261,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":261,"loc":{"start":{"line":1,"column":8},"end":{"line":11,"column":1}},
"body": [
{
"type": "ClassMethod",
"start":14,"end":259,"loc":{"start":{"line":2,"column":4},"end":{"line":10,"column":33}},
"static": false,
"key": {
"type": "Identifier",
"start":14,"end":29,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":19},"identifierName":"not_constructor"},
"name": "not_constructor"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "TSParameterProperty",
"start":39,"end":49,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":18}},
"readonly": true,
"parameter": {
"type": "Identifier",
"start":48,"end":49,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":18},"identifierName":"r"},
"name": "r"
}
},
{
"type": "TSParameterProperty",
"start":59,"end":76,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":25}},
"accessibility": "public",
"parameter": {
"type": "Identifier",
"start":66,"end":76,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":25},"identifierName":"pu"},
"name": "pu",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":68,"end":76,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":25}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":70,"end":76,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":25}}
}
}
}
},
{
"type": "TSParameterProperty",
"start":86,"end":99,"loc":{"start":{"line":5,"column":8},"end":{"line":5,"column":21}},
"accessibility": "protected",
"parameter": {
"type": "Identifier",
"start":96,"end":99,"loc":{"start":{"line":5,"column":18},"end":{"line":5,"column":21},"identifierName":"po"},
"name": "po",
"optional": true
}
},
{
"type": "TSParameterProperty",
"start":109,"end":128,"loc":{"start":{"line":6,"column":8},"end":{"line":6,"column":27}},
"accessibility": "private",
"parameter": {
"type": "Identifier",
"start":117,"end":128,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":27},"identifierName":"pi"},
"name": "pi",
"optional": true,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":120,"end":128,"loc":{"start":{"line":6,"column":19},"end":{"line":6,"column":27}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":122,"end":128,"loc":{"start":{"line":6,"column":21},"end":{"line":6,"column":27}}
}
}
}
},
{
"type": "TSParameterProperty",
"start":138,"end":157,"loc":{"start":{"line":7,"column":8},"end":{"line":7,"column":27}},
"accessibility": "public",
"readonly": true,
"parameter": {
"type": "Identifier",
"start":154,"end":157,"loc":{"start":{"line":7,"column":24},"end":{"line":7,"column":27},"identifierName":"pur"},
"name": "pur"
}
},
{
"type": "TSParameterProperty",
"start":210,"end":224,"loc":{"start":{"line":9,"column":8},"end":{"line":9,"column":22}},
"readonly": true,
"parameter": {
"type": "AssignmentPattern",
"start":219,"end":224,"loc":{"start":{"line":9,"column":17},"end":{"line":9,"column":22}},
"left": {
"type": "Identifier",
"start":219,"end":220,"loc":{"start":{"line":9,"column":17},"end":{"line":9,"column":18},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "NumericLiteral",
"start":223,"end":224,"loc":{"start":{"line":9,"column":21},"end":{"line":9,"column":22}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
"leadingComments": [
{
"type": "CommentLine",
"value": " Also works on AssignmentPattern",
"start":167,"end":201,"loc":{"start":{"line":8,"column":8},"end":{"line":8,"column":42}}
}
]
},
{
"type": "TSParameterProperty",
"start":234,"end":255,"loc":{"start":{"line":10,"column":8},"end":{"line":10,"column":29}},
"accessibility": "public",
"parameter": {
"type": "AssignmentPattern",
"start":241,"end":255,"loc":{"start":{"line":10,"column":15},"end":{"line":10,"column":29}},
"left": {
"type": "Identifier",
"start":241,"end":251,"loc":{"start":{"line":10,"column":15},"end":{"line":10,"column":25},"identifierName":"y"},
"name": "y",
"optional": true,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":243,"end":251,"loc":{"start":{"line":10,"column":17},"end":{"line":10,"column":25}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":245,"end":251,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":25}}
}
}
},
"right": {
"type": "NumericLiteral",
"start":254,"end":255,"loc":{"start":{"line":10,"column":28},"end":{"line":10,"column":29}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
}
],
"body": {
"type": "BlockStatement",
"start":257,"end":259,"loc":{"start":{"line":10,"column":31},"end":{"line":10,"column":33}},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " Also works on AssignmentPattern",
"start":167,"end":201,"loc":{"start":{"line":8,"column":8},"end":{"line":8,"column":42}}
}
]
}
@@ -0,0 +1,9 @@
function foo(
readonly r,
public pu: number,
protected po?,
private pi?: number,
public readonly pur,
readonly x = 0,
public y?: number = 0
) {}