@@ -660,4 +660,132 @@ describe('private method transform validation', () => {
660660 / P r i v a t e f i e l d s a r e n o t c u r r e n t l y s u p p o r t e d /
661661 ) ;
662662 } ) ;
663+
664+ test ( 'forward-only output transforms call sites to prefixed names' , ( ) => {
665+ const source = `
666+ import { LightningElement } from 'lwc';
667+ export default class Test extends LightningElement {
668+ #doWork(x) { return x * 2; }
669+ connectedCallback() {
670+ const result = this.#doWork(21);
671+ console.log(result);
672+ }
673+ }
674+ ` ;
675+
676+ const result = transformForwardOnly ( source ) ;
677+ const code = result . code ! ;
678+ expect ( code ) . toContain ( 'this.__lwc_component_class_internal_private_doWork(21)' ) ;
679+ expect ( code ) . not . toContain ( 'this.#doWork' ) ;
680+ } ) ;
681+
682+ test ( 'forward references in call sites are transformed' , ( ) => {
683+ const source = `
684+ import { LightningElement } from 'lwc';
685+ export default class Test extends LightningElement {
686+ connectedCallback() {
687+ return this.#helper();
688+ }
689+ #helper() { return 42; }
690+ }
691+ ` ;
692+
693+ const result = transformForwardOnly ( source ) ;
694+ const code = result . code ! ;
695+ expect ( code ) . toContain ( 'this.__lwc_component_class_internal_private_helper()' ) ;
696+ expect ( code ) . not . toContain ( 'this.#helper' ) ;
697+
698+ const roundTrip = transformWithFullPipeline ( source ) ;
699+ expect ( roundTrip . code ) . toContain ( 'this.#helper()' ) ;
700+ expect ( roundTrip . code ) . not . toContain ( '__lwc_component_class_internal_private_' ) ;
701+ } ) ;
702+
703+ test ( 'multiple invocations of the same private method are all transformed' , ( ) => {
704+ const source = `
705+ import { LightningElement } from 'lwc';
706+ export default class Test extends LightningElement {
707+ #compute(x) { return x * 2; }
708+ run() {
709+ const a = this.#compute(1);
710+ const b = this.#compute(2);
711+ const c = this.#compute(3);
712+ return a + b + c;
713+ }
714+ }
715+ ` ;
716+
717+ const result = transformForwardOnly ( source ) ;
718+ const code = result . code ! ;
719+ const matches = code . match ( / _ _ l w c _ c o m p o n e n t _ c l a s s _ i n t e r n a l _ p r i v a t e _ c o m p u t e / g) ;
720+ // 1 definition + 3 call sites = 4 occurrences
721+ expect ( matches ) . toHaveLength ( 4 ) ;
722+
723+ const roundTrip = transformWithFullPipeline ( source ) ;
724+ expect ( roundTrip . code ) . not . toContain ( '__lwc_component_class_internal_private_' ) ;
725+ const privateMatches = roundTrip . code ! . match ( / # c o m p u t e / g) ;
726+ expect ( privateMatches ) . toHaveLength ( 4 ) ;
727+ } ) ;
728+
729+ test ( 'self-referencing private method round-trips' , ( ) => {
730+ const source = `
731+ import { LightningElement } from 'lwc';
732+ export default class Test extends LightningElement {
733+ #recursive(n) {
734+ if (n <= 0) return 0;
735+ return n + this.#recursive(n - 1);
736+ }
737+ }
738+ ` ;
739+
740+ const result = transformForwardOnly ( source ) ;
741+ const code = result . code ! ;
742+ expect ( code ) . toContain ( 'this.__lwc_component_class_internal_private_recursive(n - 1)' ) ;
743+
744+ const roundTrip = transformWithFullPipeline ( source ) ;
745+ expect ( roundTrip . code ) . toContain ( 'this.#recursive(n - 1)' ) ;
746+ expect ( roundTrip . code ) . not . toContain ( '__lwc_component_class_internal_private_' ) ;
747+ } ) ;
748+
749+ test ( 'private method reference without call round-trips' , ( ) => {
750+ const source = `
751+ import { LightningElement } from 'lwc';
752+ export default class Test extends LightningElement {
753+ #handler() { return 42; }
754+ connectedCallback() {
755+ const fn = this.#handler;
756+ setTimeout(this.#handler, 100);
757+ }
758+ }
759+ ` ;
760+
761+ const result = transformForwardOnly ( source ) ;
762+ const code = result . code ! ;
763+ expect ( code ) . toContain ( 'this.__lwc_component_class_internal_private_handler;' ) ;
764+ expect ( code ) . toContain ( 'this.__lwc_component_class_internal_private_handler, 100' ) ;
765+ expect ( code ) . not . toContain ( 'this.#handler' ) ;
766+
767+ const roundTrip = transformWithFullPipeline ( source ) ;
768+ expect ( roundTrip . code ) . toContain ( 'this.#handler;' ) ;
769+ expect ( roundTrip . code ) . toContain ( 'this.#handler, 100' ) ;
770+ expect ( roundTrip . code ) . not . toContain ( '__lwc_component_class_internal_private_' ) ;
771+ } ) ;
772+
773+ test ( 'cross-method private call sites in forward-only output' , ( ) => {
774+ const source = `
775+ import { LightningElement } from 'lwc';
776+ export default class Test extends LightningElement {
777+ #helper() { return 42; }
778+ #caller() {
779+ return this.#helper() + 1;
780+ }
781+ }
782+ ` ;
783+
784+ const result = transformForwardOnly ( source ) ;
785+ const code = result . code ! ;
786+ expect ( code ) . toContain ( 'this.__lwc_component_class_internal_private_helper()' ) ;
787+ expect ( code ) . toContain ( '__lwc_component_class_internal_private_caller' ) ;
788+ expect ( code ) . not . toContain ( '#helper' ) ;
789+ expect ( code ) . not . toContain ( '#caller' ) ;
790+ } ) ;
663791} ) ;
0 commit comments