@sigil-dev/compiler 0.9.29 → 0.9.30

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.
Files changed (38) hide show
  1. package/index.ts +6 -4
  2. package/package.json +1 -1
  3. package/src/babel/index.ts +763 -750
  4. package/src/babel/jsx/anchor-mount.ts +338 -333
  5. package/src/babel/jsx/children.ts +116 -116
  6. package/src/babel/jsx/element.ts +1040 -1034
  7. package/src/babel/jsx/fragment.ts +271 -271
  8. package/src/babel/jsx/keyed-list.ts +339 -339
  9. package/src/babel/jsx/utils.ts +356 -356
  10. package/src/babel/util/bind.ts +143 -143
  11. package/src/babel/util/css.ts +208 -208
  12. package/src/babel/util/dead-code.ts +94 -93
  13. package/src/babel/util/links.ts +101 -0
  14. package/src/babel/util/reactive-fns.ts +110 -110
  15. package/src/babel/util/runtime-names.ts +88 -0
  16. package/src/babel/util/state-imports.ts +122 -121
  17. package/src/babel/util/static-assertion.ts +51 -51
  18. package/src/babel/util/static-env.ts +84 -0
  19. package/src/vite/index.ts +61 -61
  20. package/test/bun-plugin.test.ts +45 -45
  21. package/test/context.test.ts +110 -102
  22. package/test/fixtures/stores/index.ts +2 -2
  23. package/test/hydration-analysis.test.ts +133 -133
  24. package/test/hydration.test.ts +315 -313
  25. package/test/jsx.test.ts +254 -254
  26. package/test/links.test.ts +57 -0
  27. package/test/reactive-helpers.test.ts +135 -135
  28. package/test/render-parity.test.ts +909 -907
  29. package/test/runtime-names.test.ts +60 -0
  30. package/test/scoped-css.test.ts +166 -166
  31. package/test/ssr-boolean-attrs.test.ts +56 -56
  32. package/test/ssr-raw-text.test.ts +68 -68
  33. package/test/ssr.test.ts +30 -0
  34. package/test/static-assertion.test.ts +123 -123
  35. package/test/static-env.test.ts +85 -0
  36. package/test/static-skip.test.ts +292 -292
  37. package/test/style-object.test.ts +36 -36
  38. package/test/tree-shaking.test.ts +122 -120
