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

Commit edb6904

Browse files
committed
Fix: Label empty body functions (fixes #92)
There are rules in eslint that expect FunctionExpression nodes to contain a body. We should not produce an invalid estree node if we use the estree node types.
1 parent 76c33f8 commit edb6904

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/ast-converter.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,10 @@ module.exports = function(ast, extra) {
879879
}
880880
}
881881

882+
if (!node.body) {
883+
functionDeclarationType = "TSEmptyBodyFunctionDeclaration";
884+
}
885+
882886
/**
883887
* Prefix FunctionDeclarations within TypeScript namespaces with "TS"
884888
*/
@@ -1074,6 +1078,7 @@ module.exports = function(ast, extra) {
10741078
// TODO: double-check that these positions are correct
10751079
var methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
10761080
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
1081+
isEmptyBody = !(node.body),
10771082
method = {
10781083
type: "FunctionExpression",
10791084
id: null,
@@ -1137,6 +1142,11 @@ module.exports = function(ast, extra) {
11371142
}
11381143
}
11391144

1145+
if (isEmptyBody) {
1146+
methodDefinitionType = "TSEmptyBodyMethodDefinition";
1147+
method.type = "TSEmptyBodyFunctionExpression";
1148+
}
1149+
11401150
assign(result, {
11411151
type: methodDefinitionType,
11421152
key: convertChild(node.name),
@@ -1167,6 +1177,7 @@ module.exports = function(ast, extra) {
11671177
var constructorIsStatic = Boolean(node.flags & ts.NodeFlags.Static),
11681178
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
11691179
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
1180+
constructorIsEmptyBody = !(node.body),
11701181
constructor = {
11711182
type: "FunctionExpression",
11721183
id: null,
@@ -1230,8 +1241,14 @@ module.exports = function(ast, extra) {
12301241
};
12311242
}
12321243

1244+
var constructorMethodDefinitionType = "MethodDefinition";
1245+
if (constructorIsEmptyBody) {
1246+
constructorMethodDefinitionType = "TSEmptyBodyMethodDefinition";
1247+
constructor.type = "TSEmptyBodyFunctionExpression";
1248+
}
1249+
12331250
assign(result, {
1234-
type: "MethodDefinition",
1251+
type: constructorMethodDefinitionType,
12351252
key: constructorKey,
12361253
value: constructor,
12371254
computed: constructorIsComputed,

0 commit comments

Comments
 (0)