diff --git a/Makefile b/Makefile index 74e37474c9cf..737874b67097 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25 TEST262_COMMIT = eca69e2c95972a4c5780ef58fe1f1e53e871b9b1 -TYPESCRIPT_COMMIT = dd1ef88d016dc40a145eafc0a1169e7f0a4a9ebe +TYPESCRIPT_COMMIT = 3de706a8525c2ded782fc032fa4afe2e485100d3 # 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 742cefa1dfaa..5b2c572d6971 100644 --- a/packages/babel-generator/src/generators/typescript.ts +++ b/packages/babel-generator/src/generators/typescript.ts @@ -649,10 +649,6 @@ 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(); @@ -661,6 +657,10 @@ export function tsPrintClassMemberModifiers(this: Printer, node: any, isField) { this.word("static"); this.space(); } + if (node.override) { + this.word("override"); + this.space(); + } if (node.abstract) { this.word("abstract"); 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 index d70119f8f51d..0d39fc58c990 100644 --- a/packages/babel-generator/test/fixtures/typescript/class-modifier-override/input.js +++ b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/input.js @@ -1,8 +1,6 @@ class MyClass extends BaseClass { override show() {} - override public show() {} public override show() {} override size = 5; - override readonly size = 5; - readonly override size = 5; + public static override readonly size = 5; } 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 index 2be71cd1f582..305f5ebb5f93 100644 --- a/packages/babel-generator/test/fixtures/typescript/class-modifier-override/output.js +++ b/packages/babel-generator/test/fixtures/typescript/class-modifier-override/output.js @@ -1,11 +1,8 @@ class MyClass extends BaseClass { override show() {} - override public show() {} - - override public show() {} + public override show() {} override size = 5; - override readonly size = 5; - override readonly size = 5; -} + public static override readonly size = 5; +} \ No newline at end of file diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index d888f8b1ceb3..6a0756107d8f 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -253,6 +253,20 @@ export default (superClass: Class): Class => disallowedModifiers?: TsModifier[], errorTemplate?: ErrorTemplate, ): void { + const enforceOrder = (pos, modifier, before, after) => { + if (modifier === before && modified[after]) { + this.raise(pos, TSErrors.InvalidModifiersOrder, before, after); + } + }; + const incompatible = (pos, modifier, mod1, mod2) => { + if ( + (modified[mod1] && modifier === mod2) || + (modified[mod2] && modifier === mod1) + ) { + this.raise(pos, TSErrors.IncompatibleModifiers, mod1, mod2); + } + }; + for (;;) { const startPos = this.state.start; const modifier: ?TsModifier = this.tsParseModifier( @@ -265,28 +279,22 @@ export default (superClass: Class): Class => if (modified.accessibility) { this.raise(startPos, TSErrors.DuplicateAccessibilityModifier); } else { + enforceOrder(startPos, modifier, modifier, "override"); + enforceOrder(startPos, modifier, modifier, "static"); + modified.accessibility = modifier; } } else { if (Object.hasOwnProperty.call(modified, modifier)) { this.raise(startPos, TSErrors.DuplicateModifier, modifier); - } else if (modified.readonly && modifier === "static") { - this.raise( - startPos, - TSErrors.InvalidModifiersOrder, - "static", - "readonly", - ); - } else if ( - (modified.declare && modifier === "override") || - (modified.override && modifier === "declare") - ) { - this.raise( - startPos, - TSErrors.IncompatibleModifiers, - "declare", - "override", - ); + } else { + enforceOrder(startPos, modifier, "static", "readonly"); + enforceOrder(startPos, modifier, "static", "override"); + enforceOrder(startPos, modifier, "override", "readonly"); + enforceOrder(startPos, modifier, "abstract", "override"); + + incompatible(startPos, modifier, "declare", "override"); + incompatible(startPos, modifier, "static", "abstract"); } modified[modifier] = true; } @@ -2320,10 +2328,18 @@ export default (superClass: Class): Class => "public", "protected", "override", + "static", + "abstract", + "readonly", ]); const callParseClassMember = () => { - super.parseClassMember(classBody, member, state); + this.parseClassMemberWithIsStatic( + classBody, + member, + state, + !!member.static, + ); }; if (member.declare) { this.tsInDeclareContext(callParseClassMember); @@ -2338,18 +2354,6 @@ export default (superClass: Class): Class => state: N.ParseClassMemberState, isStatic: boolean, ): void { - this.tsParseModifiers(member, [ - "abstract", - "readonly", - "declare", - "static", - "override", - ]); - - if (isStatic) { - member.static = true; - } - const idx = this.tsTryParseIndexSignature(member); if (idx) { classBody.body.push(idx); @@ -2379,14 +2383,7 @@ export default (superClass: Class): Class => } if ((member: any).override) { - if (isStatic) { - this.raise( - member.start, - TSErrors.IncompatibleModifiers, - "static", - "override", - ); - } else if (!state.hadSuperClass) { + if (!state.hadSuperClass) { this.raise(member.start, TSErrors.OverrideNotInSubClass); } } diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-incompatible/input.ts b/packages/babel-parser/test/fixtures/typescript/class/modifiers-incompatible/input.ts new file mode 100644 index 000000000000..256313f8368f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-incompatible/input.ts @@ -0,0 +1,7 @@ +abstract class A extends Base { + static abstract m1(); + abstract static m1(); + + declare override prop1: any; + override declare prop2: any; +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-incompatible/output.json b/packages/babel-parser/test/fixtures/typescript/class/modifiers-incompatible/output.json new file mode 100644 index 000000000000..a0ffbf7b019e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-incompatible/output.json @@ -0,0 +1,118 @@ +{ + "type": "File", + "start":0,"end":144,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "errors": [ + "SyntaxError: 'static' modifier cannot be used with 'abstract' modifier. (2:9)", + "SyntaxError: 'static' modifier cannot be used with 'abstract' modifier. (3:11)", + "SyntaxError: 'declare' modifier cannot be used with 'override' modifier. (5:10)", + "SyntaxError: 'declare' modifier cannot be used with 'override' modifier. (6:11)" + ], + "program": { + "type": "Program", + "start":0,"end":144,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":144,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "abstract": true, + "id": { + "type": "Identifier", + "start":15,"end":16,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":16},"identifierName":"A"}, + "name": "A" + }, + "superClass": { + "type": "Identifier", + "start":25,"end":29,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":29},"identifierName":"Base"}, + "name": "Base" + }, + "body": { + "type": "ClassBody", + "start":30,"end":144,"loc":{"start":{"line":1,"column":30},"end":{"line":7,"column":1}}, + "body": [ + { + "type": "TSDeclareMethod", + "start":34,"end":55,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":23}}, + "static": true, + "abstract": true, + "key": { + "type": "Identifier", + "start":50,"end":52,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":20},"identifierName":"m1"}, + "name": "m1" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start":58,"end":79,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":23}}, + "abstract": true, + "static": true, + "key": { + "type": "Identifier", + "start":74,"end":76,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":20},"identifierName":"m1"}, + "name": "m1" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [] + }, + { + "type": "ClassProperty", + "start":83,"end":111,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":30}}, + "declare": true, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":100,"end":105,"loc":{"start":{"line":5,"column":19},"end":{"line":5,"column":24},"identifierName":"prop1"}, + "name": "prop1" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":105,"end":110,"loc":{"start":{"line":5,"column":24},"end":{"line":5,"column":29}}, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":107,"end":110,"loc":{"start":{"line":5,"column":26},"end":{"line":5,"column":29}} + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start":114,"end":142,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":30}}, + "override": true, + "declare": true, + "static": false, + "key": { + "type": "Identifier", + "start":131,"end":136,"loc":{"start":{"line":6,"column":19},"end":{"line":6,"column":24},"identifierName":"prop2"}, + "name": "prop2" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":136,"end":141,"loc":{"start":{"line":6,"column":24},"end":{"line":6,"column":29}}, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":138,"end":141,"loc":{"start":{"line":6,"column":26},"end":{"line":6,"column":29}} + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/input.ts b/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/input.ts new file mode 100644 index 000000000000..87368f121bf7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/input.ts @@ -0,0 +1,28 @@ +abstract class A extends B { + static override m1() {} + override static m2() {} + + override readonly p4; + readonly override p3; + + public override m5() {} + override public m6() {} + + protected override m7() {} + override protected m8() {} + + private override m9() {} + override private m10() {} + + abstract override m12(); + override abstract m11(); + + public static m14() {} + static public m13() {} + + protected static m16() {} + static protected m15() {} + + private static m18() {} + static private m17() {} +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/output.json b/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/output.json new file mode 100644 index 000000000000..858e2bba559b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/output.json @@ -0,0 +1,437 @@ +{ + "type": "File", + "start":0,"end":515,"loc":{"start":{"line":1,"column":0},"end":{"line":28,"column":1}}, + "errors": [ + "SyntaxError: 'static' modifier must precede 'override' modifier. (3:11)", + "SyntaxError: 'override' modifier must precede 'readonly' modifier. (6:11)", + "SyntaxError: 'public' modifier must precede 'override' modifier. (9:11)", + "SyntaxError: 'protected' modifier must precede 'override' modifier. (12:11)", + "SyntaxError: 'private' modifier must precede 'override' modifier. (15:11)", + "SyntaxError: 'abstract' modifier must precede 'override' modifier. (18:11)", + "SyntaxError: 'public' modifier must precede 'static' modifier. (21:9)", + "SyntaxError: 'protected' modifier must precede 'static' modifier. (24:9)", + "SyntaxError: 'private' modifier must precede 'static' modifier. (27:9)" + ], + "program": { + "type": "Program", + "start":0,"end":515,"loc":{"start":{"line":1,"column":0},"end":{"line":28,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":515,"loc":{"start":{"line":1,"column":0},"end":{"line":28,"column":1}}, + "abstract": true, + "id": { + "type": "Identifier", + "start":15,"end":16,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":16},"identifierName":"A"}, + "name": "A" + }, + "superClass": { + "type": "Identifier", + "start":25,"end":26,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":26},"identifierName":"B"}, + "name": "B" + }, + "body": { + "type": "ClassBody", + "start":27,"end":515,"loc":{"start":{"line":1,"column":27},"end":{"line":28,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":31,"end":54,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":25}}, + "static": true, + "override": true, + "key": { + "type": "Identifier", + "start":47,"end":49,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":20},"identifierName":"m1"}, + "name": "m1" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":52,"end":54,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":25}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":57,"end":80,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":25}}, + "override": true, + "static": true, + "key": { + "type": "Identifier", + "start":73,"end":75,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":20},"identifierName":"m2"}, + "name": "m2" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":78,"end":80,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":25}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassProperty", + "start":84,"end":105,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":23}}, + "override": true, + "readonly": true, + "static": false, + "key": { + "type": "Identifier", + "start":102,"end":104,"loc":{"start":{"line":5,"column":20},"end":{"line":5,"column":22},"identifierName":"p4"}, + "name": "p4" + }, + "computed": false, + "value": null + }, + { + "type": "ClassProperty", + "start":108,"end":129,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":23}}, + "readonly": true, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":126,"end":128,"loc":{"start":{"line":6,"column":20},"end":{"line":6,"column":22},"identifierName":"p3"}, + "name": "p3" + }, + "computed": false, + "value": null + }, + { + "type": "ClassMethod", + "start":133,"end":156,"loc":{"start":{"line":8,"column":2},"end":{"line":8,"column":25}}, + "accessibility": "public", + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":149,"end":151,"loc":{"start":{"line":8,"column":18},"end":{"line":8,"column":20},"identifierName":"m5"}, + "name": "m5" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":154,"end":156,"loc":{"start":{"line":8,"column":23},"end":{"line":8,"column":25}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":159,"end":182,"loc":{"start":{"line":9,"column":2},"end":{"line":9,"column":25}}, + "override": true, + "accessibility": "public", + "static": false, + "key": { + "type": "Identifier", + "start":175,"end":177,"loc":{"start":{"line":9,"column":18},"end":{"line":9,"column":20},"identifierName":"m6"}, + "name": "m6" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":180,"end":182,"loc":{"start":{"line":9,"column":23},"end":{"line":9,"column":25}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":186,"end":212,"loc":{"start":{"line":11,"column":2},"end":{"line":11,"column":28}}, + "accessibility": "protected", + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":205,"end":207,"loc":{"start":{"line":11,"column":21},"end":{"line":11,"column":23},"identifierName":"m7"}, + "name": "m7" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":210,"end":212,"loc":{"start":{"line":11,"column":26},"end":{"line":11,"column":28}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":215,"end":241,"loc":{"start":{"line":12,"column":2},"end":{"line":12,"column":28}}, + "override": true, + "accessibility": "protected", + "static": false, + "key": { + "type": "Identifier", + "start":234,"end":236,"loc":{"start":{"line":12,"column":21},"end":{"line":12,"column":23},"identifierName":"m8"}, + "name": "m8" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":239,"end":241,"loc":{"start":{"line":12,"column":26},"end":{"line":12,"column":28}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":245,"end":269,"loc":{"start":{"line":14,"column":2},"end":{"line":14,"column":26}}, + "accessibility": "private", + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":262,"end":264,"loc":{"start":{"line":14,"column":19},"end":{"line":14,"column":21},"identifierName":"m9"}, + "name": "m9" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":267,"end":269,"loc":{"start":{"line":14,"column":24},"end":{"line":14,"column":26}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":272,"end":297,"loc":{"start":{"line":15,"column":2},"end":{"line":15,"column":27}}, + "override": true, + "accessibility": "private", + "static": false, + "key": { + "type": "Identifier", + "start":289,"end":292,"loc":{"start":{"line":15,"column":19},"end":{"line":15,"column":22},"identifierName":"m10"}, + "name": "m10" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":295,"end":297,"loc":{"start":{"line":15,"column":25},"end":{"line":15,"column":27}}, + "body": [], + "directives": [] + } + }, + { + "type": "TSDeclareMethod", + "start":301,"end":325,"loc":{"start":{"line":17,"column":2},"end":{"line":17,"column":26}}, + "abstract": true, + "override": true, + "static": false, + "key": { + "type": "Identifier", + "start":319,"end":322,"loc":{"start":{"line":17,"column":20},"end":{"line":17,"column":23},"identifierName":"m12"}, + "name": "m12" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [] + }, + { + "type": "TSDeclareMethod", + "start":328,"end":352,"loc":{"start":{"line":18,"column":2},"end":{"line":18,"column":26}}, + "override": true, + "abstract": true, + "static": false, + "key": { + "type": "Identifier", + "start":346,"end":349,"loc":{"start":{"line":18,"column":20},"end":{"line":18,"column":23},"identifierName":"m11"}, + "name": "m11" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start":356,"end":378,"loc":{"start":{"line":20,"column":2},"end":{"line":20,"column":24}}, + "accessibility": "public", + "static": true, + "key": { + "type": "Identifier", + "start":370,"end":373,"loc":{"start":{"line":20,"column":16},"end":{"line":20,"column":19},"identifierName":"m14"}, + "name": "m14" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":376,"end":378,"loc":{"start":{"line":20,"column":22},"end":{"line":20,"column":24}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":381,"end":403,"loc":{"start":{"line":21,"column":2},"end":{"line":21,"column":24}}, + "static": true, + "accessibility": "public", + "key": { + "type": "Identifier", + "start":395,"end":398,"loc":{"start":{"line":21,"column":16},"end":{"line":21,"column":19},"identifierName":"m13"}, + "name": "m13" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":401,"end":403,"loc":{"start":{"line":21,"column":22},"end":{"line":21,"column":24}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":407,"end":432,"loc":{"start":{"line":23,"column":2},"end":{"line":23,"column":27}}, + "accessibility": "protected", + "static": true, + "key": { + "type": "Identifier", + "start":424,"end":427,"loc":{"start":{"line":23,"column":19},"end":{"line":23,"column":22},"identifierName":"m16"}, + "name": "m16" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":430,"end":432,"loc":{"start":{"line":23,"column":25},"end":{"line":23,"column":27}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":435,"end":460,"loc":{"start":{"line":24,"column":2},"end":{"line":24,"column":27}}, + "static": true, + "accessibility": "protected", + "key": { + "type": "Identifier", + "start":452,"end":455,"loc":{"start":{"line":24,"column":19},"end":{"line":24,"column":22},"identifierName":"m15"}, + "name": "m15" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":458,"end":460,"loc":{"start":{"line":24,"column":25},"end":{"line":24,"column":27}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":464,"end":487,"loc":{"start":{"line":26,"column":2},"end":{"line":26,"column":25}}, + "accessibility": "private", + "static": true, + "key": { + "type": "Identifier", + "start":479,"end":482,"loc":{"start":{"line":26,"column":17},"end":{"line":26,"column":20},"identifierName":"m18"}, + "name": "m18" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":485,"end":487,"loc":{"start":{"line":26,"column":23},"end":{"line":26,"column":25}}, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start":490,"end":513,"loc":{"start":{"line":27,"column":2},"end":{"line":27,"column":25}}, + "static": true, + "accessibility": "private", + "key": { + "type": "Identifier", + "start":505,"end":508,"loc":{"start":{"line":27,"column":17},"end":{"line":27,"column":20},"identifierName":"m17"}, + "name": "m17" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":511,"end":513,"loc":{"start":{"line":27,"column":23},"end":{"line":27,"column":25}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file 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 index 346b74aaf281..b7520b92e311 100644 --- 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 @@ -1,13 +1,6 @@ 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 { 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 index 85c86638186c..a0182a9984d9 100644 --- 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 @@ -1,24 +1,20 @@ { "type": "File", - "start":0,"end":296,"loc":{"start":{"line":1,"column":0},"end":{"line":15,"column":1}}, + "start":0,"end":134,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"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)" + "SyntaxError: This member cannot have an 'override' modifier because its containing class does not extend another class. (7:2)" ], "program": { "type": "Program", - "start":0,"end":296,"loc":{"start":{"line":1,"column":0},"end":{"line":15,"column":1}}, + "start":0,"end":134,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ClassDeclaration", - "start":0,"end":149,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "start":0,"end":93,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, "id": { "type": "Identifier", "start":6,"end":14,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":14},"identifierName":"MyClass2"}, @@ -31,7 +27,7 @@ }, "body": { "type": "ClassBody", - "start":33,"end":149,"loc":{"start":{"line":1,"column":33},"end":{"line":6,"column":1}}, + "start":33,"end":93,"loc":{"start":{"line":1,"column":33},"end":{"line":4,"column":1}}, "body": [ { "type": "ClassMethod", @@ -83,147 +79,40 @@ "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}}, + "start":95,"end":134,"loc":{"start":{"line":6,"column":0},"end":{"line":8,"column":1}}, "id": { "type": "Identifier", - "start":263,"end":271,"loc":{"start":{"line":13,"column":6},"end":{"line":13,"column":14},"identifierName":"MyClass4"}, + "start":101,"end":109,"loc":{"start":{"line":6,"column":6},"end":{"line":6,"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}}, + "start":110,"end":134,"loc":{"start":{"line":6,"column":15},"end":{"line":8,"column":1}}, "body": [ { "type": "ClassProperty", - "start":276,"end":294,"loc":{"start":{"line":14,"column":2},"end":{"line":14,"column":20}}, + "start":114,"end":132,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"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"}, + "start":123,"end":127,"loc":{"start":{"line":7,"column":11},"end":{"line":7,"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}}, + "start":127,"end":132,"loc":{"start":{"line":7,"column":15},"end":{"line":7,"column":20}}, "typeAnnotation": { "type": "TSAnyKeyword", - "start":291,"end":294,"loc":{"start":{"line":14,"column":17},"end":{"line":14,"column":20}} + "start":129,"end":132,"loc":{"start":{"line":7,"column":17},"end":{"line":7,"column":20}} } }, "value": null 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 index b532036d7e90..df2255f6042a 100644 --- a/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/input.ts @@ -1,10 +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; override get text() {} override set text(value) {} 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 index 293d14d661e2..0308713d4a6d 100644 --- a/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/output.json +++ b/packages/babel-parser/test/fixtures/typescript/class/modifiers-override/output.json @@ -1,15 +1,15 @@ { "type": "File", - "start":0,"end":390,"loc":{"start":{"line":1,"column":0},"end":{"line":20,"column":1}}, + "start":0,"end":332,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"column":1}}, "program": { "type": "Program", - "start":0,"end":390,"loc":{"start":{"line":1,"column":0},"end":{"line":20,"column":1}}, + "start":0,"end":332,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"column":1}}, "sourceType": "module", "interpreter": null, "body": [ { "type": "ClassDeclaration", - "start":0,"end":318,"loc":{"start":{"line":1,"column":0},"end":{"line":16,"column":1}}, + "start":0,"end":260,"loc":{"start":{"line":1,"column":0},"end":{"line":14,"column":1}}, "id": { "type": "Identifier", "start":6,"end":13,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":13},"identifierName":"MyClass"}, @@ -22,7 +22,7 @@ }, "body": { "type": "ClassBody", - "start":32,"end":318,"loc":{"start":{"line":1,"column":32},"end":{"line":16,"column":1}}, + "start":32,"end":260,"loc":{"start":{"line":1,"column":32},"end":{"line":14,"column":1}}, "body": [ { "type": "ClassMethod", @@ -50,8 +50,8 @@ { "type": "ClassMethod", "start":57,"end":82,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":27}}, - "override": true, "accessibility": "public", + "override": true, "static": false, "key": { "type": "Identifier", @@ -71,44 +71,20 @@ "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}}, + "start":85,"end":103,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"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"}, + "start":94,"end":98,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"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}}, + "start":101,"end":102,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":19}}, "extra": { "rawValue": 5, "raw": "5" @@ -118,41 +94,19 @@ }, { "type": "ClassProperty", - "start":134,"end":161,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":29}}, + "start":106,"end":133,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"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"}, + "start":124,"end":128,"loc":{"start":{"line":5,"column":20},"end":{"line":5,"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}}, + "start":131,"end":132,"loc":{"start":{"line":5,"column":27},"end":{"line":5,"column":28}}, "extra": { "rawValue": 5, "raw": "5" @@ -162,12 +116,12 @@ }, { "type": "ClassMethod", - "start":195,"end":217,"loc":{"start":{"line":9,"column":2},"end":{"line":9,"column":24}}, + "start":137,"end":159,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"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"}, + "start":150,"end":154,"loc":{"start":{"line":7,"column":15},"end":{"line":7,"column":19},"identifierName":"text"}, "name": "text" }, "computed": false, @@ -178,19 +132,19 @@ "params": [], "body": { "type": "BlockStatement", - "start":215,"end":217,"loc":{"start":{"line":9,"column":22},"end":{"line":9,"column":24}}, + "start":157,"end":159,"loc":{"start":{"line":7,"column":22},"end":{"line":7,"column":24}}, "body": [], "directives": [] } }, { "type": "ClassMethod", - "start":220,"end":247,"loc":{"start":{"line":10,"column":2},"end":{"line":10,"column":29}}, + "start":162,"end":189,"loc":{"start":{"line":8,"column":2},"end":{"line":8,"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"}, + "start":175,"end":179,"loc":{"start":{"line":8,"column":15},"end":{"line":8,"column":19},"identifierName":"text"}, "name": "text" }, "computed": false, @@ -201,25 +155,25 @@ "params": [ { "type": "Identifier", - "start":238,"end":243,"loc":{"start":{"line":10,"column":20},"end":{"line":10,"column":25},"identifierName":"value"}, + "start":180,"end":185,"loc":{"start":{"line":8,"column":20},"end":{"line":8,"column":25},"identifierName":"value"}, "name": "value" } ], "body": { "type": "BlockStatement", - "start":245,"end":247,"loc":{"start":{"line":10,"column":27},"end":{"line":10,"column":29}}, + "start":187,"end":189,"loc":{"start":{"line":8,"column":27},"end":{"line":8,"column":29}}, "body": [], "directives": [] } }, { "type": "ClassMethod", - "start":251,"end":276,"loc":{"start":{"line":12,"column":2},"end":{"line":12,"column":27}}, + "start":193,"end":218,"loc":{"start":{"line":10,"column":2},"end":{"line":10,"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"}, + "start":208,"end":213,"loc":{"start":{"line":10,"column":17},"end":{"line":10,"column":22},"identifierName":"fetch"}, "name": "fetch" }, "computed": false, @@ -230,25 +184,25 @@ "params": [], "body": { "type": "BlockStatement", - "start":274,"end":276,"loc":{"start":{"line":12,"column":25},"end":{"line":12,"column":27}}, + "start":216,"end":218,"loc":{"start":{"line":10,"column":25},"end":{"line":10,"column":27}}, "body": [], "directives": [] } }, { "type": "ClassProperty", - "start":280,"end":296,"loc":{"start":{"line":14,"column":2},"end":{"line":14,"column":18}}, + "start":222,"end":238,"loc":{"start":{"line":12,"column":2},"end":{"line":12,"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"}, + "start":232,"end":233,"loc":{"start":{"line":12,"column":12},"end":{"line":12,"column":13},"identifierName":"x"}, "name": "x" }, "value": { "type": "NumericLiteral", - "start":295,"end":296,"loc":{"start":{"line":14,"column":17},"end":{"line":14,"column":18}}, + "start":237,"end":238,"loc":{"start":{"line":12,"column":17},"end":{"line":12,"column":18}}, "extra": { "rawValue": 2, "raw": "2" @@ -258,13 +212,13 @@ }, { "type": "ClassMethod", - "start":299,"end":316,"loc":{"start":{"line":15,"column":2},"end":{"line":15,"column":19}}, + "start":241,"end":258,"loc":{"start":{"line":13,"column":2},"end":{"line":13,"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"}, + "start":251,"end":252,"loc":{"start":{"line":13,"column":12},"end":{"line":13,"column":13},"identifierName":"x"}, "name": "x" }, "kind": "method", @@ -274,7 +228,7 @@ "params": [], "body": { "type": "BlockStatement", - "start":314,"end":316,"loc":{"start":{"line":15,"column":17},"end":{"line":15,"column":19}}, + "start":256,"end":258,"loc":{"start":{"line":13,"column":17},"end":{"line":13,"column":19}}, "body": [], "directives": [] } @@ -284,30 +238,30 @@ }, { "type": "ClassDeclaration", - "start":320,"end":390,"loc":{"start":{"line":18,"column":0},"end":{"line":20,"column":1}}, + "start":262,"end":332,"loc":{"start":{"line":16,"column":0},"end":{"line":18,"column":1}}, "declare": true, "id": { "type": "Identifier", - "start":334,"end":347,"loc":{"start":{"line":18,"column":14},"end":{"line":18,"column":27},"identifierName":"DeclaredClass"}, + "start":276,"end":289,"loc":{"start":{"line":16,"column":14},"end":{"line":16,"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"}, + "start":298,"end":307,"loc":{"start":{"line":16,"column":36},"end":{"line":16,"column":45},"identifierName":"BaseClass"}, "name": "BaseClass" }, "body": { "type": "ClassBody", - "start":366,"end":390,"loc":{"start":{"line":18,"column":46},"end":{"line":20,"column":1}}, + "start":308,"end":332,"loc":{"start":{"line":16,"column":46},"end":{"line":18,"column":1}}, "body": [ { "type": "ClassMethod", - "start":370,"end":388,"loc":{"start":{"line":19,"column":2},"end":{"line":19,"column":20}}, + "start":312,"end":330,"loc":{"start":{"line":17,"column":2},"end":{"line":17,"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"}, + "start":321,"end":325,"loc":{"start":{"line":17,"column":11},"end":{"line":17,"column":15},"identifierName":"test"}, "name": "test" }, "computed": false, @@ -318,7 +272,7 @@ "params": [], "body": { "type": "BlockStatement", - "start":386,"end":388,"loc":{"start":{"line":19,"column":18},"end":{"line":19,"column":20}}, + "start":328,"end":330,"loc":{"start":{"line":17,"column":18},"end":{"line":17,"column":20}}, "body": [], "directives": [] } diff --git a/scripts/parser-tests/typescript/allowlist.txt b/scripts/parser-tests/typescript/allowlist.txt index dd8f1b29553a..c6b12a718071 100644 --- a/scripts/parser-tests/typescript/allowlist.txt +++ b/scripts/parser-tests/typescript/allowlist.txt @@ -232,6 +232,7 @@ functionCall15.ts functionDeclarationWithResolutionOfTypeNamedArguments01.ts functionExpressionInWithBlock.ts functionExpressionWithResolutionOfTypeNamedArguments01.ts +getterErrorMessageNotDuplicated.ts gettersAndSettersErrors.ts giant.ts globalThisDeclarationEmit.ts @@ -407,8 +408,7 @@ shorthandPropertyAssignmentInES6Module.ts sourceMap-LineBreaks.ts sourceMapValidationDecorators.ts sourceMapValidationStatements.ts -staticAsIdentifier.ts -staticModifierAlreadySeen.ts +stackDepthLimitCastingType.ts strictModeReservedWord.ts superCallFromClassThatHasNoBaseType1.ts symbolLinkDeclarationEmitModuleNames.ts