@sigil-dev/compiler 0.10.1 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@sigil-dev/compiler",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "0.10.1",
5
+ "version": "0.10.2",
6
6
  "private": false,
7
7
  "description": "Compiles Sigil's JSX and $state/$derived macros into direct DOM updates",
8
8
  "license": "MIT",
@@ -166,7 +166,7 @@ export function buildAnchorMount(
166
166
  t.binaryExpression(
167
167
  "instanceof",
168
168
  t.identifier("_item"),
169
- t.identifier("Node"),
169
+ t.memberExpression(t.identifier("globalThis"), t.identifier("Node")),
170
170
  ),
171
171
  t.identifier("_item"),
172
172
  t.callExpression(
@@ -202,7 +202,7 @@ export function buildAnchorMount(
202
202
  t.binaryExpression(
203
203
  "instanceof",
204
204
  t.identifier("_v"),
205
- t.identifier("Node"),
205
+ t.memberExpression(t.identifier("globalThis"), t.identifier("Node")),
206
206
  ),
207
207
  t.blockStatement([
208
208
  keepNodes("_v"),
@@ -62,7 +62,8 @@ export function buildTextNode(
62
62
  ]),
63
63
  );
64
64
 
65
- // if (!(_t instanceof Text)) { const _nt = createTextNode(""); if (_anchor) _anchor.after(_nt); _t = _nt; }
65
+ // if (!(_t instanceof globalThis.Text)) { const _nt = createTextNode(""); if (_anchor) _anchor.after(_nt); _t = _nt; }
66
+ // globalThis.Text, not Text: the user's module may have its own `Text` (a component) shadowing the DOM's
66
67
  const newTextVar = genId();
67
68
  statements.push(
68
69
  t.ifStatement(
@@ -72,7 +73,7 @@ export function buildTextNode(
72
73
  t.binaryExpression(
73
74
  "instanceof",
74
75
  t.identifier(textVar),
75
- t.identifier("Text"),
76
+ t.memberExpression(t.identifier("globalThis"), t.identifier("Text")),
76
77
  ),
77
78
  ),
78
79
  ),
@@ -71,7 +71,7 @@ describe("Hydration", () => {
71
71
  // then gets nextSibling; creates text node if it's not a Text (SPA navigation)
72
72
  expect(code).toContain("claimComment(");
73
73
  expect(code).toContain("createEffect");
74
- expect(code).toContain("instanceof Text");
74
+ expect(code).toContain("instanceof globalThis.Text");
75
75
  });
76
76
 
77
77
  test("attributes still work in hydrate mode", () => {
@@ -907,38 +907,49 @@ test("compiler and runtime agree on the SVG attribute names", async () => {
907
907
  const { SVG_ATTR } = await import("../src/babel/util/svg-attrs.ts");
908
908
  expect(runtime.SVG_ATTR).toEqual(SVG_ATTR);
909
909
  });
910
-
911
- describe("textarea content", () => {
912
- // a textarea's content is raw text: markers would be typed into the box
913
- const valueOf = (host: HTMLElement) => (host.querySelector("textarea") as HTMLTextAreaElement).value;
914
-
915
- test("{expr} children become its value, with no markers anywhere", () => {
916
- const src = `const body = "a <b> & c"; export const App = () => <form><textarea name="b">{body}</textarea></form>;`;
917
- const r = render(src);
918
- const ssr = String(compile(src, "ssr")());
919
- expect(ssr).toBe('<form><textarea name="b">a &lt;b&gt; &amp; c</textarea></form>');
920
- expect(valueOf(r.hydrated)).toBe("a <b> & c");
921
- expect(valueOf(r.spa)).toBe("a <b> & c");
922
- });
923
-
924
- test("text and expressions join; null, undefined and booleans are empty", () => {
925
- const src = `const n = null; const u = undefined; const f = false; const x = 3; export const App = () => (
926
- <textarea>
927
- count: {x}{n}{u}{f}
928
- </textarea>
929
- );`;
930
- expect(String(compile(src, "ssr")())).toBe("<textarea>count: 3</textarea>");
931
- expect(valueOf(render(src).spa)).toBe("count: 3");
932
- });
933
-
934
- test("an explicit value wins over children", () => {
935
- const src = `export const App = () => <textarea value={"v"}>{"ignored"}</textarea>;`;
936
- expect(String(compile(src, "ssr")())).toBe("<textarea>v</textarea>");
937
- });
938
-
939
- test("a textarea nested in a condition is handled too", () => {
940
- const src = `const on = true; const t = "hi"; export const App = () => <div>{on ? <textarea>{t}</textarea> : null}</div>;`;
941
- expect(noMarkers(String(compile(src, "ssr")()))).toBe("<div><textarea>hi</textarea></div>");
942
- expect(String(compile(src, "ssr")())).toContain("<textarea>hi</textarea>");
943
- });
944
- });
910
+
911
+ describe("textarea content", () => {
912
+ // a textarea's content is raw text: markers would be typed into the box
913
+ const valueOf = (host: HTMLElement) => (host.querySelector("textarea") as HTMLTextAreaElement).value;
914
+
915
+ test("{expr} children become its value, with no markers anywhere", () => {
916
+ const src = `const body = "a <b> & c"; export const App = () => <form><textarea name="b">{body}</textarea></form>;`;
917
+ const r = render(src);
918
+ const ssr = String(compile(src, "ssr")());
919
+ expect(ssr).toBe('<form><textarea name="b">a &lt;b&gt; &amp; c</textarea></form>');
920
+ expect(valueOf(r.hydrated)).toBe("a <b> & c");
921
+ expect(valueOf(r.spa)).toBe("a <b> & c");
922
+ });
923
+
924
+ test("text and expressions join; null, undefined and booleans are empty", () => {
925
+ const src = `const n = null; const u = undefined; const f = false; const x = 3; export const App = () => (
926
+ <textarea>
927
+ count: {x}{n}{u}{f}
928
+ </textarea>
929
+ );`;
930
+ expect(String(compile(src, "ssr")())).toBe("<textarea>count: 3</textarea>");
931
+ expect(valueOf(render(src).spa)).toBe("count: 3");
932
+ });
933
+
934
+ test("an explicit value wins over children", () => {
935
+ const src = `export const App = () => <textarea value={"v"}>{"ignored"}</textarea>;`;
936
+ expect(String(compile(src, "ssr")())).toBe("<textarea>v</textarea>");
937
+ });
938
+
939
+ test("a textarea nested in a condition is handled too", () => {
940
+ const src = `const on = true; const t = "hi"; export const App = () => <div>{on ? <textarea>{t}</textarea> : null}</div>;`;
941
+ expect(noMarkers(String(compile(src, "ssr")()))).toBe("<div><textarea>hi</textarea></div>");
942
+ expect(String(compile(src, "ssr")())).toContain("<textarea>hi</textarea>");
943
+ });
944
+ });
945
+
946
+ describe("names the compiled code uses itself", () => {
947
+ // hydration checks `instanceof Text` / `instanceof Node`: a component with
948
+ // that name must not shadow the DOM's (it threw "non-object prototype")
949
+ test("components called Text and Node", () => {
950
+ expectAll(
951
+ `let name = $state("a"); let items = $state(["x"]); const Text = ({ l }) => <i>{l}</i>; const Node = () => <u />; export const App = () => <pre><span>{name}</span><b>{items.map((i) => <em>{i}</em>)}</b><Text l="t" /><Node /></pre>;`,
952
+ "<pre><span>a</span><b><em>x</em></b><i>t</i><u></u></pre>",
953
+ );
954
+ });
955
+ });