-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
all-comments-parser.js
28 lines (25 loc) · 1001 Bytes
/
all-comments-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Similar to the default parser, but considers leading and trailing comments to be part of the root node.
// Some custom parsers like @typescript-eslint/parser behave in this way.
const espree = require("espree");
exports.parse = function(code, options) {
const ast = espree.parse(code, options);
if (ast.range && ast.comments && ast.comments.length > 0) {
const firstComment = ast.comments[0];
const lastComment = ast.comments[ast.comments.length - 1];
if (ast.range[0] > firstComment.range[0]) {
ast.range[0] = firstComment.range[0];
ast.start = firstComment.start;
if (ast.loc) {
ast.loc.start = firstComment.loc.start;
}
}
if (ast.range[1] < lastComment.range[1]) {
ast.range[1] = lastComment.range[1];
ast.end = lastComment.end;
if (ast.loc) {
ast.loc.end = lastComment.loc.end;
}
}
}
return ast;
};