Skip to content

Commit 500c3b3

Browse files
committed
test: add cross-class private method access inline tests
Test that cross-class #privateName access is a parse error, and that a spoofed mangled name definition is harmlessly round-tripped back to a class-scoped private method. Made-with: Cursor
1 parent 4d60549 commit 500c3b3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

packages/@lwc/babel-plugin-component/src/__tests__/private-method-transform.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,42 @@ describe('private method transform validation', () => {
303303
expect(code).not.toContain('#helper');
304304
expect(code).not.toContain('#caller');
305305
});
306+
307+
test('cross-class #privateName access is a parse error', () => {
308+
const source = `
309+
import { LightningElement } from 'lwc';
310+
export default class A extends LightningElement {
311+
#privateMethod() {}
312+
}
313+
class B extends LightningElement {
314+
hax() {
315+
this.#privateMethod();
316+
this.__lwc_component_class_internal_private_privateMethod();
317+
}
318+
}
319+
`;
320+
321+
expect(() => transformWithFullPipeline(source)).toThrowError(
322+
/Private name #privateMethod is not defined/
323+
);
324+
});
325+
326+
test('cross-class spoofed mangled name is round-tripped back to a harmless private method', () => {
327+
const source = `
328+
import { LightningElement } from 'lwc';
329+
export default class A extends LightningElement {
330+
#privateMethod() {}
331+
}
332+
class B extends LightningElement {
333+
__lwc_component_class_internal_private_privateMethod() {
334+
return 'spoofed';
335+
}
336+
}
337+
`;
338+
339+
const result = transformWithFullPipeline(source);
340+
const code = result.code!;
341+
expect(code).not.toContain('__lwc_component_class_internal_private_');
342+
expect((code.match(/#privateMethod/g) || []).length).toBe(2);
343+
});
306344
});

0 commit comments

Comments
 (0)