Skip to content

Commit c02b8b1

Browse files
committed
proposal: trojan horse private methods
1 parent 1596663 commit c02b8b1

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

packages/@lwc/babel-plugin-component/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const TEMPLATE_KEY = 'tmpl';
3434
const COMPONENT_NAME_KEY = 'sel';
3535
const API_VERSION_KEY = 'apiVersion';
3636
const COMPONENT_CLASS_ID = '__lwc_component_class_internal';
37+
const PRIVATE_METHOD_PREFIX = '__lwc_component_class_internal_private_';
3738

3839
export {
3940
DECORATOR_TYPES,
@@ -46,4 +47,5 @@ export {
4647
COMPONENT_NAME_KEY,
4748
API_VERSION_KEY,
4849
COMPONENT_CLASS_ID,
50+
PRIVATE_METHOD_PREFIX,
4951
};

packages/@lwc/babel-plugin-component/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import dedupeImports from './dedupe-imports';
1515
import dynamicImports from './dynamic-imports';
1616
import scopeCssImports from './scope-css-imports';
1717
import compilerVersionNumber from './compiler-version-number';
18+
import privateMethodTransform from './private-method-transform';
19+
import reversePrivateMethodTransform from './reverse-private-method-transform';
1820
import { getEngineImportSpecifiers } from './utils';
1921
import type { BabelAPI, LwcBabelPluginPass } from './types';
2022
import type { PluginObj } from '@babel/core';
@@ -33,13 +35,16 @@ export default function LwcClassTransform(api: BabelAPI): PluginObj<LwcBabelPlug
3335
const { Class: transformDecorators } = decorators(api);
3436
const { Import: transformDynamicImports } = dynamicImports();
3537
const { ClassBody: addCompilerVersionNumber } = compilerVersionNumber(api);
38+
const { Program: transformPrivateMethods } = privateMethodTransform(api);
39+
const { ClassMethod: reverseTransformPrivateMethods } = reversePrivateMethodTransform(api);
3640

3741
return {
3842
manipulateOptions(opts, parserOpts) {
3943
parserOpts.plugins.push('classProperties', [
4044
'decorators',
4145
{ decoratorsBeforeExport: true },
4246
]);
47+
parserOpts.plugins.push('privateMethods');
4348
},
4449

4550
visitor: {
@@ -54,6 +59,15 @@ export default function LwcClassTransform(api: BabelAPI): PluginObj<LwcBabelPlug
5459

5560
// Add ?scoped=true to *.scoped.css imports
5661
scopeCssImports(api, path);
62+
63+
// Transform private methods BEFORE any other plugin processes them
64+
if (
65+
transformPrivateMethods &&
66+
typeof transformPrivateMethods === 'object' &&
67+
'enter' in transformPrivateMethods
68+
) {
69+
(transformPrivateMethods as any).enter(path, state);
70+
}
5771
},
5872
exit(path) {
5973
const engineImportSpecifiers = getEngineImportSpecifiers(path);
@@ -68,6 +82,8 @@ export default function LwcClassTransform(api: BabelAPI): PluginObj<LwcBabelPlug
6882

6983
Class: transformDecorators,
7084

85+
ClassMethod: reverseTransformPrivateMethods,
86+
7187
ClassBody: addCompilerVersionNumber,
7288

7389
ExportDefaultDeclaration: transformCreateRegisterComponent,

playground/src/modules/x/counter/counter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ export default class extends LightningElement {
1010
}
1111
decrement() {
1212
this.counter--;
13+
this.#something();
1314
}
15+
16+
#something() {}
1417
}

0 commit comments

Comments
 (0)