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

Fix: Label empty body functions (fixes #92) #166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,10 @@ module.exports = function(ast, extra) {
}
}

if (!node.body) {
functionDeclarationType = "TSEmptyBodyFunctionDeclaration";
}

/**
* Prefix FunctionDeclarations within TypeScript namespaces with "TS"
*/
Expand Down Expand Up @@ -1074,6 +1078,7 @@ module.exports = function(ast, extra) {
// TODO: double-check that these positions are correct
var methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
isEmptyBody = !(node.body),
method = {
type: "FunctionExpression",
id: null,
Expand Down Expand Up @@ -1127,16 +1132,24 @@ module.exports = function(ast, extra) {
/**
* TypeScript class methods can be defined as "abstract"
*/
var methodDefinitionType = "MethodDefinition";
var methodDefinitionType = "MethodDefinition",
isAbstractMethod = false;
if (node.modifiers && node.modifiers.length) {
var isAbstractMethod = node.modifiers.some(function(modifier) {
isAbstractMethod = node.modifiers.some(function(modifier) {
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
});
if (isAbstractMethod) {
methodDefinitionType = "TSAbstractMethodDefinition";
}
}

if (isEmptyBody) {
if (!isAbstractMethod) {
methodDefinitionType = "TSEmptyBodyMethodDefinition";
}
method.type = "TSEmptyBodyFunctionExpression";
}

assign(result, {
type: methodDefinitionType,
key: convertChild(node.name),
Expand Down Expand Up @@ -1167,6 +1180,7 @@ module.exports = function(ast, extra) {
var constructorIsStatic = Boolean(node.flags & ts.NodeFlags.Static),
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
constructorIsEmptyBody = !(node.body),
constructor = {
type: "FunctionExpression",
id: null,
Expand Down Expand Up @@ -1230,8 +1244,14 @@ module.exports = function(ast, extra) {
};
}

var constructorMethodDefinitionType = "MethodDefinition";
if (constructorIsEmptyBody) {
constructorMethodDefinitionType = "TSEmptyBodyMethodDefinition";
constructor.type = "TSEmptyBodyFunctionExpression";
}

assign(result, {
type: "MethodDefinition",
type: constructorMethodDefinitionType,
key: constructorKey,
value: constructor,
computed: constructorIsComputed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = {
"name": "createSocket"
},
"value": {
"type": "FunctionExpression",
"type": "TSEmptyBodyFunctionExpression",
"id": null,
"generator": false,
"expression": false,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/typescript/basics/declare-function.result.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
},
"body": [
{
"type": "DeclareFunction",
"type": "TSEmptyBodyFunctionDeclaration",
"range": [
0,
42
Expand Down Expand Up @@ -344,4 +344,4 @@ module.exports = {
}
}
]
};
};
Loading