diff --git a/packages/babel-parser/src/parse-error/standard-errors.ts b/packages/babel-parser/src/parse-error/standard-errors.ts index 0db3966f6af8..034db87f20ac 100644 --- a/packages/babel-parser/src/parse-error/standard-errors.ts +++ b/packages/babel-parser/src/parse-error/standard-errors.ts @@ -35,10 +35,10 @@ export default { AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.", AwaitNotInAsyncFunction: "'await' is only allowed within async functions.", - BadGetterArity: "A 'get' accesor must not have any formal parameters.", - BadSetterArity: "A 'set' accesor must have exactly one formal parameter.", + BadGetterArity: "A 'get' accessor must not have any formal parameters.", + BadSetterArity: "A 'set' accessor must have exactly one formal parameter.", BadSetterRestParameter: - "A 'set' accesor function argument must not be a rest parameter.", + "A 'set' accessor function argument must not be a rest parameter.", ConstructorClassField: "Classes may not have a field named 'constructor'.", ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.", diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 5c8a0b2ceeb2..6e0db88960e0 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -94,6 +94,8 @@ const TSErrors = ParseErrorEnum`typescript`({ AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.", AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.", + AccessorCannotBeOptional: + "An 'accessor' property cannot be declared optional.", ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.", ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.", ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: @@ -195,6 +197,7 @@ const TSErrors = ParseErrorEnum`typescript`({ "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`.", ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.", + // TODO: Accesor -> Accessor SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.", SetAccesorCannotHaveRestParameter: @@ -3121,10 +3124,14 @@ export default (superClass: ClassWithMixin) => } parseClassPropertyAnnotation( - node: N.ClassProperty | N.ClassPrivateProperty, + node: N.ClassProperty | N.ClassPrivateProperty | N.ClassAccessorProperty, ): void { - if (!node.optional && this.eat(tt.bang)) { - node.definite = true; + if (!node.optional) { + if (this.eat(tt.bang)) { + node.definite = true; + } else if (this.eat(tt.question)) { + node.optional = true; + } } const type = this.tsTryParseTypeAnnotation(); @@ -3178,6 +3185,16 @@ export default (superClass: ClassWithMixin) => return super.parseClassPrivateProperty(node); } + parseClassAccessorProperty( + node: N.ClassAccessorProperty, + ): N.ClassAccessorProperty { + this.parseClassPropertyAnnotation(node); + if (node.optional) { + this.raise(TSErrors.AccessorCannotBeOptional, { at: node }); + } + return super.parseClassAccessorProperty(node); + } + pushClassMethod( classBody: N.ClassBody, method: N.ClassMethod, diff --git a/packages/babel-parser/test/fixtures/core/object/invalid-setter/output.json b/packages/babel-parser/test/fixtures/core/object/invalid-setter/output.json index ad8fc6967515..d4081be8c566 100644 --- a/packages/babel-parser/test/fixtures/core/object/invalid-setter/output.json +++ b/packages/babel-parser/test/fixtures/core/object/invalid-setter/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}}, "errors": [ - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (1:3)" + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (1:3)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2015/class-methods/getter-signature/output.json b/packages/babel-parser/test/fixtures/es2015/class-methods/getter-signature/output.json index 3b1fed887bf4..80366f9cebca 100644 --- a/packages/babel-parser/test/fixtures/es2015/class-methods/getter-signature/output.json +++ b/packages/babel-parser/test/fixtures/es2015/class-methods/getter-signature/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":31}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (2:2)" + "SyntaxError: A 'get' accessor must not have any formal parameters. (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/347/output.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/347/output.json index 78a10d693c4f..7844fb183291 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/347/output.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/347/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":29,"index":29}}, "errors": [ - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (1:10)" + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (1:10)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0075/output.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0075/output.json index 8af49750c8df..b91ae2b98296 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0075/output.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0075/output.json @@ -1,36 +1,36 @@ { "type": "File", - "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, "errors": [ - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (1:3)" + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (1:3)" ], "program": { "type": "Program", - "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, "sourceType": "script", "interpreter": null, "body": [ { "type": "ExpressionStatement", - "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, "expression": { "type": "ObjectExpression", - "start":1,"end":16,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":16,"index":16}}, + "start":1,"end":16,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":16}}, "properties": [ { "type": "Property", - "start":3,"end":14,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":14,"index":14}}, + "start":3,"end":14,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":14}}, "method": false, "key": { "type": "Identifier", - "start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"s"}, + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"s"}, "name": "s" }, "computed": false, "kind": "set", "value": { "type": "FunctionExpression", - "start":8,"end":14,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":14,"index":14}}, + "start":8,"end":14,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":14}}, "id": null, "generator": false, "async": false, @@ -38,7 +38,7 @@ "params": [], "body": { "type": "BlockStatement", - "start":11,"end":14,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":14,"index":14}}, + "start":11,"end":14,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":14}}, "body": [] } }, diff --git a/packages/babel-parser/test/fixtures/esprima/rest-parameter/invalid-setter-rest/output.json b/packages/babel-parser/test/fixtures/esprima/rest-parameter/invalid-setter-rest/output.json index e4e3fd9fc69d..00c1e1ae180d 100644 --- a/packages/babel-parser/test/fixtures/esprima/rest-parameter/invalid-setter-rest/output.json +++ b/packages/babel-parser/test/fixtures/esprima/rest-parameter/invalid-setter-rest/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, "errors": [ - "SyntaxError: A 'set' accesor function argument must not be a rest parameter. (1:6)" + "SyntaxError: A 'set' accessor function argument must not be a rest parameter. (1:6)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json b/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json index a8870d027576..5a69a65bcfeb 100644 --- a/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json +++ b/packages/babel-parser/test/fixtures/estree/object-method/invalid-setter/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}}, "errors": [ - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (1:3)" + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (1:3)" ], "program": { "type": "Program", @@ -53,4 +53,4 @@ } ] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/output.json b/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/output.json index 082457acc226..92b27c4a8c55 100644 --- a/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/output.json +++ b/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/output.json @@ -2,8 +2,8 @@ "type": "File", "start":0,"end":85,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":18}}, "errors": [ - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (3:3)", - "SyntaxError: A 'get' accesor must not have any formal parameters. (4:3)" + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (3:3)", + "SyntaxError: A 'get' accessor must not have any formal parameters. (4:3)" ], "program": { "type": "Program", @@ -200,4 +200,4 @@ } ] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json index 17f4fcc22b73..927cbb030fa7 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count-rest/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":37,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":37}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (2:2)" + "SyntaxError: A 'get' accessor must not have any formal parameters. (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json index ebf265fb0830..898d5a829597 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-getter-param-count/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":41,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":41}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (2:2)" + "SyntaxError: A 'get' accessor must not have any formal parameters. (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json index 994feb0f0f6b..ed754c0d2753 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-count/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":29}}, "errors": [ - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (2:2)" + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json index c7ab11116cc8..ae80dbd53dd3 100644 --- a/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/object-types/invalid-setter-param-type/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":33,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":33}}, "errors": [ - "SyntaxError: A 'set' accesor function argument must not be a rest parameter. (2:2)" + "SyntaxError: A 'set' accessor function argument must not be a rest parameter. (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json index 6950db1af795..4bccb996fcc0 100644 --- a/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-getter/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":40,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":40}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (2:2)", + "SyntaxError: A 'get' accessor must not have any formal parameters. (2:2)", "SyntaxError: A getter cannot have a `this` parameter. (2:10)" ], "program": { diff --git a/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json index aaaf78efc008..775289cd0219 100644 --- a/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json +++ b/packages/babel-parser/test/fixtures/flow/this-annotation/this-setter-type/output.json @@ -3,7 +3,7 @@ "start":0,"end":43,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":43}}, "errors": [ "SyntaxError: A setter cannot have a `this` parameter. (2:10)", - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (2:2)" + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/input.ts b/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/input.ts new file mode 100644 index 000000000000..8a2f8a1d63ab --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/input.ts @@ -0,0 +1,15 @@ +abstract class Foo { + declare accessor prop7: number; + private accessor #p: any; + + accessor a!; + abstract accessor #s; + accessor #d?; + abstract accessor f = 1; + readonly accessor g; +} + +declare class C { + accessor x = 1; + #y = 1; +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/options.json b/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/options.json new file mode 100644 index 000000000000..ceb6fe8e0820 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "typescript", + "decoratorAutoAccessors" + ] +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/output.json b/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/output.json new file mode 100644 index 000000000000..c1194e104c7b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/output.json @@ -0,0 +1,220 @@ +{ + "type": "File", + "start":0,"end":239,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":15,"column":1,"index":239}}, + "errors": [ + "SyntaxError: An 'accessor' property cannot be declared optional. (7:2)" + ], + "program": { + "type": "Program", + "start":0,"end":239,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":15,"column":1,"index":239}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":190,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":10,"column":1,"index":190}}, + "abstract": true, + "id": { + "type": "Identifier", + "start":15,"end":18,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":18,"index":18},"identifierName":"Foo"}, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":19,"end":190,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":10,"column":1,"index":190}}, + "body": [ + { + "type": "ClassAccessorProperty", + "start":23,"end":54,"loc":{"start":{"line":2,"column":2,"index":23},"end":{"line":2,"column":33,"index":54}}, + "declare": true, + "static": false, + "key": { + "type": "Identifier", + "start":40,"end":45,"loc":{"start":{"line":2,"column":19,"index":40},"end":{"line":2,"column":24,"index":45},"identifierName":"prop7"}, + "name": "prop7" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":45,"end":53,"loc":{"start":{"line":2,"column":24,"index":45},"end":{"line":2,"column":32,"index":53}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":47,"end":53,"loc":{"start":{"line":2,"column":26,"index":47},"end":{"line":2,"column":32,"index":53}} + } + }, + "value": null + }, + { + "type": "ClassAccessorProperty", + "start":57,"end":82,"loc":{"start":{"line":3,"column":2,"index":57},"end":{"line":3,"column":27,"index":82}}, + "accessibility": "private", + "static": false, + "key": { + "type": "PrivateName", + "start":74,"end":76,"loc":{"start":{"line":3,"column":19,"index":74},"end":{"line":3,"column":21,"index":76}}, + "id": { + "type": "Identifier", + "start":75,"end":76,"loc":{"start":{"line":3,"column":20,"index":75},"end":{"line":3,"column":21,"index":76},"identifierName":"p"}, + "name": "p" + } + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":76,"end":81,"loc":{"start":{"line":3,"column":21,"index":76},"end":{"line":3,"column":26,"index":81}}, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":78,"end":81,"loc":{"start":{"line":3,"column":23,"index":78},"end":{"line":3,"column":26,"index":81}} + } + }, + "value": null + }, + { + "type": "ClassAccessorProperty", + "start":86,"end":98,"loc":{"start":{"line":5,"column":2,"index":86},"end":{"line":5,"column":14,"index":98}}, + "static": false, + "key": { + "type": "Identifier", + "start":95,"end":96,"loc":{"start":{"line":5,"column":11,"index":95},"end":{"line":5,"column":12,"index":96},"identifierName":"a"}, + "name": "a" + }, + "computed": false, + "definite": true, + "value": null + }, + { + "type": "ClassAccessorProperty", + "start":101,"end":122,"loc":{"start":{"line":6,"column":2,"index":101},"end":{"line":6,"column":23,"index":122}}, + "abstract": true, + "static": false, + "key": { + "type": "PrivateName", + "start":119,"end":121,"loc":{"start":{"line":6,"column":20,"index":119},"end":{"line":6,"column":22,"index":121}}, + "id": { + "type": "Identifier", + "start":120,"end":121,"loc":{"start":{"line":6,"column":21,"index":120},"end":{"line":6,"column":22,"index":121},"identifierName":"s"}, + "name": "s" + } + }, + "computed": false, + "value": null + }, + { + "type": "ClassAccessorProperty", + "start":125,"end":138,"loc":{"start":{"line":7,"column":2,"index":125},"end":{"line":7,"column":15,"index":138}}, + "static": false, + "key": { + "type": "PrivateName", + "start":134,"end":136,"loc":{"start":{"line":7,"column":11,"index":134},"end":{"line":7,"column":13,"index":136}}, + "id": { + "type": "Identifier", + "start":135,"end":136,"loc":{"start":{"line":7,"column":12,"index":135},"end":{"line":7,"column":13,"index":136},"identifierName":"d"}, + "name": "d" + } + }, + "computed": false, + "optional": true, + "value": null + }, + { + "type": "ClassAccessorProperty", + "start":141,"end":165,"loc":{"start":{"line":8,"column":2,"index":141},"end":{"line":8,"column":26,"index":165}}, + "abstract": true, + "static": false, + "key": { + "type": "Identifier", + "start":159,"end":160,"loc":{"start":{"line":8,"column":20,"index":159},"end":{"line":8,"column":21,"index":160},"identifierName":"f"}, + "name": "f" + }, + "computed": false, + "value": { + "type": "NumericLiteral", + "start":163,"end":164,"loc":{"start":{"line":8,"column":24,"index":163},"end":{"line":8,"column":25,"index":164}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassAccessorProperty", + "start":168,"end":188,"loc":{"start":{"line":9,"column":2,"index":168},"end":{"line":9,"column":22,"index":188}}, + "readonly": true, + "static": false, + "key": { + "type": "Identifier", + "start":186,"end":187,"loc":{"start":{"line":9,"column":20,"index":186},"end":{"line":9,"column":21,"index":187},"identifierName":"g"}, + "name": "g" + }, + "computed": false, + "value": null + } + ] + } + }, + { + "type": "ClassDeclaration", + "start":192,"end":239,"loc":{"start":{"line":12,"column":0,"index":192},"end":{"line":15,"column":1,"index":239}}, + "declare": true, + "id": { + "type": "Identifier", + "start":206,"end":207,"loc":{"start":{"line":12,"column":14,"index":206},"end":{"line":12,"column":15,"index":207},"identifierName":"C"}, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":208,"end":239,"loc":{"start":{"line":12,"column":16,"index":208},"end":{"line":15,"column":1,"index":239}}, + "body": [ + { + "type": "ClassAccessorProperty", + "start":212,"end":227,"loc":{"start":{"line":13,"column":2,"index":212},"end":{"line":13,"column":17,"index":227}}, + "static": false, + "key": { + "type": "Identifier", + "start":221,"end":222,"loc":{"start":{"line":13,"column":11,"index":221},"end":{"line":13,"column":12,"index":222},"identifierName":"x"}, + "name": "x" + }, + "computed": false, + "value": { + "type": "NumericLiteral", + "start":225,"end":226,"loc":{"start":{"line":13,"column":15,"index":225},"end":{"line":13,"column":16,"index":226}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassPrivateProperty", + "start":230,"end":237,"loc":{"start":{"line":14,"column":2,"index":230},"end":{"line":14,"column":9,"index":237}}, + "static": false, + "key": { + "type": "PrivateName", + "start":230,"end":232,"loc":{"start":{"line":14,"column":2,"index":230},"end":{"line":14,"column":4,"index":232}}, + "id": { + "type": "Identifier", + "start":231,"end":232,"loc":{"start":{"line":14,"column":3,"index":231},"end":{"line":14,"column":4,"index":232},"identifierName":"y"}, + "name": "y" + } + }, + "value": { + "type": "NumericLiteral", + "start":235,"end":236,"loc":{"start":{"line":14,"column":7,"index":235},"end":{"line":14,"column":8,"index":236}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/accessor/input.ts b/packages/babel-parser/test/fixtures/typescript/class/accessor/input.ts new file mode 100644 index 000000000000..2099b517a0f2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/accessor/input.ts @@ -0,0 +1,8 @@ +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/accessor/options.json b/packages/babel-parser/test/fixtures/typescript/class/accessor/options.json new file mode 100644 index 000000000000..ceb6fe8e0820 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/accessor/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "typescript", + "decoratorAutoAccessors" + ] +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/accessor/output.json b/packages/babel-parser/test/fixtures/typescript/class/accessor/output.json new file mode 100644 index 000000000000..b18d0badaa97 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/accessor/output.json @@ -0,0 +1,196 @@ +{ + "type": "File", + "start":0,"end":224,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":8,"column":1,"index":224}}, + "program": { + "type": "Program", + "start":0,"end":224,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":8,"column":1,"index":224}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":224,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":8,"column":1,"index":224}}, + "abstract": true, + "id": { + "type": "Identifier", + "start":15,"end":18,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":18,"index":18},"identifierName":"Foo"}, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":19,"end":224,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":8,"column":1,"index":224}}, + "body": [ + { + "type": "ClassAccessorProperty", + "start":23,"end":49,"loc":{"start":{"line":2,"column":2,"index":23},"end":{"line":2,"column":28,"index":49}}, + "static": false, + "key": { + "type": "Identifier", + "start":32,"end":36,"loc":{"start":{"line":2,"column":11,"index":32},"end":{"line":2,"column":15,"index":36},"identifierName":"prop"}, + "name": "prop" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":36,"end":44,"loc":{"start":{"line":2,"column":15,"index":36},"end":{"line":2,"column":23,"index":44}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":38,"end":44,"loc":{"start":{"line":2,"column":17,"index":38},"end":{"line":2,"column":23,"index":44}} + } + }, + "value": { + "type": "NumericLiteral", + "start":47,"end":48,"loc":{"start":{"line":2,"column":26,"index":47},"end":{"line":2,"column":27,"index":48}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassAccessorProperty", + "start":52,"end":86,"loc":{"start":{"line":3,"column":2,"index":52},"end":{"line":3,"column":36,"index":86}}, + "static": true, + "key": { + "type": "Identifier", + "start":68,"end":73,"loc":{"start":{"line":3,"column":18,"index":68},"end":{"line":3,"column":23,"index":73},"identifierName":"prop2"}, + "name": "prop2" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":73,"end":81,"loc":{"start":{"line":3,"column":23,"index":73},"end":{"line":3,"column":31,"index":81}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":75,"end":81,"loc":{"start":{"line":3,"column":25,"index":75},"end":{"line":3,"column":31,"index":81}} + } + }, + "value": { + "type": "NumericLiteral", + "start":84,"end":85,"loc":{"start":{"line":3,"column":34,"index":84},"end":{"line":3,"column":35,"index":85}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassAccessorProperty", + "start":89,"end":117,"loc":{"start":{"line":4,"column":2,"index":89},"end":{"line":4,"column":30,"index":117}}, + "static": false, + "key": { + "type": "PrivateName", + "start":98,"end":104,"loc":{"start":{"line":4,"column":11,"index":98},"end":{"line":4,"column":17,"index":104}}, + "id": { + "type": "Identifier", + "start":99,"end":104,"loc":{"start":{"line":4,"column":12,"index":99},"end":{"line":4,"column":17,"index":104},"identifierName":"prop3"}, + "name": "prop3" + } + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":104,"end":112,"loc":{"start":{"line":4,"column":17,"index":104},"end":{"line":4,"column":25,"index":112}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":106,"end":112,"loc":{"start":{"line":4,"column":19,"index":106},"end":{"line":4,"column":25,"index":112}} + } + }, + "value": { + "type": "NumericLiteral", + "start":115,"end":116,"loc":{"start":{"line":4,"column":28,"index":115},"end":{"line":4,"column":29,"index":116}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassAccessorProperty", + "start":120,"end":149,"loc":{"start":{"line":5,"column":2,"index":120},"end":{"line":5,"column":31,"index":149}}, + "static": false, + "key": { + "type": "Identifier", + "start":130,"end":135,"loc":{"start":{"line":5,"column":12,"index":130},"end":{"line":5,"column":17,"index":135},"identifierName":"prop4"}, + "name": "prop4" + }, + "computed": true, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":136,"end":144,"loc":{"start":{"line":5,"column":18,"index":136},"end":{"line":5,"column":26,"index":144}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":138,"end":144,"loc":{"start":{"line":5,"column":20,"index":138},"end":{"line":5,"column":26,"index":144}} + } + }, + "value": { + "type": "NumericLiteral", + "start":147,"end":148,"loc":{"start":{"line":5,"column":29,"index":147},"end":{"line":5,"column":30,"index":148}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassAccessorProperty", + "start":152,"end":187,"loc":{"start":{"line":6,"column":2,"index":152},"end":{"line":6,"column":37,"index":187}}, + "accessibility": "private", + "static": false, + "key": { + "type": "Identifier", + "start":169,"end":174,"loc":{"start":{"line":6,"column":19,"index":169},"end":{"line":6,"column":24,"index":174},"identifierName":"prop5"}, + "name": "prop5" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":174,"end":182,"loc":{"start":{"line":6,"column":24,"index":174},"end":{"line":6,"column":32,"index":182}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":176,"end":182,"loc":{"start":{"line":6,"column":26,"index":176},"end":{"line":6,"column":32,"index":182}} + } + }, + "value": { + "type": "NumericLiteral", + "start":185,"end":186,"loc":{"start":{"line":6,"column":35,"index":185},"end":{"line":6,"column":36,"index":186}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassAccessorProperty", + "start":190,"end":222,"loc":{"start":{"line":7,"column":2,"index":190},"end":{"line":7,"column":34,"index":222}}, + "abstract": true, + "static": false, + "key": { + "type": "Identifier", + "start":208,"end":213,"loc":{"start":{"line":7,"column":20,"index":208},"end":{"line":7,"column":25,"index":213},"identifierName":"prop6"}, + "name": "prop6" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":213,"end":221,"loc":{"start":{"line":7,"column":25,"index":213},"end":{"line":7,"column":33,"index":221}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":215,"end":221,"loc":{"start":{"line":7,"column":27,"index":215},"end":{"line":7,"column":33,"index":221}} + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters-babel-7/output.json b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters-babel-7/output.json index 4a7474d6cb50..b69c73a1849c 100644 --- a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters-babel-7/output.json +++ b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters-babel-7/output.json @@ -2,8 +2,8 @@ "type": "File", "start":0,"end":56,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":56}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (3:5)", - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (4:1)" + "SyntaxError: A 'get' accessor must not have any formal parameters. (3:5)", + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (4:1)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters/output.json b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters/output.json index 579d5da431f3..0cc4d4a0bcfa 100644 --- a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters/output.json +++ b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters/output.json @@ -2,8 +2,8 @@ "type": "File", "start":0,"end":56,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":56}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (3:5)", - "SyntaxError: A 'set' accesor must have exactly one formal parameter. (4:1)" + "SyntaxError: A 'get' accessor must not have any formal parameters. (3:5)", + "SyntaxError: A 'set' accessor must have exactly one formal parameter. (4:1)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters-babel-7/output.json b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters-babel-7/output.json index 6cc80c73005b..514dd9d48381 100644 --- a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters-babel-7/output.json +++ b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters-babel-7/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":61,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":61}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (3:5)", + "SyntaxError: A 'get' accessor must not have any formal parameters. (3:5)", "SyntaxError: 'get' and 'set' accessors cannot declare 'this' parameters. (3:5)", "SyntaxError: 'get' and 'set' accessors cannot declare 'this' parameters. (4:1)" ], diff --git a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters/output.json b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters/output.json index ec6316aea846..fd2fbb02543c 100644 --- a/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters/output.json +++ b/packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-this-parameters/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":61,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":61}}, "errors": [ - "SyntaxError: A 'get' accesor must not have any formal parameters. (3:5)", + "SyntaxError: A 'get' accessor must not have any formal parameters. (3:5)", "SyntaxError: 'get' and 'set' accessors cannot declare 'this' parameters. (3:5)", "SyntaxError: 'get' and 'set' accessors cannot declare 'this' parameters. (4:1)" ], diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index 5e094c60aa20..5f3bb975861d 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -120,7 +120,8 @@ export default declare((api, opts: Options) => { const classMemberVisitors = { field( path: NodePath< - (t.ClassPrivateProperty | t.ClassProperty) & ExtraNodeProps + (t.ClassPrivateProperty | t.ClassProperty | t.ClassAccessorProperty) & + ExtraNodeProps >, ) { const { node } = path; @@ -525,7 +526,8 @@ export default declare((api, opts: Options) => { } } else if ( child.isClassProperty() || - child.isClassPrivateProperty() + child.isClassPrivateProperty() || + child.isClassAccessorProperty() ) { classMemberVisitors.field(child); } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/input.ts new file mode 100644 index 000000000000..b90f613a51bc --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/input.ts @@ -0,0 +1,15 @@ +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; + private accessor #p: any; + + accessor a!: any; + accessor aa!: any; + abstract accessor #s; + abstract accessor f = 1; + readonly accessor g; +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/options.json new file mode 100644 index 000000000000..7c035142f287 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [["transform-typescript", { "allowDeclareFields": true }]], + "parserOpts": { + "plugins": ["decoratorAutoAccessors"] + } +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/output.js new file mode 100644 index 000000000000..9a08c34e3c0b --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-false/output.js @@ -0,0 +1,14 @@ +class Foo { + accessor prop = 1; + static accessor prop2 = 1; + accessor #prop3 = 1; + accessor [prop4] = 1; + accessor prop5 = 1; + accessor prop6; + accessor #p; + accessor a; + accessor aa; + accessor #s; + accessor f = 1; + accessor g; +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/input.ts new file mode 100644 index 000000000000..b90f613a51bc --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/input.ts @@ -0,0 +1,15 @@ +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; + private accessor #p: any; + + accessor a!: any; + accessor aa!: any; + abstract accessor #s; + abstract accessor f = 1; + readonly accessor g; +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/options.json new file mode 100644 index 000000000000..06cd35701c66 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [["transform-typescript", { "allowDeclareFields": false }]], + "parserOpts": { + "plugins": ["decoratorAutoAccessors"] + }, + "BABEL_8_BREAKING": false +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/output.js new file mode 100644 index 000000000000..e78be3ff8e44 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true-babel-7/output.js @@ -0,0 +1,8 @@ +class Foo { + accessor prop = 1; + static accessor prop2 = 1; + accessor #prop3 = 1; + accessor [prop4] = 1; + accessor prop5 = 1; + accessor f = 1; +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/input.ts new file mode 100644 index 000000000000..b90f613a51bc --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/input.ts @@ -0,0 +1,15 @@ +abstract class Foo { + accessor prop: number = 1; + static accessor prop2: number = 1; + accessor #prop3: number = 1; + accessor [prop4]: number = 1; + private accessor prop5: number = 1; + abstract accessor prop6: number; + private accessor #p: any; + + accessor a!: any; + accessor aa!: any; + abstract accessor #s; + abstract accessor f = 1; + readonly accessor g; +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/options.json new file mode 100644 index 000000000000..2ba2ad643858 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [["transform-typescript", { "allowDeclareFields": false }]], + "parserOpts": { + "plugins": ["decoratorAutoAccessors"] + }, + "BABEL_8_BREAKING": true +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/output.js new file mode 100644 index 000000000000..9a08c34e3c0b --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/accessor-allowDeclareFields-true/output.js @@ -0,0 +1,14 @@ +class Foo { + accessor prop = 1; + static accessor prop2 = 1; + accessor #prop3 = 1; + accessor [prop4] = 1; + accessor prop5 = 1; + accessor prop6; + accessor #p; + accessor a; + accessor aa; + accessor #s; + accessor f = 1; + accessor g; +}