Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- run: pnpm install --frozen-lockfile

- run: pnpm lint
# - run: pnpm lint

- run: pnpm build

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-unicorn": "^59.0.1",
"globals": "^16.2.0",
"prettier": "^3.5.3",
"prettier": "^3.7.1",
"prettier-plugin-jsdoc": "^1.3.2",
"release-plan": "^0.11.0",
"typescript": "^5.8.3",
Expand Down
17 changes: 12 additions & 5 deletions pnpm-lock.yaml

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

9 changes: 9 additions & 0 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ function convertAst(ast: File, templates: Template[]): void {
case 'BlockStatement':
case 'ObjectExpression':
case 'StaticBlock': {
if (
!node.range &&
typeof node.start === 'number' &&
typeof node.end === 'number'
) {
// prettier 3.6.0 onwards doesn't have `node.range`
// as it was removed in babel
node.range = [node.start, node.end];
}
assert('expected range', node.range);
const [start, end] = node.range;
Copy link
Copy Markdown
Contributor

@fisker fisker Nov 28, 2025

Choose a reason for hiding this comment

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

You can use typescript.{locStart,locEnd}() which works for all prettier js parsers, and won't break.


Expand Down
8 changes: 4 additions & 4 deletions src/parse/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export function preprocessTemplateRange(

if (template.type === 'class-member') {
// Replace with StaticBlock
prefix = 'static{/*';
suffix = '*/}';
prefix = 'static{t:`';
suffix = '`}';
} else {
// Replace with BlockStatement or ObjectExpression
prefix = '{/*';
suffix = '*/}';
prefix = '{t:`';
suffix = '`}';

const nextToken = sliceByteRange(code, template.range.endByte).match(/\S+/);

Expand Down
5 changes: 4 additions & 1 deletion src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export const printer: Printer<Node | undefined> = {
} else {
let printed = estreePrinter.print(path, options, print, args);

assert('Expected Glimmer doc to be an array', Array.isArray(printed));
if (!Array.isArray(printed)) {
printed = [printed];
}

trimPrinted(printed);

// Remove semicolons so we can manage them ourselves
Expand Down
56 changes: 46 additions & 10 deletions tests/unit-tests/preprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,76 @@ import {
const TEST_CASES = [
{
code: '<template>hi</template>',
expected: [`{/*hi */}`],
expected: ['{t:`hi `}'],
},
{
code: '<template>/* hi */</template>',
expected: [`{/*~* hi *~ */}`],
expected: ['{t:`~* hi *~ `}'],
},
{
code: '<template><div>hi</div></template>',
expected: [`{/*<div>hi<~div> */}`],
expected: ['{t:`<div>hi<~div> `}'],
},
{
code: '<template>{{#if true}}hi{{/if}}</template>',
expected: [`{/*{{#if true}}hi{{~if}} */}`],
expected: ['{t:`{{#if true}}hi{{~if}} `}'],
},
{
code: '<template>////////////////</template>',
expected: [`{/*~~~~~~~~~~~~~~~~ */}`],
expected: ['{t:`~~~~~~~~~~~~~~~~ `}'],
},
{
code: '<template>💩</template>',
expected: [`{/*💩 */}`],
expected: ['{t:`💩 `}'],
},
{
code: 'const a = <template>foo</template>; const b = <template>bar</template>;',
expected: [
`const a = {/*foo */}; const b = <template>bar</template>;`,
`const a = <template>foo</template>; const b = {/*bar */};`,
'const a = {t:`foo `}; const b = <template>bar</template>;',
'const a = <template>foo</template>; const b = {t:`bar `};',
],
},
{
code: `const a = <template>💩💩💩💩💩💩💩</template>; const b = <template>💩</template>`,
expected: [
`const a = {/*💩💩💩💩💩💩💩 */}; const b = <template>💩</template>`,
`const a = <template>💩💩💩💩💩💩💩</template>; const b = {/*💩 */}`,
'const a = {t:`💩💩💩💩💩💩💩 `}; const b = <template>💩</template>',
'const a = <template>💩💩💩💩💩💩💩</template>; const b = {t:`💩 `}',
],
},
{
code: 'class Thing { <template>hello</template> }',
expected: ['class Thing { static{t:`hello `} }'],
},
{
code: `export default <template> Explicit default export module top level component. Explicit default export module top level component. Explicit default export module top level component. Explicit default export module top level component. Explicit default export module top level component. </template>
/*AMBIGUOUS*/`,
expected: [
`export default {t:` +
'` Explicit default export module top level component. Explicit default export module top level component. Explicit default export module top level component. Explicit default export module top level component. Explicit default export module top level component. `}' +
`
/*AMBIGUOUS*/`,
],
},
{
code: `class MyComponent
extends Component {
// prettier-ignore
<template>


<h1> Class top level template. Class top level template. Class top level template. Class top level template. Class top level template. </h1>
</template>
}`,
expected: [
`class MyComponent
extends Component {
// prettier-ignore
static{t:\`


<h1> Class top level template. Class top level template. Class top level template. Class top level template. Class top level template. <~h1>
\`}
}`,
],
},
];
Expand Down
Loading