From d72d2a7fc07c84ac8bd598315d5a888dbc15dc98 Mon Sep 17 00:00:00 2001 From: James Henry Date: Sun, 20 Aug 2017 19:52:12 +0100 Subject: [PATCH] Breaking: Handle TSModuleDeclaration and refactor (fixes #371) --- lib/convert.js | 16 +++ tests/ast-alignment/spec.js | 4 + tests/lib/__snapshots__/typescript.js.snap | 148 +++++++-------------- 3 files changed, 67 insertions(+), 101 deletions(-) diff --git a/lib/convert.js b/lib/convert.js index 9e6301e..e6ada83 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -361,6 +361,10 @@ module.exports = function convert(config) { result.const = true; handledModifierIndices[i] = true; break; + case SyntaxKind.DeclareKeyword: + result.declare = true; + handledModifierIndices[i] = true; + break; default: } } @@ -2072,6 +2076,18 @@ module.exports = function convert(config) { break; } + case SyntaxKind.ModuleDeclaration: { + Object.assign(result, { + type: AST_NODE_TYPES.TSModuleDeclaration, + id: convertChild(node.name) + }); + applyModifiersToResult(node.modifiers); + if (node.body) { + result.body = convertChild(node.body); + } + break; + } + default: deeplyCopy(); } diff --git a/tests/ast-alignment/spec.js b/tests/ast-alignment/spec.js index 188ff0c..a014341 100644 --- a/tests/ast-alignment/spec.js +++ b/tests/ast-alignment/spec.js @@ -549,6 +549,10 @@ const fixturePatternsToTest = [ // "typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts", // "typescript/namespaces-and-modules/declare-namespace-with-exported-function.src.ts", // "typescript/namespaces-and-modules/module-with-default-exports.src.ts", + + /** + * Requires fix in https://github.com/babel/babylon/pull/684 + */ // "typescript/namespaces-and-modules/shorthand-ambient-module-declaration.src.ts" ]; diff --git a/tests/lib/__snapshots__/typescript.js.snap b/tests/lib/__snapshots__/typescript.js.snap index bb6bfe6..70650d0 100644 --- a/tests/lib/__snapshots__/typescript.js.snap +++ b/tests/lib/__snapshots__/typescript.js.snap @@ -60668,36 +60668,8 @@ Object { ], "type": "TSModuleBlock", }, - "loc": Object { - "end": Object { - "column": 1, - "line": 3, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "modifiers": Array [ - Object { - "loc": Object { - "end": Object { - "column": 7, - "line": 1, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "range": Array [ - 0, - 7, - ], - "type": "TSDeclareKeyword", - }, - ], - "name": Object { + "declare": true, + "id": Object { "loc": Object { "end": Object { "column": 29, @@ -60716,6 +60688,16 @@ Object { "type": "Literal", "value": "i-use-things", }, + "loc": Object { + "end": Object { + "column": 1, + "line": 3, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, "range": Array [ 0, 56, @@ -61165,36 +61147,8 @@ Object { ], "type": "TSModuleBlock", }, - "loc": Object { - "end": Object { - "column": 1, - "line": 3, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "modifiers": Array [ - Object { - "loc": Object { - "end": Object { - "column": 7, - "line": 1, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "range": Array [ - 0, - 7, - ], - "type": "TSDeclareKeyword", - }, - ], - "name": Object { + "declare": true, + "id": Object { "loc": Object { "end": Object { "column": 20, @@ -61212,6 +61166,16 @@ Object { ], "type": "Identifier", }, + "loc": Object { + "end": Object { + "column": 1, + "line": 3, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, "range": Array [ 0, 84, @@ -61881,17 +61845,7 @@ Object { ], "type": "TSModuleBlock", }, - "loc": Object { - "end": Object { - "column": 1, - "line": 6, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "name": Object { + "id": Object { "loc": Object { "end": Object { "column": 12, @@ -61910,6 +61864,16 @@ Object { "type": "Literal", "value": "foo", }, + "loc": Object { + "end": Object { + "column": 1, + "line": 6, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, "range": Array [ 0, 112, @@ -62410,36 +62374,8 @@ exports[`typescript fixtures/namespaces-and-modules/shorthand-ambient-module-dec Object { "body": Array [ Object { - "loc": Object { - "end": Object { - "column": 32, - "line": 1, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "modifiers": Array [ - Object { - "loc": Object { - "end": Object { - "column": 7, - "line": 1, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "range": Array [ - 0, - 7, - ], - "type": "TSDeclareKeyword", - }, - ], - "name": Object { + "declare": true, + "id": Object { "loc": Object { "end": Object { "column": 31, @@ -62458,6 +62394,16 @@ Object { "type": "Literal", "value": "hot-new-module", }, + "loc": Object { + "end": Object { + "column": 32, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, "range": Array [ 0, 32,