Skip to content

Commit 2cbbfe5

Browse files
committed
fix: externalize prettier/plugins/estree to prevent bundling stale code
The vite config listed `prettier/plugins/estree.js` as external, but the source imports `prettier/plugins/estree` (no .js extension). This mismatch caused vite to bundle prettier's estree printer into the plugin instead of keeping it external. When the user installs a newer version of prettier (e.g. 3.6.2), the plugin still uses the old bundled estree code whose `canAttachComment` function expects 2 arguments, while prettier 3.6+'s core calls it with 1 argument. This causes a crash: TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) when formatting any .gts file that contains both a comment and a <template> tag. The fix removes the .js extension from the external config so it matches the actual import paths. Also adds a regression test case. Closes #424
1 parent a4bff8f commit 2cbbfe5

6 files changed

Lines changed: 57 additions & 2 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// A comment before template
2+
<template>
3+
<div>hello</div>
4+
</template>

tests/unit-tests/__snapshots__/format.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ const three = <template>{{if true "true"}}</template>;
295295
"
296296
`;
297297
298+
exports[`format > config > default > it formats ../cases/gts/comment-with-template.gts 1`] = `
299+
"// A comment before template
300+
<template>
301+
<div>hello</div>
302+
</template>
303+
"
304+
`;
305+
298306
exports[`format > config > default > it formats ../cases/gts/complex.gts 1`] = `
299307
"import { on } from "@ember/modifier";
300308
import { service } from "@ember/service";
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { readFileSync } from 'node:fs';
2+
import { resolve } from 'node:path';
3+
import { describe, expect, test } from 'vitest';
4+
5+
describe('build externals', () => {
6+
const distPath = resolve(
7+
import.meta.dirname,
8+
'../../dist/prettier-plugin-ember-template-tag.js',
9+
);
10+
11+
test('prettier/plugins/estree is externalized, not bundled', () => {
12+
const dist = readFileSync(distPath, 'utf-8');
13+
14+
// The dist should require estree as an external dependency
15+
expect(dist).toContain('require("prettier/plugins/estree")');
16+
17+
// The dist should NOT contain estree printer internals (canAttachComment is
18+
// a function from the estree printer that would be present if bundled)
19+
expect(dist).not.toMatch(/function\s+\w*canAttachComment/);
20+
});
21+
22+
test('prettier/plugins/babel is externalized, not bundled', () => {
23+
const dist = readFileSync(distPath, 'utf-8');
24+
25+
expect(dist).toContain('require("prettier/plugins/babel")');
26+
});
27+
});

tests/unit-tests/config/__snapshots__/semi-false.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ const three = <template>{{if true "true"}}</template>
295295
"
296296
`;
297297
298+
exports[`config > semi: false > it formats ../cases/gts/comment-with-template.gts 1`] = `
299+
"// A comment before template
300+
<template>
301+
<div>hello</div>
302+
</template>
303+
"
304+
`;
305+
298306
exports[`config > semi: false > it formats ../cases/gts/complex.gts 1`] = `
299307
"import { on } from "@ember/modifier"
300308
import { service } from "@ember/service"

tests/unit-tests/config/__snapshots__/template-export-default.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ const three = <template>{{if true "true"}}</template>;
295295
"
296296
`;
297297
298+
exports[`config > templateExportDefault: true > it formats ../cases/gts/comment-with-template.gts 1`] = `
299+
"// A comment before template
300+
<template>
301+
<div>hello</div>
302+
</template>
303+
"
304+
`;
305+
298306
exports[`config > templateExportDefault: true > it formats ../cases/gts/complex.gts 1`] = `
299307
"import { on } from "@ember/modifier";
300308
import { service } from "@ember/service";

vite.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export default defineConfig({
66
external: [
77
'content-tag',
88
'prettier',
9-
'prettier/plugins/estree.js',
10-
'prettier/plugins/babel.js',
9+
'prettier/plugins/estree',
10+
'prettier/plugins/babel',
1111
],
1212
},
1313
lib: {

0 commit comments

Comments
 (0)