diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 7ece3b69c00a..b4369ad27df3 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -3708,6 +3708,7 @@ export default (superClass: ClassWithMixin) => TSTypeCastExpression: true, TSParameterProperty: "parameter", TSNonNullExpression: "expression", + TSInstantiationExpression: "expression", TSAsExpression: (binding !== BindingFlag.TYPE_NONE || !isUnparenthesizedInAssign) && ["expression", true], TSSatisfiesExpression: (binding !== BindingFlag.TYPE_NONE || diff --git a/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/input.ts b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/input.ts new file mode 100644 index 000000000000..53fe5cbde57c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/input.ts @@ -0,0 +1,18 @@ +export abstract class AbstractClass { + public static myFunc?: ( + this: AbstractClass, + instance: M, + ) => void; +} + +export class ConcreteClass extends AbstractClass { + declare myField: string; +} + +export const init = () => { + const cls = ConcreteClass; + + cls.myFunc = (instance) => { + console.log(instance.myField); + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/options.json b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/options.json new file mode 100644 index 000000000000..088fb893488a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/options.json @@ -0,0 +1,4 @@ +{ + "BABEL_8_BREAKING": false, + "plugins": ["typescript"] +} diff --git a/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/output.json b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/output.json new file mode 100644 index 000000000000..344a88489102 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation-babel-7/output.json @@ -0,0 +1,336 @@ +{ + "type": "File", + "start":0,"end":397,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":18,"column":1,"index":397}}, + "program": { + "type": "Program", + "start":0,"end":397,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":18,"column":1,"index":397}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":158,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":158}}, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start":7,"end":158,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":6,"column":1,"index":158}}, + "abstract": true, + "id": { + "type": "Identifier", + "start":22,"end":35,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":1,"column":35,"index":35},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":36,"end":158,"loc":{"start":{"line":1,"column":36,"index":36},"end":{"line":6,"column":1,"index":158}}, + "body": [ + { + "type": "ClassProperty", + "start":42,"end":156,"loc":{"start":{"line":2,"column":4,"index":42},"end":{"line":5,"column":14,"index":156}}, + "accessibility": "public", + "static": true, + "key": { + "type": "Identifier", + "start":56,"end":62,"loc":{"start":{"line":2,"column":18,"index":56},"end":{"line":2,"column":24,"index":62},"identifierName":"myFunc"}, + "name": "myFunc" + }, + "computed": false, + "optional": true, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":63,"end":155,"loc":{"start":{"line":2,"column":25,"index":63},"end":{"line":5,"column":13,"index":155}}, + "typeAnnotation": { + "type": "TSFunctionType", + "start":65,"end":155,"loc":{"start":{"line":2,"column":27,"index":65},"end":{"line":5,"column":13,"index":155}}, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":65,"end":90,"loc":{"start":{"line":2,"column":27,"index":65},"end":{"line":2,"column":52,"index":90}}, + "params": [ + { + "type": "TSTypeParameter", + "start":66,"end":89,"loc":{"start":{"line":2,"column":28,"index":66},"end":{"line":2,"column":51,"index":89}}, + "name": "M", + "constraint": { + "type": "TSTypeReference", + "start":76,"end":89,"loc":{"start":{"line":2,"column":38,"index":76},"end":{"line":2,"column":51,"index":89}}, + "typeName": { + "type": "Identifier", + "start":76,"end":89,"loc":{"start":{"line":2,"column":38,"index":76},"end":{"line":2,"column":51,"index":89},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + } + } + } + ] + }, + "parameters": [ + { + "type": "Identifier", + "start":100,"end":119,"loc":{"start":{"line":3,"column":8,"index":100},"end":{"line":3,"column":27,"index":119},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":104,"end":119,"loc":{"start":{"line":3,"column":12,"index":104},"end":{"line":3,"column":27,"index":119}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":106,"end":119,"loc":{"start":{"line":3,"column":14,"index":106},"end":{"line":3,"column":27,"index":119}}, + "typeName": { + "type": "Identifier", + "start":106,"end":119,"loc":{"start":{"line":3,"column":14,"index":106},"end":{"line":3,"column":27,"index":119},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + } + } + } + }, + { + "type": "Identifier", + "start":129,"end":140,"loc":{"start":{"line":4,"column":8,"index":129},"end":{"line":4,"column":19,"index":140},"identifierName":"instance"}, + "name": "instance", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":137,"end":140,"loc":{"start":{"line":4,"column":16,"index":137},"end":{"line":4,"column":19,"index":140}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":139,"end":140,"loc":{"start":{"line":4,"column":18,"index":139},"end":{"line":4,"column":19,"index":140}}, + "typeName": { + "type": "Identifier", + "start":139,"end":140,"loc":{"start":{"line":4,"column":18,"index":139},"end":{"line":4,"column":19,"index":140},"identifierName":"M"}, + "name": "M" + } + } + } + } + ], + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":148,"end":155,"loc":{"start":{"line":5,"column":6,"index":148},"end":{"line":5,"column":13,"index":155}}, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start":151,"end":155,"loc":{"start":{"line":5,"column":9,"index":151},"end":{"line":5,"column":13,"index":155}} + } + } + } + }, + "value": null + } + ] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start":160,"end":241,"loc":{"start":{"line":8,"column":0,"index":160},"end":{"line":10,"column":1,"index":241}}, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start":167,"end":241,"loc":{"start":{"line":8,"column":7,"index":167},"end":{"line":10,"column":1,"index":241}}, + "id": { + "type": "Identifier", + "start":173,"end":186,"loc":{"start":{"line":8,"column":13,"index":173},"end":{"line":8,"column":26,"index":186},"identifierName":"ConcreteClass"}, + "name": "ConcreteClass" + }, + "superClass": { + "type": "Identifier", + "start":195,"end":208,"loc":{"start":{"line":8,"column":35,"index":195},"end":{"line":8,"column":48,"index":208},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + }, + "body": { + "type": "ClassBody", + "start":209,"end":241,"loc":{"start":{"line":8,"column":49,"index":209},"end":{"line":10,"column":1,"index":241}}, + "body": [ + { + "type": "ClassProperty", + "start":215,"end":239,"loc":{"start":{"line":9,"column":4,"index":215},"end":{"line":9,"column":28,"index":239}}, + "declare": true, + "static": false, + "key": { + "type": "Identifier", + "start":223,"end":230,"loc":{"start":{"line":9,"column":12,"index":223},"end":{"line":9,"column":19,"index":230},"identifierName":"myField"}, + "name": "myField" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":230,"end":238,"loc":{"start":{"line":9,"column":19,"index":230},"end":{"line":9,"column":27,"index":238}}, + "typeAnnotation": { + "type": "TSStringKeyword", + "start":232,"end":238,"loc":{"start":{"line":9,"column":21,"index":232},"end":{"line":9,"column":27,"index":238}} + } + }, + "value": null + } + ] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start":243,"end":397,"loc":{"start":{"line":12,"column":0,"index":243},"end":{"line":18,"column":1,"index":397}}, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start":250,"end":397,"loc":{"start":{"line":12,"column":7,"index":250},"end":{"line":18,"column":1,"index":397}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":256,"end":397,"loc":{"start":{"line":12,"column":13,"index":256},"end":{"line":18,"column":1,"index":397}}, + "id": { + "type": "Identifier", + "start":256,"end":260,"loc":{"start":{"line":12,"column":13,"index":256},"end":{"line":12,"column":17,"index":260},"identifierName":"init"}, + "name": "init" + }, + "init": { + "type": "ArrowFunctionExpression", + "start":263,"end":397,"loc":{"start":{"line":12,"column":20,"index":263},"end":{"line":18,"column":1,"index":397}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":269,"end":397,"loc":{"start":{"line":12,"column":26,"index":269},"end":{"line":18,"column":1,"index":397}}, + "body": [ + { + "type": "VariableDeclaration", + "start":275,"end":301,"loc":{"start":{"line":13,"column":4,"index":275},"end":{"line":13,"column":30,"index":301}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":281,"end":300,"loc":{"start":{"line":13,"column":10,"index":281},"end":{"line":13,"column":29,"index":300}}, + "id": { + "type": "Identifier", + "start":281,"end":284,"loc":{"start":{"line":13,"column":10,"index":281},"end":{"line":13,"column":13,"index":284},"identifierName":"cls"}, + "name": "cls" + }, + "init": { + "type": "Identifier", + "start":287,"end":300,"loc":{"start":{"line":13,"column":16,"index":287},"end":{"line":13,"column":29,"index":300},"identifierName":"ConcreteClass"}, + "name": "ConcreteClass" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start":307,"end":395,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":17,"column":5,"index":395}}, + "expression": { + "type": "AssignmentExpression", + "start":307,"end":395,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":17,"column":5,"index":395}}, + "operator": "=", + "left": { + "type": "TSInstantiationExpression", + "start":307,"end":332,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":15,"column":29,"index":332}}, + "expression": { + "type": "MemberExpression", + "start":307,"end":317,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":15,"column":14,"index":317}}, + "object": { + "type": "Identifier", + "start":307,"end":310,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":15,"column":7,"index":310},"identifierName":"cls"}, + "name": "cls" + }, + "computed": false, + "property": { + "type": "Identifier", + "start":311,"end":317,"loc":{"start":{"line":15,"column":8,"index":311},"end":{"line":15,"column":14,"index":317},"identifierName":"myFunc"}, + "name": "myFunc" + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "start":317,"end":332,"loc":{"start":{"line":15,"column":14,"index":317},"end":{"line":15,"column":29,"index":332}}, + "params": [ + { + "type": "TSTypeReference", + "start":318,"end":331,"loc":{"start":{"line":15,"column":15,"index":318},"end":{"line":15,"column":28,"index":331}}, + "typeName": { + "type": "Identifier", + "start":318,"end":331,"loc":{"start":{"line":15,"column":15,"index":318},"end":{"line":15,"column":28,"index":331},"identifierName":"ConcreteClass"}, + "name": "ConcreteClass" + } + } + ] + } + }, + "right": { + "type": "ArrowFunctionExpression", + "start":335,"end":395,"loc":{"start":{"line":15,"column":32,"index":335},"end":{"line":17,"column":5,"index":395}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":336,"end":344,"loc":{"start":{"line":15,"column":33,"index":336},"end":{"line":15,"column":41,"index":344},"identifierName":"instance"}, + "name": "instance" + } + ], + "body": { + "type": "BlockStatement", + "start":349,"end":395,"loc":{"start":{"line":15,"column":46,"index":349},"end":{"line":17,"column":5,"index":395}}, + "body": [ + { + "type": "ExpressionStatement", + "start":359,"end":389,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":38,"index":389}}, + "expression": { + "type": "CallExpression", + "start":359,"end":388,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":37,"index":388}}, + "callee": { + "type": "MemberExpression", + "start":359,"end":370,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":19,"index":370}}, + "object": { + "type": "Identifier", + "start":359,"end":366,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":15,"index":366},"identifierName":"console"}, + "name": "console" + }, + "computed": false, + "property": { + "type": "Identifier", + "start":367,"end":370,"loc":{"start":{"line":16,"column":16,"index":367},"end":{"line":16,"column":19,"index":370},"identifierName":"log"}, + "name": "log" + } + }, + "arguments": [ + { + "type": "MemberExpression", + "start":371,"end":387,"loc":{"start":{"line":16,"column":20,"index":371},"end":{"line":16,"column":36,"index":387}}, + "object": { + "type": "Identifier", + "start":371,"end":379,"loc":{"start":{"line":16,"column":20,"index":371},"end":{"line":16,"column":28,"index":379},"identifierName":"instance"}, + "name": "instance" + }, + "computed": false, + "property": { + "type": "Identifier", + "start":380,"end":387,"loc":{"start":{"line":16,"column":29,"index":380},"end":{"line":16,"column":36,"index":387},"identifierName":"myField"}, + "name": "myField" + } + } + ] + } + } + ], + "directives": [] + } + } + } + } + ], + "directives": [] + } + } + } + ], + "kind": "const" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/input.ts b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/input.ts new file mode 100644 index 000000000000..53fe5cbde57c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/input.ts @@ -0,0 +1,18 @@ +export abstract class AbstractClass { + public static myFunc?: ( + this: AbstractClass, + instance: M, + ) => void; +} + +export class ConcreteClass extends AbstractClass { + declare myField: string; +} + +export const init = () => { + const cls = ConcreteClass; + + cls.myFunc = (instance) => { + console.log(instance.myField); + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/options.json b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/options.json new file mode 100644 index 000000000000..08d8091d4386 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/options.json @@ -0,0 +1,4 @@ +{ + "BABEL_8_BREAKING": true, + "plugins": ["typescript"] +} diff --git a/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/output.json b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/output.json new file mode 100644 index 000000000000..6f1ac6e05f90 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/TSTypeParameterInstantiation/output.json @@ -0,0 +1,340 @@ +{ + "type": "File", + "start":0,"end":397,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":18,"column":1,"index":397}}, + "program": { + "type": "Program", + "start":0,"end":397,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":18,"column":1,"index":397}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":158,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":158}}, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start":7,"end":158,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":6,"column":1,"index":158}}, + "abstract": true, + "id": { + "type": "Identifier", + "start":22,"end":35,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":1,"column":35,"index":35},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":36,"end":158,"loc":{"start":{"line":1,"column":36,"index":36},"end":{"line":6,"column":1,"index":158}}, + "body": [ + { + "type": "ClassProperty", + "start":42,"end":156,"loc":{"start":{"line":2,"column":4,"index":42},"end":{"line":5,"column":14,"index":156}}, + "accessibility": "public", + "static": true, + "key": { + "type": "Identifier", + "start":56,"end":62,"loc":{"start":{"line":2,"column":18,"index":56},"end":{"line":2,"column":24,"index":62},"identifierName":"myFunc"}, + "name": "myFunc" + }, + "computed": false, + "optional": true, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":63,"end":155,"loc":{"start":{"line":2,"column":25,"index":63},"end":{"line":5,"column":13,"index":155}}, + "typeAnnotation": { + "type": "TSFunctionType", + "start":65,"end":155,"loc":{"start":{"line":2,"column":27,"index":65},"end":{"line":5,"column":13,"index":155}}, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":65,"end":90,"loc":{"start":{"line":2,"column":27,"index":65},"end":{"line":2,"column":52,"index":90}}, + "params": [ + { + "type": "TSTypeParameter", + "start":66,"end":89,"loc":{"start":{"line":2,"column":28,"index":66},"end":{"line":2,"column":51,"index":89}}, + "name": { + "type": "Identifier", + "start":66,"end":67,"loc":{"start":{"line":2,"column":28,"index":66},"end":{"line":2,"column":29,"index":67},"identifierName":"M"}, + "name": "M" + }, + "constraint": { + "type": "TSTypeReference", + "start":76,"end":89,"loc":{"start":{"line":2,"column":38,"index":76},"end":{"line":2,"column":51,"index":89}}, + "typeName": { + "type": "Identifier", + "start":76,"end":89,"loc":{"start":{"line":2,"column":38,"index":76},"end":{"line":2,"column":51,"index":89},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + } + } + } + ] + }, + "params": [ + { + "type": "Identifier", + "start":100,"end":119,"loc":{"start":{"line":3,"column":8,"index":100},"end":{"line":3,"column":27,"index":119},"identifierName":"this"}, + "name": "this", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":104,"end":119,"loc":{"start":{"line":3,"column":12,"index":104},"end":{"line":3,"column":27,"index":119}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":106,"end":119,"loc":{"start":{"line":3,"column":14,"index":106},"end":{"line":3,"column":27,"index":119}}, + "typeName": { + "type": "Identifier", + "start":106,"end":119,"loc":{"start":{"line":3,"column":14,"index":106},"end":{"line":3,"column":27,"index":119},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + } + } + } + }, + { + "type": "Identifier", + "start":129,"end":140,"loc":{"start":{"line":4,"column":8,"index":129},"end":{"line":4,"column":19,"index":140},"identifierName":"instance"}, + "name": "instance", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":137,"end":140,"loc":{"start":{"line":4,"column":16,"index":137},"end":{"line":4,"column":19,"index":140}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":139,"end":140,"loc":{"start":{"line":4,"column":18,"index":139},"end":{"line":4,"column":19,"index":140}}, + "typeName": { + "type": "Identifier", + "start":139,"end":140,"loc":{"start":{"line":4,"column":18,"index":139},"end":{"line":4,"column":19,"index":140},"identifierName":"M"}, + "name": "M" + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start":148,"end":155,"loc":{"start":{"line":5,"column":6,"index":148},"end":{"line":5,"column":13,"index":155}}, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start":151,"end":155,"loc":{"start":{"line":5,"column":9,"index":151},"end":{"line":5,"column":13,"index":155}} + } + } + } + }, + "value": null + } + ] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start":160,"end":241,"loc":{"start":{"line":8,"column":0,"index":160},"end":{"line":10,"column":1,"index":241}}, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start":167,"end":241,"loc":{"start":{"line":8,"column":7,"index":167},"end":{"line":10,"column":1,"index":241}}, + "id": { + "type": "Identifier", + "start":173,"end":186,"loc":{"start":{"line":8,"column":13,"index":173},"end":{"line":8,"column":26,"index":186},"identifierName":"ConcreteClass"}, + "name": "ConcreteClass" + }, + "superClass": { + "type": "Identifier", + "start":195,"end":208,"loc":{"start":{"line":8,"column":35,"index":195},"end":{"line":8,"column":48,"index":208},"identifierName":"AbstractClass"}, + "name": "AbstractClass" + }, + "body": { + "type": "ClassBody", + "start":209,"end":241,"loc":{"start":{"line":8,"column":49,"index":209},"end":{"line":10,"column":1,"index":241}}, + "body": [ + { + "type": "ClassProperty", + "start":215,"end":239,"loc":{"start":{"line":9,"column":4,"index":215},"end":{"line":9,"column":28,"index":239}}, + "declare": true, + "static": false, + "key": { + "type": "Identifier", + "start":223,"end":230,"loc":{"start":{"line":9,"column":12,"index":223},"end":{"line":9,"column":19,"index":230},"identifierName":"myField"}, + "name": "myField" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":230,"end":238,"loc":{"start":{"line":9,"column":19,"index":230},"end":{"line":9,"column":27,"index":238}}, + "typeAnnotation": { + "type": "TSStringKeyword", + "start":232,"end":238,"loc":{"start":{"line":9,"column":21,"index":232},"end":{"line":9,"column":27,"index":238}} + } + }, + "value": null + } + ] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start":243,"end":397,"loc":{"start":{"line":12,"column":0,"index":243},"end":{"line":18,"column":1,"index":397}}, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start":250,"end":397,"loc":{"start":{"line":12,"column":7,"index":250},"end":{"line":18,"column":1,"index":397}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":256,"end":397,"loc":{"start":{"line":12,"column":13,"index":256},"end":{"line":18,"column":1,"index":397}}, + "id": { + "type": "Identifier", + "start":256,"end":260,"loc":{"start":{"line":12,"column":13,"index":256},"end":{"line":12,"column":17,"index":260},"identifierName":"init"}, + "name": "init" + }, + "init": { + "type": "ArrowFunctionExpression", + "start":263,"end":397,"loc":{"start":{"line":12,"column":20,"index":263},"end":{"line":18,"column":1,"index":397}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":269,"end":397,"loc":{"start":{"line":12,"column":26,"index":269},"end":{"line":18,"column":1,"index":397}}, + "body": [ + { + "type": "VariableDeclaration", + "start":275,"end":301,"loc":{"start":{"line":13,"column":4,"index":275},"end":{"line":13,"column":30,"index":301}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":281,"end":300,"loc":{"start":{"line":13,"column":10,"index":281},"end":{"line":13,"column":29,"index":300}}, + "id": { + "type": "Identifier", + "start":281,"end":284,"loc":{"start":{"line":13,"column":10,"index":281},"end":{"line":13,"column":13,"index":284},"identifierName":"cls"}, + "name": "cls" + }, + "init": { + "type": "Identifier", + "start":287,"end":300,"loc":{"start":{"line":13,"column":16,"index":287},"end":{"line":13,"column":29,"index":300},"identifierName":"ConcreteClass"}, + "name": "ConcreteClass" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start":307,"end":395,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":17,"column":5,"index":395}}, + "expression": { + "type": "AssignmentExpression", + "start":307,"end":395,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":17,"column":5,"index":395}}, + "operator": "=", + "left": { + "type": "TSInstantiationExpression", + "start":307,"end":332,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":15,"column":29,"index":332}}, + "expression": { + "type": "MemberExpression", + "start":307,"end":317,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":15,"column":14,"index":317}}, + "object": { + "type": "Identifier", + "start":307,"end":310,"loc":{"start":{"line":15,"column":4,"index":307},"end":{"line":15,"column":7,"index":310},"identifierName":"cls"}, + "name": "cls" + }, + "computed": false, + "property": { + "type": "Identifier", + "start":311,"end":317,"loc":{"start":{"line":15,"column":8,"index":311},"end":{"line":15,"column":14,"index":317},"identifierName":"myFunc"}, + "name": "myFunc" + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "start":317,"end":332,"loc":{"start":{"line":15,"column":14,"index":317},"end":{"line":15,"column":29,"index":332}}, + "params": [ + { + "type": "TSTypeReference", + "start":318,"end":331,"loc":{"start":{"line":15,"column":15,"index":318},"end":{"line":15,"column":28,"index":331}}, + "typeName": { + "type": "Identifier", + "start":318,"end":331,"loc":{"start":{"line":15,"column":15,"index":318},"end":{"line":15,"column":28,"index":331},"identifierName":"ConcreteClass"}, + "name": "ConcreteClass" + } + } + ] + } + }, + "right": { + "type": "ArrowFunctionExpression", + "start":335,"end":395,"loc":{"start":{"line":15,"column":32,"index":335},"end":{"line":17,"column":5,"index":395}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":336,"end":344,"loc":{"start":{"line":15,"column":33,"index":336},"end":{"line":15,"column":41,"index":344},"identifierName":"instance"}, + "name": "instance" + } + ], + "body": { + "type": "BlockStatement", + "start":349,"end":395,"loc":{"start":{"line":15,"column":46,"index":349},"end":{"line":17,"column":5,"index":395}}, + "body": [ + { + "type": "ExpressionStatement", + "start":359,"end":389,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":38,"index":389}}, + "expression": { + "type": "CallExpression", + "start":359,"end":388,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":37,"index":388}}, + "callee": { + "type": "MemberExpression", + "start":359,"end":370,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":19,"index":370}}, + "object": { + "type": "Identifier", + "start":359,"end":366,"loc":{"start":{"line":16,"column":8,"index":359},"end":{"line":16,"column":15,"index":366},"identifierName":"console"}, + "name": "console" + }, + "computed": false, + "property": { + "type": "Identifier", + "start":367,"end":370,"loc":{"start":{"line":16,"column":16,"index":367},"end":{"line":16,"column":19,"index":370},"identifierName":"log"}, + "name": "log" + } + }, + "arguments": [ + { + "type": "MemberExpression", + "start":371,"end":387,"loc":{"start":{"line":16,"column":20,"index":371},"end":{"line":16,"column":36,"index":387}}, + "object": { + "type": "Identifier", + "start":371,"end":379,"loc":{"start":{"line":16,"column":20,"index":371},"end":{"line":16,"column":28,"index":379},"identifierName":"instance"}, + "name": "instance" + }, + "computed": false, + "property": { + "type": "Identifier", + "start":380,"end":387,"loc":{"start":{"line":16,"column":29,"index":380},"end":{"line":16,"column":36,"index":387},"identifierName":"myField"}, + "name": "myField" + } + } + ] + } + } + ], + "directives": [] + } + } + } + } + ], + "directives": [] + } + } + } + ], + "kind": "const" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/input.ts new file mode 100644 index 000000000000..53fe5cbde57c --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/input.ts @@ -0,0 +1,18 @@ +export abstract class AbstractClass { + public static myFunc?: ( + this: AbstractClass, + instance: M, + ) => void; +} + +export class ConcreteClass extends AbstractClass { + declare myField: string; +} + +export const init = () => { + const cls = ConcreteClass; + + cls.myFunc = (instance) => { + console.log(instance.myField); + } +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/options.json new file mode 100644 index 000000000000..7592c35ead36 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/options.json @@ -0,0 +1,11 @@ +{ + "sourceType": "module", + "plugins": [ + [ + "transform-typescript", + { + "allowDeclareFields": true + } + ] + ] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/output.mjs new file mode 100644 index 000000000000..aa08c8b950c9 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/lvalues/TSTypeParameterInstantiation/output.mjs @@ -0,0 +1,10 @@ +export class AbstractClass { + static myFunc; +} +export class ConcreteClass extends AbstractClass {} +export const init = () => { + const cls = ConcreteClass; + cls.myFunc = instance => { + console.log(instance.myField); + }; +};