@sigil-dev/compiler 0.9.26 → 0.9.28
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 +1 -1
- package/src/analysis/hydration.ts +1012 -899
- package/src/babel/handlers/dom/derived.ts +2 -0
- package/src/babel/handlers/dom/state.ts +108 -106
- package/src/babel/handlers/ssr/derived.ts +2 -0
- package/src/babel/index.ts +750 -685
- package/src/babel/jsx/anchor-mount.ts +333 -426
- package/src/babel/jsx/element.ts +1034 -1027
- package/src/babel/jsx/ssr.ts +317 -307
- package/src/babel/jsx/utils.ts +356 -355
- package/src/babel/util/compile-state.ts +11 -5
- package/src/babel/util/dead-code.ts +85 -20
- package/src/babel/util/owner-awaits.ts +64 -0
- package/src/babel/util/props.ts +202 -256
- package/src/babel/util/shadowed-globals.ts +31 -0
- package/src/babel/util/state-imports.ts +121 -0
- package/src/babel/util/svg-attrs.ts +86 -0
- package/src/babel/util/warn.ts +36 -0
- package/src/bun-plugin.ts +106 -103
- package/test/bun-plugin.test.ts +3 -1
- package/test/components.test.ts +4 -4
- package/test/context.test.ts +102 -0
- package/test/fixtures/stores/cart.ts +1 -0
- package/test/fixtures/stores/counter.ts +8 -0
- package/test/fixtures/stores/index.ts +2 -0
- package/test/fixtures/stores/macros.d.ts +3 -0
- package/test/fixtures/stores/plain.ts +4 -0
- package/test/hydration.test.ts +313 -277
- package/test/render-parity.test.ts +907 -882
- package/test/shadowed-globals.test.ts +47 -0
- package/test/ssr.test.ts +130 -130
- package/test/state-imports.test.ts +92 -0
- package/test/tree-shaking.test.ts +45 -3
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
|
|
2
|
+
import { transform, transformSSR } from "./helpers/transform.ts";
|
|
3
|
+
|
|
4
|
+
describe("shadowed global components", () => {
|
|
5
|
+
let warn: ReturnType<typeof spyOn>;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
8
|
+
});
|
|
9
|
+
afterEach(() => warn.mockRestore());
|
|
10
|
+
|
|
11
|
+
const shadowWarnings = () =>
|
|
12
|
+
warn.mock.calls.filter((c: unknown[]) =>
|
|
13
|
+
String(c[0]).includes("is a built-in element"),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
test("warns for a locally declared Window", () => {
|
|
17
|
+
transform(`
|
|
18
|
+
function Window() { return <div />; }
|
|
19
|
+
const el = <Window onResize={() => {}} />;
|
|
20
|
+
`);
|
|
21
|
+
expect(shadowWarnings()).toHaveLength(1);
|
|
22
|
+
expect(String(shadowWarnings()[0][0])).toContain("test.tsx:2");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("warns for an imported Body, once per name", () => {
|
|
26
|
+
transformSSR(`
|
|
27
|
+
import { Body } from "./ui";
|
|
28
|
+
const a = <Body />;
|
|
29
|
+
const b = <Body />;
|
|
30
|
+
`);
|
|
31
|
+
expect(shadowWarnings()).toHaveLength(1);
|
|
32
|
+
expect(String(shadowWarnings()[0][0])).toContain("imported");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("no warning for the plain built-in", () => {
|
|
36
|
+
transform(`const el = <Document onKeydown={() => {}} />;`);
|
|
37
|
+
expect(shadowWarnings()).toHaveLength(0);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("no warning for a user Head (only grimoire's is special)", () => {
|
|
41
|
+
transform(`
|
|
42
|
+
function Head() { return <div />; }
|
|
43
|
+
const el = <Head />;
|
|
44
|
+
`);
|
|
45
|
+
expect(shadowWarnings()).toHaveLength(0);
|
|
46
|
+
});
|
|
47
|
+
});
|
package/test/ssr.test.ts
CHANGED
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { transformSSR } from "./helpers/transform.ts";
|
|
3
|
-
|
|
4
|
-
describe("SSR", () => {
|
|
5
|
-
describe("$state", () => {
|
|
6
|
-
test("becomes plain let", () => {
|
|
7
|
-
const result = transformSSR(`let count = $state(0);`);
|
|
8
|
-
expect(result).toContain("let count = 0");
|
|
9
|
-
expect(result).not.toContain("createSignal");
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("mutations stay as mutations", () => {
|
|
13
|
-
const result = transformSSR(`let count = $state(0); count++;`);
|
|
14
|
-
expect(result).toContain("let count = 0");
|
|
15
|
-
expect(result).toContain("count++");
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
describe("$derived", () => {
|
|
20
|
-
test("becomes arrow function", () => {
|
|
21
|
-
const result = transformSSR(
|
|
22
|
-
`let count = $state(0); let doubled = $derived(count * 2);`,
|
|
23
|
-
);
|
|
24
|
-
expect(result).toContain("const doubled = () => count * 2");
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
describe("$effect", () => {
|
|
29
|
-
test("is dropped entirely", () => {
|
|
30
|
-
const result = transformSSR(`$effect(() => { console.log("hi"); });`);
|
|
31
|
-
expect(result).not.toContain("createEffect");
|
|
32
|
-
expect(result).not.toContain("console.log");
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("JSX", () => {
|
|
37
|
-
test("static element", () => {
|
|
38
|
-
const result = transformSSR(`const el = <div />;`);
|
|
39
|
-
expect(result).toContain("<div>");
|
|
40
|
-
expect(result).toContain("</div>");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("static element with class", () => {
|
|
44
|
-
const result = transformSSR(`const el = <div class="app" />;`);
|
|
45
|
-
expect(result).toContain('class="app"');
|
|
46
|
-
expect(result).not.toContain('className="');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("void element self closes", () => {
|
|
50
|
-
const result = transformSSR(`const el = <input type="text" />;`);
|
|
51
|
-
expect(result).toContain("<input");
|
|
52
|
-
expect(result).not.toContain("</input>");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("static text child", () => {
|
|
56
|
-
const result = transformSSR(`const el = <div>hello</div>;`);
|
|
57
|
-
expect(result).toContain("<div>");
|
|
58
|
-
expect(result).toContain("hello");
|
|
59
|
-
expect(result).toContain("</div>");
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("dynamic child is escaped", () => {
|
|
63
|
-
const result = transformSSR(`
|
|
64
|
-
let count = $state(0);
|
|
65
|
-
const el = <div>{count}</div>;
|
|
66
|
-
`);
|
|
67
|
-
expect(result).toContain("__e(");
|
|
68
|
-
expect(result).toContain("</div>");
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("event handlers dropped", () => {
|
|
72
|
-
const result = transformSSR(
|
|
73
|
-
`const el = <button onClick={() => {}}>click</button>;`,
|
|
74
|
-
);
|
|
75
|
-
expect(result).not.toContain("addEventListener");
|
|
76
|
-
expect(result).not.toContain("onClick");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("nested elements", () => {
|
|
80
|
-
const result = transformSSR(`const el = <div><span>hi</span></div>;`);
|
|
81
|
-
expect(result).toContain("<div>");
|
|
82
|
-
expect(result).toContain("<span>");
|
|
83
|
-
expect(result).toContain("hi");
|
|
84
|
-
expect(result).toContain("</span>");
|
|
85
|
-
expect(result).toContain("</div>");
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test("component called as function returning string", () => {
|
|
89
|
-
const result = transformSSR(`const el = <Card title="hi" />;`);
|
|
90
|
-
expect(result).toContain("Card
|
|
91
|
-
expect(result).toContain('title: "hi"');
|
|
92
|
-
expect(result).not.toContain("createElement");
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("component children passed as string", () => {
|
|
96
|
-
const result = transformSSR(`const el = <Card><p>body</p></Card>;`);
|
|
97
|
-
expect(result).toContain("Card
|
|
98
|
-
expect(result).toContain("children:");
|
|
99
|
-
expect(result).toContain("<p>");
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test("runtime import is escape only", () => {
|
|
103
|
-
const result = transformSSR(
|
|
104
|
-
`const name = $state("John"); const el = <div>{name}</div>;`,
|
|
105
|
-
);
|
|
106
|
-
expect(result).toContain("__e(");
|
|
107
|
-
expect(result).not.toContain("createSignal");
|
|
108
|
-
expect(result).not.toContain("createEffect");
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test("innerHTML passes through unsanitized", () => {
|
|
112
|
-
const result = transformSSR(
|
|
113
|
-
`const html = "<b>bold</b>"; const el = <div innerHTML={html}></div>;`,
|
|
114
|
-
);
|
|
115
|
-
expect(result).toContain("html");
|
|
116
|
-
// innerHTML wraps in __h() for SafeHtml, not __e() for escaping
|
|
117
|
-
expect(result).toContain("__h(");
|
|
118
|
-
expect(result).not.toMatch(/__e\([^)]*html/);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test("keyed list converts key to data-key", () => {
|
|
122
|
-
const result = transformSSR(
|
|
123
|
-
`let items = $state([{id: 1, text: "A"}]);
|
|
124
|
-
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;`,
|
|
125
|
-
);
|
|
126
|
-
expect(result).toContain("data-key");
|
|
127
|
-
expect(result).not.toContain('"key"');
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { transformSSR } from "./helpers/transform.ts";
|
|
3
|
+
|
|
4
|
+
describe("SSR", () => {
|
|
5
|
+
describe("$state", () => {
|
|
6
|
+
test("becomes plain let", () => {
|
|
7
|
+
const result = transformSSR(`let count = $state(0);`);
|
|
8
|
+
expect(result).toContain("let count = 0");
|
|
9
|
+
expect(result).not.toContain("createSignal");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("mutations stay as mutations", () => {
|
|
13
|
+
const result = transformSSR(`let count = $state(0); count++;`);
|
|
14
|
+
expect(result).toContain("let count = 0");
|
|
15
|
+
expect(result).toContain("count++");
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("$derived", () => {
|
|
20
|
+
test("becomes arrow function", () => {
|
|
21
|
+
const result = transformSSR(
|
|
22
|
+
`let count = $state(0); let doubled = $derived(count * 2);`,
|
|
23
|
+
);
|
|
24
|
+
expect(result).toContain("const doubled = () => count * 2");
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("$effect", () => {
|
|
29
|
+
test("is dropped entirely", () => {
|
|
30
|
+
const result = transformSSR(`$effect(() => { console.log("hi"); });`);
|
|
31
|
+
expect(result).not.toContain("createEffect");
|
|
32
|
+
expect(result).not.toContain("console.log");
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("JSX", () => {
|
|
37
|
+
test("static element", () => {
|
|
38
|
+
const result = transformSSR(`const el = <div />;`);
|
|
39
|
+
expect(result).toContain("<div>");
|
|
40
|
+
expect(result).toContain("</div>");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("static element with class", () => {
|
|
44
|
+
const result = transformSSR(`const el = <div class="app" />;`);
|
|
45
|
+
expect(result).toContain('class="app"');
|
|
46
|
+
expect(result).not.toContain('className="');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("void element self closes", () => {
|
|
50
|
+
const result = transformSSR(`const el = <input type="text" />;`);
|
|
51
|
+
expect(result).toContain("<input");
|
|
52
|
+
expect(result).not.toContain("</input>");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("static text child", () => {
|
|
56
|
+
const result = transformSSR(`const el = <div>hello</div>;`);
|
|
57
|
+
expect(result).toContain("<div>");
|
|
58
|
+
expect(result).toContain("hello");
|
|
59
|
+
expect(result).toContain("</div>");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("dynamic child is escaped", () => {
|
|
63
|
+
const result = transformSSR(`
|
|
64
|
+
let count = $state(0);
|
|
65
|
+
const el = <div>{count}</div>;
|
|
66
|
+
`);
|
|
67
|
+
expect(result).toContain("__e(");
|
|
68
|
+
expect(result).toContain("</div>");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("event handlers dropped", () => {
|
|
72
|
+
const result = transformSSR(
|
|
73
|
+
`const el = <button onClick={() => {}}>click</button>;`,
|
|
74
|
+
);
|
|
75
|
+
expect(result).not.toContain("addEventListener");
|
|
76
|
+
expect(result).not.toContain("onClick");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("nested elements", () => {
|
|
80
|
+
const result = transformSSR(`const el = <div><span>hi</span></div>;`);
|
|
81
|
+
expect(result).toContain("<div>");
|
|
82
|
+
expect(result).toContain("<span>");
|
|
83
|
+
expect(result).toContain("hi");
|
|
84
|
+
expect(result).toContain("</span>");
|
|
85
|
+
expect(result).toContain("</div>");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("component called as function returning string", () => {
|
|
89
|
+
const result = transformSSR(`const el = <Card title="hi" />;`);
|
|
90
|
+
expect(result).toContain("__component(Card,");
|
|
91
|
+
expect(result).toContain('title: "hi"');
|
|
92
|
+
expect(result).not.toContain("createElement");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("component children passed as string", () => {
|
|
96
|
+
const result = transformSSR(`const el = <Card><p>body</p></Card>;`);
|
|
97
|
+
expect(result).toContain("__component(Card,");
|
|
98
|
+
expect(result).toContain("children:");
|
|
99
|
+
expect(result).toContain("<p>");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("runtime import is escape only", () => {
|
|
103
|
+
const result = transformSSR(
|
|
104
|
+
`const name = $state("John"); const el = <div>{name}</div>;`,
|
|
105
|
+
);
|
|
106
|
+
expect(result).toContain("__e(");
|
|
107
|
+
expect(result).not.toContain("createSignal");
|
|
108
|
+
expect(result).not.toContain("createEffect");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("innerHTML passes through unsanitized", () => {
|
|
112
|
+
const result = transformSSR(
|
|
113
|
+
`const html = "<b>bold</b>"; const el = <div innerHTML={html}></div>;`,
|
|
114
|
+
);
|
|
115
|
+
expect(result).toContain("html");
|
|
116
|
+
// innerHTML wraps in __h() for SafeHtml, not __e() for escaping
|
|
117
|
+
expect(result).toContain("__h(");
|
|
118
|
+
expect(result).not.toMatch(/__e\([^)]*html/);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("keyed list converts key to data-key", () => {
|
|
122
|
+
const result = transformSSR(
|
|
123
|
+
`let items = $state([{id: 1, text: "A"}]);
|
|
124
|
+
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;`,
|
|
125
|
+
);
|
|
126
|
+
expect(result).toContain("data-key");
|
|
127
|
+
expect(result).not.toContain('"key"');
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { transformSync } from "@babel/core";
|
|
4
|
+
import sigilPlugin from "../src/babel/index.ts";
|
|
5
|
+
|
|
6
|
+
const file = join(import.meta.dir, "fixtures/stores/app.tsx");
|
|
7
|
+
const compile = (code: string, mode: "dom" | "ssr" | "hydrate" = "dom") =>
|
|
8
|
+
transformSync(code, {
|
|
9
|
+
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
10
|
+
plugins: [[sigilPlugin, { mode }]],
|
|
11
|
+
filename: file,
|
|
12
|
+
})!.code!;
|
|
13
|
+
|
|
14
|
+
describe("imported state compiles like local state", () => {
|
|
15
|
+
test("named imports of state become calls", () => {
|
|
16
|
+
const out = compile(`import { count, cart } from "./counter";
|
|
17
|
+
import { cart as c } from "./cart";
|
|
18
|
+
const n = count + c.items.length;`);
|
|
19
|
+
expect(out).toContain("count() + c().items.length");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("re-exports are followed (export * and export { a as b } from)", () => {
|
|
23
|
+
const out = compile(`import { count, basket } from "./index";
|
|
24
|
+
const x = count; const y = basket.items;`);
|
|
25
|
+
expect(out).toContain("const x = count()");
|
|
26
|
+
expect(out).toContain("const y = basket().items");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("namespace imports: only state members become calls", () => {
|
|
30
|
+
const out = compile(`import * as s from "./counter";
|
|
31
|
+
const a = s.count; const b = s.bump;`);
|
|
32
|
+
expect(out).toContain("const a = s.count()");
|
|
33
|
+
expect(out).toContain("const b = s.bump;");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("plain imports are left alone", () => {
|
|
37
|
+
const out = compile(`import { config, shout } from "./plain";
|
|
38
|
+
const t = shout(config.title);`);
|
|
39
|
+
expect(out).toContain("shout(config.title)");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("types, shorthand properties and functions", () => {
|
|
43
|
+
const out = compile(`import { count, bump } from "./counter";
|
|
44
|
+
type N = typeof count;
|
|
45
|
+
const o = { count };
|
|
46
|
+
bump();`);
|
|
47
|
+
expect(out).toContain("typeof count;");
|
|
48
|
+
expect(out).toContain("count: count()");
|
|
49
|
+
expect(out).toContain("bump();");
|
|
50
|
+
expect(out).not.toContain("bump()()");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("JSX reading an imported reactive function gets an effect", () => {
|
|
54
|
+
const out = compile(`import { label } from "./counter";
|
|
55
|
+
export const A = () => <p>{label()}</p>;`);
|
|
56
|
+
expect(out).toContain("createEffect");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("ssr: state stays a value, $derived becomes a call", () => {
|
|
60
|
+
const out = compile(
|
|
61
|
+
`import { count, doubledAtModule } from "./counter";
|
|
62
|
+
const a = count; const b = doubledAtModule;`,
|
|
63
|
+
"ssr",
|
|
64
|
+
);
|
|
65
|
+
expect(out).toContain("const a = count;");
|
|
66
|
+
expect(out).toContain("const b = doubledAtModule();");
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("an exporter edited on disk is re-read (long-lived dev server)", async () => {
|
|
71
|
+
const { mkdtemp, writeFile, utimes, rm } = await import("node:fs/promises");
|
|
72
|
+
const { tmpdir } = await import("node:os");
|
|
73
|
+
const dir = await mkdtemp(join(tmpdir(), "sigil-state-imports-"));
|
|
74
|
+
try {
|
|
75
|
+
const store = join(dir, "s.ts");
|
|
76
|
+
const importer = `import { x } from "./s"; const y = x;`;
|
|
77
|
+
const run = () =>
|
|
78
|
+
transformSync(importer, {
|
|
79
|
+
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
80
|
+
plugins: [[sigilPlugin, { mode: "dom" }]],
|
|
81
|
+
filename: join(dir, "app.ts"),
|
|
82
|
+
})!.code!;
|
|
83
|
+
await writeFile(store, "export let x = 1;");
|
|
84
|
+
await utimes(store, 1000, 1000);
|
|
85
|
+
expect(run()).toContain("const y = x;");
|
|
86
|
+
await writeFile(store, "export let x = $store(1);");
|
|
87
|
+
await utimes(store, 2000, 2000);
|
|
88
|
+
expect(run()).toContain("const y = x();");
|
|
89
|
+
} finally {
|
|
90
|
+
await rm(dir, { recursive: true, force: true });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
@@ -9,7 +9,7 @@ describe("Dead code warnings", () => {
|
|
|
9
9
|
const el = <div />;
|
|
10
10
|
`);
|
|
11
11
|
expect(warn).toHaveBeenCalledWith(
|
|
12
|
-
expect.stringContaining('
|
|
12
|
+
expect.stringContaining('test.tsx:2: "unused" ($state) is never read'),
|
|
13
13
|
);
|
|
14
14
|
warn.mockRestore();
|
|
15
15
|
});
|
|
@@ -22,7 +22,7 @@ describe("Dead code warnings", () => {
|
|
|
22
22
|
const el = <div>{count}</div>;
|
|
23
23
|
`);
|
|
24
24
|
expect(warn).toHaveBeenCalledWith(
|
|
25
|
-
expect.stringContaining('
|
|
25
|
+
expect.stringContaining('"doubled" ($derived) is never read'),
|
|
26
26
|
);
|
|
27
27
|
warn.mockRestore();
|
|
28
28
|
});
|
|
@@ -54,7 +54,7 @@ describe("Dead code warnings", () => {
|
|
|
54
54
|
export let user = $store({ name: "" });
|
|
55
55
|
`);
|
|
56
56
|
expect(warn).not.toHaveBeenCalledWith(
|
|
57
|
-
expect.stringContaining("
|
|
57
|
+
expect.stringContaining("never read"),
|
|
58
58
|
);
|
|
59
59
|
warn.mockRestore();
|
|
60
60
|
});
|
|
@@ -75,4 +75,46 @@ describe("Dead code warnings", () => {
|
|
|
75
75
|
expect(code).toContain("count()");
|
|
76
76
|
expect(code).not.toContain("$store");
|
|
77
77
|
});
|
|
78
|
+
|
|
79
|
+
test("warns on state that is only written", () => {
|
|
80
|
+
const warn = spyOn(console, "warn");
|
|
81
|
+
transform(`
|
|
82
|
+
let clicks = $state(0);
|
|
83
|
+
const el = <button onClick={() => clicks++}>go</button>;
|
|
84
|
+
`);
|
|
85
|
+
expect(warn).toHaveBeenCalledWith(
|
|
86
|
+
expect.stringContaining('"clicks" ($state) is written but never read'),
|
|
87
|
+
);
|
|
88
|
+
warn.mockRestore();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("exported $state is read by other modules: no warning", () => {
|
|
92
|
+
const warn = spyOn(console, "warn");
|
|
93
|
+
transform(`
|
|
94
|
+
export let count = $state(0);
|
|
95
|
+
let total = $state(0);
|
|
96
|
+
export { total };
|
|
97
|
+
`);
|
|
98
|
+
expect(warn).not.toHaveBeenCalledWith(expect.stringContaining("never read"));
|
|
99
|
+
warn.mockRestore();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("a file compiled for server, hydrate and SPA warns once", async () => {
|
|
103
|
+
const { transformSync } = await import("@babel/core");
|
|
104
|
+
const sigilPlugin = (await import("../src/babel/index.ts")).default;
|
|
105
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
106
|
+
const code = "let unused = $state(0);\nconst el = <div />;";
|
|
107
|
+
for (const mode of ["ssr", "hydrate", "dom", "ssr", "hydrate", "dom"]) {
|
|
108
|
+
transformSync(code, {
|
|
109
|
+
plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode }]],
|
|
110
|
+
filename: "once.tsx",
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const hits = warn.mock.calls.filter((c) =>
|
|
114
|
+
String(c[0]).includes('"unused"'),
|
|
115
|
+
);
|
|
116
|
+
// once per build; the second round is a new build (or an edit in dev)
|
|
117
|
+
expect(hits).toHaveLength(2);
|
|
118
|
+
warn.mockRestore();
|
|
119
|
+
});
|
|
78
120
|
});
|