always attach trailingComments to parent node if possible

this is required to correctly calculate expression's ending index correctly
with expr.trailingComments.at(-1).end
in `compiler/phases/1-parse/read/expression.js`
pull/11866/head
Yoonchae Lee 2 years ago
parent e94f1ae236
commit ee098cced4

@ -92,7 +92,7 @@ function get_comment_handlers(source) {
if (comments.length === 0) return;
walk(ast, null, {
_(node, { next }) {
_(node, { next, path }) {
let comment;
while (comments[0] && comments[0].start < node.start) {
@ -103,16 +103,19 @@ function get_comment_handlers(source) {
next();
if (comments[0]) {
const slice = source.slice(node.end, comments[0].start);
const parent = path.at(-1);
if (parent === undefined || node.end !== parent.end) {
const slice = source.slice(node.end, comments[0].start);
if (/^[,) \t]*$/.test(slice)) {
node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
if (/^[,) \t]*$/.test(slice)) {
node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
}
}
}
}
});
if (comments.length > 0) {
(ast.trailingComments ||= []).push(...comments.splice(0))
(ast.trailingComments ||= []).push(...comments.splice(0));
}
}
};

Loading…
Cancel
Save