} }`).runtime).toBe(
runtime,
);
}
});
test("maps literal unions to their runtime primitive types", async () => {
const { runtimeTypeOf } = await import("../src/types.ts");
expect(runtimeTypeOf('"_blank" | "_self"')).toBe("string");
expect(runtimeTypeOf("1 | 2 | 3")).toBe("number");
expect(runtimeTypeOf("true | false")).toBe("boolean");
});
// An apostrophe in prose is not a string. The brace scanner used to treat one
// as an opening quote and swallow every brace until the next apostrophe, so a
// comment like "the Input's slot" broke the whole component with a baffling
// "Unbalanced braces" error pointing at the block's first line.
test("comments may contain apostrophes without unbalancing a block", () => {
const source = `component Demo {
style {
/* The panel's own color -- do not inherit it. */
.demo {
color: red;
}
// A trailing note about the card's border.
.demo-b {
color: blue;
}
}
view {
Docs at https://example.com/a//b are not comments.
}
}
`;
const ast = parse(source);
expect(ast.name).toBe("Demo");
expect(ast.styles.join(" ")).toContain(".demo-b");
// The URL in view text must survive: `//` is only a comment at line start.
expect(JSON.stringify(ast.view)).toContain("https://example.com/a//b");
});
// A regex literal is not a string and not a pair of braces. The brace scanner
// knew about quotes and comments but had no case for regexes, so a quote inside
// one opened a phantom string that swallowed every brace until the next quote,
// and a lone `{` or `}` inside one miscounted depth. Both failed the whole
// component -- with a green build in the reported case, because the damage
// landed in generated output rather than at parse time.
test("regex literals do not unbalance a block", () => {
const mk = (body: string) =>
`page P {
load server {
${body}
return { x };
}
view {
{x}
}
}
`;
// A quote inside a regex used to open a string that ran to the next quote.
expect(parse(mk(` const x = /it's/.test("its");`)).name).toBe("P");
expect(parse(mk(` const x = /"/.test("q");`)).name).toBe("P");
// A brace inside a regex used to be counted as block depth.
expect(parse(mk(` const x = /\{/.test("{");`)).name).toBe("P");
expect(parse(mk(` const x = /}/.test("}");`)).name).toBe("P");
// A brace quantifier is balanced, but must not be counted either.
expect(parse(mk(` const x = /^a{2,3}$/.test("aa");`)).name).toBe("P");
// A `/` inside a character class does not close the regex.
expect(parse(mk(` const x = /[/'"{]/.test("/");`)).name).toBe("P");
// The case that already worked must keep working.
expect(parse(mk(` const x = "a-b".replace(/-/g, " ");`)).name).toBe("P");
});
// Division must not be mistaken for a regex, or the scanner would swallow code
// from the `/` to the next one and lose any braces in between.
test("division is not treated as a regex literal", () => {
const source = `page P {
load server {
const half = 10 / 2;
const ratio = (a + b) / 2;
const each = items[0] / total;
if (half > 1) {
return { half };
}
return { half: 0 };
}
view {