Skip to content

Commit f1a5193

Browse files
Merge pull request #408 from ember-tooling/upgrade-prettier
Fix problems with prettier v3.6.0+
2 parents 0ceae89 + 854621c commit f1a5193

File tree

8 files changed

+145
-361
lines changed

8 files changed

+145
-361
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# compiled output
22
/dist/
3-
3+
44
# misc
55
!.*
66
.*/
@@ -13,3 +13,4 @@ pnpm-lock.yaml
1313
CHANGELOG.md
1414
README.md
1515
RELEASE.md
16+
CONTRIBUTING.md

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "node ./bin/test.mjs"
99
},
1010
"dependencies": {
11-
"prettier": "^3.5.3",
11+
"prettier": "^3.7.3",
1212
"prettier-plugin-ember-template-tag": "workspace:^"
1313
},
1414
"devDependencies": {

examples/pnpm-lock.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
"eslint-plugin-simple-import-sort": "^12.1.1",
7676
"eslint-plugin-unicorn": "^59.0.1",
7777
"globals": "^16.2.0",
78-
"prettier": "^3.5.3",
79-
"prettier-plugin-jsdoc": "^1.3.2",
78+
"prettier": "^3.7.3",
79+
"prettier-plugin-jsdoc": "^1.7.0",
8080
"release-plan": "^0.11.0",
8181
"typescript": "^5.8.3",
8282
"typescript-eslint": "^8.34.0",

pnpm-lock.yaml

Lines changed: 90 additions & 13 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: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,44 @@ 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+
return (
44+
(commentStart === start && commentEnd === end) ||
45+
(commentStart === start + 1 && commentEnd === end - 1) ||
46+
(commentStart === start + 7 && commentEnd === end - 1)
47+
);
48+
});
49+
}
50+
3151
/** Traverses the AST and replaces the transformed template parts with other AST */
3252
function convertAst(ast: File, templates: Template[]): void {
3353
traverse(ast, {
3454
enter(path) {
55+
if (templates.length === 0) {
56+
return null;
57+
}
58+
3559
const { node } = path;
3660

3761
switch (node.type) {
3862
case 'BlockStatement':
3963
case 'ObjectExpression':
4064
case 'StaticBlock': {
41-
assert('expected range', node.range);
42-
const [start, end] = node.range;
65+
const [start, end] = [
66+
typescript.locStart(node),
67+
typescript.locEnd(node),
68+
];
4369

4470
const templateIndex = templates.findIndex((template) => {
4571
const { utf16Range } = template;
@@ -67,12 +93,16 @@ function convertAst(ast: File, templates: Template[]): void {
6793
);
6894
}
6995

70-
const index =
71-
node.innerComments?.[0] &&
72-
ast.comments?.indexOf(node.innerComments[0]);
96+
if (ast.comments && ast.comments.length > 0) {
97+
const commentBlockIndex = findCorrectCommentBlockIndex(
98+
ast.comments,
99+
start,
100+
end,
101+
);
73102

74-
if (ast.comments && index !== undefined && index >= 0) {
75-
ast.comments.splice(index, 1);
103+
if (commentBlockIndex !== undefined && commentBlockIndex >= 0) {
104+
ast.comments.splice(commentBlockIndex, 1);
105+
}
76106
}
77107

78108
convertNode(node, rawTemplate);

src/print/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export const printer: Printer<Node | undefined> = {
3232
return estreePrinter.getVisitorKeys?.(node, nonTraversableKeys) || [];
3333
},
3434

35+
printPrettierIgnored(path: AstPath<Node | undefined>, options: Options) {
36+
return printRawText(path, options);
37+
},
38+
3539
print(
3640
path: AstPath<Node | undefined>,
3741
options: Options,
@@ -154,7 +158,15 @@ function printRawText(
154158
}
155159

156160
function hasPrettierIgnore(path: AstPath<Node | undefined>): boolean {
157-
return path.node?.leadingComments?.at(-1)?.value.trim() === 'prettier-ignore';
161+
let possibleComment = path.node?.leadingComments?.at(-1)?.value.trim();
162+
163+
// @ts-expect-error Comments exist on node sometimes
164+
if (!path.node?.leadingComments && path.node?.comments) {
165+
// @ts-expect-error Comments exist on node sometimes
166+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
167+
possibleComment = path.node.comments?.at(-1)?.value.trim();
168+
}
169+
return possibleComment === 'prettier-ignore';
158170
}
159171

160172
function checkPrettierIgnore(path: AstPath<Node | undefined>): boolean {

0 commit comments

Comments
 (0)