Skip to content

Commit aa1c276

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 76c33f8 commit aa1c276

8 files changed

+1444
-4
lines changed

lib/ast-converter.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ module.exports = function(ast, extra) {
875875
return modifier.kind === ts.SyntaxKind.DeclareKeyword;
876876
});
877877
if (isDeclareFunction) {
878-
functionDeclarationType = "DeclareFunction";
878+
functionDeclarationType = "TSAmbientFunctionDeclaration";
879879
}
880880
}
881881

@@ -1074,6 +1074,7 @@ module.exports = function(ast, extra) {
10741074
// TODO: double-check that these positions are correct
10751075
var methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
10761076
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
1077+
isAmbient = ts.isInAmbientContext(node),
10771078
method = {
10781079
type: "FunctionExpression",
10791080
id: null,
@@ -1136,6 +1137,10 @@ module.exports = function(ast, extra) {
11361137
methodDefinitionType = "TSAbstractMethodDefinition";
11371138
}
11381139
}
1140+
if (isAmbient) {
1141+
methodDefinitionType = "TSAmbientMethodDefinition";
1142+
method.type = "TSAmbientFunctionExpression";
1143+
}
11391144

11401145
assign(result, {
11411146
type: methodDefinitionType,
@@ -1167,6 +1172,7 @@ module.exports = function(ast, extra) {
11671172
var constructorIsStatic = Boolean(node.flags & ts.NodeFlags.Static),
11681173
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
11691174
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
1175+
constructorIsAmbient = ts.isInAmbientContext(node),
11701176
constructor = {
11711177
type: "FunctionExpression",
11721178
id: null,
@@ -1230,8 +1236,14 @@ module.exports = function(ast, extra) {
12301236
};
12311237
}
12321238

1239+
var constructorMethodDefinitionType = "MethodDefinition";
1240+
if (constructorIsAmbient) {
1241+
constructorMethodDefinitionType = "TSAmbientMethodDefinition";
1242+
constructor.type = "TSAmbientFunctionExpression";
1243+
}
1244+
12331245
assign(result, {
1234-
type: "MethodDefinition",
1246+
type: constructorMethodDefinitionType,
12351247
key: constructorKey,
12361248
value: constructor,
12371249
computed: constructorIsComputed,

0 commit comments

Comments
 (0)