Skip to content

Commit e599d98

Browse files
committed
different approach to find pragma comments as previous did not work in all cases
1 parent c9cedd6 commit e599d98

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

scripts/babel/transform-test-gate-pragma.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -332,25 +332,24 @@ function transform(babel) {
332332
}
333333

334334
function getComments(path) {
335+
const allComments = path.hub.file.ast.comments;
335336
if (path.node.leadingComments) {
336337
// Babel AST includes comments.
337338
return path.node.leadingComments;
338339
}
339340
// In Hermes AST we need to find the comments by range.
340-
const comments = path.hub.file.ast.comments;
341-
let prevSibling = path.getPrevSibling();
342-
let searchStart;
343-
if (prevSibling.node) {
344-
searchStart = prevSibling.node.end;
345-
} else if (path.parentPath.node) {
346-
searchStart = path.parentPath.node.start;
347-
} else {
348-
throw new Error('Unexpected AST structure');
341+
const comments = [];
342+
let line = path.node.loc.start.line;
343+
let i = allComments.length - 1;
344+
while (i >= 0 && allComments[i].loc.end.line >= line) {
345+
i--;
349346
}
350-
const filteredComments = comments.filter(
351-
c => c.start >= searchStart && c.end <= path.node.start
352-
);
353-
return filteredComments.length > 0 ? filteredComments : undefined;
347+
while (i >= 0 && allComments[i].loc.end.line === line - 1) {
348+
line = allComments[i].loc.start.line;
349+
comments.unshift(allComments[i]);
350+
i--;
351+
}
352+
return comments;
354353
}
355354

356355
module.exports = transform;

0 commit comments

Comments
 (0)