Skip to content

Commit d2c9b33

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 7c37344 commit d2c9b33

4 files changed

+1238
-3
lines changed

lib/ast-converter.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,10 @@ module.exports = function(ast, extra) {
943943
}
944944
}
945945

946+
if (!node.body) {
947+
functionDeclarationType = "TSEmptyBodyFunctionDeclaration";
948+
}
949+
946950
/**
947951
* Prefix FunctionDeclarations within TypeScript namespaces with "TS"
948952
*/
@@ -1142,6 +1146,7 @@ module.exports = function(ast, extra) {
11421146
var methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
11431147
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
11441148
isAmbient = ts.isInAmbientContext(node),
1149+
isEmptyBody = !(node.body),
11451150
method = {
11461151
type: "FunctionExpression",
11471152
id: null,
@@ -1198,9 +1203,10 @@ module.exports = function(ast, extra) {
11981203
var methodDefinitionType = hasModifier(SyntaxKind.AbstractKeyword, node)
11991204
? "TSAbstractMethodDefinition"
12001205
: "MethodDefinition";
1201-
var methodDefinitionType = "MethodDefinition";
1206+
var methodDefinitionType = "MethodDefinition",
1207+
isAbstractMethod = false;
12021208
if (node.modifiers && node.modifiers.length) {
1203-
var isAbstractMethod = node.modifiers.some(function(modifier) {
1209+
isAbstractMethod = node.modifiers.some(function(modifier) {
12041210
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
12051211
});
12061212
if (isAbstractMethod) {
@@ -1212,6 +1218,13 @@ module.exports = function(ast, extra) {
12121218
method.type = "TSAmbientFunctionExpression";
12131219
}
12141220

1221+
if (isEmptyBody) {
1222+
if (!isAbstractMethod) {
1223+
methodDefinitionType = "TSEmptyBodyMethodDefinition";
1224+
}
1225+
method.type = "TSEmptyBodyFunctionExpression";
1226+
}
1227+
12151228
assign(result, {
12161229
type: methodDefinitionType,
12171230
key: convertChild(node.name),
@@ -1250,6 +1263,7 @@ module.exports = function(ast, extra) {
12501263
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
12511264
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
12521265
constructorIsAmbient = ts.isInAmbientContext(node),
1266+
constructorIsEmptyBody = !(node.body),
12531267
constructor = {
12541268
type: "FunctionExpression",
12551269
id: null,
@@ -1320,6 +1334,9 @@ module.exports = function(ast, extra) {
13201334
if (constructorIsAmbient) {
13211335
constructorMethodDefinitionType = "TSAmbientMethodDefinition";
13221336
constructor.type = "TSAmbientFunctionExpression";
1337+
} else if (constructorIsEmptyBody) {
1338+
constructorMethodDefinitionType = "TSEmptyBodyMethodDefinition";
1339+
constructor.type = "TSEmptyBodyFunctionExpression";
13231340
}
13241341

13251342
assign(result, {

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,

0 commit comments

Comments
 (0)