Skip to content

Commit c59f08a

Browse files
authored
Merge pull request #1 from a-chabot/a-chabot/private-methods-compiler-flag
feat: gate private method transform behind enablePrivateMethods compi…
2 parents f18b317 + d38ae20 commit c59f08a

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

packages/@lwc/compiler/src/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ export interface TransformOptions {
149149
experimentalErrorRecoveryMode?: boolean;
150150
/** Full module path for a feature flag to import and enforce at runtime (e.g., '@salesforce/featureFlag/name'). */
151151
componentFeatureFlagModulePath?: string;
152+
/** Flag to enable the private method round-trip transform. When false or omitted, private methods pass through to standard Babel handling. */
153+
enablePrivateMethods?: boolean;
152154
}
153155

154156
type OptionalTransformKeys =
@@ -164,6 +166,7 @@ type OptionalTransformKeys =
164166
| 'experimentalDynamicDirective'
165167
| 'dynamicImports'
166168
| 'componentFeatureFlagModulePath'
169+
| 'enablePrivateMethods'
167170
| 'instrumentation';
168171

169172
type RequiredTransformOptions = RecursiveRequired<Omit<TransformOptions, OptionalTransformKeys>>;

packages/@lwc/compiler/src/transformers/__tests__/transform-javascript.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,38 @@ describe('unnecessary registerDecorators', () => {
224224
});
225225
});
226226

227+
describe('enablePrivateMethods', () => {
228+
const source = `
229+
import { LightningElement } from 'lwc';
230+
export default class Foo extends LightningElement {
231+
#secret() { return 42; }
232+
connectedCallback() { this.#secret(); }
233+
}
234+
`;
235+
236+
it('should transform and restore private methods when enabled', async () => {
237+
const { code } = await transform(source, 'foo.js', {
238+
...BASE_TRANSFORM_OPTIONS,
239+
enablePrivateMethods: true,
240+
});
241+
expect(code).toContain('#secret');
242+
expect(code).not.toContain('__lwc_component_class_internal_private_');
243+
});
244+
245+
it('should reject private methods when disabled', async () => {
246+
await expect(
247+
transform(source, 'foo.js', {
248+
...BASE_TRANSFORM_OPTIONS,
249+
enablePrivateMethods: false,
250+
})
251+
).rejects.toThrow();
252+
});
253+
254+
it('should reject private methods when omitted', async () => {
255+
await expect(transform(source, 'foo.js', BASE_TRANSFORM_OPTIONS)).rejects.toThrow();
256+
});
257+
});
258+
227259
describe('sourcemaps', () => {
228260
it("should generate inline sourcemaps when the output config includes the 'inline' option for sourcemaps", () => {
229261
const source = `

packages/@lwc/compiler/src/transformers/javascript.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default function scriptTransform(
4848
dynamicImports,
4949
outputConfig: { sourcemap },
5050
enableLightningWebSecurityTransforms,
51+
enablePrivateMethods,
5152
namespace,
5253
name,
5354
instrumentation,
@@ -68,10 +69,10 @@ export default function scriptTransform(
6869
};
6970

7071
const plugins: babel.PluginItem[] = [
71-
LwcPrivateMethodTransform,
72+
...(enablePrivateMethods ? [LwcPrivateMethodTransform as babel.PluginItem] : []),
7273
[lwcClassTransformPlugin, lwcBabelPluginOptions],
7374
[babelClassPropertiesPlugin, { loose: true }],
74-
LwcReversePrivateMethodTransform,
75+
...(enablePrivateMethods ? [LwcReversePrivateMethodTransform as babel.PluginItem] : []),
7576
];
7677

7778
if (!isAPIFeatureEnabled(APIFeature.DISABLE_OBJECT_REST_SPREAD_TRANSFORMATION, apiVersion)) {

packages/@lwc/rollup-plugin/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export interface RollupLwcOptions {
7474
* @example '@salesforce/featureFlag/name'
7575
*/
7676
componentFeatureFlagModulePath?: string;
77+
enablePrivateMethods?: boolean;
7778
}
7879

7980
const PLUGIN_NAME = 'rollup-plugin-lwc-compiler';
@@ -198,6 +199,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
198199
apiVersion,
199200
defaultModules = DEFAULT_MODULES,
200201
componentFeatureFlagModulePath,
202+
enablePrivateMethods,
201203
} = pluginOptions;
202204

203205
return {
@@ -395,6 +397,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
395397
targetSSR,
396398
ssrMode,
397399
componentFeatureFlagModulePath,
400+
enablePrivateMethods,
398401
});
399402

400403
if (warnings) {

0 commit comments

Comments
 (0)