Skip to content

Commit 53f409f

Browse files
committed
Fix: Label empty body functions (fixes eslint#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 53f409f

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

Diff for: lib/ast-converter.js

+23-3
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,
@@ -1127,16 +1132,24 @@ module.exports = function(ast, extra) {
11271132
/**
11281133
* TypeScript class methods can be defined as "abstract"
11291134
*/
1130-
var methodDefinitionType = "MethodDefinition";
1135+
var methodDefinitionType = "MethodDefinition",
1136+
isAbstractMethod = false;
11311137
if (node.modifiers && node.modifiers.length) {
1132-
var isAbstractMethod = node.modifiers.some(function(modifier) {
1138+
isAbstractMethod = node.modifiers.some(function(modifier) {
11331139
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
11341140
});
11351141
if (isAbstractMethod) {
11361142
methodDefinitionType = "TSAbstractMethodDefinition";
11371143
}
11381144
}
11391145

1146+
if (isEmptyBody) {
1147+
if (!isAbstractMethod) {
1148+
methodDefinitionType = "TSEmptyBodyMethodDefinition";
1149+
}
1150+
method.type = "TSEmptyBodyFunctionExpression";
1151+
}
1152+
11401153
assign(result, {
11411154
type: methodDefinitionType,
11421155
key: convertChild(node.name),
@@ -1167,6 +1180,7 @@ module.exports = function(ast, extra) {
11671180
var constructorIsStatic = Boolean(node.flags & ts.NodeFlags.Static),
11681181
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
11691182
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
1183+
constructorIsEmptyBody = !(node.body),
11701184
constructor = {
11711185
type: "FunctionExpression",
11721186
id: null,
@@ -1230,8 +1244,14 @@ module.exports = function(ast, extra) {
12301244
};
12311245
}
12321246

1247+
var constructorMethodDefinitionType = "MethodDefinition";
1248+
if (constructorIsEmptyBody) {
1249+
constructorMethodDefinitionType = "TSEmptyBodyMethodDefinition";
1250+
constructor.type = "TSEmptyBodyFunctionExpression";
1251+
}
1252+
12331253
assign(result, {
1234-
type: "MethodDefinition",
1254+
type: constructorMethodDefinitionType,
12351255
key: constructorKey,
12361256
value: constructor,
12371257
computed: constructorIsComputed,

Diff for: tests/fixtures/typescript/basics/abstract-class-with-abstract-method.result.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module.exports = {
8989
"name": "createSocket"
9090
},
9191
"value": {
92-
"type": "FunctionExpression",
92+
"type": "TSEmptyBodyFunctionExpression",
9393
"id": null,
9494
"generator": false,
9595
"expression": false,

Diff for: tests/fixtures/typescript/basics/declare-function.result.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
},
1717
"body": [
1818
{
19-
"type": "DeclareFunction",
19+
"type": "TSEmptyBodyFunctionDeclaration",
2020
"range": [
2121
0,
2222
42
@@ -344,4 +344,4 @@ module.exports = {
344344
}
345345
}
346346
]
347-
};
347+
};

0 commit comments

Comments
 (0)