Skip to content

Commit 74df8bd

Browse files
committed
Fix: Label Functions and Methods declartions as Ambient (fixes eslint#162)
Ambient functions do not have a body and will cuase rules to throw an excpetion. We use the types TSAmbientFunctionExpression and TSAmbientMethoDeclaration.
1 parent 4d755ed commit 74df8bd

8 files changed

+1444
-4
lines changed

lib/ast-converter.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ module.exports = function(ast, extra) {
905905
return modifier.kind === ts.SyntaxKind.DeclareKeyword;
906906
});
907907
if (isDeclareFunction) {
908-
functionDeclarationType = "DeclareFunction";
908+
functionDeclarationType = "TSAmbientFunctionDeclaration";
909909
}
910910
}
911911

@@ -1105,6 +1105,7 @@ module.exports = function(ast, extra) {
11051105
// TODO: double-check that these positions are correct
11061106
var methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
11071107
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
1108+
isAmbient = ts.isInAmbientContext(node),
11081109
method = {
11091110
type: "FunctionExpression",
11101111
id: null,
@@ -1167,6 +1168,10 @@ module.exports = function(ast, extra) {
11671168
methodDefinitionType = "TSAbstractMethodDefinition";
11681169
}
11691170
}
1171+
if (isAmbient) {
1172+
methodDefinitionType = "TSAmbientMethodDefinition";
1173+
method.type = "TSAmbientFunctionExpression";
1174+
}
11701175

11711176
assign(result, {
11721177
type: methodDefinitionType,
@@ -1198,6 +1203,7 @@ module.exports = function(ast, extra) {
11981203
var constructorIsStatic = Boolean(ts.getModifierFlags(node) & ts.ModifierFlags.Static),
11991204
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
12001205
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
1206+
constructorIsAmbient = ts.isInAmbientContext(node),
12011207
constructor = {
12021208
type: "FunctionExpression",
12031209
id: null,
@@ -1278,8 +1284,14 @@ module.exports = function(ast, extra) {
12781284
};
12791285
}
12801286

1287+
var constructorMethodDefinitionType = "MethodDefinition";
1288+
if (constructorIsAmbient) {
1289+
constructorMethodDefinitionType = "TSAmbientMethodDefinition";
1290+
constructor.type = "TSAmbientFunctionExpression";
1291+
}
1292+
12811293
assign(result, {
1282-
type: "MethodDefinition",
1294+
type: constructorMethodDefinitionType,
12831295
key: constructorKey,
12841296
value: constructor,
12851297
computed: constructorIsComputed,

0 commit comments

Comments
 (0)