@@ -1,120 +1,122 @@
1
- import { describe, expect, spyOn, test } from "bun:test";
2
- import { transform } from "./helpers/transform.ts";
3
-
4
- describe("Dead code warnings", () => {
5
- test("warns on unused $state", () => {
6
- const warn = spyOn(console, "warn");
7
- transform(`
8
- let unused = $state(0);
9
- const el = <div />;
10
- `);
11
- expect(warn).toHaveBeenCalledWith(
12
- expect.stringContaining('test.tsx:2: "unused" ($state) is never read'),
13
- );
14
- warn.mockRestore();
15
- });
16
-
17
- test("warns on unused $derived", () => {
18
- const warn = spyOn(console, "warn");
19
- transform(`
20
- let count = $state(0);
21
- let doubled = $derived(count * 2);
22
- const el = <div>{count}</div>;
23
- `);
24
- expect(warn).toHaveBeenCalledWith(
25
- expect.stringContaining('"doubled" ($derived) is never read'),
26
- );
27
- warn.mockRestore();
28
- });
29
-
30
- test("no warning when $state is used in JSX", () => {
31
- const warn = spyOn(console, "warn");
32
- transform(`
33
- let count = $state(0);
34
- const el = <div>{count}</div>;
35
- `);
36
- expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
37
- warn.mockRestore();
38
- });
39
-
40
- test("no warning when $state is used in effect", () => {
41
- const warn = spyOn(console, "warn");
42
- transform(`
43
- let count = $state(0);
44
- $effect(() => { console.log(count); });
45
- `);
46
- expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
47
- warn.mockRestore();
48
- });
49
-
50
- test("$store does not warn even if not used in same file", () => {
51
- const warn = spyOn(console, "warn");
52
- transform(`
53
- export let count = $store(0);
54
- export let user = $store({ name: "" });
55
- `);
56
- expect(warn).not.toHaveBeenCalledWith(
57
- expect.stringContaining("never read"),
58
- );
59
- warn.mockRestore();
60
- });
61
-
62
- test("$store compiles to createSignal like $state", () => {
63
- const code = transform(`
64
- export let count = $store(0);
65
- `);
66
- expect(code).toContain("createSignal(0)");
67
- expect(code).not.toContain("$store");
68
- });
69
-
70
- test("$store in JSX works like $state", () => {
71
- const code = transform(`
72
- export let count = $store(0);
73
- const el = <div>{count}</div>;
74
- `);
75
- expect(code).toContain("count()");
76
- expect(code).not.toContain("$store");
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
- });
120
- });
1
+ import { describe, expect, spyOn, test } from "bun:test";
2
+ import { transform } from "./helpers/transform.ts";
3
+
4
+ describe("Dead code warnings", () => {
5
+ test("warns on unused $state", () => {
6
+ const warn = spyOn(console, "warn");
7
+ transform(`
8
+ let unused = $state(0);
9
+ const el = <div />;
10
+ `);
11
+ expect(warn).toHaveBeenCalledWith(
12
+ expect.stringContaining('test.tsx:2: "unused" ($state) is never read'),
13
+ );
14
+ warn.mockRestore();
15
+ });
16
+
17
+ test("warns on unused $derived", () => {
18
+ const warn = spyOn(console, "warn");
19
+ transform(`
20
+ let count = $state(0);
21
+ let doubled = $derived(count * 2);
22
+ const el = <div>{count}</div>;
23
+ `);
24
+ expect(warn).toHaveBeenCalledWith(
25
+ expect.stringContaining('"doubled" ($derived) is never read'),
26
+ );
27
+ warn.mockRestore();
28
+ });
29
+
30
+ test("no warning when $state is used in JSX", () => {
31
+ const warn = spyOn(console, "warn");
32
+ transform(`
33
+ let count = $state(0);
34
+ const el = <div>{count}</div>;
35
+ `);
36
+ expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
37
+ warn.mockRestore();
38
+ });
39
+
40
+ test("no warning when $state is used in effect", () => {
41
+ const warn = spyOn(console, "warn");
42
+ transform(`
43
+ let count = $state(0);
44
+ $effect(() => { console.log(count); });
45
+ `);
46
+ expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
47
+ warn.mockRestore();
48
+ });
49
+
50
+ test("$store does not warn even if not used in same file", () => {
51
+ const warn = spyOn(console, "warn");
52
+ transform(`
53
+ export let count = $store(0);
54
+ export let user = $store({ name: "" });
55
+ `);
56
+ expect(warn).not.toHaveBeenCalledWith(
57
+ expect.stringContaining("never read"),
58
+ );
59
+ warn.mockRestore();
60
+ });
61
+
62
+ test("$store compiles to createSignal like $state", () => {
63
+ const code = transform(`
64
+ export let count = $store(0);
65
+ `);
66
+ expect(code).toContain("createSignal(0)");
67
+ expect(code).not.toContain("$store");
68
+ });
69
+
70
+ test("$store in JSX works like $state", () => {
71
+ const code = transform(`
72
+ export let count = $store(0);
73
+ const el = <div>{count}</div>;
74
+ `);
75
+ expect(code).toContain("count()");
76
+ expect(code).not.toContain("$store");
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(
99
+ expect.stringContaining("never read"),
100
+ );
101
+ warn.mockRestore();
102
+ });
103
+
104
+ test("a file compiled for server, hydrate and SPA warns once", async () => {
105
+ const { transformSync } = await import("@babel/core");
106
+ const sigilPlugin = (await import("../src/babel/index.ts")).default;
107
+ const warn = spyOn(console, "warn").mockImplementation(() => {});
108
+ const code = "let unused = $state(0);\nconst el = <div />;";
109
+ for (const mode of ["ssr", "hydrate", "dom", "ssr", "hydrate", "dom"]) {
110
+ transformSync(code, {
111
+ plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode }]],
112
+ filename: "once.tsx",
113
+ });
114
+ }
115
+ const hits = warn.mock.calls.filter((c) =>
116
+ String(c[0]).includes('"unused"'),
117
+ );
118
+ // once per build; the second round is a new build (or an edit in dev)
119
+ expect(hits).toHaveLength(2);
120
+ warn.mockRestore();
121
+ });
122
+ });