Skip to content

Commit 883c813

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 74df8bd commit 883c813

4 files changed

+1238
-3
lines changed

Diff for: lib/ast-converter.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,10 @@ module.exports = function(ast, extra) {
909909
}
910910
}
911911

912+
if (!node.body) {
913+
functionDeclarationType = "TSEmptyBodyFunctionDeclaration";
914+
}
915+
912916
/**
913917
* Prefix FunctionDeclarations within TypeScript namespaces with "TS"
914918
*/
@@ -1106,6 +1110,7 @@ module.exports = function(ast, extra) {
11061110
var methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
11071111
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
11081112
isAmbient = ts.isInAmbientContext(node),
1113+
isEmptyBody = !(node.body),
11091114
method = {
11101115
type: "FunctionExpression",
11111116
id: null,
@@ -1159,9 +1164,10 @@ module.exports = function(ast, extra) {
11591164
/**
11601165
* TypeScript class methods can be defined as "abstract"
11611166
*/
1162-
var methodDefinitionType = "MethodDefinition";
1167+
var methodDefinitionType = "MethodDefinition",
1168+
isAbstractMethod = false;
11631169
if (node.modifiers && node.modifiers.length) {
1164-
var isAbstractMethod = node.modifiers.some(function(modifier) {
1170+
isAbstractMethod = node.modifiers.some(function(modifier) {
11651171
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
11661172
});
11671173
if (isAbstractMethod) {
@@ -1173,6 +1179,13 @@ module.exports = function(ast, extra) {
11731179
method.type = "TSAmbientFunctionExpression";
11741180
}
11751181

1182+
if (isEmptyBody) {
1183+
if (!isAbstractMethod) {
1184+
methodDefinitionType = "TSEmptyBodyMethodDefinition";
1185+
}
1186+
method.type = "TSEmptyBodyFunctionExpression";
1187+
}
1188+
11761189
assign(result, {
11771190
type: methodDefinitionType,
11781191
key: convertChild(node.name),
@@ -1204,6 +1217,7 @@ module.exports = function(ast, extra) {
12041217
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
12051218
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
12061219
constructorIsAmbient = ts.isInAmbientContext(node),
1220+
constructorIsEmptyBody = !(node.body),
12071221
constructor = {
12081222
type: "FunctionExpression",
12091223
id: null,
@@ -1288,6 +1302,9 @@ module.exports = function(ast, extra) {
12881302
if (constructorIsAmbient) {
12891303
constructorMethodDefinitionType = "TSAmbientMethodDefinition";
12901304
constructor.type = "TSAmbientFunctionExpression";
1305+
} else if (constructorIsEmptyBody) {
1306+
constructorMethodDefinitionType = "TSEmptyBodyMethodDefinition";
1307+
constructor.type = "TSEmptyBodyFunctionExpression";
12911308
}
12921309

12931310
assign(result, {

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,

0 commit comments

Comments
 (0)