Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# compiled output
/dist/

# misc
!.*
.*/
Expand All @@ -13,3 +13,4 @@ pnpm-lock.yaml
CHANGELOG.md
README.md
RELEASE.md
CONTRIBUTING.md
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "node ./bin/test.mjs"
},
"dependencies": {
"prettier": "^3.5.3",
"prettier": "^3.7.3",
"prettier-plugin-ember-template-tag": "workspace:^"
},
"devDependencies": {
Expand Down
10 changes: 0 additions & 10 deletions examples/pnpm-lock.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-unicorn": "^59.0.1",
"globals": "^16.2.0",
"prettier": "^3.5.3",
"prettier-plugin-jsdoc": "^1.3.2",
"prettier": "^3.7.3",
"prettier-plugin-jsdoc": "^1.7.0",
"release-plan": "^0.11.0",
"typescript": "^5.8.3",
"typescript-eslint": "^8.34.0",
Expand Down
103 changes: 90 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 37 additions & 7 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,44 @@ function convertNode(
});
}

function findCorrectCommentBlockIndex(
comments: File['comments'],
start: number,
end: number,
): number {
if (!comments) {
return -1;
}

return comments.findIndex((comment) => {
const { start: commentStart, end: commentEnd } = comment;

return (
(commentStart === start && commentEnd === end) ||
(commentStart === start + 1 && commentEnd === end - 1) ||
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do 1 and 7 represent?

Copy link
Copy Markdown
Member Author

@evoactivity evoactivity Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The offsets from the original template don't always match the offsets of the AST comments due to the prefix and suffix.

start + 1 = {
start + 7 = static{
end - 1 = }

eg template start is 14 in a class component. AST sees /*the template */ so starts at 21. So bump the template start to 21 to account for static{ before the comment begins and we can match.

(commentStart === start + 7 && commentEnd === end - 1)
);
});
}

/** Traverses the AST and replaces the transformed template parts with other AST */
function convertAst(ast: File, templates: Template[]): void {
traverse(ast, {
enter(path) {
if (templates.length === 0) {
return null;
}

const { node } = path;

switch (node.type) {
case 'BlockStatement':
case 'ObjectExpression':
case 'StaticBlock': {
assert('expected range', node.range);
const [start, end] = node.range;
const [start, end] = [
typescript.locStart(node),
typescript.locEnd(node),
];

const templateIndex = templates.findIndex((template) => {
const { utf16Range } = template;
Expand Down Expand Up @@ -67,12 +93,16 @@ function convertAst(ast: File, templates: Template[]): void {
);
}

const index =
node.innerComments?.[0] &&
ast.comments?.indexOf(node.innerComments[0]);
if (ast.comments && ast.comments.length > 0) {
const commentBlockIndex = findCorrectCommentBlockIndex(
ast.comments,
start,
end,
);

if (ast.comments && index !== undefined && index >= 0) {
ast.comments.splice(index, 1);
if (commentBlockIndex !== undefined && commentBlockIndex >= 0) {
ast.comments.splice(commentBlockIndex, 1);
}
}

convertNode(node, rawTemplate);
Expand Down
14 changes: 13 additions & 1 deletion src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const printer: Printer<Node | undefined> = {
return estreePrinter.getVisitorKeys?.(node, nonTraversableKeys) || [];
},

printPrettierIgnored(path: AstPath<Node | undefined>, options: Options) {
return printRawText(path, options);
},

print(
path: AstPath<Node | undefined>,
options: Options,
Expand Down Expand Up @@ -154,7 +158,15 @@ function printRawText(
}

function hasPrettierIgnore(path: AstPath<Node | undefined>): boolean {
return path.node?.leadingComments?.at(-1)?.value.trim() === 'prettier-ignore';
let possibleComment = path.node?.leadingComments?.at(-1)?.value.trim();

// @ts-expect-error Comments exist on node sometimes
if (!path.node?.leadingComments && path.node?.comments) {
// @ts-expect-error Comments exist on node sometimes
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
possibleComment = path.node.comments?.at(-1)?.value.trim();
}
return possibleComment === 'prettier-ignore';
}

function checkPrettierIgnore(path: AstPath<Node | undefined>): boolean {
Expand Down
Loading