diff --git a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/1-TSESTree-AST.shot index 6d438a8c596..7109c2c236f 100644 --- a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/1-TSESTree-AST.shot @@ -17,7 +17,6 @@ Program { }, }, importKind: "value", - isExport: false, moduleReference: TSQualifiedName { type: "TSQualifiedName", left: TSQualifiedName { diff --git a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/5-AST-Alignment-AST.shot index 88ec007b237..381cbd538ab 100644 --- a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/snapshots/5-AST-Alignment-AST.shot @@ -2,5 +2,88 @@ exports[`AST Fixtures declaration TSImportEqualsDeclaration entity-name-many AST Alignment - AST 1`] = ` "Snapshot Diff: -Compared values have no visual difference." +- TSESTree ++ Babel + + Program { + type: 'Program', + body: Array [ + TSImportEqualsDeclaration { + type: 'TSImportEqualsDeclaration', + id: Identifier { + type: 'Identifier', + name: 'F', + + range: [7, 8], + loc: { + start: { column: 7, line: 1 }, + end: { column: 8, line: 1 }, + }, + }, + importKind: 'value', ++ isExport: false, + moduleReference: TSQualifiedName { + type: 'TSQualifiedName', + left: TSQualifiedName { + type: 'TSQualifiedName', + left: Identifier { + type: 'Identifier', + name: 'A', + + range: [11, 12], + loc: { + start: { column: 11, line: 1 }, + end: { column: 12, line: 1 }, + }, + }, + right: Identifier { + type: 'Identifier', + name: 'B', + + range: [13, 14], + loc: { + start: { column: 13, line: 1 }, + end: { column: 14, line: 1 }, + }, + }, + + range: [11, 14], + loc: { + start: { column: 11, line: 1 }, + end: { column: 14, line: 1 }, + }, + }, + right: Identifier { + type: 'Identifier', + name: 'C', + + range: [15, 16], + loc: { + start: { column: 15, line: 1 }, + end: { column: 16, line: 1 }, + }, + }, + + range: [11, 16], + loc: { + start: { column: 11, line: 1 }, + end: { column: 16, line: 1 }, + }, + }, + + range: [0, 17], + loc: { + start: { column: 0, line: 1 }, + end: { column: 17, line: 1 }, + }, + }, + ], + sourceType: 'script', + + range: [0, 18], + loc: { + start: { column: 0, line: 1 }, + end: { column: 0, line: 2 }, + }, + }" `; diff --git a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/1-TSESTree-AST.shot index aa18656723b..4053336bc98 100644 --- a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/1-TSESTree-AST.shot @@ -17,7 +17,6 @@ Program { }, }, importKind: "value", - isExport: false, moduleReference: Identifier { type: "Identifier", name: "A", diff --git a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/5-AST-Alignment-AST.shot index 0cb17969d4f..4989243086f 100644 --- a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/snapshots/5-AST-Alignment-AST.shot @@ -2,5 +2,50 @@ exports[`AST Fixtures declaration TSImportEqualsDeclaration entity-name-one AST Alignment - AST 1`] = ` "Snapshot Diff: -Compared values have no visual difference." +- TSESTree ++ Babel + + Program { + type: 'Program', + body: Array [ + TSImportEqualsDeclaration { + type: 'TSImportEqualsDeclaration', + id: Identifier { + type: 'Identifier', + name: 'F', + + range: [7, 8], + loc: { + start: { column: 7, line: 1 }, + end: { column: 8, line: 1 }, + }, + }, + importKind: 'value', ++ isExport: false, + moduleReference: Identifier { + type: 'Identifier', + name: 'A', + + range: [11, 12], + loc: { + start: { column: 11, line: 1 }, + end: { column: 12, line: 1 }, + }, + }, + + range: [0, 13], + loc: { + start: { column: 0, line: 1 }, + end: { column: 13, line: 1 }, + }, + }, + ], + sourceType: 'script', + + range: [0, 14], + loc: { + start: { column: 0, line: 1 }, + end: { column: 0, line: 2 }, + }, + }" `; diff --git a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/1-TSESTree-AST.shot index aca9d9ab5f3..c36dc7f14ac 100644 --- a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/1-TSESTree-AST.shot @@ -17,7 +17,6 @@ Program { }, }, importKind: "value", - isExport: false, moduleReference: TSExternalModuleReference { type: "TSExternalModuleReference", expression: Literal { diff --git a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/5-AST-Alignment-AST.shot index a0b231ec7a1..4c496755649 100644 --- a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/snapshots/5-AST-Alignment-AST.shot @@ -21,7 +21,7 @@ exports[`AST Fixtures declaration TSImportEqualsDeclaration external-module-ref- }, }, importKind: 'value', - isExport: false, ++ isExport: false, moduleReference: TSExternalModuleReference { type: 'TSExternalModuleReference', expression: Literal { diff --git a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/spec.ts b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/spec.ts index d4db9a64168..15fd64f11ad 100644 --- a/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/spec.ts +++ b/packages/ast-spec/src/declaration/TSImportEqualsDeclaration/spec.ts @@ -22,12 +22,4 @@ export interface TSImportEqualsDeclaration extends BaseNode { moduleReference: EntityName | TSExternalModuleReference; // TODO(#1852) - breaking change remove this as it is invalid importKind: ImportKind; - /** - * Whether this is immediately exported - * ``` - * export import F = A; - * ``` - */ - // TODO(#4130) - this should be represented in the AST - isExport: boolean; } diff --git a/packages/ast-spec/src/unions/ExportDeclaration.ts b/packages/ast-spec/src/unions/ExportDeclaration.ts index b78996d1405..b3a25fd3811 100644 --- a/packages/ast-spec/src/unions/ExportDeclaration.ts +++ b/packages/ast-spec/src/unions/ExportDeclaration.ts @@ -8,6 +8,7 @@ import type { } from '../declaration/FunctionDeclaration/spec'; import type { TSDeclareFunction } from '../declaration/TSDeclareFunction/spec'; import type { TSEnumDeclaration } from '../declaration/TSEnumDeclaration/spec'; +import type { TSImportEqualsDeclaration } from '../declaration/TSImportEqualsDeclaration/spec'; import type { TSInterfaceDeclaration } from '../declaration/TSInterfaceDeclaration/spec'; import type { TSModuleDeclaration } from '../declaration/TSModuleDeclaration/spec'; import type { TSTypeAliasDeclaration } from '../declaration/TSTypeAliasDeclaration/spec'; @@ -37,6 +38,7 @@ export type NamedExportDeclarations = | FunctionDeclarationWithOptionalName | TSDeclareFunction | TSEnumDeclaration + | TSImportEqualsDeclaration | TSInterfaceDeclaration | TSModuleDeclaration | TSTypeAliasDeclaration diff --git a/packages/ast-spec/tests/fixtures-with-differences-ast.shot b/packages/ast-spec/tests/fixtures-with-differences-ast.shot index 233fc7f4f95..4cbde8bc61c 100644 --- a/packages/ast-spec/tests/fixtures-with-differences-ast.shot +++ b/packages/ast-spec/tests/fixtures-with-differences-ast.shot @@ -17,6 +17,8 @@ Set { "declaration/FunctionDeclaration/fixtures/type-param-one/fixture.ts", "declaration/TSDeclareFunction/fixtures/type-param-many/fixture.ts", "declaration/TSDeclareFunction/fixtures/type-param-one/fixture.ts", + "declaration/TSImportEqualsDeclaration/fixtures/entity-name-many/fixture.ts", + "declaration/TSImportEqualsDeclaration/fixtures/entity-name-one/fixture.ts", "declaration/TSImportEqualsDeclaration/fixtures/external-module-ref-string/fixture.ts", "declaration/TSInterfaceDeclaration/fixtures/extends-many/fixture.ts", "declaration/TSInterfaceDeclaration/fixtures/extends-one/fixture.ts", diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 60636a8b2f5..bc63ec3ecd9 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -143,6 +143,7 @@ export class Converter { | ts.ClassDeclaration | ts.ClassExpression | ts.TypeAliasDeclaration + | ts.ImportEqualsDeclaration | ts.InterfaceDeclaration | ts.EnumDeclaration | ts.ModuleDeclaration, @@ -159,7 +160,7 @@ export class Converter { const exportKeyword = modifiers[0]; const nextModifier = modifiers[1]; const declarationIsDefault = - nextModifier && nextModifier.kind === SyntaxKind.DefaultKeyword; + nextModifier?.kind === SyntaxKind.DefaultKeyword; const varToken = declarationIsDefault ? findNextToken(nextModifier, this.ast, this.ast) @@ -169,12 +170,15 @@ export class Converter { result.loc = getLocFor(result.range[0], result.range[1], this.ast); if (declarationIsDefault) { - return this.createNode(node, { - type: AST_NODE_TYPES.ExportDefaultDeclaration, - declaration: result, - range: [exportKeyword.getStart(this.ast), result.range[1]], - exportKind: 'value', - }); + return this.createNode( + node as Exclude, + { + type: AST_NODE_TYPES.ExportDefaultDeclaration, + declaration: result as TSESTree.DefaultExportDeclarations, + range: [exportKeyword.getStart(this.ast), result.range[1]], + exportKind: 'value', + }, + ); } else { const isType = result.type === AST_NODE_TYPES.TSInterfaceDeclaration || @@ -2778,13 +2782,15 @@ export class Converter { }); } case SyntaxKind.ImportEqualsDeclaration: { - return this.createNode(node, { - type: AST_NODE_TYPES.TSImportEqualsDeclaration, - id: this.convertChild(node.name), - moduleReference: this.convertChild(node.moduleReference), - importKind: node.isTypeOnly ? 'type' : 'value', - isExport: hasModifier(SyntaxKind.ExportKeyword, node), - }); + return this.fixExports( + node, + this.createNode(node, { + type: AST_NODE_TYPES.TSImportEqualsDeclaration, + id: this.convertChild(node.name), + importKind: node.isTypeOnly ? 'type' : 'value', + moduleReference: this.convertChild(node.moduleReference), + }), + ); } case SyntaxKind.ExternalModuleReference: { return this.createNode(node, { diff --git a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts index 9c99786dfca..7d758d66d87 100644 --- a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts +++ b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts @@ -55,6 +55,7 @@ export interface EstreeToTsNodeTypes { | ts.ClassDeclaration | ts.ClassExpression | ts.TypeAliasDeclaration + | ts.ImportEqualsDeclaration | ts.InterfaceDeclaration | ts.EnumDeclaration | ts.ModuleDeclaration; diff --git a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts index ac87ee62178..4510792c1a1 100644 --- a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts +++ b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts @@ -371,6 +371,16 @@ tester.addFixturePatternConfig('typescript/basics', { 'class-with-two-methods-computed-constructor', 'export-type-star-from', 'import-type-error', + + /** + * Babel's 'typescript' transform gives these TypeScript-like properties + * such as `isExport: false`, but we don't include those in our AST. + */ + 'import-equal-declaration', + 'import-export-equal-declaration', + 'import-equal-type-declaration', + 'import-export-equal-type-declaration', + /** * [TS-ESTREE ERRORED, BUT BABEL DID NOT] * This is intentional; babel is not checking types @@ -404,10 +414,6 @@ tester.addFixturePatternConfig('typescript/basics', { * @see https://github.com/babel/babel/issues/9213 */ 'export-assignment', - 'import-equal-declaration', - 'import-export-equal-declaration', - 'import-equal-type-declaration', - 'import-export-equal-type-declaration', // babel treats declare and types as not a module 'export-declare-const-named-enum', 'export-declare-named-enum', diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-declaration.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-declaration.src.ts.shot index 99c14c70ea9..c2b9689ceb7 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-declaration.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-declaration.src.ts.shot @@ -23,7 +23,6 @@ Object { "type": "Identifier", }, "importKind": "value", - "isExport": false, "loc": Object { "end": Object { "column": 28, diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-type-declaration.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-type-declaration.src.ts.shot index 1e5a56be081..67ac8fa7d05 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-type-declaration.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/import-equal-type-declaration.src.ts.shot @@ -23,7 +23,6 @@ Object { "type": "Identifier", }, "importKind": "type", - "isExport": false, "loc": Object { "end": Object { "column": 33, diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-declaration.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-declaration.src.ts.shot index ac4b760010d..bba09fe03ba 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-declaration.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-declaration.src.ts.shot @@ -4,77 +4,97 @@ exports[`typescript basics import-export-equal-declaration.src 1`] = ` Object { "body": Array [ Object { - "id": Object { - "loc": Object { - "end": Object { - "column": 17, - "line": 1, - }, - "start": Object { - "column": 14, - "line": 1, - }, - }, - "name": "foo", - "range": Array [ - 14, - 17, - ], - "type": "Identifier", - }, - "importKind": "value", - "isExport": true, - "loc": Object { - "end": Object { - "column": 35, - "line": 1, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "moduleReference": Object { - "expression": Object { + "assertions": Array [], + "declaration": Object { + "id": Object { "loc": Object { "end": Object { - "column": 33, + "column": 17, "line": 1, }, "start": Object { - "column": 28, + "column": 14, "line": 1, }, }, + "name": "foo", "range": Array [ - 28, - 33, + 14, + 17, ], - "raw": "'bar'", - "type": "Literal", - "value": "bar", + "type": "Identifier", }, + "importKind": "value", "loc": Object { "end": Object { - "column": 34, + "column": 35, "line": 1, }, "start": Object { - "column": 20, + "column": 7, "line": 1, }, }, + "moduleReference": Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 33, + "line": 1, + }, + "start": Object { + "column": 28, + "line": 1, + }, + }, + "range": Array [ + 28, + 33, + ], + "raw": "'bar'", + "type": "Literal", + "value": "bar", + }, + "loc": Object { + "end": Object { + "column": 34, + "line": 1, + }, + "start": Object { + "column": 20, + "line": 1, + }, + }, + "range": Array [ + 20, + 34, + ], + "type": "TSExternalModuleReference", + }, "range": Array [ - 20, - 34, + 7, + 35, ], - "type": "TSExternalModuleReference", + "type": "TSImportEqualsDeclaration", + }, + "exportKind": "value", + "loc": Object { + "end": Object { + "column": 35, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, }, "range": Array [ 0, 35, ], - "type": "TSImportEqualsDeclaration", + "source": null, + "specifiers": Array [], + "type": "ExportNamedDeclaration", }, ], "comments": Array [], diff --git a/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-type-declaration.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-type-declaration.src.ts.shot index f5763d6fe01..8fea08bafea 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-type-declaration.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/basics/import-export-equal-type-declaration.src.ts.shot @@ -4,77 +4,97 @@ exports[`typescript basics import-export-equal-type-declaration.src 1`] = ` Object { "body": Array [ Object { - "id": Object { - "loc": Object { - "end": Object { - "column": 22, - "line": 1, - }, - "start": Object { - "column": 19, - "line": 1, - }, - }, - "name": "foo", - "range": Array [ - 19, - 22, - ], - "type": "Identifier", - }, - "importKind": "type", - "isExport": true, - "loc": Object { - "end": Object { - "column": 40, - "line": 1, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "moduleReference": Object { - "expression": Object { + "assertions": Array [], + "declaration": Object { + "id": Object { "loc": Object { "end": Object { - "column": 38, + "column": 22, "line": 1, }, "start": Object { - "column": 33, + "column": 19, "line": 1, }, }, + "name": "foo", "range": Array [ - 33, - 38, + 19, + 22, ], - "raw": "'bar'", - "type": "Literal", - "value": "bar", + "type": "Identifier", }, + "importKind": "type", "loc": Object { "end": Object { - "column": 39, + "column": 40, "line": 1, }, "start": Object { - "column": 25, + "column": 7, "line": 1, }, }, + "moduleReference": Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 38, + "line": 1, + }, + "start": Object { + "column": 33, + "line": 1, + }, + }, + "range": Array [ + 33, + 38, + ], + "raw": "'bar'", + "type": "Literal", + "value": "bar", + }, + "loc": Object { + "end": Object { + "column": 39, + "line": 1, + }, + "start": Object { + "column": 25, + "line": 1, + }, + }, + "range": Array [ + 25, + 39, + ], + "type": "TSExternalModuleReference", + }, "range": Array [ - 25, - 39, + 7, + 40, ], - "type": "TSExternalModuleReference", + "type": "TSImportEqualsDeclaration", + }, + "exportKind": "value", + "loc": Object { + "end": Object { + "column": 40, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, }, "range": Array [ 0, 40, ], - "type": "TSImportEqualsDeclaration", + "source": null, + "specifiers": Array [], + "type": "ExportNamedDeclaration", }, ], "comments": Array [],