Skip to content
Closed
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: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"publint": "^0.3.18",
"release-plan": "^0.18.0",
"typescript": "^5.9.3",
"vitest": "^3.2.4",
"zimmerframe": "^1.1.4"
"vitest": "^3.2.4"
},
"packageManager": "pnpm@10.18.2"
}
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

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

8 changes: 1 addition & 7 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ export function toTree(source, options = {}) {
? (options.visitors(result.ast) ?? {})
: (options.visitors ?? {});
const hasVisitors = Object.keys(visitors).length > 0;
// Guard against dispatching a handler twice on the same node.
// Visitors that relocate nodes (e.g. moving Glimmer comments into
// `program.comments`) would otherwise fire a second time when the walk
// reaches the new location.
const seen = new WeakSet();
const hasTemplates = parseResults.length > 0;

// Nothing to walk — attach visitor keys and return.
Expand Down Expand Up @@ -253,8 +248,7 @@ export function toTree(source, options = {}) {

const path = { node, parent: parentPath?.node ?? null, parentPath };

if (hasVisitors && !seen.has(node)) {
seen.add(node);
if (hasVisitors) {
const handler = visitors[node.type];
if (handler) handler(node, path);
if ("blockParams" in node && visitors.GlimmerBlockParams) {
Expand Down
6 changes: 3 additions & 3 deletions tests/custom-parser-ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ describe("toTree — custom parser (typescript-eslint) — scope manager", () =>
expect(scopeManager).toBeDefined();

// ESLint's context.getScope() ultimately calls scopeManager.acquire(node).
// If zimmerframe cloned a function/arrow node, the original scope.block
// is still the old node, and acquire(scope.block) may return null or the
// wrong scope. Every scope should round-trip.
// If a function/arrow node got cloned during splicing, the original
// scope.block would still be the old node, and acquire(scope.block) may
// return null or the wrong scope. Every scope should round-trip.
for (const scope of scopeManager.scopes) {
if (!scope.block) continue;
const looked = scopeManager.acquire(scope.block);
Expand Down
119 changes: 59 additions & 60 deletions tests/glimmer-comments.test.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,105 @@
import { describe, expect, it } from "vitest";
import { walk } from "zimmerframe";
import { toTree, print } from "../src/index.js";

describe("Glimmer comment nodes — parse + mutate + print", () => {
it("HTML comments (<!-- -->) appear in GlimmerTemplate.body as GlimmerCommentStatement", () => {
const ast = toTree(`const X = <template><!-- a comment --></template>;`);

const comments = [];
walk(ast, null, {
GlimmerCommentStatement(node) {
comments.push(node);
},
toTree(`const X = <template><!-- a comment --></template>;`, {
visitors: () => ({
GlimmerCommentStatement(node) {
comments.push(node);
},
}),
});

expect(comments.length).toBe(1);
expect(comments[0].value).toBe(" a comment ");
});

it("short mustache comments ({{! }}) appear as GlimmerMustacheCommentStatement", () => {
const ast = toTree(`const X = <template>{{! a comment }}</template>;`);

const comments = [];
walk(ast, null, {
GlimmerMustacheCommentStatement(node) {
comments.push(node);
},
toTree(`const X = <template>{{! a comment }}</template>;`, {
visitors: () => ({
GlimmerMustacheCommentStatement(node) {
comments.push(node);
},
}),
});

expect(comments.length).toBe(1);
expect(comments[0].value).toBe(" a comment ");
});

it("long mustache comments ({{!-- --}}) appear as GlimmerMustacheCommentStatement", () => {
const ast = toTree(`const X = <template>{{!-- a comment --}}</template>;`);

const comments = [];
walk(ast, null, {
GlimmerMustacheCommentStatement(node) {
comments.push(node);
},
toTree(`const X = <template>{{!-- a comment --}}</template>;`, {
visitors: () => ({
GlimmerMustacheCommentStatement(node) {
comments.push(node);
},
}),
});

expect(comments.length).toBe(1);
expect(comments[0].value).toBe(" a comment ");
});

it("mutating an HTML comment value via zimmerframe changes print output", () => {
const ast = toTree(`const X = <template><!-- old content --></template>;`);

it("mutating an HTML comment value via visitors changes print output", () => {
let comment;
walk(ast, null, {
GlimmerCommentStatement(node) {
node.value = " new content ";
comment = node;
},
toTree(`const X = <template><!-- old content --></template>;`, {
visitors: () => ({
GlimmerCommentStatement(node) {
node.value = " new content ";
comment = node;
},
}),
});

expect(print(comment)).toBe("<!-- new content -->");
});

it("mutating a short mustache comment value via zimmerframe changes print output", () => {
const ast = toTree(`const X = <template>{{! old content }}</template>;`);

it("mutating a short mustache comment value via visitors changes print output", () => {
let comment;
walk(ast, null, {
GlimmerMustacheCommentStatement(node) {
node.value = "new content";
comment = node;
},
toTree(`const X = <template>{{! old content }}</template>;`, {
visitors: () => ({
GlimmerMustacheCommentStatement(node) {
node.value = "new content";
comment = node;
},
}),
});

expect(print(comment)).toBe("{{! new content }}");
});

it("mutating a long mustache comment value via zimmerframe changes print output", () => {
const ast = toTree(`const X = <template>{{!-- old content --}}</template>;`);

it("mutating a long mustache comment value via visitors changes print output", () => {
let comment;
walk(ast, null, {
GlimmerMustacheCommentStatement(node) {
node.value = "new content";
comment = node;
},
toTree(`const X = <template>{{!-- old content --}}</template>;`, {
visitors: () => ({
GlimmerMustacheCommentStatement(node) {
node.value = "new content";
comment = node;
},
}),
});

expect(print(comment)).toBe("{{!-- new content --}}");
});

it("mutates all three comment types in one walk", () => {
const ast = toTree(`const X = <template><!-- html -->{{! short }}{{!-- long --}}</template>;`);

const htmlComments = [];
const mustacheComments = [];
walk(ast, null, {
GlimmerCommentStatement(node) {
node.value = " updated html ";
htmlComments.push(node);
},
GlimmerMustacheCommentStatement(node) {
node.value = "updated mustache";
mustacheComments.push(node);
},
toTree(`const X = <template><!-- html -->{{! short }}{{!-- long --}}</template>;`, {
visitors: () => ({
GlimmerCommentStatement(node) {
node.value = " updated html ";
htmlComments.push(node);
},
GlimmerMustacheCommentStatement(node) {
node.value = "updated mustache";
mustacheComments.push(node);
},
}),
});

expect(print(htmlComments[0])).toBe("<!-- updated html -->");
Expand All @@ -111,13 +110,13 @@ describe("Glimmer comment nodes — parse + mutate + print", () => {

it("comment nodes carry correct start/end positions matching source", () => {
const source = `const X = <template><!-- my comment --></template>;`;
const ast = toTree(source);

let comment;
walk(ast, null, {
GlimmerCommentStatement(node) {
comment = node;
},
toTree(source, {
visitors: () => ({
GlimmerCommentStatement(node) {
comment = node;
},
}),
});

expect(source.slice(comment.start, comment.end)).toBe("<!-- my comment -->");
Expand Down
Loading