diff --git a/lib/ast-converter.js b/lib/ast-converter.js index b63aa32..a39b14d 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -215,8 +215,18 @@ function fixExports(node, result, ast) { result.range[0] = varToken.getStart(); result.loc = getLocFor(result.range[0], result.range[1], ast); + var declarationType = declarationIsDefault ? "ExportDefaultDeclaration" : "ExportNamedDeclaration"; + + /** + * Prefix exports from TypeScript namespaces with "TS" to distinguish + * them from ES2015 exports + */ + if (node.parent && node.parent.kind === SyntaxKind.ModuleBlock) { + declarationType = "TSNamespaceExportDeclaration"; + } + var newResult = { - type: declarationIsDefault ? "ExportDefaultDeclaration" : "ExportNamedDeclaration", + type: declarationType, declaration: result, range: [ exportKeyword.getStart(), result.range[1] ], loc: getLocFor(exportKeyword.getStart(), result.range[1], ast) @@ -776,6 +786,13 @@ module.exports = function(ast, extra) { } } + /** + * Prefix FunctionDeclarations within TypeScript namespaces with "TS" + */ + if (node.parent && node.parent.kind === SyntaxKind.ModuleBlock) { + functionDeclarationType = "TSNamespaceFunctionDeclaration"; + } + assign(result, { type: functionDeclarationType, id: convertChild(node.name), diff --git a/tests/fixtures/typescript/basics/declare-namespace-with-exported-function.result.js b/tests/fixtures/typescript/basics/declare-namespace-with-exported-function.result.js new file mode 100644 index 0000000..86d1256 --- /dev/null +++ b/tests/fixtures/typescript/basics/declare-namespace-with-exported-function.result.js @@ -0,0 +1,590 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 84 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSModuleDeclaration", + "range": [ + 0, + 84 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "flags": 65540, + "modifiers": [ + { + "type": "TSDeclareKeyword", + "range": [ + 0, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "flags": 0 + } + ], + "name": { + "type": "Identifier", + "range": [ + 18, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "name": "d3" + }, + "body": { + "type": "TSModuleBlock", + "range": [ + 21, + 84 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "flags": 0, + "statements": [ + { + "type": "TSNamespaceExportDeclaration", + "declaration": { + "type": "TSNamespaceFunctionDeclaration", + "range": [ + 32, + 82 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 59 + } + }, + "id": { + "type": "Identifier", + "range": [ + 41, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "name": "select" + }, + "generator": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "range": [ + 48, + 56 + ], + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "name": "selector" + } + ], + "body": null, + "returnType": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 58 + } + }, + "range": [ + 67, + 81 + ], + "typeAnnotation": { + "type": "TSTypeReference", + "range": [ + 67, + 81 + ], + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 58 + } + }, + "flags": 0, + "typeName": { + "type": "Identifier", + "range": [ + 67, + 76 + ], + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 53 + } + }, + "name": "Selection" + }, + "typeArguments": [ + { + "type": "TSAnyKeyword", + "range": [ + 77, + 80 + ], + "loc": { + "start": { + "line": 2, + "column": 54 + }, + "end": { + "line": 2, + "column": 57 + } + }, + "flags": 0 + } + ] + } + } + }, + "range": [ + 25, + 82 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 59 + } + }, + "specifiers": [], + "source": null + } + ] + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Identifier", + "value": "declare", + "range": [ + 0, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Identifier", + "value": "namespace", + "range": [ + 8, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "Identifier", + "value": "d3", + "range": [ + 18, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Keyword", + "value": "export", + "range": [ + 25, + 31 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + } + } + }, + { + "type": "Keyword", + "value": "function", + "range": [ + 32, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 17 + } + } + }, + { + "type": "Identifier", + "value": "select", + "range": [ + 41, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 47, + 48 + ], + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + } + } + }, + { + "type": "Identifier", + "value": "selector", + "range": [ + 48, + 56 + ], + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 33 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 56, + 57 + ], + "loc": { + "start": { + "line": 2, + "column": 33 + }, + "end": { + "line": 2, + "column": 34 + } + } + }, + { + "type": "Identifier", + "value": "string", + "range": [ + 58, + 64 + ], + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 41 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 64, + 65 + ], + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 42 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 65, + 66 + ], + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 43 + } + } + }, + { + "type": "Identifier", + "value": "Selection", + "range": [ + 67, + 76 + ], + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 53 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 76, + 77 + ], + "loc": { + "start": { + "line": 2, + "column": 53 + }, + "end": { + "line": 2, + "column": 54 + } + } + }, + { + "type": "Identifier", + "value": "any", + "range": [ + 77, + 80 + ], + "loc": { + "start": { + "line": 2, + "column": 54 + }, + "end": { + "line": 2, + "column": 57 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 80, + 81 + ], + "loc": { + "start": { + "line": 2, + "column": 57 + }, + "end": { + "line": 2, + "column": 58 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 81, + 82 + ], + "loc": { + "start": { + "line": 2, + "column": 58 + }, + "end": { + "line": 2, + "column": 59 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 83, + 84 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/declare-namespace-with-exported-function.src.ts b/tests/fixtures/typescript/basics/declare-namespace-with-exported-function.src.ts new file mode 100644 index 0000000..4035578 --- /dev/null +++ b/tests/fixtures/typescript/basics/declare-namespace-with-exported-function.src.ts @@ -0,0 +1,3 @@ +declare namespace d3 { + export function select(selector: string): Selection; +} \ No newline at end of file