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

Commit 7cc865e

Browse files
authored
Fix: Support abstract classes and methods (fixes #80) (#81)
1 parent 3f802d5 commit 7cc865e

File tree

3 files changed

+561
-8
lines changed

3 files changed

+561
-8
lines changed

lib/ast-converter.js

+38-8
Original file line numberDiff line numberDiff line change
@@ -1027,13 +1027,26 @@ module.exports = function(ast, extra) {
10271027
return convertedParam;
10281028
});
10291029

1030-
var methodNameIsComputed = (node.name.kind === SyntaxKind.ComputedPropertyName);
1030+
var isMethodNameComputed = (node.name.kind === SyntaxKind.ComputedPropertyName);
1031+
1032+
/**
1033+
* TypeScript class methods can be defined as "abstract"
1034+
*/
1035+
var methodDefinitionType = "MethodDefinition";
1036+
if (node.modifiers && node.modifiers.length) {
1037+
var isAbstractMethod = node.modifiers.some(function(modifier) {
1038+
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
1039+
});
1040+
if (isAbstractMethod) {
1041+
methodDefinitionType = "TSAbstractMethodDefinition";
1042+
}
1043+
}
10311044

10321045
assign(result, {
1033-
type: "MethodDefinition",
1046+
type: methodDefinitionType,
10341047
key: convertChild(node.name),
10351048
value: method,
1036-
computed: methodNameIsComputed,
1049+
computed: isMethodNameComputed,
10371050
static: Boolean(node.flags & ts.NodeFlags.Static),
10381051
kind: "method",
10391052
decorators: (node.decorators) ? node.decorators.map(function(d) {
@@ -1330,15 +1343,32 @@ module.exports = function(ast, extra) {
13301343

13311344
case SyntaxKind.ClassDeclaration:
13321345
case SyntaxKind.ClassExpression:
1346+
13331347
var heritageClauses = node.heritageClauses || [];
13341348
var lastClassToken = heritageClauses.length ? heritageClauses[heritageClauses.length - 1] : node.name;
1335-
/**
1336-
* We need check for modifiers, and use the last one, as there
1337-
* could be multiple before the open brace
1338-
*/
1349+
var classNodeType = SyntaxKind[node.kind];
1350+
13391351
if (node.modifiers && node.modifiers.length) {
1352+
1353+
/**
1354+
* TypeScript class declarations can be defined as "abstract"
1355+
*/
1356+
if (node.kind === SyntaxKind.ClassDeclaration) {
1357+
var isAbstractClass = node.modifiers.some(function(modifier) {
1358+
return modifier.kind === ts.SyntaxKind.AbstractKeyword;
1359+
});
1360+
if (isAbstractClass) {
1361+
classNodeType = "TSAbstract" + classNodeType;
1362+
}
1363+
}
1364+
1365+
/**
1366+
* We need check for modifiers, and use the last one, as there
1367+
* could be multiple before the open brace
1368+
*/
13401369
var lastModifier = node.modifiers[node.modifiers.length - 1];
13411370
lastClassToken = ts.findNextToken(lastModifier, ast);
1371+
13421372
} else if (!lastClassToken) { // no name
13431373
lastClassToken = node.getFirstToken();
13441374
}
@@ -1355,7 +1385,7 @@ module.exports = function(ast, extra) {
13551385
hasImplements = heritageClauses.length > 0;
13561386

13571387
assign(result, {
1358-
type: SyntaxKind[node.kind],
1388+
type: classNodeType,
13591389
id: convertChild(node.name),
13601390
body: {
13611391
type: "ClassBody",

0 commit comments

Comments
 (0)