Skip to content

Commit 7c37344

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 00ad71d commit 7c37344

8 files changed

+1452
-3
lines changed

Diff for: lib/ast-converter.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ module.exports = function(ast, extra) {
939939
if (node.modifiers && node.modifiers.length) {
940940
var isDeclareFunction = hasModifier(ts.SyntaxKind.DeclareKeyword, node);
941941
if (isDeclareFunction) {
942-
functionDeclarationType = "DeclareFunction";
942+
functionDeclarationType = "TSAmbientFunctionDeclaration";
943943
}
944944
}
945945

@@ -1141,6 +1141,7 @@ module.exports = function(ast, extra) {
11411141
// TODO: double-check that these positions are correct
11421142
var methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
11431143
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
1144+
isAmbient = ts.isInAmbientContext(node),
11441145
method = {
11451146
type: "FunctionExpression",
11461147
id: null,
@@ -1197,6 +1198,19 @@ module.exports = function(ast, extra) {
11971198
var methodDefinitionType = hasModifier(SyntaxKind.AbstractKeyword, node)
11981199
? "TSAbstractMethodDefinition"
11991200
: "MethodDefinition";
1201+
var methodDefinitionType = "MethodDefinition";
1202+
if (node.modifiers && node.modifiers.length) {
1203+
var isAbstractMethod = node.modifiers.some(function(modifier) {
1204+
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
1205+
});
1206+
if (isAbstractMethod) {
1207+
methodDefinitionType = "TSAbstractMethodDefinition";
1208+
}
1209+
}
1210+
if (isAmbient) {
1211+
methodDefinitionType = "TSAmbientMethodDefinition";
1212+
method.type = "TSAmbientFunctionExpression";
1213+
}
12001214

12011215
assign(result, {
12021216
type: methodDefinitionType,
@@ -1235,6 +1249,7 @@ module.exports = function(ast, extra) {
12351249
constructorIsAbstract = hasModifier(SyntaxKind.AbstractKeyword, node),
12361250
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
12371251
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
1252+
constructorIsAmbient = ts.isInAmbientContext(node),
12381253
constructor = {
12391254
type: "FunctionExpression",
12401255
id: null,
@@ -1301,6 +1316,12 @@ module.exports = function(ast, extra) {
13011316
};
13021317
}
13031318

1319+
var constructorMethodDefinitionType = "MethodDefinition";
1320+
if (constructorIsAmbient) {
1321+
constructorMethodDefinitionType = "TSAmbientMethodDefinition";
1322+
constructor.type = "TSAmbientFunctionExpression";
1323+
}
1324+
13041325
assign(result, {
13051326
type: constructorIsAbstract ? "TSAbstractMethodDefinition" : "MethodDefinition",
13061327
key: constructorKey,

0 commit comments

Comments
 (0)