From 269b558a5f7f8ef4a7cee899c896ca65931cb21e Mon Sep 17 00:00:00 2001 From: Rasmus Eneman Date: Sun, 19 Mar 2017 15:02:00 +0100 Subject: [PATCH] Fix: Label variable declartions as Ambient (fixes #185) --- lib/ast-converter.js | 8 +- .../basics/declare-variable.result.js | 220 ++++++++++++++++++ .../typescript/basics/declare-variable.src.ts | 1 + 3 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/typescript/basics/declare-variable.result.js create mode 100644 tests/fixtures/typescript/basics/declare-variable.src.ts diff --git a/lib/ast-converter.js b/lib/ast-converter.js index ea8a187..8c00bd6 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -954,8 +954,14 @@ module.exports = function(ast, extra) { break; case SyntaxKind.VariableStatement: + var variableDeclarationType = "VariableDeclaration", + variableIsAmbient = ts.isInAmbientContext(node); + + if (variableIsAmbient) { + variableDeclarationType = "TSAmbientVariableDeclaration"; + } assign(result, { - type: "VariableDeclaration", + type: variableDeclarationType, declarations: node.declarationList.declarations.map(convertChild), kind: getDeclarationKind(node.declarationList) }); diff --git a/tests/fixtures/typescript/basics/declare-variable.result.js b/tests/fixtures/typescript/basics/declare-variable.result.js new file mode 100644 index 0000000..16525c1 --- /dev/null +++ b/tests/fixtures/typescript/basics/declare-variable.result.js @@ -0,0 +1,220 @@ +module.exports = { + "body": [ + { + "declarations": [ + { + "id": { + "loc": { + "end": { + "column": 13, + "line": 1 + }, + "start": { + "column": 12, + "line": 1 + } + }, + "name": "a", + "range": [ + 12, + 13 + ], + "type": "Identifier", + "typeAnnotation": { + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 15, + "line": 1 + } + }, + "range": [ + 15, + 21 + ], + "type": "TypeAnnotation", + "typeAnnotation": { + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 15, + "line": 1 + } + }, + "range": [ + 15, + 21 + ], + "type": "TSStringKeyword" + } + } + }, + "init": null, + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 12, + "line": 1 + } + }, + "range": [ + 12, + 21 + ], + "type": "VariableDeclarator" + } + ], + "kind": "let", + "loc": { + "end": { + "column": 22, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 22 + ], + "type": "TSAmbientVariableDeclaration" + } + ], + "loc": { + "end": { + "column": 22, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 22 + ], + "sourceType": "script", + "tokens": [ + { + "loc": { + "end": { + "column": 7, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 7 + ], + "type": "Identifier", + "value": "declare" + }, + { + "loc": { + "end": { + "column": 11, + "line": 1 + }, + "start": { + "column": 8, + "line": 1 + } + }, + "range": [ + 8, + 11 + ], + "type": "Keyword", + "value": "let" + }, + { + "loc": { + "end": { + "column": 13, + "line": 1 + }, + "start": { + "column": 12, + "line": 1 + } + }, + "range": [ + 12, + 13 + ], + "type": "Identifier", + "value": "a" + }, + { + "loc": { + "end": { + "column": 14, + "line": 1 + }, + "start": { + "column": 13, + "line": 1 + } + }, + "range": [ + 13, + 14 + ], + "type": "Punctuator", + "value": ":" + }, + { + "loc": { + "end": { + "column": 21, + "line": 1 + }, + "start": { + "column": 15, + "line": 1 + } + }, + "range": [ + 15, + 21 + ], + "type": "Identifier", + "value": "string" + }, + { + "loc": { + "end": { + "column": 22, + "line": 1 + }, + "start": { + "column": 21, + "line": 1 + } + }, + "range": [ + 21, + 22 + ], + "type": "Punctuator", + "value": ";" + } + ], + "type": "Program" +}; diff --git a/tests/fixtures/typescript/basics/declare-variable.src.ts b/tests/fixtures/typescript/basics/declare-variable.src.ts new file mode 100644 index 0000000..dd91f32 --- /dev/null +++ b/tests/fixtures/typescript/basics/declare-variable.src.ts @@ -0,0 +1 @@ +declare let a: string; \ No newline at end of file