From 02cdbaa2d4237aab8892de0755e43a3551d7f657 Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 1 Jan 2019 21:27:21 +0100 Subject: [PATCH] feat: add support for export equal declaration BREAKING CHANGE --- src/ast-node-types.ts | 2 + src/convert.ts | 17 + tests/ast-alignment/fixtures-to-test.ts | 4 +- .../basics/import-equal-declaration.src.ts | 1 + .../import-export-equal-declaration.src.ts | 1 + .../semantic-diagnostics-enabled.ts.snap | 4 + tests/lib/__snapshots__/typescript.ts.snap | 500 ++++++++++++++++++ 7 files changed, 528 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/typescript/basics/import-equal-declaration.src.ts create mode 100644 tests/fixtures/typescript/basics/import-export-equal-declaration.src.ts diff --git a/src/ast-node-types.ts b/src/ast-node-types.ts index d4b9455..fbe848e 100644 --- a/src/ast-node-types.ts +++ b/src/ast-node-types.ts @@ -115,6 +115,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = { TSEnumMember: 'TSEnumMember', TSExportAssignment: 'TSExportAssignment', TSExportKeyword: 'TSExportKeyword', + TSExternalModuleReference: 'TSExternalModuleReference', TSImportType: 'TSImportType', TSInferType: 'TSInferType', TSLiteralType: 'TSLiteralType', @@ -123,6 +124,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = { TSInterfaceBody: 'TSInterfaceBody', TSInterfaceDeclaration: 'TSInterfaceDeclaration', TSInterfaceHeritage: 'TSInterfaceHeritage', + TSImportEqualsDeclaration: 'TSImportEqualsDeclaration', TSFunctionType: 'TSFunctionType', TSMethodSignature: 'TSMethodSignature', TSModuleBlock: 'TSModuleBlock', diff --git a/src/convert.ts b/src/convert.ts index 438331b..0857be5 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -2689,6 +2689,23 @@ export default function convert(config: ConvertConfig): ESTreeNode | null { }); break; } + case SyntaxKind.ImportEqualsDeclaration: { + Object.assign(result, { + type: AST_NODE_TYPES.TSImportEqualsDeclaration, + id: convertChild(node.name), + moduleReference: convertChild(node.moduleReference), + isExport: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) + }); + break; + } + case SyntaxKind.ExternalModuleReference: { + Object.assign(result, { + type: AST_NODE_TYPES.TSExternalModuleReference, + expression: convertChild(node.expression) + }); + break; + } + default: deeplyCopy(); } diff --git a/tests/ast-alignment/fixtures-to-test.ts b/tests/ast-alignment/fixtures-to-test.ts index 01525c4..7ab5572 100644 --- a/tests/ast-alignment/fixtures-to-test.ts +++ b/tests/ast-alignment/fixtures-to-test.ts @@ -390,7 +390,9 @@ let fixturePatternConfigsToTest = [ ], ignoreSourceType: [ // https://github.com/babel/babel/issues/9213 - 'export-assignment' + 'export-assignment', + 'import-equal-declaration', + 'import-export-equal-declaration' ] }), diff --git a/tests/fixtures/typescript/basics/import-equal-declaration.src.ts b/tests/fixtures/typescript/basics/import-equal-declaration.src.ts new file mode 100644 index 0000000..8cb5940 --- /dev/null +++ b/tests/fixtures/typescript/basics/import-equal-declaration.src.ts @@ -0,0 +1 @@ +import foo = require('bar'); diff --git a/tests/fixtures/typescript/basics/import-export-equal-declaration.src.ts b/tests/fixtures/typescript/basics/import-export-equal-declaration.src.ts new file mode 100644 index 0000000..0b50a96 --- /dev/null +++ b/tests/fixtures/typescript/basics/import-export-equal-declaration.src.ts @@ -0,0 +1 @@ +export import foo = require('bar'); diff --git a/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap b/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap index 450f0df..5fd15a3 100644 --- a/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap +++ b/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap @@ -1699,6 +1699,10 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" en exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/function-with-types-assignation.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; +exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/import-equal-declaration.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; + +exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/import-export-equal-declaration.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; + exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/import-type.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/import-type-with-type-parameters-in-type-reference.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; diff --git a/tests/lib/__snapshots__/typescript.ts.snap b/tests/lib/__snapshots__/typescript.ts.snap index 5884298..a41f38c 100644 --- a/tests/lib/__snapshots__/typescript.ts.snap +++ b/tests/lib/__snapshots__/typescript.ts.snap @@ -34609,6 +34609,506 @@ Object { } `; +exports[`typescript fixtures/basics/import-equal-declaration.src 1`] = ` +Object { + "body": Array [ + Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "name": "foo", + "range": Array [ + 7, + 10, + ], + "type": "Identifier", + }, + "isExport": false, + "loc": Object { + "end": Object { + "column": 28, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "moduleReference": Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 21, + "line": 1, + }, + }, + "range": Array [ + 21, + 26, + ], + "raw": "'bar'", + "type": "Literal", + "value": "bar", + }, + "loc": Object { + "end": Object { + "column": 27, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "range": Array [ + 13, + 27, + ], + "type": "TSExternalModuleReference", + }, + "range": Array [ + 0, + 28, + ], + "type": "TSImportEqualsDeclaration", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 2, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 29, + ], + "sourceType": "module", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 6, + ], + "type": "Keyword", + "value": "import", + }, + Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 10, + ], + "type": "Identifier", + "value": "foo", + }, + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 11, + "line": 1, + }, + }, + "range": Array [ + 11, + 12, + ], + "type": "Punctuator", + "value": "=", + }, + Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "range": Array [ + 13, + 20, + ], + "type": "Identifier", + "value": "require", + }, + Object { + "loc": Object { + "end": Object { + "column": 21, + "line": 1, + }, + "start": Object { + "column": 20, + "line": 1, + }, + }, + "range": Array [ + 20, + 21, + ], + "type": "Punctuator", + "value": "(", + }, + Object { + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 21, + "line": 1, + }, + }, + "range": Array [ + 21, + 26, + ], + "type": "String", + "value": "'bar'", + }, + Object { + "loc": Object { + "end": Object { + "column": 27, + "line": 1, + }, + "start": Object { + "column": 26, + "line": 1, + }, + }, + "range": Array [ + 26, + 27, + ], + "type": "Punctuator", + "value": ")", + }, + Object { + "loc": Object { + "end": Object { + "column": 28, + "line": 1, + }, + "start": Object { + "column": 27, + "line": 1, + }, + }, + "range": Array [ + 27, + 28, + ], + "type": "Punctuator", + "value": ";", + }, + ], + "type": "Program", +} +`; + +exports[`typescript fixtures/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", + }, + "isExport": true, + "loc": Object { + "end": Object { + "column": 35, + "line": 1, + }, + "start": Object { + "column": 0, + "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 [ + 0, + 35, + ], + "type": "TSImportEqualsDeclaration", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 2, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 36, + ], + "sourceType": "module", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 6, + ], + "type": "Keyword", + "value": "export", + }, + Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 13, + ], + "type": "Keyword", + "value": "import", + }, + Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 1, + }, + "start": Object { + "column": 14, + "line": 1, + }, + }, + "range": Array [ + 14, + 17, + ], + "type": "Identifier", + "value": "foo", + }, + Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 18, + "line": 1, + }, + }, + "range": Array [ + 18, + 19, + ], + "type": "Punctuator", + "value": "=", + }, + Object { + "loc": Object { + "end": Object { + "column": 27, + "line": 1, + }, + "start": Object { + "column": 20, + "line": 1, + }, + }, + "range": Array [ + 20, + 27, + ], + "type": "Identifier", + "value": "require", + }, + Object { + "loc": Object { + "end": Object { + "column": 28, + "line": 1, + }, + "start": Object { + "column": 27, + "line": 1, + }, + }, + "range": Array [ + 27, + 28, + ], + "type": "Punctuator", + "value": "(", + }, + Object { + "loc": Object { + "end": Object { + "column": 33, + "line": 1, + }, + "start": Object { + "column": 28, + "line": 1, + }, + }, + "range": Array [ + 28, + 33, + ], + "type": "String", + "value": "'bar'", + }, + Object { + "loc": Object { + "end": Object { + "column": 34, + "line": 1, + }, + "start": Object { + "column": 33, + "line": 1, + }, + }, + "range": Array [ + 33, + 34, + ], + "type": "Punctuator", + "value": ")", + }, + Object { + "loc": Object { + "end": Object { + "column": 35, + "line": 1, + }, + "start": Object { + "column": 34, + "line": 1, + }, + }, + "range": Array [ + 34, + 35, + ], + "type": "Punctuator", + "value": ";", + }, + ], + "type": "Program", +} +`; + exports[`typescript fixtures/basics/import-type.src 1`] = ` Object { "body": Array [