Skip to content

Commit 7c885ae

Browse files
committed
fix(CodecrumbsIO#3): support consecutive codecrumbs
removed babelTraverse and instead used ast.comments returned by babylon.parse()
1 parent 36284e5 commit 7c885ae

1 file changed

Lines changed: 17 additions & 35 deletions

File tree

  • src/server/code-parse/codecrumbs

‎src/server/code-parse/codecrumbs/index.js‎

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const babylon = require('@babel/parser');
2-
const babelTraverse = require('@babel/traverse');
32

43
const { config: astParseConfig, getNodeLines } = require('../../../shared/astParse');
54
const NO_TRAIL_FLOW = require('../../../shared/constants').NO_TRAIL_FLOW;
@@ -53,41 +52,24 @@ const getCrumbs = (fileCode, path) => {
5352

5453
try {
5554
ast = babylon.parse(fileCode, astParseConfig);
56-
57-
// TODO: ast has comments array, maybe use it
58-
babelTraverse.default(ast, {
59-
enter(path) {
60-
const node = path.node;
61-
if (!node || !(node.leadingComments || node.trailingComments)) return;
62-
63-
const leadingComment = node.leadingComments
64-
? node.leadingComments[node.leadingComments.length - 1]
65-
: null;
66-
67-
let trailingComment = node.trailingComments ? node.trailingComments[0] : null;
68-
69-
if (trailingComment && trailingComment.loc.start.line !== node.loc.start.line) {
70-
trailingComment = null;
55+
if (ast.comments) {
56+
ast.comments.forEach(comment => {
57+
if (comment && isCodecrumb(comment)) {
58+
const params = parseCodecrumbComment(comment);
59+
60+
const loc = comment.loc.start;
61+
const crumbNodeLines = getNodeLines(comment);
62+
crumbsList.push({
63+
name: params.name || '', //TODO: check, can be bug with layout calc
64+
displayLoc: `#${loc.line}`,
65+
crumbNodeLines: params.linesRange
66+
? [crumbNodeLines[0], crumbNodeLines[1] + params.linesRange]
67+
: crumbNodeLines,
68+
params
69+
});
7170
}
72-
73-
[leadingComment, trailingComment].forEach(comment => {
74-
if (comment && isCodecrumb(comment)) {
75-
const params = parseCodecrumbComment(comment);
76-
77-
const loc = comment.loc.start;
78-
const crumbNodeLines = getNodeLines(comment);
79-
crumbsList.push({
80-
name: params.name || '', //TODO: check, can be bug with layout calc
81-
displayLoc: `#${loc.line}`,
82-
crumbNodeLines: params.linesRange
83-
? [crumbNodeLines[0], crumbNodeLines[1] + params.linesRange]
84-
: crumbNodeLines,
85-
params
86-
});
87-
}
88-
});
89-
}
90-
});
71+
});
72+
}
9173

9274
return crumbsList;
9375
} catch (e) {

0 commit comments

Comments
 (0)