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

Commit 32a46b3

Browse files
JamesHenrynzakas
authored andcommitted
New: Attaches comments to the ESTree AST (fixes #31) (#49)
1 parent f4856f9 commit 32a46b3

18 files changed

+577
-1170
lines changed

lib/ast-converter.js

+28-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var ts = require("typescript"),
2020
//------------------------------------------------------------------------------
2121

2222
var SyntaxKind = ts.SyntaxKind;
23-
// var TokenClass = ts.TokenClass;
2423

2524
var ASSIGNMENT_OPERATORS = [
2625
SyntaxKind.EqualsToken,
@@ -268,6 +267,8 @@ function getTokenType(token) {
268267

269268
case SyntaxKind.GetKeyword:
270269
case SyntaxKind.SetKeyword:
270+
case SyntaxKind.TypeKeyword:
271+
case SyntaxKind.ModuleKeyword:
271272
return "Identifier";
272273

273274
default:
@@ -276,6 +277,10 @@ function getTokenType(token) {
276277
}
277278

278279
if (token.kind >= SyntaxKind.FirstKeyword && token.kind <= SyntaxKind.LastFutureReservedWord) {
280+
if (token.kind === SyntaxKind.FalseKeyword || token.kind === SyntaxKind.TrueKeyword) {
281+
return "Boolean";
282+
}
283+
279284
return "Keyword";
280285
}
281286

@@ -1189,12 +1194,19 @@ module.exports = function(ast, extra) {
11891194
case SyntaxKind.ClassExpression:
11901195
var heritageClauses = node.heritageClauses || [];
11911196
var lastClassToken = heritageClauses.length ? heritageClauses[heritageClauses.length - 1] : node.name;
1192-
if (!lastClassToken) { // no name
1197+
/**
1198+
* We need check for modifiers, and use the last one, as there
1199+
* could be multiple before the open brace
1200+
*/
1201+
if (node.modifiers && node.modifiers.length) {
1202+
var lastModifier = node.modifiers[node.modifiers.length - 1];
1203+
lastClassToken = ts.findNextToken(lastModifier, ast);
1204+
} else if (!lastClassToken) { // no name
11931205
lastClassToken = node.getFirstToken();
11941206
}
11951207

1196-
var openBrace = ts.findNextToken(lastClassToken, ast),
1197-
hasExtends = (heritageClauses.length && node.heritageClauses[0].token === SyntaxKind.ExtendsKeyword),
1208+
var openBrace = ts.findNextToken(lastClassToken, ast);
1209+
var hasExtends = (heritageClauses.length && node.heritageClauses[0].token === SyntaxKind.ExtendsKeyword),
11981210
superClass,
11991211
hasImplements = false;
12001212

@@ -1474,21 +1486,24 @@ module.exports = function(ast, extra) {
14741486
case SyntaxKind.TrueKeyword:
14751487
assign(result, {
14761488
type: "Literal",
1477-
value: true
1489+
value: true,
1490+
raw: "true"
14781491
});
14791492
break;
14801493

14811494
case SyntaxKind.FalseKeyword:
14821495
assign(result, {
14831496
type: "Literal",
1484-
value: false
1497+
value: false,
1498+
raw: "false"
14851499
});
14861500
break;
14871501

14881502
case SyntaxKind.NullKeyword:
14891503
assign(result, {
14901504
type: "Literal",
1491-
value: null
1505+
value: null,
1506+
raw: "null"
14921507
});
14931508
break;
14941509

@@ -1628,16 +1643,19 @@ module.exports = function(ast, extra) {
16281643
return result;
16291644
}
16301645

1631-
1632-
16331646
var estree = convert(ast);
16341647

16351648
if (extra.tokens) {
16361649
estree.tokens = convertTokens(ast);
16371650
}
16381651

1652+
/**
1653+
* Add the comment nodes to the AST (that were parsed separately in parser.js)
1654+
* TODO: Track the progress of https://github.com/eslint/eslint/issues/6724
1655+
* regarding ESLint itself becoming responsible for attributing comment nodes
1656+
*/
16391657
if (extra.comment || extra.attachComment) {
1640-
estree.comments = [];
1658+
estree.comments = extra.comments || [];
16411659
}
16421660

16431661
return estree;

lib/comment-attachment.js

-163
This file was deleted.

0 commit comments

Comments
 (0)