From e51a51022ae62ff220bb2e14ec978f818e73182f Mon Sep 17 00:00:00 2001 From: Philipp A Date: Wed, 12 Apr 2017 16:04:54 +0200 Subject: [PATCH] Breaking: Add .body to TSModuleBlock nodes (fixes #217) --- lib/ast-converter.js | 7 + ...t-module-declaration-with-import.result.js | 349 ++++++++++++++++++ ...ient-module-declaration-with-import.src.ts | 3 + ...namespace-with-exported-function.result.js | 4 +- 4 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.result.js create mode 100644 tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts diff --git a/lib/ast-converter.js b/lib/ast-converter.js index f0ded24..c116d59 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1491,6 +1491,13 @@ module.exports = function(ast, extra) { break; // Modules + case SyntaxKind.ModuleBlock: + assign(result, { + type: "TSModuleBlock", + body: node.statements.map(convertChild) + }); + break; + case SyntaxKind.ImportDeclaration: assign(result, { type: "ImportDeclaration", diff --git a/tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.result.js b/tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.result.js new file mode 100644 index 0000000..63172cd --- /dev/null +++ b/tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.result.js @@ -0,0 +1,349 @@ +module.exports = { + "body": [ + { + "body": { + "body": [ + { + "loc": { + "end": { + "column": 22, + "line": 2 + }, + "start": { + "column": 2, + "line": 2 + } + }, + "range": [ + 34, + 54 + ], + "source": { + "loc": { + "end": { + "column": 21, + "line": 2 + }, + "start": { + "column": 17, + "line": 2 + } + }, + "range": [ + 49, + 53 + ], + "raw": "'fs'", + "type": "Literal", + "value": "fs" + }, + "specifiers": [ + { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "local": { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "name": "fs", + "range": [ + 41, + 43 + ], + "type": "Identifier" + }, + "range": [ + 41, + 43 + ], + "type": "ImportDefaultSpecifier" + } + ], + "type": "ImportDeclaration" + } + ], + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 30, + "line": 1 + } + }, + "range": [ + 30, + 56 + ], + "type": "TSModuleBlock" + }, + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "modifiers": [ + { + "loc": { + "end": { + "column": 7, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 7 + ], + "type": "TSDeclareKeyword" + } + ], + "name": { + "loc": { + "end": { + "column": 29, + "line": 1 + }, + "start": { + "column": 15, + "line": 1 + } + }, + "range": [ + 15, + 29 + ], + "raw": "\"i-use-things\"", + "type": "Literal", + "value": "i-use-things" + }, + "range": [ + 0, + 56 + ], + "type": "TSModuleDeclaration" + } + ], + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 56 + ], + "sourceType": "script", + "tokens": [ + { + "loc": { + "end": { + "column": 7, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 7 + ], + "type": "Identifier", + "value": "declare" + }, + { + "loc": { + "end": { + "column": 14, + "line": 1 + }, + "start": { + "column": 8, + "line": 1 + } + }, + "range": [ + 8, + 14 + ], + "type": "Identifier", + "value": "module" + }, + { + "loc": { + "end": { + "column": 29, + "line": 1 + }, + "start": { + "column": 15, + "line": 1 + } + }, + "range": [ + 15, + 29 + ], + "type": "String", + "value": "\"i-use-things\"" + }, + { + "loc": { + "end": { + "column": 31, + "line": 1 + }, + "start": { + "column": 30, + "line": 1 + } + }, + "range": [ + 30, + 31 + ], + "type": "Punctuator", + "value": "{" + }, + { + "loc": { + "end": { + "column": 8, + "line": 2 + }, + "start": { + "column": 2, + "line": 2 + } + }, + "range": [ + 34, + 40 + ], + "type": "Keyword", + "value": "import" + }, + { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "range": [ + 41, + 43 + ], + "type": "Identifier", + "value": "fs" + }, + { + "loc": { + "end": { + "column": 16, + "line": 2 + }, + "start": { + "column": 12, + "line": 2 + } + }, + "range": [ + 44, + 48 + ], + "type": "Identifier", + "value": "from" + }, + { + "loc": { + "end": { + "column": 21, + "line": 2 + }, + "start": { + "column": 17, + "line": 2 + } + }, + "range": [ + 49, + 53 + ], + "type": "String", + "value": "'fs'" + }, + { + "loc": { + "end": { + "column": 22, + "line": 2 + }, + "start": { + "column": 21, + "line": 2 + } + }, + "range": [ + 53, + 54 + ], + "type": "Punctuator", + "value": ";" + }, + { + "loc": { + "end": { + "column": 1, + "line": 3 + }, + "start": { + "column": 0, + "line": 3 + } + }, + "range": [ + 55, + 56 + ], + "type": "Punctuator", + "value": "}" + } + ], + "type": "Program" +} diff --git a/tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts b/tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts new file mode 100644 index 0000000..9b551c2 --- /dev/null +++ b/tests/fixtures/typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts @@ -0,0 +1,3 @@ +declare module "i-use-things" { + import fs from 'fs'; +} diff --git a/tests/fixtures/typescript/namespaces-and-modules/declare-namespace-with-exported-function.result.js b/tests/fixtures/typescript/namespaces-and-modules/declare-namespace-with-exported-function.result.js index 9768931..ab33f26 100644 --- a/tests/fixtures/typescript/namespaces-and-modules/declare-namespace-with-exported-function.result.js +++ b/tests/fixtures/typescript/namespaces-and-modules/declare-namespace-with-exported-function.result.js @@ -84,7 +84,7 @@ module.exports = { "column": 1 } }, - "statements": [ + "body": [ { "type": "TSNamespaceExportDeclaration", "declaration": { @@ -616,4 +616,4 @@ module.exports = { } } ] -}; \ No newline at end of file +};