Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
Fix: Distinguish between DeclareFunction and FunctionDeclaration (#79)
Browse files Browse the repository at this point in the history
Refs #78
  • Loading branch information
JamesHenry authored and nzakas committed Sep 7, 2016
1 parent d58041f commit 799fd63
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
313 changes: 313 additions & 0 deletions tests/fixtures/typescript/basics/declare-function.result.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
]
};
1 change: 1 addition & 0 deletions tests/fixtures/typescript/basics/declare-function.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare function foo(bar: string): string;

0 comments on commit 799fd63

Please sign in to comment.