๐Ÿ“ฆ oven-sh / bun

๐Ÿ“„ bundler_minify_symbol_for.test.ts ยท 341 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341import { describe, expect } from "bun:test";
import { itBundled } from "./expectBundled";

describe("bundler", () => {
  describe("minify/Symbol.for", () => {
    // Test basic Symbol.for removal when unused
    itBundled("minify/SymbolForUnused", {
      files: {
        "/entry.js": /* js */ `
          // These should be removed when minifySyntax is true
          Symbol.for("test1");
          Symbol.for("test2");
          Symbol.for(\`test3\`);
          Symbol.for("test" + 4); // This has a side effect (string concatenation)
          
          // Keep reference to prove concatenation happened
          var sideEffect = "test" + 4;
          Symbol.for(sideEffect);
          
          // These should NOT be removed (used values)
          const s1 = Symbol.for("used1");
          let s2 = Symbol.for("used2");
          var s3 = Symbol.for("used3");
          
          // Function argument - should not be removed
          console.log(Symbol.for("argument"));
          
          // Property access - should not be removed
          const obj = { prop: Symbol.for("property") };
          
          // Return value - should not be removed
          function getSymbol() {
            return Symbol.for("return");
          }
          
          capture(s1, s2, s3, obj.prop, getSymbol(), sideEffect);
        `,
      },
      minifySyntax: true,
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // Should remove unused Symbol.for calls
        expect(output).not.toContain('Symbol.for("test1")');
        expect(output).not.toContain('Symbol.for("test2")');
        expect(output).not.toContain("Symbol.for(`test3`)");

        // Should keep the concatenation because sideEffect variable is used
        expect(output).toContain("test4");

        // Should keep used Symbol.for calls
        expect(output).toContain('Symbol.for("used1")');
        expect(output).toContain('Symbol.for("used2")');
        expect(output).toContain('Symbol.for("used3")');
        expect(output).toContain('Symbol.for("argument")');
        expect(output).toContain('Symbol.for("property")');
        expect(output).toContain('Symbol.for("return")');
      },
    });

    // Test that Symbol.for is not removed when minifySyntax is false
    itBundled("minify/SymbolForNoMinifySyntax", {
      files: {
        "/entry.js": /* js */ `
          Symbol.for("test1");
          Symbol.for("test2");
          const s = Symbol.for("test3");
          capture(s);
        `,
      },
      minifySyntax: false,
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // Should keep all Symbol.for calls when minifySyntax is false
        expect(output).toContain('Symbol.for("test1")');
        expect(output).toContain('Symbol.for("test2")');
        expect(output).toContain('Symbol.for("test3")');
      },
    });

    // Test interaction with other minification options
    itBundled("minify/SymbolForWithWhitespace", {
      files: {
        "/entry.js": /* js */ `
          // Unused calls should be removed
          Symbol.for("remove-me-1");
          Symbol.for("remove-me-2");
          
          // Used call should remain
          const sym = Symbol.for("keep-me");
          
          // Test with complex expressions
          const ab = "a" + "b";
          Symbol.for(ab); // Keep the variable to ensure concatenation happens
          Symbol.for(\`template\`);
          
          capture(sym, ab);
        `,
      },
      minifySyntax: true,
      minifyWhitespace: true,
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // Should remove unused calls
        expect(output).not.toContain("remove-me-1");
        expect(output).not.toContain("remove-me-2");

        // Should keep used call
        expect(output).toContain('Symbol.for("keep-me")');

        // Should keep side effect (the concatenation is kept because ab is used)
        expect(output).toContain("ab");
      },
    });

    // Test edge cases
    itBundled("minify/SymbolForEdgeCases", {
      files: {
        "/entry.js": /* js */ `
          // Optional chaining - these are preserved because optional chaining has observable behavior
          Symbol?.for("optional1");
          Symbol?.for?.("optional2");
          
          // In conditional - these should be optimized based on the condition
          true && Symbol.for("conditional1");
          false || Symbol.for("conditional2");
          
          // In ternary - these should be optimized based on the condition
          true ? Symbol.for("ternary1") : null;
          false ? null : Symbol.for("ternary2");
          
          // Nested calls
          Symbol.for(Symbol.for("nested"));
          
          // With spread
          const arr = [...[Symbol.for("spread")]];
          
          // Property key
          const obj = {
            [Symbol.for("key")]: "value"
          };
          
          capture(arr, obj);
        `,
      },
      minifySyntax: true,
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // Optional chaining preserves the call because it has observable behavior (checking if Symbol exists)
        expect(output).toContain("optional1");
        expect(output).toContain("optional2");

        // All the conditional/ternary expressions were optimized away completely
        // because they evaluate to unused Symbol.for calls
        expect(output).not.toContain("conditional1");
        expect(output).not.toContain("conditional2");
        expect(output).not.toContain("ternary1");
        expect(output).not.toContain("ternary2");

        // Nested call was also optimized away
        expect(output).not.toContain("nested");

        // Used in spread - should keep
        expect(output).toContain('Symbol.for("spread")');

        // Used as property key - should keep
        expect(output).toContain('Symbol.for("key")');
      },
    });

    // Test that Symbol.keyFor is not affected (it's still in the property access list)
    itBundled("minify/SymbolKeyForNotAffected", {
      files: {
        "/entry.js": /* js */ `
          // Symbol.keyFor should still be removed as a property access
          Symbol.keyFor;
          
          // But not when called
          const sym = Symbol.for("test");
          const key = Symbol.keyFor(sym);
          
          capture(key);
        `,
      },
      minifySyntax: true,
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // The unused property access "Symbol.keyFor;" should be removed
        // But the function call "Symbol.keyFor(sym)" should remain
        // So we should find exactly one occurrence of "Symbol.keyFor"
        const keyForMatches = output.match(/Symbol\.keyFor/g) || [];
        expect(keyForMatches.length).toBe(1);

        // Function call should remain
        expect(output).toContain("Symbol.keyFor(");
      },
    });

    // Test interaction with production mode
    itBundled("minify/SymbolForProduction", {
      files: {
        "/entry.js": /* js */ `
          // Unused
          Symbol.for("remove-in-prod");
          
          // Used
          const s = Symbol.for("keep-in-prod");
          
          // Side effects
          Symbol.for(someGlobal);
          
          capture(s);
        `,
      },
      production: true, // This enables minifySyntax
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // Should remove unused
        expect(output).not.toContain("remove-in-prod");

        // Should keep used
        expect(output).toContain("keep-in-prod");

        // Should keep side effects
        expect(output).toContain("someGlobal");
      },
    });

    // Test with bundling disabled (transform mode)
    itBundled("minify/SymbolForTransformMode", {
      files: {
        "/entry.js": /* js */ `
          Symbol.for("unused");
          const used = Symbol.for("used");
          export { used };
        `,
      },
      bundling: false,
      minifySyntax: true,
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // Should remove unused in transform mode too
        expect(output).not.toContain('"unused"');

        // Should keep used
        expect(output).toContain('Symbol.for("used")');
      },
    });

    // Test interaction with tree shaking
    itBundled("minify/SymbolForTreeShaking", {
      files: {
        "/entry.js": /* js */ `
          import { sym } from "./lib.js";
          
          // This should be removed
          Symbol.for("entry-unused");
          
          capture(sym);
        `,
        "/lib.js": /* js */ `
          // This should be removed (unused export)
          export const unused = Symbol.for("lib-unused-export");
          
          // This should be kept (used export)
          export const sym = Symbol.for("lib-used-export");
          
          // This should be removed (not exported)
          Symbol.for("lib-internal");
        `,
      },
      minifySyntax: true,
      treeShaking: true,
      onAfterBundle(api) {
        const output = api.readFile("/out.js");

        // Should remove all unused Symbol.for calls
        expect(output).not.toContain("entry-unused");
        expect(output).not.toContain("lib-unused-export");
        expect(output).not.toContain("lib-internal");

        // Should keep used Symbol.for call
        expect(output).toContain("lib-used-export");
      },
    });

    // Test that Symbol.for is still called at runtime when overridden
    itBundled("minify/SymbolForRuntimeOverride", {
      files: {
        "/entry.js": /* js */ `
          let callCount = 0;
          const originalSymbolFor = Symbol.for;
          
          // Override Symbol.for to count calls
          Symbol.for = function(key) {
            callCount++;
            return originalSymbolFor.call(this, key);
          };
          
          // These unused calls should be removed at bundle time
          Symbol.for("unused1");
          Symbol.for("unused2");
          
          // These used calls should remain and increment callCount
          const s1 = Symbol.for("used1");
          const s2 = Symbol.for("used2");
          
          // Restore original
          Symbol.for = originalSymbolFor;
          
          // Verify that Symbol.for was called for the used symbols
          if (callCount !== 2) {
            throw new Error(\`Expected 2 calls to Symbol.for, got \${callCount}\`);
          }
          
          // Verify the symbols work correctly
          if (s1 !== Symbol.for("used1")) {
            throw new Error("Symbol s1 mismatch");
          }
          if (s2 !== Symbol.for("used2")) {
            throw new Error("Symbol s2 mismatch");
          }
          
          console.log("PASS");
        `,
      },

      minifySyntax: true,
      run: {
        stdout: "PASS",
      },
    });
  });
});