@lwc/babel-plugin-component 9.0.1 → 9.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1297 -0
- package/dist/index.js +1 -1
- package/package.json +4 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Salesforce, Inc.
|
|
3
|
+
*/
|
|
4
|
+
'use strict';
|
|
5
|
+
|
|
6
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
7
|
+
|
|
8
|
+
var node_path = require('node:path');
|
|
9
|
+
var helperModuleImports = require('@babel/helper-module-imports');
|
|
10
|
+
var shared = require('@lwc/shared');
|
|
11
|
+
var errors = require('@lwc/errors');
|
|
12
|
+
var lineColumn = require('line-column');
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
16
|
+
* All rights reserved.
|
|
17
|
+
* SPDX-License-Identifier: MIT
|
|
18
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
19
|
+
*/
|
|
20
|
+
const LWC_PACKAGE_ALIAS = 'lwc';
|
|
21
|
+
const LWC_PACKAGE_EXPORTS = {
|
|
22
|
+
API_DECORATOR: 'api',
|
|
23
|
+
TRACK_DECORATOR: 'track',
|
|
24
|
+
WIRE_DECORATOR: 'wire',
|
|
25
|
+
};
|
|
26
|
+
const LWC_COMPONENT_PROPERTIES = {
|
|
27
|
+
PUBLIC_PROPS: 'publicProps',
|
|
28
|
+
PUBLIC_METHODS: 'publicMethods',
|
|
29
|
+
WIRE: 'wire',
|
|
30
|
+
TRACK: 'track',
|
|
31
|
+
};
|
|
32
|
+
const DECORATOR_TYPES = {
|
|
33
|
+
PROPERTY: 'property',
|
|
34
|
+
GETTER: 'getter',
|
|
35
|
+
SETTER: 'setter',
|
|
36
|
+
METHOD: 'method',
|
|
37
|
+
};
|
|
38
|
+
const REGISTER_COMPONENT_ID = 'registerComponent';
|
|
39
|
+
const REGISTER_DECORATORS_ID = 'registerDecorators';
|
|
40
|
+
const TEMPLATE_KEY = 'tmpl';
|
|
41
|
+
const COMPONENT_NAME_KEY = 'sel';
|
|
42
|
+
const API_VERSION_KEY = 'apiVersion';
|
|
43
|
+
const COMPONENT_CLASS_ID = '__lwc_component_class_internal';
|
|
44
|
+
const SYNTHETIC_ELEMENT_INTERNALS_KEY = 'enableSyntheticElementInternals';
|
|
45
|
+
const COMPONENT_FEATURE_FLAG_KEY = 'componentFeatureFlag';
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
49
|
+
* All rights reserved.
|
|
50
|
+
* SPDX-License-Identifier: MIT
|
|
51
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
52
|
+
*/
|
|
53
|
+
function getBaseName(classPath) {
|
|
54
|
+
const ext = node_path.extname(classPath);
|
|
55
|
+
return node_path.basename(classPath, ext);
|
|
56
|
+
}
|
|
57
|
+
function importDefaultTemplate(path, state) {
|
|
58
|
+
const { filename } = state.file.opts;
|
|
59
|
+
const componentName = getBaseName(filename);
|
|
60
|
+
return helperModuleImports.addDefault(path, `./${componentName}.html`, {
|
|
61
|
+
nameHint: TEMPLATE_KEY,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function needsComponentRegistration(path) {
|
|
65
|
+
return ((path.isIdentifier() && path.node.name !== 'undefined' && path.node.name !== 'null') ||
|
|
66
|
+
path.isCallExpression() ||
|
|
67
|
+
path.isClassDeclaration() ||
|
|
68
|
+
path.isConditionalExpression());
|
|
69
|
+
}
|
|
70
|
+
function getComponentRegisteredName(t, state) {
|
|
71
|
+
const { namespace, name } = state.opts;
|
|
72
|
+
const componentName = shared.generateCustomElementTagName(namespace, name);
|
|
73
|
+
return t.stringLiteral(componentName);
|
|
74
|
+
}
|
|
75
|
+
function component ({ types: t }) {
|
|
76
|
+
function createRegisterComponent(declarationPath, state) {
|
|
77
|
+
const registerComponentId = helperModuleImports.addNamed(declarationPath, REGISTER_COMPONENT_ID, LWC_PACKAGE_ALIAS);
|
|
78
|
+
const templateIdentifier = importDefaultTemplate(declarationPath, state);
|
|
79
|
+
// Optionally import feature flag module if provided via compiler options
|
|
80
|
+
let componentFeatureFlagIdentifier;
|
|
81
|
+
if (state.opts.componentFeatureFlagModulePath) {
|
|
82
|
+
componentFeatureFlagIdentifier = helperModuleImports.addDefault(declarationPath, state.opts.componentFeatureFlagModulePath, {
|
|
83
|
+
nameHint: COMPONENT_FEATURE_FLAG_KEY,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const statementPath = declarationPath.getStatementParent();
|
|
87
|
+
const componentRegisteredName = getComponentRegisteredName(t, state);
|
|
88
|
+
let node = declarationPath.node;
|
|
89
|
+
if (declarationPath.isClassDeclaration()) {
|
|
90
|
+
const hasIdentifier = t.isIdentifier(node.id);
|
|
91
|
+
if (hasIdentifier) {
|
|
92
|
+
statementPath.insertBefore(node);
|
|
93
|
+
node = node.id;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// if it does not have an id, we can treat it as a ClassExpression
|
|
97
|
+
t.toExpression(node);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const apiVersion = shared.getAPIVersionFromNumber(state.opts.apiVersion);
|
|
101
|
+
// Example:
|
|
102
|
+
// registerComponent(cmp, {
|
|
103
|
+
// tmpl: template,
|
|
104
|
+
// sel: 'x-foo',
|
|
105
|
+
// apiVersion: '58'
|
|
106
|
+
// })
|
|
107
|
+
const properties = [
|
|
108
|
+
t.objectProperty(t.identifier(TEMPLATE_KEY), templateIdentifier),
|
|
109
|
+
t.objectProperty(t.identifier(COMPONENT_NAME_KEY), componentRegisteredName),
|
|
110
|
+
// It's important that, at this point, we have an APIVersion rather than just a number.
|
|
111
|
+
// The client needs to trust the server that it's providing an actual known API version
|
|
112
|
+
t.objectProperty(t.identifier(API_VERSION_KEY), t.numericLiteral(apiVersion)),
|
|
113
|
+
];
|
|
114
|
+
if (componentFeatureFlagIdentifier) {
|
|
115
|
+
properties.push(t.objectProperty(t.identifier(COMPONENT_FEATURE_FLAG_KEY), t.objectExpression([
|
|
116
|
+
t.objectProperty(t.identifier('value'), t.callExpression(t.identifier('Boolean'), [
|
|
117
|
+
componentFeatureFlagIdentifier,
|
|
118
|
+
])),
|
|
119
|
+
t.objectProperty(t.identifier('path'), t.stringLiteral(state.opts.componentFeatureFlagModulePath)),
|
|
120
|
+
])));
|
|
121
|
+
}
|
|
122
|
+
// Only include enableSyntheticElementInternals if set to true
|
|
123
|
+
if (state.opts.enableSyntheticElementInternals === true) {
|
|
124
|
+
properties.push(t.objectProperty(t.identifier(SYNTHETIC_ELEMENT_INTERNALS_KEY), t.booleanLiteral(true)));
|
|
125
|
+
}
|
|
126
|
+
const registerComponentExpression = t.callExpression(registerComponentId, [
|
|
127
|
+
node,
|
|
128
|
+
t.objectExpression(properties),
|
|
129
|
+
]);
|
|
130
|
+
// Example:
|
|
131
|
+
// const __lwc_component_class_internal = registerComponent(cmp, ...);
|
|
132
|
+
// This provides a way to access the component class for other lwc tools
|
|
133
|
+
const classIdentifier = t.identifier(COMPONENT_CLASS_ID);
|
|
134
|
+
declarationPath.parentPath.insertBefore(t.variableDeclaration('const', [
|
|
135
|
+
t.variableDeclarator(classIdentifier, registerComponentExpression),
|
|
136
|
+
]));
|
|
137
|
+
return classIdentifier;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
ExportDefaultDeclaration(path, state) {
|
|
141
|
+
const implicitResolution = !state.opts.isExplicitImport;
|
|
142
|
+
if (implicitResolution) {
|
|
143
|
+
const declaration = path.get('declaration');
|
|
144
|
+
if (needsComponentRegistration(declaration)) {
|
|
145
|
+
declaration.replaceWith(createRegisterComponent(declaration, state));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/*
|
|
153
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
154
|
+
* All rights reserved.
|
|
155
|
+
* SPDX-License-Identifier: MIT
|
|
156
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
157
|
+
*/
|
|
158
|
+
function isClassMethod(classMethod, properties = {}) {
|
|
159
|
+
const { kind = 'method', name } = properties;
|
|
160
|
+
return (classMethod.isClassMethod({ kind }) &&
|
|
161
|
+
(!name || classMethod.get('key').isIdentifier({ name })) &&
|
|
162
|
+
(properties.static === undefined || classMethod.node.static === properties.static));
|
|
163
|
+
}
|
|
164
|
+
function isGetterClassMethod(classMethod, properties = {}) {
|
|
165
|
+
return isClassMethod(classMethod, {
|
|
166
|
+
kind: 'get',
|
|
167
|
+
name: properties.name,
|
|
168
|
+
static: properties.static,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
function isSetterClassMethod(classMethod, properties = {}) {
|
|
172
|
+
return isClassMethod(classMethod, {
|
|
173
|
+
kind: 'set',
|
|
174
|
+
name: properties.name,
|
|
175
|
+
static: properties.static,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function getEngineImportsStatements(path) {
|
|
179
|
+
const programPath = path.isProgram()
|
|
180
|
+
? path
|
|
181
|
+
: path.findParent((node) => node.isProgram());
|
|
182
|
+
return programPath.get('body').filter((node) => {
|
|
183
|
+
const source = node.get('source');
|
|
184
|
+
return node.isImportDeclaration() && source.isStringLiteral({ value: LWC_PACKAGE_ALIAS });
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
function getEngineImportSpecifiers(path) {
|
|
188
|
+
const imports = getEngineImportsStatements(path);
|
|
189
|
+
return (imports
|
|
190
|
+
// Flat-map the specifier list for each import statement
|
|
191
|
+
.flatMap((importStatement) => importStatement.get('specifiers'))
|
|
192
|
+
// Skip ImportDefaultSpecifier and ImportNamespaceSpecifier
|
|
193
|
+
.filter((specifier) => specifier.type === 'ImportSpecifier')
|
|
194
|
+
// Get the list of specifiers with their name
|
|
195
|
+
.map((specifier) => {
|
|
196
|
+
const imported = specifier.get('imported').node
|
|
197
|
+
.name;
|
|
198
|
+
return { name: imported, path: specifier };
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
function normalizeLocation(source) {
|
|
202
|
+
const location = (source.node && (source.node.loc || source.node._loc)) || null;
|
|
203
|
+
if (!location) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
const code = source.hub.getCode();
|
|
207
|
+
if (!code) {
|
|
208
|
+
return {
|
|
209
|
+
line: location.start.line,
|
|
210
|
+
column: location.start.column,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
const lineFinder = lineColumn(code);
|
|
214
|
+
const startOffset = lineFinder.toIndex(location.start.line, location.start.column + 1);
|
|
215
|
+
const endOffset = lineFinder.toIndex(location.end.line, location.end.column) + 1;
|
|
216
|
+
const length = endOffset - startOffset;
|
|
217
|
+
return {
|
|
218
|
+
line: location.start.line,
|
|
219
|
+
column: location.start.column,
|
|
220
|
+
start: startOffset,
|
|
221
|
+
length,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function generateError(source, { errorInfo, messageArgs }, state) {
|
|
225
|
+
const message = errors.generateErrorMessage(errorInfo, messageArgs);
|
|
226
|
+
const error = source.buildCodeFrameError(message);
|
|
227
|
+
error.filename = state.filename;
|
|
228
|
+
error.loc = normalizeLocation(source);
|
|
229
|
+
error.lwcCode = errorInfo && errorInfo.code;
|
|
230
|
+
return error;
|
|
231
|
+
}
|
|
232
|
+
function collectError(source, { errorInfo, messageArgs }, state) {
|
|
233
|
+
const diagnostic = errors.generateCompilerDiagnostic(errorInfo, {
|
|
234
|
+
messageArgs,
|
|
235
|
+
origin: {
|
|
236
|
+
filename: state.filename,
|
|
237
|
+
location: normalizeLocation(source) ?? undefined,
|
|
238
|
+
},
|
|
239
|
+
}, true);
|
|
240
|
+
if (diagnostic.level === errors.DiagnosticLevel.Fatal) {
|
|
241
|
+
throw generateError(source, { errorInfo, messageArgs }, state);
|
|
242
|
+
}
|
|
243
|
+
if (!state.file.metadata.lwcErrors) {
|
|
244
|
+
state.file.metadata.lwcErrors = [];
|
|
245
|
+
}
|
|
246
|
+
state.file.metadata.lwcErrors.push(diagnostic);
|
|
247
|
+
}
|
|
248
|
+
function handleError(source, decoratorErrorOpts, state) {
|
|
249
|
+
if (isErrorRecoveryMode(state)) {
|
|
250
|
+
collectError(source, decoratorErrorOpts, state);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
throw generateError(source, decoratorErrorOpts, state);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function incrementMetricCounter(metric, state) {
|
|
257
|
+
state.opts.instrumentation?.incrementCounter(metric);
|
|
258
|
+
}
|
|
259
|
+
function isErrorRecoveryMode(state) {
|
|
260
|
+
return state.file.opts?.parserOpts?.errorRecovery ?? false;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/*
|
|
264
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
265
|
+
* All rights reserved.
|
|
266
|
+
* SPDX-License-Identifier: MIT
|
|
267
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
268
|
+
*/
|
|
269
|
+
const { API_DECORATOR: API_DECORATOR$2 } = LWC_PACKAGE_EXPORTS;
|
|
270
|
+
function isApiDecorator(decorator) {
|
|
271
|
+
return decorator.name === API_DECORATOR$2;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/*
|
|
275
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
276
|
+
* All rights reserved.
|
|
277
|
+
* SPDX-License-Identifier: MIT
|
|
278
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
279
|
+
*/
|
|
280
|
+
const { TRACK_DECORATOR: TRACK_DECORATOR$2 } = LWC_PACKAGE_EXPORTS;
|
|
281
|
+
function validateConflict(path, decorators, state) {
|
|
282
|
+
const isPublicFieldTracked = decorators.some((decorator) => decorator.name === TRACK_DECORATOR$2 &&
|
|
283
|
+
decorator.path.parentPath.node === path.parentPath.node);
|
|
284
|
+
if (isPublicFieldTracked) {
|
|
285
|
+
handleError(path, {
|
|
286
|
+
errorInfo: errors.DecoratorErrors.API_AND_TRACK_DECORATOR_CONFLICT,
|
|
287
|
+
}, state);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function isBooleanPropDefaultTrue(property) {
|
|
291
|
+
const propertyValue = property.node.value;
|
|
292
|
+
return propertyValue && propertyValue.type === 'BooleanLiteral' && propertyValue.value;
|
|
293
|
+
}
|
|
294
|
+
function validatePropertyValue(property, state) {
|
|
295
|
+
if (isBooleanPropDefaultTrue(property)) {
|
|
296
|
+
handleError(property, {
|
|
297
|
+
errorInfo: errors.DecoratorErrors.INVALID_BOOLEAN_PUBLIC_PROPERTY,
|
|
298
|
+
}, state);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function validatePropertyName(property, state) {
|
|
302
|
+
if (property.node.computed) {
|
|
303
|
+
handleError(property, {
|
|
304
|
+
errorInfo: errors.DecoratorErrors.PROPERTY_CANNOT_BE_COMPUTED,
|
|
305
|
+
}, state);
|
|
306
|
+
}
|
|
307
|
+
const propertyName = property.get('key.name').node;
|
|
308
|
+
if (propertyName === 'part') {
|
|
309
|
+
handleError(property, {
|
|
310
|
+
errorInfo: errors.DecoratorErrors.PROPERTY_NAME_PART_IS_RESERVED,
|
|
311
|
+
messageArgs: [propertyName],
|
|
312
|
+
}, state);
|
|
313
|
+
}
|
|
314
|
+
else if (propertyName.startsWith('on')) {
|
|
315
|
+
handleError(property, {
|
|
316
|
+
errorInfo: errors.DecoratorErrors.PROPERTY_NAME_CANNOT_START_WITH_ON,
|
|
317
|
+
messageArgs: [propertyName],
|
|
318
|
+
}, state);
|
|
319
|
+
}
|
|
320
|
+
else if (propertyName.startsWith('data') && propertyName.length > 4) {
|
|
321
|
+
handleError(property, {
|
|
322
|
+
errorInfo: errors.DecoratorErrors.PROPERTY_NAME_CANNOT_START_WITH_DATA,
|
|
323
|
+
messageArgs: [propertyName],
|
|
324
|
+
}, state);
|
|
325
|
+
}
|
|
326
|
+
else if (shared.DISALLOWED_PROP_SET.has(propertyName)) {
|
|
327
|
+
handleError(property, {
|
|
328
|
+
errorInfo: errors.DecoratorErrors.PROPERTY_NAME_IS_RESERVED,
|
|
329
|
+
messageArgs: [propertyName],
|
|
330
|
+
}, state);
|
|
331
|
+
}
|
|
332
|
+
else if (shared.AMBIGUOUS_PROP_SET.has(propertyName)) {
|
|
333
|
+
const camelCased = shared.AMBIGUOUS_PROP_SET.get(propertyName);
|
|
334
|
+
handleError(property, {
|
|
335
|
+
errorInfo: errors.DecoratorErrors.PROPERTY_NAME_IS_AMBIGUOUS,
|
|
336
|
+
messageArgs: [propertyName, camelCased],
|
|
337
|
+
}, state);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function validateSingleApiDecoratorOnSetterGetterPair(decorators, state) {
|
|
341
|
+
// keep track of visited class methods
|
|
342
|
+
const visitedMethods = new Set();
|
|
343
|
+
decorators.forEach((decorator) => {
|
|
344
|
+
const { path, decoratedNodeType } = decorator;
|
|
345
|
+
// since we are validating get/set we only look at @api methods
|
|
346
|
+
if (isApiDecorator(decorator) &&
|
|
347
|
+
(decoratedNodeType === DECORATOR_TYPES.GETTER ||
|
|
348
|
+
decoratedNodeType === DECORATOR_TYPES.SETTER)) {
|
|
349
|
+
const methodPath = path.parentPath;
|
|
350
|
+
const methodName = methodPath.get('key.name').node;
|
|
351
|
+
if (visitedMethods.has(methodName)) {
|
|
352
|
+
handleError(methodPath, {
|
|
353
|
+
errorInfo: errors.DecoratorErrors.SINGLE_DECORATOR_ON_SETTER_GETTER_PAIR,
|
|
354
|
+
messageArgs: [methodName],
|
|
355
|
+
}, state);
|
|
356
|
+
}
|
|
357
|
+
visitedMethods.add(methodName);
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
function validateUniqueness(decorators, state) {
|
|
362
|
+
const apiDecorators = decorators.filter(isApiDecorator);
|
|
363
|
+
for (let i = 0; i < apiDecorators.length; i++) {
|
|
364
|
+
const { path: currentPath, type: currentType } = apiDecorators[i];
|
|
365
|
+
const currentPropertyName = currentPath.parentPath.get('key.name').node;
|
|
366
|
+
for (let j = 0; j < apiDecorators.length; j++) {
|
|
367
|
+
const { path: comparePath, type: compareType } = apiDecorators[j];
|
|
368
|
+
const comparePropertyName = comparePath.parentPath.get('key.name')
|
|
369
|
+
.node;
|
|
370
|
+
// We will throw if the considered properties have the same name, and when their
|
|
371
|
+
// are not part of a pair of getter/setter.
|
|
372
|
+
const haveSameName = currentPropertyName === comparePropertyName;
|
|
373
|
+
const isDifferentProperty = currentPath !== comparePath;
|
|
374
|
+
const isGetterSetterPair = (currentType === DECORATOR_TYPES.GETTER &&
|
|
375
|
+
compareType === DECORATOR_TYPES.SETTER) ||
|
|
376
|
+
(currentType === DECORATOR_TYPES.SETTER && compareType === DECORATOR_TYPES.GETTER);
|
|
377
|
+
if (haveSameName && isDifferentProperty && !isGetterSetterPair) {
|
|
378
|
+
handleError(comparePath, {
|
|
379
|
+
errorInfo: errors.DecoratorErrors.DUPLICATE_API_PROPERTY,
|
|
380
|
+
messageArgs: [currentPropertyName],
|
|
381
|
+
}, state);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
function validate$3(decorators, state) {
|
|
387
|
+
const apiDecorators = decorators.filter(isApiDecorator);
|
|
388
|
+
if (apiDecorators.length === 0) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
apiDecorators.forEach(({ path, decoratedNodeType }) => {
|
|
392
|
+
validateConflict(path, decorators, state);
|
|
393
|
+
if (decoratedNodeType !== DECORATOR_TYPES.METHOD) {
|
|
394
|
+
const property = path.parentPath;
|
|
395
|
+
validatePropertyName(property, state);
|
|
396
|
+
validatePropertyValue(property, state);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
validateSingleApiDecoratorOnSetterGetterPair(decorators, state);
|
|
400
|
+
validateUniqueness(decorators, state);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/*
|
|
404
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
405
|
+
* All rights reserved.
|
|
406
|
+
* SPDX-License-Identifier: MIT
|
|
407
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
408
|
+
*/
|
|
409
|
+
const { PUBLIC_PROPS, PUBLIC_METHODS } = LWC_COMPONENT_PROPERTIES;
|
|
410
|
+
const PUBLIC_PROP_BIT_MASK = {
|
|
411
|
+
PROPERTY: 0,
|
|
412
|
+
GETTER: 1,
|
|
413
|
+
SETTER: 2,
|
|
414
|
+
};
|
|
415
|
+
function getPropertyBitmask(type) {
|
|
416
|
+
switch (type) {
|
|
417
|
+
case DECORATOR_TYPES.GETTER:
|
|
418
|
+
return PUBLIC_PROP_BIT_MASK.GETTER;
|
|
419
|
+
case DECORATOR_TYPES.SETTER:
|
|
420
|
+
return PUBLIC_PROP_BIT_MASK.SETTER;
|
|
421
|
+
default:
|
|
422
|
+
return PUBLIC_PROP_BIT_MASK.PROPERTY;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
function getSiblingGetSetPairType(propertyName, type, classBodyItems) {
|
|
426
|
+
const siblingKind = type === DECORATOR_TYPES.GETTER ? 'set' : 'get';
|
|
427
|
+
const siblingNode = classBodyItems.find((classBodyItem) => {
|
|
428
|
+
const isClassMethod = classBodyItem.isClassMethod({ kind: siblingKind });
|
|
429
|
+
const isSamePropertyName = classBodyItem.node.key.name ===
|
|
430
|
+
propertyName;
|
|
431
|
+
return isClassMethod && isSamePropertyName;
|
|
432
|
+
});
|
|
433
|
+
if (siblingNode) {
|
|
434
|
+
return siblingKind === 'get' ? DECORATOR_TYPES.GETTER : DECORATOR_TYPES.SETTER;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function computePublicPropsConfig(publicPropertyMetas, classBodyItems, state) {
|
|
438
|
+
return publicPropertyMetas.reduce((acc, { propertyName, decoratedNodeType }) => {
|
|
439
|
+
// This should never happen as we filter null in class visitor and
|
|
440
|
+
// collect appropriate errors in errorRecoveryMode || throw otherwise
|
|
441
|
+
if (isErrorRecoveryMode(state) && !decoratedNodeType)
|
|
442
|
+
return acc;
|
|
443
|
+
if (!(propertyName in acc)) {
|
|
444
|
+
acc[propertyName] = {};
|
|
445
|
+
}
|
|
446
|
+
acc[propertyName].config |= getPropertyBitmask(decoratedNodeType);
|
|
447
|
+
if (decoratedNodeType === DECORATOR_TYPES.GETTER ||
|
|
448
|
+
decoratedNodeType === DECORATOR_TYPES.SETTER) {
|
|
449
|
+
// With the latest decorator spec, only one of the getter/setter pair needs a decorator.
|
|
450
|
+
// We need to add the proper bitmask for the sibling getter/setter if it exists.
|
|
451
|
+
const pairType = getSiblingGetSetPairType(propertyName, decoratedNodeType, classBodyItems);
|
|
452
|
+
if (pairType) {
|
|
453
|
+
acc[propertyName].config |= getPropertyBitmask(pairType);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return acc;
|
|
457
|
+
}, {});
|
|
458
|
+
}
|
|
459
|
+
function transform$2(t, decoratorMetas, classBodyItems, state) {
|
|
460
|
+
const objectProperties = [];
|
|
461
|
+
const apiDecoratorMetas = decoratorMetas.filter(isApiDecorator);
|
|
462
|
+
const publicPropertyMetas = apiDecoratorMetas.filter(({ decoratedNodeType }) => decoratedNodeType !== DECORATOR_TYPES.METHOD);
|
|
463
|
+
if (publicPropertyMetas.length) {
|
|
464
|
+
const propsConfig = computePublicPropsConfig(publicPropertyMetas, classBodyItems, state);
|
|
465
|
+
objectProperties.push(t.objectProperty(t.identifier(PUBLIC_PROPS), t.valueToNode(propsConfig)));
|
|
466
|
+
}
|
|
467
|
+
const publicMethodMetas = apiDecoratorMetas.filter(({ decoratedNodeType }) => decoratedNodeType === DECORATOR_TYPES.METHOD);
|
|
468
|
+
if (publicMethodMetas.length) {
|
|
469
|
+
const methodNames = publicMethodMetas.map(({ propertyName }) => propertyName);
|
|
470
|
+
objectProperties.push(t.objectProperty(t.identifier(PUBLIC_METHODS), t.valueToNode(methodNames)));
|
|
471
|
+
}
|
|
472
|
+
return objectProperties;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/*
|
|
476
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
477
|
+
* All rights reserved.
|
|
478
|
+
* SPDX-License-Identifier: MIT
|
|
479
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
480
|
+
*/
|
|
481
|
+
const { API_DECORATOR: API_DECORATOR$1 } = LWC_PACKAGE_EXPORTS;
|
|
482
|
+
var api = {
|
|
483
|
+
name: API_DECORATOR$1,
|
|
484
|
+
validate: validate$3,
|
|
485
|
+
transform: transform$2,
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
/*
|
|
489
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
490
|
+
* All rights reserved.
|
|
491
|
+
* SPDX-License-Identifier: MIT
|
|
492
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
493
|
+
*/
|
|
494
|
+
const { WIRE_DECORATOR: WIRE_DECORATOR$2 } = LWC_PACKAGE_EXPORTS;
|
|
495
|
+
function isWireDecorator(decorator) {
|
|
496
|
+
return decorator.name === WIRE_DECORATOR$2;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/*
|
|
500
|
+
* Copyright (c) 2024, salesforce.com, inc.
|
|
501
|
+
* All rights reserved.
|
|
502
|
+
* SPDX-License-Identifier: MIT
|
|
503
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
504
|
+
*/
|
|
505
|
+
const { TRACK_DECORATOR: TRACK_DECORATOR$1, WIRE_DECORATOR: WIRE_DECORATOR$1, API_DECORATOR } = LWC_PACKAGE_EXPORTS;
|
|
506
|
+
function validateWireId(id, path, state) {
|
|
507
|
+
if (!id) {
|
|
508
|
+
handleError(path, {
|
|
509
|
+
errorInfo: errors.DecoratorErrors.ADAPTER_SHOULD_BE_FIRST_PARAMETER,
|
|
510
|
+
}, state);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
let adapter;
|
|
514
|
+
if (id.isIdentifier()) {
|
|
515
|
+
// @wire(adapter)
|
|
516
|
+
adapter = id;
|
|
517
|
+
}
|
|
518
|
+
else if (id.isMemberExpression()) {
|
|
519
|
+
if (id.node.computed) {
|
|
520
|
+
// @wire(adapter[computed])
|
|
521
|
+
handleError(id, {
|
|
522
|
+
errorInfo: errors.DecoratorErrors.FUNCTION_IDENTIFIER_CANNOT_HAVE_COMPUTED_PROPS,
|
|
523
|
+
}, state);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
const object = id.get('object');
|
|
527
|
+
if (object.isIdentifier()) {
|
|
528
|
+
// @wire(adapter.foo)
|
|
529
|
+
adapter = object;
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
// @wire(adapter.foo.bar)
|
|
533
|
+
handleError(id, {
|
|
534
|
+
errorInfo: errors.DecoratorErrors.FUNCTION_IDENTIFIER_CANNOT_HAVE_NESTED_MEMBER_EXRESSIONS,
|
|
535
|
+
}, state);
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
else {
|
|
540
|
+
// @wire(1), @wire('adapter'), @wire(function adapter() {}), etc.
|
|
541
|
+
handleError(id, {
|
|
542
|
+
errorInfo: errors.DecoratorErrors.FUNCTION_IDENTIFIER_SHOULD_BE_FIRST_PARAMETER,
|
|
543
|
+
}, state);
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
// Ensure wire adapter is imported (check for member expression or identifier)
|
|
547
|
+
const adapterBinding = path.scope.getBinding(adapter.node.name);
|
|
548
|
+
if (!adapterBinding) {
|
|
549
|
+
handleError(id, {
|
|
550
|
+
errorInfo: errors.DecoratorErrors.WIRE_ADAPTER_SHOULD_BE_IMPORTED,
|
|
551
|
+
messageArgs: [adapter.node.name],
|
|
552
|
+
}, state);
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
// ensure wire adapter is a first parameter
|
|
556
|
+
if (!adapterBinding.path.isImportSpecifier() &&
|
|
557
|
+
!adapterBinding.path.isImportDefaultSpecifier()) {
|
|
558
|
+
handleError(id, {
|
|
559
|
+
errorInfo: errors.DecoratorErrors.IMPORTED_FUNCTION_IDENTIFIER_SHOULD_BE_FIRST_PARAMETER,
|
|
560
|
+
}, state);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
function validateWireConfig(config, path, state) {
|
|
564
|
+
if (!config.isObjectExpression()) {
|
|
565
|
+
handleError(config, {
|
|
566
|
+
errorInfo: errors.DecoratorErrors.CONFIG_OBJECT_SHOULD_BE_SECOND_PARAMETER,
|
|
567
|
+
}, state);
|
|
568
|
+
}
|
|
569
|
+
const properties = config.get('properties');
|
|
570
|
+
if (Array.isArray(properties)) {
|
|
571
|
+
for (const prop of properties) {
|
|
572
|
+
// Only validate {[computed]: true} object properties; {static: true} props are all valid
|
|
573
|
+
// and we ignore {...spreads} and {methods(){}}
|
|
574
|
+
if (!prop.isObjectProperty() || !prop.node.computed)
|
|
575
|
+
continue;
|
|
576
|
+
const key = prop.get('key');
|
|
577
|
+
if (key.isIdentifier()) {
|
|
578
|
+
// Only allow identifiers that originated from a `const` declaration
|
|
579
|
+
const binding = key.scope.getBinding(key.node.name);
|
|
580
|
+
// TODO [#3956]: Investigate allowing imported constants
|
|
581
|
+
if (binding?.kind === 'const')
|
|
582
|
+
continue;
|
|
583
|
+
// By default, the identifier `undefined` has no binding (when it's actually undefined),
|
|
584
|
+
// but has a binding if it's used as a variable (e.g. `let undefined = "don't do this"`)
|
|
585
|
+
if (key.node.name === 'undefined' && !binding)
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
else if (key.isLiteral()) {
|
|
589
|
+
// A literal can be a regexp, template literal, or primitive; only allow primitives
|
|
590
|
+
if (key.isTemplateLiteral()) {
|
|
591
|
+
// A template literal is not guaranteed to always result in the same value
|
|
592
|
+
// (e.g. `${Math.random()}`), so we disallow them entirely.
|
|
593
|
+
// TODO [#3956]: Investigate allowing template literals
|
|
594
|
+
handleError(key, {
|
|
595
|
+
errorInfo: errors.DecoratorErrors.COMPUTED_PROPERTY_CANNOT_BE_TEMPLATE_LITERAL,
|
|
596
|
+
}, state);
|
|
597
|
+
}
|
|
598
|
+
else if (!key.isRegExpLiteral()) {
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
handleError(key, {
|
|
603
|
+
errorInfo: errors.DecoratorErrors.COMPUTED_PROPERTY_MUST_BE_CONSTANT_OR_LITERAL,
|
|
604
|
+
}, state);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
function validateWireParameters(path, state) {
|
|
609
|
+
const expressionArguments = path.get('expression.arguments');
|
|
610
|
+
if (Array.isArray(expressionArguments)) {
|
|
611
|
+
// Multiple arguments: should be [id, config?]
|
|
612
|
+
const [id, config] = expressionArguments;
|
|
613
|
+
validateWireId(id, path, state);
|
|
614
|
+
if (config)
|
|
615
|
+
validateWireConfig(config, path, state);
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
// Single argument: should just be id
|
|
619
|
+
validateWireId(expressionArguments, path, state);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
function validateUsageWithOtherDecorators(path, decorators, state) {
|
|
623
|
+
decorators.forEach((decorator) => {
|
|
624
|
+
if (path !== decorator.path &&
|
|
625
|
+
decorator.name === WIRE_DECORATOR$1 &&
|
|
626
|
+
decorator.path.parentPath.node === path.parentPath.node) {
|
|
627
|
+
handleError(path, {
|
|
628
|
+
errorInfo: errors.DecoratorErrors.ONE_WIRE_DECORATOR_ALLOWED,
|
|
629
|
+
}, state);
|
|
630
|
+
}
|
|
631
|
+
if ((decorator.name === API_DECORATOR || decorator.name === TRACK_DECORATOR$1) &&
|
|
632
|
+
decorator.path.parentPath.node === path.parentPath.node) {
|
|
633
|
+
handleError(path, {
|
|
634
|
+
errorInfo: errors.DecoratorErrors.CONFLICT_WITH_ANOTHER_DECORATOR,
|
|
635
|
+
messageArgs: [decorator.name],
|
|
636
|
+
}, state);
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
function validate$2(decorators, state) {
|
|
641
|
+
decorators.filter(isWireDecorator).forEach(({ path }) => {
|
|
642
|
+
validateUsageWithOtherDecorators(path, decorators, state);
|
|
643
|
+
validateWireParameters(path, state);
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/*
|
|
648
|
+
* Copyright (c) 2024, salesforce.com, inc.
|
|
649
|
+
* All rights reserved.
|
|
650
|
+
* SPDX-License-Identifier: MIT
|
|
651
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
652
|
+
*/
|
|
653
|
+
const WIRE_PARAM_PREFIX = '$';
|
|
654
|
+
const WIRE_CONFIG_ARG_NAME = '$cmp';
|
|
655
|
+
function isObservedProperty(configProperty) {
|
|
656
|
+
const propertyValue = configProperty.get('value');
|
|
657
|
+
return (propertyValue.isStringLiteral() && propertyValue.node.value.startsWith(WIRE_PARAM_PREFIX));
|
|
658
|
+
}
|
|
659
|
+
function getWiredStatic(wireConfig, state) {
|
|
660
|
+
const properties = wireConfig.get('properties');
|
|
661
|
+
// Should only occurs in error recovery mode when config validation has already failed
|
|
662
|
+
// Skip processing since the error has been logged upstream
|
|
663
|
+
if (isErrorRecoveryMode(state) && !Array.isArray(properties)) {
|
|
664
|
+
return [];
|
|
665
|
+
}
|
|
666
|
+
return properties
|
|
667
|
+
.filter((property) => !isObservedProperty(property))
|
|
668
|
+
.map((path) => path.node);
|
|
669
|
+
}
|
|
670
|
+
function getWiredParams(t, wireConfig, state) {
|
|
671
|
+
const properties = wireConfig.get('properties');
|
|
672
|
+
// Should only occur in error recovery mode when config validation has already failed
|
|
673
|
+
// Skip processing since the error has been logged upstream
|
|
674
|
+
if (isErrorRecoveryMode(state) && !Array.isArray(properties)) {
|
|
675
|
+
// In error recovery mode, return empty array instead of crashing
|
|
676
|
+
return [];
|
|
677
|
+
}
|
|
678
|
+
return properties
|
|
679
|
+
.filter((property) => isObservedProperty(property))
|
|
680
|
+
.map((path) => {
|
|
681
|
+
// Need to clone deep the observed property to remove the param prefix
|
|
682
|
+
const clonedProperty = t.cloneNode(path.node);
|
|
683
|
+
clonedProperty.value.value = clonedProperty.value.value.slice(1);
|
|
684
|
+
return clonedProperty;
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
function getGeneratedConfig(t, wiredValue) {
|
|
688
|
+
let counter = 0;
|
|
689
|
+
const configBlockBody = [];
|
|
690
|
+
const configProps = [];
|
|
691
|
+
const generateParameterConfigValue = (memberExprPaths) => {
|
|
692
|
+
// Note: When memberExprPaths ($foo.bar) has an invalid identifier (eg: foo..bar, foo.bar[3])
|
|
693
|
+
// it should (ideally) resolve in a compilation error during validation phase.
|
|
694
|
+
// This is not possible due that platform components may have a param definition which is invalid
|
|
695
|
+
// but passes compilation, and throwing at compile time would break such components.
|
|
696
|
+
// In such cases where the param does not have proper notation, the config generated will use the bracket
|
|
697
|
+
// notation to match the current behavior (that most likely end up resolving that param as undefined).
|
|
698
|
+
const isInvalidMemberExpr = memberExprPaths.some((maybeIdentifier) => !(t.isValidES3Identifier(maybeIdentifier) && maybeIdentifier.length > 0));
|
|
699
|
+
const memberExprPropertyGen = !isInvalidMemberExpr
|
|
700
|
+
? t.identifier
|
|
701
|
+
: t.StringLiteral;
|
|
702
|
+
if (memberExprPaths.length === 1) {
|
|
703
|
+
return {
|
|
704
|
+
configValueExpression: t.memberExpression(t.identifier(WIRE_CONFIG_ARG_NAME), memberExprPropertyGen(memberExprPaths[0])),
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
const varName = 'v' + ++counter;
|
|
708
|
+
const varDeclaration = t.variableDeclaration('let', [
|
|
709
|
+
t.variableDeclarator(t.identifier(varName), t.memberExpression(t.identifier(WIRE_CONFIG_ARG_NAME), memberExprPropertyGen(memberExprPaths[0]), isInvalidMemberExpr)),
|
|
710
|
+
]);
|
|
711
|
+
// Results in: v != null && ... (v = v.i) != null && ... (v = v.(n-1)) != null
|
|
712
|
+
let conditionTest = t.binaryExpression('!=', t.identifier(varName), t.nullLiteral());
|
|
713
|
+
for (let i = 1, n = memberExprPaths.length; i < n - 1; i++) {
|
|
714
|
+
const nextPropValue = t.assignmentExpression('=', t.identifier(varName), t.memberExpression(t.identifier(varName), memberExprPropertyGen(memberExprPaths[i]), isInvalidMemberExpr));
|
|
715
|
+
conditionTest = t.logicalExpression('&&', conditionTest, t.binaryExpression('!=', nextPropValue, t.nullLiteral()));
|
|
716
|
+
}
|
|
717
|
+
// conditionTest ? v.n : undefined
|
|
718
|
+
const configValueExpression = t.conditionalExpression(conditionTest, t.memberExpression(t.identifier(varName), memberExprPropertyGen(memberExprPaths[memberExprPaths.length - 1]), isInvalidMemberExpr), t.identifier('undefined'));
|
|
719
|
+
return {
|
|
720
|
+
varDeclaration,
|
|
721
|
+
configValueExpression,
|
|
722
|
+
};
|
|
723
|
+
};
|
|
724
|
+
if (wiredValue.static) {
|
|
725
|
+
Array.prototype.push.apply(configProps, wiredValue.static);
|
|
726
|
+
}
|
|
727
|
+
if (wiredValue.params) {
|
|
728
|
+
wiredValue.params.forEach((param) => {
|
|
729
|
+
const memberExprPaths = param.value.value.split('.');
|
|
730
|
+
const paramConfigValue = generateParameterConfigValue(memberExprPaths);
|
|
731
|
+
configProps.push(t.objectProperty(param.key, paramConfigValue.configValueExpression, param.computed));
|
|
732
|
+
if (paramConfigValue.varDeclaration) {
|
|
733
|
+
configBlockBody.push(paramConfigValue.varDeclaration);
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
configBlockBody.push(t.returnStatement(t.objectExpression(configProps)));
|
|
738
|
+
const fnExpression = t.functionExpression(null, [t.identifier(WIRE_CONFIG_ARG_NAME)], t.blockStatement(configBlockBody));
|
|
739
|
+
return t.objectProperty(t.identifier('config'), fnExpression);
|
|
740
|
+
}
|
|
741
|
+
function buildWireConfigValue(t, wiredValues) {
|
|
742
|
+
return t.objectExpression(wiredValues.map((wiredValue) => {
|
|
743
|
+
const wireConfig = [];
|
|
744
|
+
if (wiredValue.adapter) {
|
|
745
|
+
wireConfig.push(t.objectProperty(t.identifier('adapter'), wiredValue.adapter.expression));
|
|
746
|
+
}
|
|
747
|
+
if (wiredValue.params) {
|
|
748
|
+
const dynamicParamNames = wiredValue.params.map((p) => {
|
|
749
|
+
if (t.isIdentifier(p.key)) {
|
|
750
|
+
return p.computed ? t.identifier(p.key.name) : t.stringLiteral(p.key.name);
|
|
751
|
+
}
|
|
752
|
+
else if (t.isLiteral(p.key) &&
|
|
753
|
+
// Template literals may contain expressions, so they are not allowed
|
|
754
|
+
!t.isTemplateLiteral(p.key) &&
|
|
755
|
+
// RegExp are not primitives, so they are not allowed
|
|
756
|
+
!t.isRegExpLiteral(p.key)) {
|
|
757
|
+
const value = t.isNullLiteral(p.key) ? null : p.key.value;
|
|
758
|
+
return t.stringLiteral(String(value));
|
|
759
|
+
}
|
|
760
|
+
// If it's not an identifier or primitive literal then it's a computed expression
|
|
761
|
+
throw new TypeError(`Expected object property key to be an identifier or a literal, but instead saw "${p.key.type}".`);
|
|
762
|
+
});
|
|
763
|
+
wireConfig.push(t.objectProperty(t.identifier('dynamic'), t.arrayExpression(dynamicParamNames)));
|
|
764
|
+
}
|
|
765
|
+
if (wiredValue.isClassMethod) {
|
|
766
|
+
wireConfig.push(t.objectProperty(t.identifier('method'), t.numericLiteral(1)));
|
|
767
|
+
}
|
|
768
|
+
wireConfig.push(getGeneratedConfig(t, wiredValue));
|
|
769
|
+
return t.objectProperty(t.identifier(wiredValue.propertyName), t.objectExpression(wireConfig));
|
|
770
|
+
}));
|
|
771
|
+
}
|
|
772
|
+
const SUPPORTED_VALUE_TO_TYPE_MAP = {
|
|
773
|
+
StringLiteral: 'string',
|
|
774
|
+
NumericLiteral: 'number',
|
|
775
|
+
BooleanLiteral: 'boolean',
|
|
776
|
+
};
|
|
777
|
+
const scopedReferenceLookup = (scope) => (name) => {
|
|
778
|
+
const binding = scope.getBinding(name);
|
|
779
|
+
let type;
|
|
780
|
+
let value;
|
|
781
|
+
if (binding) {
|
|
782
|
+
if (binding.kind === 'module') {
|
|
783
|
+
// Resolves module import to the name of the module imported
|
|
784
|
+
// e.g. import { foo } from 'bar' gives value 'bar' for `name == 'foo'
|
|
785
|
+
const parentPathNode = binding.path.parentPath.node;
|
|
786
|
+
if (parentPathNode && parentPathNode.source) {
|
|
787
|
+
type = 'module';
|
|
788
|
+
value = parentPathNode.source.value;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
else if (binding.kind === 'const') {
|
|
792
|
+
// Resolves `const foo = 'text';` references to value 'text', where `name == 'foo'`
|
|
793
|
+
const init = binding.path.node.init;
|
|
794
|
+
if (init &&
|
|
795
|
+
SUPPORTED_VALUE_TO_TYPE_MAP[init.type]) {
|
|
796
|
+
type =
|
|
797
|
+
SUPPORTED_VALUE_TO_TYPE_MAP[init.type];
|
|
798
|
+
value = init
|
|
799
|
+
.value;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
return {
|
|
804
|
+
type,
|
|
805
|
+
value,
|
|
806
|
+
};
|
|
807
|
+
};
|
|
808
|
+
function transform$1(t, decoratorMetas, state) {
|
|
809
|
+
const objectProperties = [];
|
|
810
|
+
const wiredValues = decoratorMetas.filter(isWireDecorator).map(({ path }) => {
|
|
811
|
+
const [id, config] = path.get('expression.arguments');
|
|
812
|
+
const propertyName = path.parentPath.get('key.name').node;
|
|
813
|
+
const isClassMethod = path.parentPath.isClassMethod({
|
|
814
|
+
kind: 'method',
|
|
815
|
+
});
|
|
816
|
+
const wiredValue = {
|
|
817
|
+
propertyName,
|
|
818
|
+
isClassMethod,
|
|
819
|
+
};
|
|
820
|
+
if (config) {
|
|
821
|
+
wiredValue.static = getWiredStatic(config, state);
|
|
822
|
+
wiredValue.params = getWiredParams(t, config, state);
|
|
823
|
+
}
|
|
824
|
+
const referenceLookup = scopedReferenceLookup(path.scope);
|
|
825
|
+
const isMemberExpression = id.isMemberExpression();
|
|
826
|
+
const isIdentifier = id.isIdentifier();
|
|
827
|
+
if (isIdentifier || isMemberExpression) {
|
|
828
|
+
const referenceName = isMemberExpression ? id.node.object.name : id.node.name;
|
|
829
|
+
const reference = referenceLookup(referenceName);
|
|
830
|
+
wiredValue.adapter = {
|
|
831
|
+
name: referenceName,
|
|
832
|
+
expression: t.cloneNode(id.node),
|
|
833
|
+
reference: reference.type === 'module' ? reference.value : undefined,
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
return wiredValue;
|
|
837
|
+
});
|
|
838
|
+
if (wiredValues.length) {
|
|
839
|
+
objectProperties.push(t.objectProperty(t.identifier(LWC_COMPONENT_PROPERTIES.WIRE), buildWireConfigValue(t, wiredValues)));
|
|
840
|
+
}
|
|
841
|
+
return objectProperties;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/*
|
|
845
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
846
|
+
* All rights reserved.
|
|
847
|
+
* SPDX-License-Identifier: MIT
|
|
848
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
849
|
+
*/
|
|
850
|
+
const { WIRE_DECORATOR } = LWC_PACKAGE_EXPORTS;
|
|
851
|
+
var wire = {
|
|
852
|
+
name: WIRE_DECORATOR,
|
|
853
|
+
validate: validate$2,
|
|
854
|
+
transform: transform$1,
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
/*
|
|
858
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
859
|
+
* All rights reserved.
|
|
860
|
+
* SPDX-License-Identifier: MIT
|
|
861
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
862
|
+
*/
|
|
863
|
+
const { TRACK_DECORATOR } = LWC_PACKAGE_EXPORTS;
|
|
864
|
+
const TRACK_PROPERTY_VALUE = 1;
|
|
865
|
+
function isTrackDecorator(decorator) {
|
|
866
|
+
return decorator.name === TRACK_DECORATOR;
|
|
867
|
+
}
|
|
868
|
+
function validate$1(decorators, state) {
|
|
869
|
+
decorators.filter(isTrackDecorator).forEach(({ path }) => {
|
|
870
|
+
if (!path.parentPath.isClassProperty()) {
|
|
871
|
+
handleError(path, {
|
|
872
|
+
errorInfo: errors.DecoratorErrors.TRACK_ONLY_ALLOWED_ON_CLASS_PROPERTIES,
|
|
873
|
+
}, state);
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
function transform(t, decoratorMetas) {
|
|
878
|
+
const objectProperties = [];
|
|
879
|
+
const trackDecoratorMetas = decoratorMetas.filter(isTrackDecorator);
|
|
880
|
+
if (trackDecoratorMetas.length) {
|
|
881
|
+
const config = trackDecoratorMetas.reduce((acc, meta) => {
|
|
882
|
+
acc[meta.propertyName] = TRACK_PROPERTY_VALUE;
|
|
883
|
+
return acc;
|
|
884
|
+
}, {});
|
|
885
|
+
objectProperties.push(t.objectProperty(t.identifier(LWC_COMPONENT_PROPERTIES.TRACK), t.valueToNode(config)));
|
|
886
|
+
}
|
|
887
|
+
return objectProperties;
|
|
888
|
+
}
|
|
889
|
+
var track = {
|
|
890
|
+
name: TRACK_DECORATOR,
|
|
891
|
+
transform,
|
|
892
|
+
validate: validate$1,
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
/*
|
|
896
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
897
|
+
* All rights reserved.
|
|
898
|
+
* SPDX-License-Identifier: MIT
|
|
899
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
900
|
+
*/
|
|
901
|
+
const DECORATOR_TRANSFORMS = [api, wire, track];
|
|
902
|
+
const AVAILABLE_DECORATORS = DECORATOR_TRANSFORMS.map((transform) => transform.name).join(', ');
|
|
903
|
+
function isLwcDecoratorName(name) {
|
|
904
|
+
return DECORATOR_TRANSFORMS.some((transform) => transform.name === name);
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* Returns a list of all the references to an identifier
|
|
908
|
+
* @param identifier
|
|
909
|
+
*/
|
|
910
|
+
function getReferences(identifier) {
|
|
911
|
+
return identifier.scope.getBinding(identifier.node.name).referencePaths;
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Returns the type of decorator depdending on the property or method if get applied to
|
|
915
|
+
* @param decoratorPath
|
|
916
|
+
* @param state
|
|
917
|
+
*/
|
|
918
|
+
function getDecoratedNodeType(decoratorPath, state) {
|
|
919
|
+
const propertyOrMethod = decoratorPath.parentPath;
|
|
920
|
+
if (isClassMethod(propertyOrMethod)) {
|
|
921
|
+
return DECORATOR_TYPES.METHOD;
|
|
922
|
+
}
|
|
923
|
+
else if (isGetterClassMethod(propertyOrMethod)) {
|
|
924
|
+
return DECORATOR_TYPES.GETTER;
|
|
925
|
+
}
|
|
926
|
+
else if (isSetterClassMethod(propertyOrMethod)) {
|
|
927
|
+
return DECORATOR_TYPES.SETTER;
|
|
928
|
+
}
|
|
929
|
+
else if (propertyOrMethod.isClassProperty()) {
|
|
930
|
+
return DECORATOR_TYPES.PROPERTY;
|
|
931
|
+
}
|
|
932
|
+
handleError(propertyOrMethod, {
|
|
933
|
+
errorInfo: errors.DecoratorErrors.INVALID_DECORATOR_TYPE,
|
|
934
|
+
}, state);
|
|
935
|
+
// We should only be here when we are running in errorRecoveryMode
|
|
936
|
+
// otherwise, the handleError method should already "throw"
|
|
937
|
+
// since, we couldn't determine a node type, we will return a null here
|
|
938
|
+
// so we can filter out this node and attempt to proceed with the compilation process
|
|
939
|
+
return null;
|
|
940
|
+
}
|
|
941
|
+
function validateImportedLwcDecoratorUsage(engineImportSpecifiers, state) {
|
|
942
|
+
engineImportSpecifiers
|
|
943
|
+
.filter(({ name }) => isLwcDecoratorName(name))
|
|
944
|
+
.reduce((acc, { name, path }) => {
|
|
945
|
+
// Get a list of all the local references
|
|
946
|
+
const local = path.get('imported');
|
|
947
|
+
const references = getReferences(local).map((reference) => ({
|
|
948
|
+
name,
|
|
949
|
+
reference,
|
|
950
|
+
}));
|
|
951
|
+
return [...acc, ...references];
|
|
952
|
+
}, [])
|
|
953
|
+
.forEach(({ name, reference }) => {
|
|
954
|
+
// Get the decorator from the identifier
|
|
955
|
+
// If the the decorator is:
|
|
956
|
+
// - an identifier @track : the decorator is the parent of the identifier
|
|
957
|
+
// - a call expression @wire("foo") : the decorator is the grand-parent of the identifier
|
|
958
|
+
const decorator = reference.parentPath.isDecorator()
|
|
959
|
+
? reference.parentPath
|
|
960
|
+
: reference.parentPath.parentPath;
|
|
961
|
+
if (!decorator.isDecorator()) {
|
|
962
|
+
handleError(decorator, {
|
|
963
|
+
errorInfo: errors.DecoratorErrors.IS_NOT_DECORATOR,
|
|
964
|
+
messageArgs: [name],
|
|
965
|
+
}, state);
|
|
966
|
+
}
|
|
967
|
+
const propertyOrMethod = decorator.parentPath;
|
|
968
|
+
if (propertyOrMethod === null ||
|
|
969
|
+
(!propertyOrMethod.isClassProperty() && !propertyOrMethod.isClassMethod())) {
|
|
970
|
+
handleError(propertyOrMethod === null ? decorator : propertyOrMethod, {
|
|
971
|
+
errorInfo: errors.DecoratorErrors.IS_NOT_CLASS_PROPERTY_OR_CLASS_METHOD,
|
|
972
|
+
messageArgs: [name],
|
|
973
|
+
}, state);
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
function isImportedFromLwcSource(bindingPath) {
|
|
978
|
+
return (bindingPath.isImportSpecifier() &&
|
|
979
|
+
bindingPath.parent.source.value === 'lwc');
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Validate the usage of decorator by calling each validation function
|
|
983
|
+
* @param decorators
|
|
984
|
+
* @param state
|
|
985
|
+
*/
|
|
986
|
+
function validate(decorators, state) {
|
|
987
|
+
for (const { name, path } of decorators) {
|
|
988
|
+
const binding = path.scope.getBinding(name);
|
|
989
|
+
if (binding === undefined || !isImportedFromLwcSource(binding.path)) {
|
|
990
|
+
handleInvalidDecoratorError(path, state);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
DECORATOR_TRANSFORMS.forEach(({ validate }) => validate(decorators, state));
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Remove import specifiers. It also removes the import statement if the specifier list becomes empty
|
|
997
|
+
* @param engineImportSpecifiers
|
|
998
|
+
*/
|
|
999
|
+
function removeImportedDecoratorSpecifiers(engineImportSpecifiers) {
|
|
1000
|
+
engineImportSpecifiers
|
|
1001
|
+
.filter(({ name }) => isLwcDecoratorName(name))
|
|
1002
|
+
.forEach(({ path }) => {
|
|
1003
|
+
const importStatement = path.parentPath;
|
|
1004
|
+
path.remove();
|
|
1005
|
+
if (importStatement.get('specifiers').length === 0) {
|
|
1006
|
+
importStatement.remove();
|
|
1007
|
+
}
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
function handleInvalidDecoratorError(path, state) {
|
|
1011
|
+
const expressionPath = path.get('expression');
|
|
1012
|
+
const { node } = path;
|
|
1013
|
+
const { expression } = node;
|
|
1014
|
+
let name;
|
|
1015
|
+
if (expressionPath.isIdentifier()) {
|
|
1016
|
+
name = expression.name;
|
|
1017
|
+
}
|
|
1018
|
+
else if (expressionPath.isCallExpression()) {
|
|
1019
|
+
name = expression.callee.name;
|
|
1020
|
+
}
|
|
1021
|
+
if (name) {
|
|
1022
|
+
handleError(path.parentPath, {
|
|
1023
|
+
errorInfo: errors.DecoratorErrors.INVALID_DECORATOR_WITH_NAME,
|
|
1024
|
+
messageArgs: [name, AVAILABLE_DECORATORS, LWC_PACKAGE_ALIAS],
|
|
1025
|
+
}, state);
|
|
1026
|
+
}
|
|
1027
|
+
else {
|
|
1028
|
+
handleError(path.parentPath, {
|
|
1029
|
+
errorInfo: errors.DecoratorErrors.INVALID_DECORATOR,
|
|
1030
|
+
messageArgs: [AVAILABLE_DECORATORS, LWC_PACKAGE_ALIAS],
|
|
1031
|
+
}, state);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
function collectDecoratorPaths(bodyItems) {
|
|
1035
|
+
return bodyItems.reduce((acc, bodyItem) => {
|
|
1036
|
+
const decorators = bodyItem.get('decorators');
|
|
1037
|
+
if (decorators && decorators.length) {
|
|
1038
|
+
acc.push(...decorators);
|
|
1039
|
+
}
|
|
1040
|
+
return acc;
|
|
1041
|
+
}, []);
|
|
1042
|
+
}
|
|
1043
|
+
function getDecoratorMetadata(decoratorPath, state) {
|
|
1044
|
+
const expressionPath = decoratorPath.get('expression');
|
|
1045
|
+
let name;
|
|
1046
|
+
if (expressionPath.isIdentifier()) {
|
|
1047
|
+
name = expressionPath.node.name;
|
|
1048
|
+
}
|
|
1049
|
+
else if (expressionPath.isCallExpression()) {
|
|
1050
|
+
name = expressionPath.node.callee.name;
|
|
1051
|
+
}
|
|
1052
|
+
else {
|
|
1053
|
+
handleInvalidDecoratorError(decoratorPath, state);
|
|
1054
|
+
return null;
|
|
1055
|
+
}
|
|
1056
|
+
const propertyName = decoratorPath.parent.key.name;
|
|
1057
|
+
const decoratedNodeType = getDecoratedNodeType(decoratorPath, state);
|
|
1058
|
+
return {
|
|
1059
|
+
name,
|
|
1060
|
+
propertyName,
|
|
1061
|
+
path: decoratorPath,
|
|
1062
|
+
decoratedNodeType,
|
|
1063
|
+
};
|
|
1064
|
+
}
|
|
1065
|
+
function getMetadataObjectPropertyList(t, decoratorMetas, classBodyItems, state) {
|
|
1066
|
+
const list = [
|
|
1067
|
+
...api.transform(t, decoratorMetas, classBodyItems, state),
|
|
1068
|
+
...track.transform(t, decoratorMetas),
|
|
1069
|
+
...wire.transform(t, decoratorMetas, state),
|
|
1070
|
+
];
|
|
1071
|
+
const fieldNames = classBodyItems
|
|
1072
|
+
.filter((field) => field.isClassProperty({ computed: false, static: false }))
|
|
1073
|
+
.filter((field) => !field.node.decorators)
|
|
1074
|
+
.map((field) => field.node.key.name);
|
|
1075
|
+
if (fieldNames.length) {
|
|
1076
|
+
list.push(t.objectProperty(t.identifier('fields'), t.valueToNode(fieldNames)));
|
|
1077
|
+
}
|
|
1078
|
+
return list;
|
|
1079
|
+
}
|
|
1080
|
+
function decorators({ types: t }) {
|
|
1081
|
+
function createRegisterDecoratorsCallExpression(path, classExpression, props) {
|
|
1082
|
+
const id = helperModuleImports.addNamed(path, REGISTER_DECORATORS_ID, LWC_PACKAGE_ALIAS);
|
|
1083
|
+
return t.callExpression(id, [classExpression, t.objectExpression(props)]);
|
|
1084
|
+
}
|
|
1085
|
+
// Babel reinvokes visitors for node reinsertion so we use this to avoid an infinite loop.
|
|
1086
|
+
const visitedClasses = new WeakSet();
|
|
1087
|
+
return {
|
|
1088
|
+
Class(path, state) {
|
|
1089
|
+
const { node } = path;
|
|
1090
|
+
if (visitedClasses.has(node)) {
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
visitedClasses.add(node);
|
|
1094
|
+
const classBodyItems = path.get('body.body');
|
|
1095
|
+
if (classBodyItems.length === 0) {
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
if (node.superClass === null &&
|
|
1099
|
+
shared.isAPIFeatureEnabled(4 /* APIFeature.SKIP_UNNECESSARY_REGISTER_DECORATORS */, shared.getAPIVersionFromNumber(state.opts.apiVersion))) {
|
|
1100
|
+
// Any class exposing a field *must* extend either LightningElement or some other superclass.
|
|
1101
|
+
// Even in the case of superclasses and mixins that expose fields, those must extend something as well.
|
|
1102
|
+
// So we can skip classes without a superclass to avoid adding unnecessary registerDecorators calls.
|
|
1103
|
+
// However, we only do this in later API versions to avoid a breaking change.
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
const decoratorPaths = collectDecoratorPaths(classBodyItems);
|
|
1107
|
+
const decoratorMetas = decoratorPaths
|
|
1108
|
+
.map((path) => getDecoratorMetadata(path, state))
|
|
1109
|
+
.filter((meta) => meta !== null);
|
|
1110
|
+
validate(decoratorMetas, state);
|
|
1111
|
+
const metaPropertyList = getMetadataObjectPropertyList(t, decoratorMetas, classBodyItems, state);
|
|
1112
|
+
if (metaPropertyList.length === 0) {
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
decoratorPaths.forEach((path) => path.remove());
|
|
1116
|
+
const isAnonymousClassDeclaration = path.isClassDeclaration() && !path.get('id').isIdentifier();
|
|
1117
|
+
const shouldTransformAsClassExpression = path.isClassExpression() || isAnonymousClassDeclaration;
|
|
1118
|
+
if (shouldTransformAsClassExpression) {
|
|
1119
|
+
// Example:
|
|
1120
|
+
// export default class extends LightningElement {}
|
|
1121
|
+
// Output:
|
|
1122
|
+
// export default registerDecorators(class extends LightningElement {});
|
|
1123
|
+
// if it does not have an id, we can treat it as a ClassExpression
|
|
1124
|
+
const classExpression = t.toExpression(node);
|
|
1125
|
+
path.replaceWith(createRegisterDecoratorsCallExpression(path, classExpression, metaPropertyList));
|
|
1126
|
+
}
|
|
1127
|
+
else {
|
|
1128
|
+
// Example: export default class NamedClass extends LightningElement {}
|
|
1129
|
+
// Output:
|
|
1130
|
+
// export default class NamedClass extends LightningElement {}
|
|
1131
|
+
// registerDecorators(NamedClass);
|
|
1132
|
+
// Note: This will be further transformed
|
|
1133
|
+
const statementPath = path.getStatementParent();
|
|
1134
|
+
statementPath.insertAfter(t.expressionStatement(createRegisterDecoratorsCallExpression(path, node.id, metaPropertyList)));
|
|
1135
|
+
}
|
|
1136
|
+
},
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/*
|
|
1141
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
1142
|
+
* All rights reserved.
|
|
1143
|
+
* SPDX-License-Identifier: MIT
|
|
1144
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
1145
|
+
*/
|
|
1146
|
+
function getImportSource(path) {
|
|
1147
|
+
return path.parentPath.get('arguments.0');
|
|
1148
|
+
}
|
|
1149
|
+
function validateImport(sourcePath, state) {
|
|
1150
|
+
if (!sourcePath.isStringLiteral()) {
|
|
1151
|
+
handleError(sourcePath, {
|
|
1152
|
+
errorInfo: errors.LWCClassErrors.INVALID_DYNAMIC_IMPORT_SOURCE_STRICT,
|
|
1153
|
+
messageArgs: [String(sourcePath)],
|
|
1154
|
+
}, state);
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* Expected API for this plugin:
|
|
1159
|
+
* { dynamicImports: { loader: string, strictSpecifier: boolean } }
|
|
1160
|
+
*/
|
|
1161
|
+
function dynamicImports () {
|
|
1162
|
+
function getLoaderRef(path, loaderName, state) {
|
|
1163
|
+
if (!state.loaderRef) {
|
|
1164
|
+
state.loaderRef = helperModuleImports.addNamed(path, 'load', loaderName);
|
|
1165
|
+
}
|
|
1166
|
+
return state.loaderRef;
|
|
1167
|
+
}
|
|
1168
|
+
function addDynamicImportDependency(dependency, state) {
|
|
1169
|
+
// TODO [#3444]: state.dynamicImports seems unused and can probably be deleted
|
|
1170
|
+
if (!state.dynamicImports) {
|
|
1171
|
+
state.dynamicImports = [];
|
|
1172
|
+
}
|
|
1173
|
+
if (!state.dynamicImports.includes(dependency)) {
|
|
1174
|
+
state.dynamicImports.push(dependency);
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
return {
|
|
1178
|
+
Import(path, state) {
|
|
1179
|
+
const { dynamicImports } = state.opts;
|
|
1180
|
+
if (!dynamicImports) {
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
const { loader, strictSpecifier } = dynamicImports;
|
|
1184
|
+
const sourcePath = getImportSource(path);
|
|
1185
|
+
if (strictSpecifier) {
|
|
1186
|
+
validateImport(sourcePath, state);
|
|
1187
|
+
}
|
|
1188
|
+
if (loader) {
|
|
1189
|
+
const loaderId = getLoaderRef(path, loader, state);
|
|
1190
|
+
path.replaceWith(loaderId);
|
|
1191
|
+
incrementMetricCounter(errors.CompilerMetrics.DynamicImportTransform, state);
|
|
1192
|
+
}
|
|
1193
|
+
if (sourcePath.isStringLiteral()) {
|
|
1194
|
+
addDynamicImportDependency(sourcePath.node.value, state);
|
|
1195
|
+
}
|
|
1196
|
+
},
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
/*
|
|
1201
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
1202
|
+
* All rights reserved.
|
|
1203
|
+
* SPDX-License-Identifier: MIT
|
|
1204
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
1205
|
+
*/
|
|
1206
|
+
function scopeCssImports ({ types: t }, path) {
|
|
1207
|
+
const programPath = path.isProgram() ? path : path.findParent((node) => node.isProgram());
|
|
1208
|
+
programPath.get('body').forEach((node) => {
|
|
1209
|
+
if (node.isImportDeclaration()) {
|
|
1210
|
+
const source = node.get('source');
|
|
1211
|
+
if (source.type === 'StringLiteral' && source.node.value.endsWith('.scoped.css')) {
|
|
1212
|
+
source.replaceWith(t.stringLiteral(source.node.value + '?scoped=true'));
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
/*
|
|
1219
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
1220
|
+
* All rights reserved.
|
|
1221
|
+
* SPDX-License-Identifier: MIT
|
|
1222
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
1223
|
+
*/
|
|
1224
|
+
function compilerVersionNumber({ types: t }) {
|
|
1225
|
+
return {
|
|
1226
|
+
ClassBody(path) {
|
|
1227
|
+
if (path.parent.superClass === null) {
|
|
1228
|
+
// Components *must* extend from either LightningElement or some other superclass (e.g. a mixin).
|
|
1229
|
+
// We can skip classes without a superclass to avoid adding unnecessary comments.
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
// If the class body is empty, we want an inner comment. Otherwise we want it after the last child
|
|
1233
|
+
// of the class body. In either case, we want it right before the `}` at the end of the function body.
|
|
1234
|
+
if (path.node.body.length > 0) {
|
|
1235
|
+
// E.g. `class Foo extends Lightning Element { /*LWC compiler v1.2.3*/ }`
|
|
1236
|
+
t.addComment(path.node.body[path.node.body.length - 1], 'trailing', shared.LWC_VERSION_COMMENT,
|
|
1237
|
+
/* line */ false);
|
|
1238
|
+
}
|
|
1239
|
+
else {
|
|
1240
|
+
// E.g. `class Foo extends Lightning Element { bar = 'baz'; /*LWC compiler v1.2.3*/ }`
|
|
1241
|
+
t.addComment(path.node, 'inner', shared.LWC_VERSION_COMMENT, /* line */ false);
|
|
1242
|
+
}
|
|
1243
|
+
},
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
/*
|
|
1248
|
+
* Copyright (c) 2023, salesforce.com, inc.
|
|
1249
|
+
* All rights reserved.
|
|
1250
|
+
* SPDX-License-Identifier: MIT
|
|
1251
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
1252
|
+
*/
|
|
1253
|
+
/**
|
|
1254
|
+
* The transform is done in 2 passes:
|
|
1255
|
+
* - First, apply in a single AST traversal the decorators and the component transformation.
|
|
1256
|
+
* - Then, in a second path transform class properties using the official babel plugin "babel-plugin-transform-class-properties".
|
|
1257
|
+
* @param api
|
|
1258
|
+
*/
|
|
1259
|
+
function LwcClassTransform(api) {
|
|
1260
|
+
const { ExportDefaultDeclaration: transformCreateRegisterComponent } = component(api);
|
|
1261
|
+
const { Class: transformDecorators } = decorators(api);
|
|
1262
|
+
const { Import: transformDynamicImports } = dynamicImports();
|
|
1263
|
+
const { ClassBody: addCompilerVersionNumber } = compilerVersionNumber(api);
|
|
1264
|
+
return {
|
|
1265
|
+
manipulateOptions(opts, parserOpts) {
|
|
1266
|
+
parserOpts.plugins.push('classProperties', [
|
|
1267
|
+
'decorators',
|
|
1268
|
+
{ decoratorsBeforeExport: true },
|
|
1269
|
+
]);
|
|
1270
|
+
},
|
|
1271
|
+
visitor: {
|
|
1272
|
+
// The LWC babel plugin is incompatible with other plugins. To get around this, we run the LWC babel plugin
|
|
1273
|
+
// first by running all its traversals from this Program visitor.
|
|
1274
|
+
Program: {
|
|
1275
|
+
enter(path, state) {
|
|
1276
|
+
const engineImportSpecifiers = getEngineImportSpecifiers(path);
|
|
1277
|
+
// Validate the usage of LWC decorators.
|
|
1278
|
+
validateImportedLwcDecoratorUsage(engineImportSpecifiers, state);
|
|
1279
|
+
// Add ?scoped=true to *.scoped.css imports
|
|
1280
|
+
scopeCssImports(api, path);
|
|
1281
|
+
},
|
|
1282
|
+
exit(path) {
|
|
1283
|
+
const engineImportSpecifiers = getEngineImportSpecifiers(path);
|
|
1284
|
+
removeImportedDecoratorSpecifiers(engineImportSpecifiers);
|
|
1285
|
+
},
|
|
1286
|
+
},
|
|
1287
|
+
Import: transformDynamicImports,
|
|
1288
|
+
Class: transformDecorators,
|
|
1289
|
+
ClassBody: addCompilerVersionNumber,
|
|
1290
|
+
ExportDefaultDeclaration: transformCreateRegisterComponent,
|
|
1291
|
+
},
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
exports.default = LwcClassTransform;
|
|
1296
|
+
/** version: 9.0.3 */
|
|
1297
|
+
//# sourceMappingURL=index.cjs.map
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"You can safely modify dependencies, devDependencies, keywords, etc., but other props will be overwritten."
|
|
5
5
|
],
|
|
6
6
|
"name": "@lwc/babel-plugin-component",
|
|
7
|
-
"version": "9.0.
|
|
7
|
+
"version": "9.0.3",
|
|
8
8
|
"description": "Babel plugin to transform a LWC module",
|
|
9
9
|
"keywords": [
|
|
10
10
|
"lwc"
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"types": "dist/index.d.ts",
|
|
35
35
|
"files": [
|
|
36
36
|
"dist/**/*.js",
|
|
37
|
+
"dist/**/*.cjs",
|
|
37
38
|
"dist/**/*.d.ts"
|
|
38
39
|
],
|
|
39
40
|
"scripts": {
|
|
@@ -51,8 +52,8 @@
|
|
|
51
52
|
},
|
|
52
53
|
"dependencies": {
|
|
53
54
|
"@babel/helper-module-imports": "7.28.6",
|
|
54
|
-
"@lwc/errors": "9.0.
|
|
55
|
-
"@lwc/shared": "9.0.
|
|
55
|
+
"@lwc/errors": "9.0.3",
|
|
56
|
+
"@lwc/shared": "9.0.3",
|
|
56
57
|
"line-column": "~1.0.2"
|
|
57
58
|
},
|
|
58
59
|
"devDependencies": {
|