Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 08238fc

Browse files
committed
Fix: Use TSAbsractMethodDefinition for abstract constructor (fixes #228)
1 parent 471f403 commit 08238fc

File tree

3 files changed

+379
-29
lines changed

3 files changed

+379
-29
lines changed

lib/ast-converter.js

+18-29
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ function isESTreeClassMember(node) {
106106
}
107107

108108
/**
109-
* Returns true if the given node is an async function
110-
* @param {TSNode} node TypeScript AST node
111-
* @returns {boolean} is an async function
109+
* Checks if a TSNode has a modifier
110+
* @param {SyntaxKind} modifierKind TypeScript SyntaxKind modifier
111+
* @param {TSNode} node TypeScript AST node
112+
* @returns {boolean} has the modifier specified
112113
*/
113-
function isAsyncFunction(node) {
114+
function hasModifier(modifierKind, node) {
114115
return !!node.modifiers && !!node.modifiers.length && node.modifiers.some(function(modifier) {
115-
return modifier.kind === SyntaxKind.AsyncKeyword;
116+
return modifier.kind === modifierKind;
116117
});
117118
}
118119

@@ -905,9 +906,7 @@ module.exports = function(ast, extra) {
905906

906907
var functionDeclarationType = "FunctionDeclaration";
907908
if (node.modifiers && node.modifiers.length) {
908-
var isDeclareFunction = node.modifiers.some(function(modifier) {
909-
return modifier.kind === ts.SyntaxKind.DeclareKeyword;
910-
});
909+
var isDeclareFunction = hasModifier(ts.SyntaxKind.DeclareKeyword, node);
911910
if (isDeclareFunction) {
912911
functionDeclarationType = "DeclareFunction";
913912
}
@@ -925,7 +924,7 @@ module.exports = function(ast, extra) {
925924
id: convertChild(node.name),
926925
generator: !!node.asteriskToken,
927926
expression: false,
928-
async: isAsyncFunction(node),
927+
async: hasModifier(SyntaxKind.AsyncKeyword, node),
929928
params: node.parameters.map(convertChild),
930929
body: convertChild(node.body)
931930
});
@@ -1114,7 +1113,7 @@ module.exports = function(ast, extra) {
11141113
id: null,
11151114
generator: false,
11161115
expression: false,
1117-
async: isAsyncFunction(node),
1116+
async: hasModifier(SyntaxKind.AsyncKeyword, node),
11181117
body: convertChild(node.body),
11191118
range: [ node.parameters.pos - 1, result.range[1]],
11201119
loc: {
@@ -1162,15 +1161,9 @@ module.exports = function(ast, extra) {
11621161
/**
11631162
* TypeScript class methods can be defined as "abstract"
11641163
*/
1165-
var methodDefinitionType = "MethodDefinition";
1166-
if (node.modifiers && node.modifiers.length) {
1167-
var isAbstractMethod = node.modifiers.some(function(modifier) {
1168-
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
1169-
});
1170-
if (isAbstractMethod) {
1171-
methodDefinitionType = "TSAbstractMethodDefinition";
1172-
}
1173-
}
1164+
var methodDefinitionType = hasModifier(SyntaxKind.AbstractKeyword, node)
1165+
? "TSAbstractMethodDefinition"
1166+
: "MethodDefinition";
11741167

11751168
assign(result, {
11761169
type: methodDefinitionType,
@@ -1206,6 +1199,7 @@ module.exports = function(ast, extra) {
12061199
case SyntaxKind.Constructor:
12071200

12081201
var constructorIsStatic = Boolean(ts.getModifierFlags(node) & ts.ModifierFlags.Static),
1202+
constructorIsAbstract = hasModifier(SyntaxKind.AbstractKeyword, node),
12091203
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
12101204
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
12111205
constructor = {
@@ -1275,7 +1269,7 @@ module.exports = function(ast, extra) {
12751269
}
12761270

12771271
assign(result, {
1278-
type: "MethodDefinition",
1272+
type: constructorIsAbstract ? "TSAbstractMethodDefinition" : "MethodDefinition",
12791273
key: constructorKey,
12801274
value: constructor,
12811275
computed: constructorIsComputed,
@@ -1292,7 +1286,7 @@ module.exports = function(ast, extra) {
12921286
generator: !!node.asteriskToken,
12931287
params: node.parameters.map(convertChild),
12941288
body: convertChild(node.body),
1295-
async: isAsyncFunction(node),
1289+
async: hasModifier(SyntaxKind.AbstractKeyword, node),
12961290
expression: false
12971291
});
12981292
// Process returnType
@@ -1375,7 +1369,7 @@ module.exports = function(ast, extra) {
13751369
id: null,
13761370
params: node.parameters.map(convertChild),
13771371
body: convertChild(node.body),
1378-
async: isAsyncFunction(node),
1372+
async: hasModifier(SyntaxKind.AsyncKeyword, node),
13791373
expression: node.body.kind !== SyntaxKind.Block
13801374
});
13811375
// Process returnType
@@ -1501,9 +1495,7 @@ module.exports = function(ast, extra) {
15011495
range: [node.getStart(), node.end],
15021496
loc: getLoc(node, ast),
15031497
accessibility: getTSNodeAccessibility(node),
1504-
isReadonly: node.modifiers.some(function(modifier) {
1505-
return modifier.kind === SyntaxKind.ReadonlyKeyword;
1506-
}),
1498+
isReadonly: hasModifier(SyntaxKind.ReadonlyKeyword, node),
15071499
parameter: result
15081500
};
15091501
}
@@ -1533,10 +1525,7 @@ module.exports = function(ast, extra) {
15331525
* TypeScript class declarations can be defined as "abstract"
15341526
*/
15351527
if (node.kind === SyntaxKind.ClassDeclaration) {
1536-
var isAbstractClass = node.modifiers.some(function(modifier) {
1537-
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
1538-
});
1539-
if (isAbstractClass) {
1528+
if (hasModifier(SyntaxKind.AbstractKeyword, node)) {
15401529
classNodeType = "TSAbstract" + classNodeType;
15411530
}
15421531
}

0 commit comments

Comments
 (0)