-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathprivate-method-transform.spec.ts
More file actions
596 lines (529 loc) · 21.5 KB
/
private-method-transform.spec.ts
File metadata and controls
596 lines (529 loc) · 21.5 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { describe, expect, test } from 'vitest';
import { transformSync } from '@babel/core';
import plugin, { LwcReversePrivateMethodTransform } from '../index';
const BASE_OPTS = {
namespace: 'lwc',
name: 'test',
};
const BASE_CONFIG = {
babelrc: false,
configFile: false,
filename: 'test.js',
compact: false,
};
function transformWithFullPipeline(source: string, opts = {}) {
return transformSync(source, {
...BASE_CONFIG,
plugins: [[plugin, { ...BASE_OPTS, ...opts }], LwcReversePrivateMethodTransform],
});
}
function transformReverseOnly(source: string) {
return transformSync(source, {
...BASE_CONFIG,
plugins: [LwcReversePrivateMethodTransform],
});
}
function transformForwardOnly(source: string, opts = {}) {
return transformSync(source, {
...BASE_CONFIG,
plugins: [[plugin, { ...BASE_OPTS, ...opts }]],
});
}
describe('private method transform validation', () => {
test('normal private methods round-trip successfully', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#privateMethod() {
return 42;
}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#privateMethod');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('multiple private methods round-trip successfully', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#methodA() { return 1; }
#methodB() { return 2; }
#methodC() { return 3; }
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#methodA');
expect(result!.code).toContain('#methodB');
expect(result!.code).toContain('#methodC');
});
test('throws error when user-defined method collides with reserved prefix', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
__lwc_component_class_internal_private_sneakyMethod() {
return 'collision';
}
}
`;
expect(() => transformWithFullPipeline(source)).toThrowError(
/conflicts with internal naming conventions\. Please rename this function to avoid conflict/
);
});
test('throws error when collision exists alongside real private methods', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#realPrivate() { return 1; }
__lwc_component_class_internal_private_fakePrivate() {
return 'collision';
}
}
`;
expect(() => transformWithFullPipeline(source)).toThrowError(
/conflicts with internal naming conventions\. Please rename this function to avoid conflict/
);
});
test('does not flag methods that do not use the reserved prefix', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#privateMethod() { return 1; }
normalPublicMethod() { return 2; }
_underscoreMethod() { return 3; }
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#privateMethod');
expect(result!.code).toContain('normalPublicMethod');
expect(result!.code).toContain('_underscoreMethod');
});
test('async private method round-trips successfully', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
async #fetchData() {
return await Promise.resolve(42);
}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#fetchData');
expect(result!.code).toContain('async');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('static private method round-trips successfully', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
static #helper() {
return 'static';
}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#helper');
expect(result!.code).toContain('static');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('private method with parameters round-trips successfully', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#compute(a, b, ...rest) {
return a + b + rest.length;
}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#compute');
expect(result!.code).toContain('a');
expect(result!.code).toContain('b');
expect(result!.code).toContain('...rest');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('private getters and setters are not transformed', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
get #value() {
return this._val;
}
set #value(v) {
this._val = v;
}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('get #value');
expect(result!.code).toContain('set #value');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('decorated private method throws', () => {
const source = `
import { LightningElement, api } from 'lwc';
export default class Test extends LightningElement {
@api #decorated() {
return 1;
}
}
`;
// The @api decorator validation (LWC1103) fires before the private method
// transform, so the error comes from decorator validation rather than the
// private method decorator check (LWC1212).
expect(() => transformWithFullPipeline(source)).toThrowError(
/"@api" can only be applied on class properties/
);
});
test('class with zero private methods succeeds', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
publicMethod() { return 1; }
anotherPublic() { return 2; }
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('publicMethod');
expect(result!.code).toContain('anotherPublic');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('error message includes the specific offending method name', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
__lwc_component_class_internal_private_mySpecificName() {
return 'collision';
}
}
`;
expect(() => transformWithFullPipeline(source)).toThrowError(
/__lwc_component_class_internal_private_mySpecificName/
);
});
test('multiple collision methods throws on first encountered', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
__lwc_component_class_internal_private_collisionA() { return 1; }
__lwc_component_class_internal_private_collisionB() { return 2; }
}
`;
expect(() => transformWithFullPipeline(source)).toThrowError(
/__lwc_component_class_internal_private_collision[AB]/
);
});
test('generator private method round-trips successfully', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
*#generate() {
yield 1;
yield 2;
}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#generate');
expect(result!.code).toContain('yield');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('reverse standalone on clean code succeeds without forward metadata', () => {
const source = `
class Test {
publicMethod() { return 1; }
}
`;
const result = transformReverseOnly(source);
expect(result!.code).toContain('publicMethod');
});
test('reverse standalone with prefixed method throws collision when metadata is missing', () => {
const source = `
class Test {
__lwc_component_class_internal_private_foo() { return 1; }
}
`;
expect(() => transformReverseOnly(source)).toThrowError(
/conflicts with internal naming conventions\. Please rename this function to avoid conflict/
);
});
test('Program.exit count mismatch throws when forward-transformed method is removed', () => {
const PREFIX = '__lwc_component_class_internal_private_';
// Custom Babel plugin that removes methods with the private prefix,
// simulating an intermediate plugin that drops a method between
// the forward and reverse transforms.
function methodRemoverPlugin(): { visitor: { ClassMethod: (path: any) => void } } {
return {
visitor: {
ClassMethod(path: any) {
const key = path.get('key');
if (key.isIdentifier() && key.node.name.startsWith(PREFIX)) {
path.remove();
}
},
},
};
}
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#disappearingMethod() { return 1; }
}
`;
expect(() =>
transformSync(source, {
...BASE_CONFIG,
plugins: [
[plugin, { ...BASE_OPTS }],
methodRemoverPlugin,
LwcReversePrivateMethodTransform,
],
})
).toThrowError(/Private method transform count mismatch/);
});
test('multiple classes in the same file round-trip private methods', () => {
const source = `
import { LightningElement } from 'lwc';
export default class First extends LightningElement {
#shared() { return 'first'; }
}
class Second extends LightningElement {
#shared() { return 'second'; }
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
const matches = result!.code!.match(/#shared/g);
expect(matches).toHaveLength(2);
});
test('private method body with call sites round-trips', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#helper() { return 42; }
#caller() {
return this.#helper() + 1;
}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#helper');
expect(result!.code).toContain('#caller');
expect(result!.code).toContain('this.#helper()');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('forward-only output contains correct prefixed names', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#foo() { return 1; }
#bar() { return 2; }
}
`;
const result = transformForwardOnly(source);
expect(result!.code).toContain('__lwc_component_class_internal_private_foo');
expect(result!.code).toContain('__lwc_component_class_internal_private_bar');
expect(result!.code).not.toContain('#foo');
expect(result!.code).not.toContain('#bar');
});
test('combined flags (static, async, default param) survive round-trip', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
static async #fetch(url, opts = {}) {
return await fetch(url, opts);
}
}
`;
const result = transformWithFullPipeline(source);
const code = result!.code!;
expect(code).toContain('static');
expect(code).toContain('async');
expect(code).toContain('#fetch');
expect(code).toContain('url');
expect(code).toContain('opts');
expect(code).not.toContain('__lwc_component_class_internal_private_');
});
test('method ordering is preserved through round-trip', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#alpha() { return 'a'; }
publicBeta() { return 'b'; }
#gamma() { return 'c'; }
publicDelta() { return 'd'; }
}
`;
const result = transformWithFullPipeline(source);
const code = result!.code!;
const alphaIdx = code.indexOf('#alpha');
const betaIdx = code.indexOf('publicBeta');
const gammaIdx = code.indexOf('#gamma');
const deltaIdx = code.indexOf('publicDelta');
expect(alphaIdx).toBeGreaterThan(-1);
expect(betaIdx).toBeGreaterThan(-1);
expect(gammaIdx).toBeGreaterThan(-1);
expect(deltaIdx).toBeGreaterThan(-1);
expect(alphaIdx).toBeLessThan(betaIdx);
expect(betaIdx).toBeLessThan(gammaIdx);
expect(gammaIdx).toBeLessThan(deltaIdx);
});
test('default parameter values survive round-trip', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#greet(name = 'world', times = 3) {
return name.repeat(times);
}
}
`;
const result = transformWithFullPipeline(source);
const code = result!.code!;
expect(code).toContain('#greet');
expect(code).toContain("'world'");
expect(code).toContain('3');
expect(code).not.toContain('__lwc_component_class_internal_private_');
});
test('destructuring parameters survive round-trip', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#process({ x, y }, [a, b]) {
return x + y + a + b;
}
}
`;
const result = transformWithFullPipeline(source);
const code = result!.code!;
expect(code).toContain('#process');
expect(code).toMatch(/\{\s*x,\s*y\s*\}/);
expect(code).toMatch(/\[\s*a,\s*b\s*\]/);
expect(code).not.toContain('__lwc_component_class_internal_private_');
});
test('empty method body round-trips', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#noop() {}
}
`;
const result = transformWithFullPipeline(source);
expect(result!.code).toContain('#noop');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('private and public method with same name coexist', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#foo() { return 'private'; }
foo() { return 'public'; }
}
`;
const result = transformWithFullPipeline(source);
const code = result!.code!;
expect(code).toContain('#foo');
expect(code).toContain("return 'private'");
expect(code).toContain("return 'public'");
expect(code).not.toContain('__lwc_component_class_internal_private_');
});
test('intermediate plugin that modifies method body does not break reverse transform', () => {
function bodyModifierPlugin({ types: t }: any) {
return {
visitor: {
ClassMethod(path: any) {
const key = path.get('key');
if (
key.isIdentifier() &&
key.node.name.startsWith('__lwc_component_class_internal_private_')
) {
const logStatement = t.expressionStatement(
t.callExpression(
t.memberExpression(
t.identifier('console'),
t.identifier('log')
),
[t.stringLiteral('injected')]
)
);
path.node.body.body.unshift(logStatement);
}
},
},
};
}
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#myMethod() { return 42; }
}
`;
const result = transformSync(source, {
...BASE_CONFIG,
plugins: [
[plugin, { ...BASE_OPTS }],
bodyModifierPlugin,
LwcReversePrivateMethodTransform,
],
});
expect(result!.code).toContain('#myMethod');
expect(result!.code).toContain('console.log("injected")');
expect(result!.code).not.toContain('__lwc_component_class_internal_private_');
});
test('intermediate plugin that adds a prefixed method triggers collision', () => {
function prefixedMethodInjectorPlugin({ types: t }: any) {
let injected = false;
return {
visitor: {
ClassMethod(path: any) {
if (injected) return;
injected = true;
path.insertAfter(
t.classMethod(
'method',
t.identifier('__lwc_component_class_internal_private_injected'),
[],
t.blockStatement([t.returnStatement(t.stringLiteral('injected'))])
)
);
},
},
};
}
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
#realMethod() { return 1; }
}
`;
expect(() =>
transformSync(source, {
...BASE_CONFIG,
plugins: [
[plugin, { ...BASE_OPTS }],
prefixedMethodInjectorPlugin,
LwcReversePrivateMethodTransform,
],
})
).toThrowError(/conflicts with internal naming conventions/);
});
test('methods with similar but non-matching prefixes are not reverse-transformed', () => {
const source = `
import { LightningElement } from 'lwc';
export default class Test extends LightningElement {
__lwc_component_class_internal_foo() { return 1; }
__lwc_component_class_internal_privatefoo() { return 2; }
}
`;
const result = transformWithFullPipeline(source);
const code = result!.code!;
expect(code).toContain('__lwc_component_class_internal_foo');
expect(code).toContain('__lwc_component_class_internal_privatefoo');
expect(code).not.toContain('#foo');
});
});