From c5d1c43bc12d7a6e3ed2091478b1dad93b4bfad6 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 17 Apr 2021 11:11:04 -0400 Subject: [PATCH] Support TS 4.3 `override` syntax in class (#13097) * support TS 4.3 `override` syntax in class * fix types * fix types * tweak error message * update TypeScript commit * split tests * add more tests * update allowlist * disallow `override` with `declare` * disallow `override` in non-sub class * update TypeScript allowlist * rename error message key * add more tests --- Makefile | 2 +- .../src/generators/typescript.ts | 4 + .../class-modifier-override/input.js | 8 + .../class-modifier-override/options.json | 4 + .../class-modifier-override/output.js | 11 + .../babel-parser/src/parser/error-message.js | 2 + packages/babel-parser/src/parser/statement.js | 12 +- .../src/plugins/typescript/index.js | 44 ++- packages/babel-parser/src/types.js | 4 +- .../class/modifiers-override-errors/input.ts | 15 + .../modifiers-override-errors/output.json | 237 +++++++++++++ .../class/modifiers-override/input.ts | 20 ++ .../class/modifiers-override/output.json | 332 ++++++++++++++++++ .../src/ast-types/generated/index.ts | 4 + packages/babel-types/src/definitions/core.ts | 3 + 15 files changed, 693 insertions(+), 9 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/typescript/class-modifier-override/input.js create mode 100644 packages/babel-generator/test/fixtures/typescript/class-modifier-override/options.json create mode 100644 packages/babel-generator/test/fixtures/typescript/class-modifier-override/output.js create mode 100644 packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/class/modifiers-override/output.json diff --git a/Makefile b/Makefile index 11fb1f20bac8..3b778bfff20b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25 TEST262_COMMIT = 6888a8df829d416b93cb7057aa860261aa05cf08 -TYPESCRIPT_COMMIT = 41dc625b0a609eb294b975dd92675e72b2b3fdec +TYPESCRIPT_COMMIT = dd1ef88d016dc40a145eafc0a1169e7f0a4a9ebe # Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967 export FORCE_COLOR = true diff --git a/packages/babel-generator/src/generators/typescript.ts b/packages/babel-generator/src/generators/typescript.ts index 475ca7ba5f13..af70e81923d5 100644 --- a/packages/babel-generator/src/generators/typescript.ts +++ b/packages/babel-generator/src/generators/typescript.ts @@ -644,6 +644,10 @@ export function tsPrintClassMemberModifiers(this: Printer, node: any, isField) { this.word("declare"); this.space(); } + if (node.override) { + this.word("override"); + this.space(); + } if (node.accessibility) { this.word(node.accessibility); this.space(); diff --git a/packages/babel-generator/test/fixtures/typescript/class-modifier-override/input.js b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/input.js new file mode 100644 index 000000000000..d70119f8f51d --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/input.js @@ -0,0 +1,8 @@ +class MyClass extends BaseClass { + override show() {} + override public show() {} + public override show() {} + override size = 5; + override readonly size = 5; + readonly override size = 5; +} diff --git a/packages/babel-generator/test/fixtures/typescript/class-modifier-override/options.json b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/options.json new file mode 100644 index 000000000000..9f3a0c2c0ff1 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["typescript", "classProperties"] +} diff --git a/packages/babel-generator/test/fixtures/typescript/class-modifier-override/output.js b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/output.js new file mode 100644 index 000000000000..2be71cd1f582 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/output.js @@ -0,0 +1,11 @@ +class MyClass extends BaseClass { + override show() {} + + override public show() {} + + override public show() {} + + override size = 5; + override readonly size = 5; + override readonly size = 5; +} diff --git a/packages/babel-parser/src/parser/error-message.js b/packages/babel-parser/src/parser/error-message.js index a12f32ae23ea..dd582890d4ab 100644 --- a/packages/babel-parser/src/parser/error-message.js +++ b/packages/babel-parser/src/parser/error-message.js @@ -128,6 +128,8 @@ export const ErrorMessages = makeErrorTemplates( "constructors in/after an Optional Chain are not allowed", OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain", + OverrideOnConstructor: + "'override' modifier cannot appear on a constructor declaration.", ParamDupe: "Argument name clash", PatternHasAccessor: "Object pattern can't contain getter or setter", PatternHasMethod: "Object pattern can't contain methods", diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 0bc322207d01..52fe66190f68 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -1205,15 +1205,12 @@ export default class StatementParser extends ExpressionParser { } // https://tc39.es/ecma262/#prod-ClassBody - parseClassBody( - constructorAllowsSuper: boolean, - oldStrict: boolean, - ): N.ClassBody { + parseClassBody(hadSuperClass: boolean, oldStrict: boolean): N.ClassBody { this.classScope.enter(); const state: N.ParseClassMemberState = { - constructorAllowsSuper, hadConstructor: false, + hadSuperClass, }; let decorators: N.Decorator[] = []; const classBody: N.ClassBody = this.startNode(); @@ -1400,8 +1397,11 @@ export default class StatementParser extends ExpressionParser { if (state.hadConstructor && !this.hasPlugin("typescript")) { this.raise(key.start, Errors.DuplicateConstructor); } + if (isConstructor && this.hasPlugin("typescript") && member.override) { + this.raise(key.start, Errors.OverrideOnConstructor); + } state.hadConstructor = true; - allowsDirectSuper = state.constructorAllowsSuper; + allowsDirectSuper = state.hadSuperClass; } this.pushClassMethod( diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 6a14194376e6..b5eaef411bcb 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -41,6 +41,7 @@ type TsModifier = | "abstract" | "declare" | "static" + | "override" | N.Accessibility; function nonNull(x: ?T): T { @@ -86,12 +87,15 @@ const TSErrors = makeErrorTemplates( ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.", ImportAliasHasImportType: "An import alias can not use 'import type'", + IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.", IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier", IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0')", IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier", + IndexSignatureHasOverride: + "'override' modifier cannot appear on an index signature.", IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier", InvalidModifierOnTypeMember: @@ -107,6 +111,8 @@ const TSErrors = makeErrorTemplates( "'abstract' modifier can only appear on a class, method, or property declaration.", OptionalTypeBeforeRequired: "A required element cannot follow an optional element.", + OverrideNotInSubClass: + "This member cannot have an 'override' modifier because its containing class does not extend another class.", PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.", PrivateElementHasAbstract: @@ -257,6 +263,16 @@ export default (superClass: Class): Class => "static", "readonly", ); + } else if ( + (modified.declare && modifier === "override") || + (modified.override && modifier === "declare") + ) { + this.raise( + startPos, + TSErrors.IncompatibleModifiers, + "declare", + "override", + ); } modified[modifier] = true; } @@ -622,7 +638,15 @@ export default (superClass: Class): Class => this.tsParseModifiers( node, ["readonly"], - ["declare", "abstract", "private", "protected", "public", "static"], + [ + "declare", + "abstract", + "private", + "protected", + "public", + "static", + "override", + ], TSErrors.InvalidModifierOnTypeMember, ); @@ -2223,6 +2247,7 @@ export default (superClass: Class): Class => "private", "public", "protected", + "override", ]); const callParseClassMember = () => { @@ -2246,6 +2271,7 @@ export default (superClass: Class): Class => "readonly", "declare", "static", + "override", ]); if (isStatic) { @@ -2269,6 +2295,9 @@ export default (superClass: Class): Class => if ((member: any).declare) { this.raise(member.start, TSErrors.IndexSignatureHasDeclare); } + if ((member: any).override) { + this.raise(member.start, TSErrors.IndexSignatureHasOverride); + } return; } @@ -2277,6 +2306,19 @@ export default (superClass: Class): Class => this.raise(member.start, TSErrors.NonAbstractClassHasAbstractMethod); } + if ((member: any).override) { + if (isStatic) { + this.raise( + member.start, + TSErrors.IncompatibleModifiers, + "static", + "override", + ); + } else if (!state.hadSuperClass) { + this.raise(member.start, TSErrors.OverrideNotInSubClass); + } + } + /*:: invariant(member.type !== "TSIndexSignature") */ super.parseClassMemberWithIsStatic(classBody, member, state, isStatic); diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index c85630767246..6bf9b9cb0f78 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -730,6 +730,7 @@ export type ClassMemberBase = NodeBase & computed: boolean, // TypeScript only: accessibility?: ?Accessibility, + override?: ?true, abstract?: ?true, optional?: ?true, }; @@ -807,6 +808,7 @@ export type ClassPrivateProperty = NodeBase & { optional?: true, definite?: true, readonly?: true, + override?: true, }; export type OptClassDeclaration = ClassBase & @@ -1536,5 +1538,5 @@ export type ParseSubscriptState = { export type ParseClassMemberState = {| hadConstructor: boolean, - constructorAllowsSuper: boolean, + hadSuperClass: boolean, |}; diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/input.ts b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/input.ts new file mode 100644 index 000000000000..346b74aaf281 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/input.ts @@ -0,0 +1,15 @@ +class MyClass2 extends BaseClass { + override constructor() {} + override [x: string]: any; + override static size = 5; + static override size = 5; +} + +declare class MyClass3 extends BaseClass { + declare override prop1: any + override declare prop2: any +} + +class MyClass4 { + override prop: any +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/output.json b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/output.json new file mode 100644 index 000000000000..85c86638186c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override-errors/output.json @@ -0,0 +1,237 @@ +{ + "type": "File", + "start":0,"end":296,"loc":{"start":{"line":1,"column":0},"end":{"line":15,"column":1}}, + "errors": [ + "SyntaxError: 'override' modifier cannot appear on a constructor declaration. (2:11)", + "SyntaxError: 'override' modifier cannot appear on an index signature. (3:2)", + "SyntaxError: 'static' modifier cannot be used with 'override' modifier. (4:2)", + "SyntaxError: 'static' modifier cannot be used with 'override' modifier. (5:2)", + "SyntaxError: 'declare' modifier cannot be used with 'override' modifier. (9:10)", + "SyntaxError: 'declare' modifier cannot be used with 'override' modifier. (10:11)", + "SyntaxError: This member cannot have an 'override' modifier because its containing class does not extend another class. (14:2)" + ], + "program": { + "type": "Program", + "start":0,"end":296,"loc":{"start":{"line":1,"column":0},"end":{"line":15,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":149,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":14,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":14},"identifierName":"MyClass2"}, + "name": "MyClass2" + }, + "superClass": { + "type": "Identifier", + "start":23,"end":32,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":32},"identifierName":"BaseClass"}, + "name": "BaseClass" + }, + "body": { + "type": "ClassBody", + "start":33,"end":149,"loc":{"start":{"line":1,"column":33},"end":{"line":6,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":37,"end":62,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":27}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":46,"end":57,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":22},"identifierName":"constructor"}, + "name": "constructor" + }, + "computed": false, + "kind": "constructor", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":60,"end":62,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":27}}, + "body": [], + "directives": [] + } + }, + { + "type": "TSIndexSignature", + "start":65,"end":91,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":28}}, + "override": true, + "parameters": [ + { + "type": "Identifier", + "start":75,"end":84,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":21},"identifierName":"x"}, + "name": "x", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":76,"end":84,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":21}}, + "typeAnnotation": { + "type": "TSStringKeyword", + "start":78,"end":84,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":21}} + } + } + } + ], + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":85,"end":90,"loc":{"start":{"line":3,"column":22},"end":{"line":3,"column":27}}, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":87,"end":90,"loc":{"start":{"line":3,"column":24},"end":{"line":3,"column":27}} + } + } + }, + { + "type": "ClassProperty", + "start":94,"end":119,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":27}}, + "override": true, + "static": true, + "key": { + "type": "Identifier", + "start":110,"end":114,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":22},"identifierName":"size"}, + "name": "size" + }, + "computed": false, + "value": { + "type": "NumericLiteral", + "start":117,"end":118,"loc":{"start":{"line":4,"column":25},"end":{"line":4,"column":26}}, + "extra": { + "rawValue": 5, + "raw": "5" + }, + "value": 5 + } + }, + { + "type": "ClassProperty", + "start":122,"end":147,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":27}}, + "override": true, + "static": true, + "key": { + "type": "Identifier", + "start":138,"end":142,"loc":{"start":{"line":5,"column":18},"end":{"line":5,"column":22},"identifierName":"size"}, + "name": "size" + }, + "computed": false, + "value": { + "type": "NumericLiteral", + "start":145,"end":146,"loc":{"start":{"line":5,"column":25},"end":{"line":5,"column":26}}, + "extra": { + "rawValue": 5, + "raw": "5" + }, + "value": 5 + } + } + ] + } + }, + { + "type": "ClassDeclaration", + "start":151,"end":255,"loc":{"start":{"line":8,"column":0},"end":{"line":11,"column":1}}, + "declare": true, + "id": { + "type": "Identifier", + "start":165,"end":173,"loc":{"start":{"line":8,"column":14},"end":{"line":8,"column":22},"identifierName":"MyClass3"}, + "name": "MyClass3" + }, + "superClass": { + "type": "Identifier", + "start":182,"end":191,"loc":{"start":{"line":8,"column":31},"end":{"line":8,"column":40},"identifierName":"BaseClass"}, + "name": "BaseClass" + }, + "body": { + "type": "ClassBody", + "start":192,"end":255,"loc":{"start":{"line":8,"column":41},"end":{"line":11,"column":1}}, + "body": [ + { + "type": "ClassProperty", + "start":196,"end":223,"loc":{"start":{"line":9,"column":2},"end":{"line":9,"column":29}}, + "declare": true, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":213,"end":218,"loc":{"start":{"line":9,"column":19},"end":{"line":9,"column":24},"identifierName":"prop1"}, + "name": "prop1" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":218,"end":223,"loc":{"start":{"line":9,"column":24},"end":{"line":9,"column":29}}, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":220,"end":223,"loc":{"start":{"line":9,"column":26},"end":{"line":9,"column":29}} + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start":226,"end":253,"loc":{"start":{"line":10,"column":2},"end":{"line":10,"column":29}}, + "override": true, + "declare": true, + "static": false, + "key": { + "type": "Identifier", + "start":243,"end":248,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":24},"identifierName":"prop2"}, + "name": "prop2" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":248,"end":253,"loc":{"start":{"line":10,"column":24},"end":{"line":10,"column":29}}, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":250,"end":253,"loc":{"start":{"line":10,"column":26},"end":{"line":10,"column":29}} + } + }, + "value": null + } + ] + } + }, + { + "type": "ClassDeclaration", + "start":257,"end":296,"loc":{"start":{"line":13,"column":0},"end":{"line":15,"column":1}}, + "id": { + "type": "Identifier", + "start":263,"end":271,"loc":{"start":{"line":13,"column":6},"end":{"line":13,"column":14},"identifierName":"MyClass4"}, + "name": "MyClass4" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":272,"end":296,"loc":{"start":{"line":13,"column":15},"end":{"line":15,"column":1}}, + "body": [ + { + "type": "ClassProperty", + "start":276,"end":294,"loc":{"start":{"line":14,"column":2},"end":{"line":14,"column":20}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":285,"end":289,"loc":{"start":{"line":14,"column":11},"end":{"line":14,"column":15},"identifierName":"prop"}, + "name": "prop" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":289,"end":294,"loc":{"start":{"line":14,"column":15},"end":{"line":14,"column":20}}, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":291,"end":294,"loc":{"start":{"line":14,"column":17},"end":{"line":14,"column":20}} + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts new file mode 100644 index 000000000000..b532036d7e90 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts @@ -0,0 +1,20 @@ +class MyClass extends BaseClass { + override show() {} + override public show() {} + public override show() {} + override size = 5; + override readonly size = 5; + readonly override size = 5; + + override get text() {} + override set text(value) {} + + override async fetch() {} + + override [x] = 2 + override [x]() {} +} + +declare class DeclaredClass extends BaseClass { + override test() {} +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/output.json b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/output.json new file mode 100644 index 000000000000..293d14d661e2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/output.json @@ -0,0 +1,332 @@ +{ + "type": "File", + "start":0,"end":390,"loc":{"start":{"line":1,"column":0},"end":{"line":20,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":390,"loc":{"start":{"line":1,"column":0},"end":{"line":20,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":318,"loc":{"start":{"line":1,"column":0},"end":{"line":16,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":13,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":13},"identifierName":"MyClass"}, + "name": "MyClass" + }, + "superClass": { + "type": "Identifier", + "start":22,"end":31,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":31},"identifierName":"BaseClass"}, + "name": "BaseClass" + }, + "body": { + "type": "ClassBody", + "start":32,"end":318,"loc":{"start":{"line":1,"column":32},"end":{"line":16,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":36,"end":54,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":20}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":45,"end":49,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":15},"identifierName":"show"}, + "name": "show" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":52,"end":54,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":20}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":57,"end":82,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":27}}, + "override": true, + "accessibility": "public", + "static": false, + "key": { + "type": "Identifier", + "start":73,"end":77,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":22},"identifierName":"show"}, + "name": "show" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":80,"end":82,"loc":{"start":{"line":3,"column":25},"end":{"line":3,"column":27}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":85,"end":110,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":27}}, + "accessibility": "public", + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":101,"end":105,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":22},"identifierName":"show"}, + "name": "show" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":108,"end":110,"loc":{"start":{"line":4,"column":25},"end":{"line":4,"column":27}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassProperty", + "start":113,"end":131,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":20}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":122,"end":126,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":15},"identifierName":"size"}, + "name": "size" + }, + "computed": false, + "value": { + "type": "NumericLiteral", + "start":129,"end":130,"loc":{"start":{"line":5,"column":18},"end":{"line":5,"column":19}}, + "extra": { + "rawValue": 5, + "raw": "5" + }, + "value": 5 + } + }, + { + "type": "ClassProperty", + "start":134,"end":161,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":29}}, + "override": true, + "readonly": true, + "static": false, + "key": { + "type": "Identifier", + "start":152,"end":156,"loc":{"start":{"line":6,"column":20},"end":{"line":6,"column":24},"identifierName":"size"}, + "name": "size" + }, + "computed": false, + "value": { + "type": "NumericLiteral", + "start":159,"end":160,"loc":{"start":{"line":6,"column":27},"end":{"line":6,"column":28}}, + "extra": { + "rawValue": 5, + "raw": "5" + }, + "value": 5 + } + }, + { + "type": "ClassProperty", + "start":164,"end":191,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"column":29}}, + "readonly": true, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":182,"end":186,"loc":{"start":{"line":7,"column":20},"end":{"line":7,"column":24},"identifierName":"size"}, + "name": "size" + }, + "computed": false, + "value": { + "type": "NumericLiteral", + "start":189,"end":190,"loc":{"start":{"line":7,"column":27},"end":{"line":7,"column":28}}, + "extra": { + "rawValue": 5, + "raw": "5" + }, + "value": 5 + } + }, + { + "type": "ClassMethod", + "start":195,"end":217,"loc":{"start":{"line":9,"column":2},"end":{"line":9,"column":24}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":208,"end":212,"loc":{"start":{"line":9,"column":15},"end":{"line":9,"column":19},"identifierName":"text"}, + "name": "text" + }, + "computed": false, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":215,"end":217,"loc":{"start":{"line":9,"column":22},"end":{"line":9,"column":24}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":220,"end":247,"loc":{"start":{"line":10,"column":2},"end":{"line":10,"column":29}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":233,"end":237,"loc":{"start":{"line":10,"column":15},"end":{"line":10,"column":19},"identifierName":"text"}, + "name": "text" + }, + "computed": false, + "kind": "set", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":238,"end":243,"loc":{"start":{"line":10,"column":20},"end":{"line":10,"column":25},"identifierName":"value"}, + "name": "value" + } + ], + "body": { + "type": "BlockStatement", + "start":245,"end":247,"loc":{"start":{"line":10,"column":27},"end":{"line":10,"column":29}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":251,"end":276,"loc":{"start":{"line":12,"column":2},"end":{"line":12,"column":27}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":266,"end":271,"loc":{"start":{"line":12,"column":17},"end":{"line":12,"column":22},"identifierName":"fetch"}, + "name": "fetch" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":274,"end":276,"loc":{"start":{"line":12,"column":25},"end":{"line":12,"column":27}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassProperty", + "start":280,"end":296,"loc":{"start":{"line":14,"column":2},"end":{"line":14,"column":18}}, + "override": true, + "static": false, + "computed": true, + "key": { + "type": "Identifier", + "start":290,"end":291,"loc":{"start":{"line":14,"column":12},"end":{"line":14,"column":13},"identifierName":"x"}, + "name": "x" + }, + "value": { + "type": "NumericLiteral", + "start":295,"end":296,"loc":{"start":{"line":14,"column":17},"end":{"line":14,"column":18}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + }, + { + "type": "ClassMethod", + "start":299,"end":316,"loc":{"start":{"line":15,"column":2},"end":{"line":15,"column":19}}, + "override": true, + "static": false, + "computed": true, + "key": { + "type": "Identifier", + "start":309,"end":310,"loc":{"start":{"line":15,"column":12},"end":{"line":15,"column":13},"identifierName":"x"}, + "name": "x" + }, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":314,"end":316,"loc":{"start":{"line":15,"column":17},"end":{"line":15,"column":19}}, + "body": [], + "directives": [] + } + } + ] + } + }, + { + "type": "ClassDeclaration", + "start":320,"end":390,"loc":{"start":{"line":18,"column":0},"end":{"line":20,"column":1}}, + "declare": true, + "id": { + "type": "Identifier", + "start":334,"end":347,"loc":{"start":{"line":18,"column":14},"end":{"line":18,"column":27},"identifierName":"DeclaredClass"}, + "name": "DeclaredClass" + }, + "superClass": { + "type": "Identifier", + "start":356,"end":365,"loc":{"start":{"line":18,"column":36},"end":{"line":18,"column":45},"identifierName":"BaseClass"}, + "name": "BaseClass" + }, + "body": { + "type": "ClassBody", + "start":366,"end":390,"loc":{"start":{"line":18,"column":46},"end":{"line":20,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":370,"end":388,"loc":{"start":{"line":19,"column":2},"end":{"line":19,"column":20}}, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":379,"end":383,"loc":{"start":{"line":19,"column":11},"end":{"line":19,"column":15},"identifierName":"test"}, + "name": "test" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":386,"end":388,"loc":{"start":{"line":19,"column":18},"end":{"line":19,"column":20}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index 80b653c30440..da47784f7529 100755 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -909,6 +909,7 @@ export interface ClassMethod extends BaseNode { accessibility?: "public" | "private" | "protected" | null; decorators?: Array | null; optional?: boolean | null; + override?: boolean; returnType?: TypeAnnotation | TSTypeAnnotation | Noop | null; typeParameters?: | TypeParameterDeclaration @@ -1535,6 +1536,7 @@ export interface ClassProperty extends BaseNode { declare?: boolean | null; definite?: boolean | null; optional?: boolean | null; + override?: boolean; readonly?: boolean | null; } @@ -1576,6 +1578,7 @@ export interface ClassPrivateMethod extends BaseNode { decorators?: Array | null; generator?: boolean; optional?: boolean | null; + override?: boolean; returnType?: TypeAnnotation | TSTypeAnnotation | Noop | null; typeParameters?: | TypeParameterDeclaration @@ -1668,6 +1671,7 @@ export interface TSDeclareMethod extends BaseNode { generator?: boolean; kind?: "get" | "set" | "method" | "constructor"; optional?: boolean | null; + override?: boolean; static?: boolean; } diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index a14636cd0fed..412f670989f3 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -1705,6 +1705,9 @@ export const classMethodOrPropertyCommon = { static: { default: false, }, + override: { + default: false, + }, computed: { default: false, },