Skip to content

Commit 61801db

Browse files
committed
find comments in AST
1 parent cd9eb7f commit 61801db

3 files changed

Lines changed: 44 additions & 25 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"eslint-plugin-simple-import-sort": "^12.1.1",
7676
"eslint-plugin-unicorn": "^59.0.1",
7777
"globals": "^16.2.0",
78-
"prettier": "3.6.0",
78+
"prettier": "3.7.1",
7979
"prettier-plugin-jsdoc": "^1.3.2",
8080
"release-plan": "^0.11.0",
8181
"typescript": "^5.8.3",

pnpm-lock.yaml

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parse/index.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,42 @@ function convertNode(
2828
});
2929
}
3030

31+
function findCorrectCommentBlockIndex(
32+
comments: File['comments'],
33+
start: number,
34+
end: number,
35+
): number {
36+
if (!comments) {
37+
return -1;
38+
}
39+
40+
return comments.findIndex((comment) => {
41+
const { start: commentStart, end: commentEnd } = comment;
42+
43+
const check1 = (commentStart === start && commentEnd === end);
44+
const check2 = (commentStart === start + 1 && commentEnd === end - 1);
45+
const check3 = (commentStart === start + 7 && commentEnd === end - 1);
46+
47+
return check1 || check2 || check3;
48+
});
49+
50+
}
51+
3152
/** Traverses the AST and replaces the transformed template parts with other AST */
3253
function convertAst(ast: File, templates: Template[]): void {
3354
traverse(ast, {
3455
enter(path) {
56+
if (templates.length === 0) {
57+
return null;
58+
}
59+
3560
const { node } = path;
3661

3762
switch (node.type) {
3863
case 'BlockStatement':
3964
case 'ObjectExpression':
4065
case 'StaticBlock': {
41-
if (
42-
!node.range &&
43-
typeof node.start === 'number' &&
44-
typeof node.end === 'number'
45-
) {
46-
// prettier 3.6.0 onwards doesn't have `node.range`
47-
// as it was removed in babel
48-
node.range = [node.start, node.end];
49-
}
50-
assert('expected range', node.range);
51-
const [start, end] = node.range;
66+
const [start, end] = [typescript.locStart(node), typescript.locEnd(node)];
5267

5368
const templateIndex = templates.findIndex((template) => {
5469
const { utf16Range } = template;
@@ -76,12 +91,16 @@ function convertAst(ast: File, templates: Template[]): void {
7691
);
7792
}
7893

79-
const index =
80-
node.innerComments?.[0] &&
81-
ast.comments?.indexOf(node.innerComments[0]);
94+
if (ast.comments && ast.comments.length > 0) {
95+
const commentBlockIndex = findCorrectCommentBlockIndex(
96+
ast.comments,
97+
start,
98+
end,
99+
);
82100

83-
if (ast.comments && index !== undefined && index >= 0) {
84-
ast.comments.splice(index, 1);
101+
if (commentBlockIndex !== undefined && commentBlockIndex >= 0) {
102+
ast.comments.splice(commentBlockIndex, 1);
103+
}
85104
}
86105

87106
convertNode(node, rawTemplate);

0 commit comments

Comments
 (0)