diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 72f8caa..b63aa32 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -765,22 +765,36 @@ module.exports = function(ast, extra) { // Declarations case SyntaxKind.FunctionDeclaration: + + var functionDeclarationType = "FunctionDeclaration"; + if (node.modifiers && node.modifiers.length) { + var isDeclareFunction = node.modifiers.some(function(modifier) { + return modifier.kind === ts.SyntaxKind.DeclareKeyword; + }); + if (isDeclareFunction) { + functionDeclarationType = "DeclareFunction"; + } + } + assign(result, { - type: "FunctionDeclaration", + type: functionDeclarationType, id: convertChild(node.name), generator: !!node.asteriskToken, expression: false, params: node.parameters.map(convertChild), body: convertChild(node.body) }); + // Process returnType if (node.type) { result.returnType = convertTypeAnnotation(node.type); } + // Process typeParameters if (node.typeParameters && node.typeParameters.length) { result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters); } + // check for exports result = fixExports(node, result, ast); diff --git a/tests/fixtures/typescript/basics/declare-function.result.js b/tests/fixtures/typescript/basics/declare-function.result.js new file mode 100644 index 0000000..8531c5b --- /dev/null +++ b/tests/fixtures/typescript/basics/declare-function.result.js @@ -0,0 +1,313 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "body": [ + { + "type": "DeclareFunction", + "range": [ + 0, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "id": { + "type": "Identifier", + "range": [ + 17, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "name": "foo" + }, + "generator": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "range": [ + 21, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "name": "bar" + } + ], + "body": null, + "returnType": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "range": [ + 35, + 41 + ], + "typeAnnotation": { + "type": "TSStringKeyword", + "range": [ + 35, + 41 + ], + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "flags": 0 + } + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Identifier", + "value": "declare", + "range": [ + 0, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Keyword", + "value": "function", + "range": [ + 8, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 16 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "range": [ + 17, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Identifier", + "value": "bar", + "range": [ + 21, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + { + "type": "Identifier", + "value": "string", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 32 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 33 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + } + } + }, + { + "type": "Identifier", + "value": "string", + "range": [ + 35, + 41 + ], + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 41 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 41, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 42 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/basics/declare-function.src.ts b/tests/fixtures/typescript/basics/declare-function.src.ts new file mode 100644 index 0000000..4f4d107 --- /dev/null +++ b/tests/fixtures/typescript/basics/declare-function.src.ts @@ -0,0 +1 @@ +declare function foo(bar: string): string; \ No newline at end of file