@komaci/esm-generator 242.0.0 → 242.1.0

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.
Files changed (41) hide show
  1. package/build/__tests__/adgAstGeneration.spec.d.ts +2 -0
  2. package/build/__tests__/adgAstGeneration.spec.js +31 -0
  3. package/build/__tests__/compositionAstGeneration.spec.d.ts +2 -0
  4. package/build/__tests__/compositionAstGeneration.spec.js +394 -0
  5. package/build/__tests__/functionAstGeneration.spec.d.ts +2 -0
  6. package/build/__tests__/functionAstGeneration.spec.js +412 -0
  7. package/build/__tests__/general-komaci-docs/template-component/source.js +3 -3
  8. package/build/__tests__/genericAstGeneration.spec.d.ts +2 -0
  9. package/build/__tests__/genericAstGeneration.spec.js +586 -0
  10. package/build/__tests__/index.spec.js +8 -8
  11. package/build/__tests__/komaci-ast-unit.spec.js +240 -448
  12. package/build/__tests__/komaci-bundle-module.fixtures.spec.js +1 -0
  13. package/build/__tests__/komaci-script-module.fixtures.spec.js +5 -1
  14. package/build/__tests__/komaci-template-module.fixtures.spec.js +2 -1
  15. package/build/componentAstProcessing.d.ts +34 -0
  16. package/build/componentAstProcessing.js +105 -0
  17. package/build/compositionAstGeneration.d.ts +76 -0
  18. package/build/compositionAstGeneration.js +299 -0
  19. package/build/functionAstGeneration.d.ts +81 -0
  20. package/build/functionAstGeneration.js +337 -0
  21. package/build/genericAstGeneration.d.ts +107 -0
  22. package/build/genericAstGeneration.js +600 -0
  23. package/build/index.d.ts +2 -3
  24. package/build/index.js +19 -36
  25. package/build/types.d.ts +13 -2
  26. package/build/types.js +69 -1
  27. package/package.json +4 -4
  28. package/build/__tests__/modGenScript.spec.d.ts +0 -2
  29. package/build/__tests__/modGenScript.spec.js +0 -110
  30. package/build/__tests__/modGenTemplate.spec.d.ts +0 -2
  31. package/build/__tests__/modGenTemplate.spec.js +0 -196
  32. package/build/__tests__/shared.spec.d.ts +0 -2
  33. package/build/__tests__/shared.spec.js +0 -66
  34. package/build/__tests__/templateStrings.spec.d.ts +0 -2
  35. package/build/__tests__/templateStrings.spec.js +0 -130
  36. package/build/modGenScript.d.ts +0 -38
  37. package/build/modGenScript.js +0 -1031
  38. package/build/modGenTemplate.d.ts +0 -37
  39. package/build/modGenTemplate.js +0 -406
  40. package/build/shared.d.ts +0 -149
  41. package/build/shared.js +0 -480
@@ -1,37 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { GeneratorState } from './types';
3
- import { KomaciDocument, Composition, Image } from '@komaci/types';
4
- /**
5
- * Generates the resolvable module for a single template file
6
- * @param {GeneratorState} generatorState the current state of the esm generator
7
- * @param {KomaciDocument} komaciDoc the KomaciDocument representation of the Template (HTML) file
8
- * @returns {File} the babel/types File representation of the Template (i.e. the AST of the Template file)
9
- */
10
- export declare function generateTemplateModule(generatorState: GeneratorState, komaciDoc: KomaciDocument): t.File;
11
- /**
12
- * Processes the imports for the template Komaci document
13
- * @param {KomaciDocument} komaciDoc KomaciDocument for the template file
14
- * @param {GeneratorState} generatorState the current state of the esm generator
15
- * @param {Statement[]} statements the array of Statement to which to add the import declaration. Should
16
- * likely be empty as input.
17
- */
18
- export declare function processTemplateImports(komaciDoc: KomaciDocument, generatorState: GeneratorState, statements: t.Statement[]): void;
19
- /**
20
- * Convert an array of Komaci Compositions into a babel array of expressions.
21
- * This is used for both the top-level compositions in the Komaci Doc, as well
22
- * as their nested child compositions.
23
- * @param {GeneratorState} generatorState the current state of the esm generator
24
- * @param {boolean} isScriptFile whether this method is called in the context of processing a Script (JS) file
25
- * @param {Composition[]} compositions an array of Compositions from a Template JS
26
- * @returns {ArrayExpression | undefined} an ArrayExpression representing the passed in Composition array, or undefined
27
- * if no compositions were passed in
28
- */
29
- export declare function compositionsToArrayExpression(generatorState: GeneratorState, isScriptFile: boolean, compositions?: Composition[]): t.ArrayExpression | undefined;
30
- /**
31
- * Converts Komaci Image into prompt for the Resolver API to make an image priming call
32
- * @param {GeneratorState} generatorState the current state of the esm generator
33
- * @param {Image} image the Image node from the KomaciDocument
34
- * @returns {Expression} the babel Expression representing the Image prompt into the Resolver API
35
- */
36
- export declare function imageToExpression(generatorState: GeneratorState, image: Image): t.Expression;
37
- //# sourceMappingURL=modGenTemplate.d.ts.map
@@ -1,406 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.imageToExpression = exports.compositionsToArrayExpression = exports.processTemplateImports = exports.generateTemplateModule = void 0;
27
- /* eslint-disable @typescript-eslint/no-use-before-define */
28
- const t = __importStar(require("@babel/types"));
29
- const shared_1 = require("./shared");
30
- const types_1 = require("./types");
31
- const common_shared_1 = require("@komaci/common-shared");
32
- /**
33
- * Generates the resolvable module for a single template file
34
- * @param {GeneratorState} generatorState the current state of the esm generator
35
- * @param {KomaciDocument} komaciDoc the KomaciDocument representation of the Template (HTML) file
36
- * @returns {File} the babel/types File representation of the Template (i.e. the AST of the Template file)
37
- */
38
- function generateTemplateModule(generatorState, komaciDoc) {
39
- const statements = [];
40
- const program = t.program(statements);
41
- const file = t.file(program);
42
- // Process the template's imports
43
- processTemplateImports(komaciDoc, generatorState, statements);
44
- // Traverse compositions tree to construct array of expressions.
45
- // One for each Komaci Composition including it's nested children.
46
- const compositionsArray = compositionsToArrayExpression(generatorState, false, komaciDoc.compositions) ||
47
- t.arrayExpression([]);
48
- // Create default export of the main template function.
49
- // e.g. `export default function (fct, ctx) {...}`
50
- statements.push(t.exportDefaultDeclaration(getMainFunction(generatorState, compositionsArray)));
51
- return file;
52
- }
53
- exports.generateTemplateModule = generateTemplateModule;
54
- /**
55
- * Processes the imports for the template Komaci document
56
- * @param {KomaciDocument} komaciDoc KomaciDocument for the template file
57
- * @param {GeneratorState} generatorState the current state of the esm generator
58
- * @param {Statement[]} statements the array of Statement to which to add the import declaration. Should
59
- * likely be empty as input.
60
- */
61
- function processTemplateImports(komaciDoc, generatorState, statements) {
62
- if (komaciDoc.imports) {
63
- for (let i = 0; i < komaciDoc.imports.length; ++i) {
64
- const komaciImport = komaciDoc.imports[i];
65
- const localName = t.identifier(`${shared_1.ADG_IMPORT_SUFFIX}${++generatorState.adgImportSuffix}`);
66
- let resourceName = komaciImport.resourceName;
67
- // If template is inline the imports will be for combined module,
68
- // so remove the file portion of the import. For example:
69
- // @salesforce/komaci/c__child1__child1.js -> @salesforce/komaci/c__child1
70
- if (generatorState.inlineTemplateDoc) {
71
- const lastIndex = resourceName.lastIndexOf('__');
72
- resourceName = resourceName.slice(0, lastIndex);
73
- }
74
- // e.g. `import adg1 from "@salesforce/komaci/c__child1__child1.js";`
75
- const specifier = t.importDefaultSpecifier(localName);
76
- const source = t.stringLiteral(resourceName);
77
- const importDeclaration = t.importDeclaration([specifier], source);
78
- statements.push(importDeclaration);
79
- const importInfo = {
80
- localName,
81
- componentId: importToComponentId(komaciImport),
82
- resourceName,
83
- // below fields not used for template ImportInfos
84
- exportName: undefined,
85
- importUsages: new Set(),
86
- importSpecifier: specifier,
87
- importDeclaration: importDeclaration,
88
- };
89
- generatorState.templateGenState.importRefsMap[`${i}/names/0`] = importInfo;
90
- }
91
- }
92
- }
93
- exports.processTemplateImports = processTemplateImports;
94
- /**
95
- * Convert an array of Komaci Compositions into a babel array of expressions.
96
- * This is used for both the top-level compositions in the Komaci Doc, as well
97
- * as their nested child compositions.
98
- * @param {GeneratorState} generatorState the current state of the esm generator
99
- * @param {boolean} isScriptFile whether this method is called in the context of processing a Script (JS) file
100
- * @param {Composition[]} compositions an array of Compositions from a Template JS
101
- * @returns {ArrayExpression | undefined} an ArrayExpression representing the passed in Composition array, or undefined
102
- * if no compositions were passed in
103
- */
104
- function compositionsToArrayExpression(generatorState, isScriptFile, compositions) {
105
- if (!compositions) {
106
- return undefined;
107
- }
108
- const elements = [];
109
- compositions.forEach((composition) => {
110
- const expr = compositionToExpression(generatorState, composition, isScriptFile);
111
- if (expr) {
112
- if (t.isArrayExpression(expr)) {
113
- // Special case, such as the top-most container, where the container has
114
- // no impact on resolution process. In which case we want to flatten
115
- // the array to effectively remove the container from the generated mod.
116
- expr.elements.forEach((element) => {
117
- if (t.isExpression(element)) {
118
- elements.push(element);
119
- }
120
- });
121
- }
122
- else {
123
- elements.push(expr);
124
- }
125
- }
126
- });
127
- return t.arrayExpression(elements);
128
- }
129
- exports.compositionsToArrayExpression = compositionsToArrayExpression;
130
- /**
131
- * Convert any of the Komaci composition types into a babel expression, to be included in the generated module.
132
- * NOTE: For instances where a composition has an isActive coditional, the module will be using the u function provided by the fct factory object to
133
- * to check for an unresolved value. This function can be overridden, otherwise the default implementaitno will be used. https://salesforce.quip.com/XY4WAbfaLR5E
134
- * @param {GeneratorState} generatorState the current state of the esm generator
135
- * @param {Composition} composition the Composition to covert
136
- * @param {boolean} isScriptFile boolean indicating whether this method is being called in the context of processing a JS Script file
137
- * @returns {Expression | ArrayExpression} the babel expression (Expression | ArrayExpression) to which the Composition was converted
138
- */
139
- function compositionToExpression(generatorState, composition, isScriptFile) {
140
- let expression;
141
- // Get the expression specific to each type of composition.
142
- switch (composition.type) {
143
- case 'Container':
144
- expression = containerToExpression(generatorState, composition, isScriptFile);
145
- break;
146
- case 'ComposedAdg':
147
- expression = composedAdgToExpression(generatorState, composition, isScriptFile);
148
- break;
149
- case 'Iteration':
150
- expression = iterationToExpression(generatorState, composition, isScriptFile);
151
- break;
152
- case 'Slot':
153
- expression = slotToExpression(generatorState, composition);
154
- break;
155
- case 'Image':
156
- expression = imageToExpression(generatorState, composition);
157
- break;
158
- }
159
- // If the composition has an isActive condition, then
160
- // expression needs to be proceded by a conditional check.
161
- if (composition.isActive) {
162
- // when checking conditional, always check unresolved
163
- generatorState.templateGenState.requiredFactoryFunctions.add(shared_1.ID.UNRESOLVED_FUNC);
164
- let conditionalTest = (0, shared_1.bindingToExpression)(generatorState, composition.isActive.input, isScriptFile);
165
- if (composition.isActive.negate) {
166
- conditionalTest = t.unaryExpression('!', conditionalTest);
167
- }
168
- const prop = (0, shared_1.bindingRootToExpression)(generatorState, composition.isActive.input);
169
- const unresolvedTest = t.callExpression(shared_1.ID.UNRESOLVED_FUNC, [prop]);
170
- const test = t.logicalExpression('||', unresolvedTest, conditionalTest);
171
- expression = t.conditionalExpression(test, expression, shared_1.ID.UNDEFINED);
172
- }
173
- if ((0, common_shared_1.isCompositionAnImage)(composition) && expression) {
174
- generatorState.templateGenState.requiredFactoryFunctions.add(shared_1.ID.IMG_FUNC);
175
- }
176
- return expression;
177
- }
178
- /**
179
- * Converts a Komaci container into a babel expression. If the container doesn't
180
- * impact data flow, then an ArrayExpression is returned for its children.
181
- * @param {GeneratorState} generatorState the current state of the esm generator
182
- * @param {Container} container the Komaci container (i.e. <div> tag)
183
- * @param {boolean} isScriptFile boolean indicating whether this method is being called in the context of processing a JS Script file
184
- * @returns {Expression | ArrayExpression} The babel Expression or ArrayExpression converted from the input Container
185
- */
186
- function containerToExpression(generatorState, container, isScriptFile) {
187
- const children = compositionsToArrayExpression(generatorState, false, container.compositions) ||
188
- t.arrayExpression([]);
189
- // If key or slot is specified, then the container will need to be
190
- // accounted for in the module via the element function.
191
- if (container.key || container.slot) {
192
- const params = [];
193
- generatorState.templateGenState.requiredFactoryFunctions.add(shared_1.ID.ELEMENT_FUNC);
194
- // arg1: Composition configuration.
195
- params.push(getCompositionConfig(generatorState, container, isScriptFile));
196
- // arg2: Context.
197
- params.push(shared_1.ID.CONTEXT);
198
- // arg3: Array of expressions for each child.
199
- params.push(children);
200
- return t.callExpression(shared_1.ID.ELEMENT_FUNC, params);
201
- }
202
- else {
203
- // No key or slot, so only need to include it's children
204
- // in the generated mod.
205
- return children.elements.length === 1
206
- ? children.elements[0]
207
- : children;
208
- }
209
- }
210
- /**
211
- * Converts a Komaci ComposedAdg (which references an Import) into a call to the Resolver API for a composed graph, `cg`.
212
- * @param {GeneratorState} generatorState the current state of the esm generator
213
- * @param {ComposedAdg} composedAdg the ComposedAdg whose input.type is an `ImportReference`
214
- * @param {boolean} isScriptFile boolean indicating whether this method is being called in the context of processing a JS Script file
215
- * @returns {Expression} a babel Expression representing the composed graph (cg) resolver API call. Throws an Error if the
216
- * composedAdg.input.type is not equal to 'ImportReference'
217
- */
218
- function composedAdgToExpression(generatorState, composedAdg, isScriptFile) {
219
- let componentId;
220
- const params = [];
221
- generatorState.templateGenState.requiredFactoryFunctions.add(shared_1.ID.COMPOSED_GRAPH_FUNC);
222
- // arg1: Input source to the composed graph function.
223
- if (composedAdg.input.type === 'ImportReference') {
224
- const importInfo = (0, shared_1.updateImportInfoState)(generatorState, composedAdg.input.value, false, types_1.ImportUsageState.composedadg);
225
- componentId = importInfo.componentId;
226
- params.push(importInfo.localName);
227
- }
228
- else {
229
- // Schema supports this, but downstream logic (e.g. analysis) doesn't (yet).
230
- throw new Error(shared_1.ERROR_PREFIX + 'binding not yet supported for ComposedAdg input');
231
- }
232
- // arg2: Composition configuration.
233
- params.push(getCompositionConfig(generatorState, composedAdg, isScriptFile, componentId));
234
- // arg3: Context.
235
- params.push(shared_1.ID.CONTEXT);
236
- // arg4: Array of expressions for each child.
237
- const children = compositionsToArrayExpression(generatorState, isScriptFile, composedAdg.compositions);
238
- if (children && children.elements.length) {
239
- params.push(children);
240
- }
241
- return t.callExpression(shared_1.ID.COMPOSED_GRAPH_FUNC, params);
242
- }
243
- /**
244
- * Converts a Komaci Iteration into a call to Resolver API for an iteration, i.e `i`.
245
- * @param {GeneratorState} generatorState the current state of the esm generator
246
- * @param {Iteration} iteration the Iteration from a template file
247
- * @param {boolean} isScriptFile boolean indicating whether this method is being called in the context of processing a JS Script file
248
- * @returns {Expression} the babel Expression that represents the call into the Resolver API for an iteration
249
- */
250
- function iterationToExpression(generatorState, iteration, isScriptFile) {
251
- const params = [];
252
- generatorState.templateGenState.requiredFactoryFunctions.add(shared_1.ID.ITERATION_FUNC);
253
- // arg1: Expression for the collection being iterated.
254
- params.push((0, shared_1.bindingToExpression)(generatorState, iteration.input, isScriptFile));
255
- // arg2: Context.
256
- params.push(shared_1.ID.CONTEXT);
257
- // arg3: Function to return compositions inside the the iteration.
258
- // Use context to understand which iterations contain the code being analyzed.
259
- generatorState.iterationContext.unshift(iteration.iterator);
260
- const children = compositionsToArrayExpression(generatorState, isScriptFile, iteration.compositions) || t.arrayExpression([]);
261
- generatorState.iterationContext.shift();
262
- // interation.iterator contains string variables like "i", "j", "k", or loop control
263
- // variables must prepend user variable prefix "user_var_" to the variable when used
264
- // in arrow function declaration, to prevent resolution issues
265
- params.push(t.arrowFunctionExpression([t.identifier(shared_1.USER_VARIABLE_PREFIX + iteration.iterator)], t.blockStatement([t.returnStatement(children)])));
266
- return t.callExpression(shared_1.ID.ITERATION_FUNC, params);
267
- }
268
- /**
269
- * Convert Komaci slot into call to Resolver API for a slot, `s`
270
- * @param {GeneratorState} generatorState the current state of the esm generator
271
- * @param {Slot} slot The Slot to be converted into an expression
272
- * @returns {Expression} the babel Expression representing the Slot call into the Resolver API
273
- */
274
- function slotToExpression(generatorState, slot) {
275
- const params = [];
276
- generatorState.templateGenState.requiredFactoryFunctions.add(shared_1.ID.SLOT_FUNC);
277
- // arg1: Slot Name.
278
- params.push(t.stringLiteral(slot.name));
279
- // arg2: Composition configuration.
280
- params.push(getCompositionConfig(generatorState, slot, false));
281
- // arg3: Context.
282
- params.push(shared_1.ID.CONTEXT);
283
- // arg4: Array of expressions for each child.
284
- const defaultContent = compositionsToArrayExpression(generatorState, false, slot.compositions);
285
- if (defaultContent && defaultContent.elements.length) {
286
- params.push(defaultContent);
287
- }
288
- return t.callExpression(shared_1.ID.SLOT_FUNC, params);
289
- }
290
- /**
291
- * Converts Komaci Image into prompt for the Resolver API to make an image priming call
292
- * @param {GeneratorState} generatorState the current state of the esm generator
293
- * @param {Image} image the Image node from the KomaciDocument
294
- * @returns {Expression} the babel Expression representing the Image prompt into the Resolver API
295
- */
296
- function imageToExpression(generatorState, image) {
297
- const params = [];
298
- const funcObjProps = [];
299
- if ((0, common_shared_1.isCompositionAnImage)(image)) {
300
- if ((0, common_shared_1.isSrcPrimitiveValue)(image.src)) {
301
- // type guard ensures `image.src` is a PrimitiveValue
302
- // lwc-platform will NOT produce an Image KomaciNode with a blank or missing src attribute.
303
- // However, just in case we do encounter this, we handle it here by simply NOT producing the
304
- // function ObjectProperty for such a node.
305
- if (image.src.value &&
306
- typeof image.src.value === 'string' &&
307
- image.src.value !== '') {
308
- funcObjProps.push(t.objectProperty(shared_1.ID.IMG_SRC, t.stringLiteral(image.src.value)));
309
- }
310
- }
311
- else if ((0, common_shared_1.isSrcABinding)(image.src)) {
312
- // type guard ensures `image.src` is a Binding
313
- // lwc-platform will NOT produce an Image KomaciNode with a blank or missing src attribute.
314
- // However, just in case we do encounter this, we handle it here by simply NOT producing the
315
- // function ObjectProperty for such a node.
316
- if (image.src.value !== '') {
317
- funcObjProps.push(t.objectProperty(shared_1.ID.IMG_SRC, (0, shared_1.bindingToExpression)(generatorState, image.src, true)));
318
- }
319
- }
320
- }
321
- params.push(t.objectExpression(funcObjProps));
322
- return t.callExpression(shared_1.ID.IMG_FUNC, params);
323
- }
324
- exports.imageToExpression = imageToExpression;
325
- /**
326
- * Creates a babel configuration ObjectExpression for provided composition. This is the `CompositionConfig`
327
- * from Resolver API in its babel ObjectExpression form.
328
- * @param {GeneratorState} generatorState the curent state of the esm generator
329
- * @param {ComposedAdg | Container | Slot} composition the Composition from which to create a configuration (as a ComposedAdg, Container, or Slot)
330
- * @param {boolean} isScriptFile boolean indicating whether this method is being called in the context of processing a JS Script file
331
- * @param {string} componentId the component indentifier, as a string
332
- * @returns {ObjectExpression} the babel ObjectExpression representing the Resolver API CompositionConfig
333
- */
334
- function getCompositionConfig(generatorState, composition, isScriptFile, componentId) {
335
- const config = [];
336
- if (composition.slot) {
337
- config.push(t.objectProperty(shared_1.ID.SLOT, t.stringLiteral(composition.slot)));
338
- }
339
- // ComposedAdg config contains additional information.
340
- if (composition.type === 'ComposedAdg') {
341
- if (componentId) {
342
- config.push(t.objectProperty(shared_1.ID.COMPONENT_ID, t.stringLiteral(componentId)));
343
- }
344
- if (composition.properties) {
345
- const props = [];
346
- for (const propName of Object.keys(composition.properties)) {
347
- const prop = composition.properties[propName];
348
- if (prop.type === 'Binding') {
349
- props.push(t.objectProperty(t.identifier(propName), (0, shared_1.bindingToExpression)(generatorState, prop, isScriptFile)));
350
- }
351
- else if (prop.type === 'PrimitiveValue') {
352
- props.push(t.objectProperty(t.identifier(propName), (0, shared_1.primitiveToExpression)(generatorState, prop)));
353
- }
354
- }
355
- config.push(t.objectProperty(shared_1.ID.PROPS, t.objectExpression(props)));
356
- }
357
- }
358
- return t.objectExpression(config);
359
- }
360
- /**
361
- * Convers an import in a Komaci template doc to a component identifier.
362
- * Example:
363
- * From: @salesforce/komaci/c__child1__child1.js
364
- * To: c-child1
365
- * @param {Import} komaciImport Komaci representation of an Import
366
- * @returns {string} a string representing the component id generated from the import resource name
367
- */
368
- function importToComponentId(komaciImport) {
369
- // First split by '/' and confirm expected # segments.
370
- let segments = komaciImport.resourceName.split('/');
371
- if (segments.length !== 3) {
372
- throw new Error(shared_1.ERROR_PREFIX + 'komaciDoc contains invalid import');
373
- }
374
- // Now split last segment by '__' and confirm expected # segments.
375
- segments = segments[2].split('__');
376
- if (segments.length !== 3) {
377
- throw new Error(shared_1.ERROR_PREFIX + 'komaciDoc contains invalid import');
378
- }
379
- const namespace = segments[0];
380
- // name converted from camel case to kebab case.
381
- const name = segments[1]
382
- .replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2')
383
- .toLowerCase();
384
- return `${namespace}-${name}`;
385
- }
386
- /**
387
- * Retrieves the babel FunctionDeclaration representation of the main function exported from the module being generated.
388
- * e.g. `function (fct, ctx) { return ... }`
389
- * @param {GeneratorState} generatorState the current state of the esm generator
390
- * @param compositionExpressionsArray babel ArrayExpression representing the compositions expresions array
391
- * @returns babel FunctionDeclaration representation of the main function exported from the module
392
- */
393
- function getMainFunction(generatorState, compositionExpressionsArray) {
394
- const params = [shared_1.ID.FACTORY, shared_1.ID.CONTEXT];
395
- const statements = [];
396
- const body = t.blockStatement(statements);
397
- // Destructuring of factory functions.
398
- const fctFuncs = (0, shared_1.getFactoryFuncDeclaration)(generatorState, false);
399
- if (fctFuncs) {
400
- statements.push(fctFuncs);
401
- }
402
- // Return the array of composition expressions.
403
- statements.push(t.returnStatement(compositionExpressionsArray));
404
- return t.functionDeclaration(null, params, body);
405
- }
406
- //# sourceMappingURL=modGenTemplate.js.map
package/build/shared.d.ts DELETED
@@ -1,149 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { GeneratorState, ImportInfo, ImportUsageState } from './types';
3
- import { Binding, PrimitiveValue, Value, PropertyReference } from '@komaci/types';
4
- import { NodePath } from '@babel/core';
5
- import { ValidGetterContext, ValidTemplateContext, ModuleLevelConsumption, AstContext, ClassPropertyContexts, ModuleContext } from '@komaci/common-shared';
6
- export declare const ERROR_PREFIX = "[komaci esm-generator] ";
7
- export declare const CONTEXT = "ctx";
8
- export declare const PROPS = "props";
9
- export declare const ADG_IMPORT_SUFFIX = "getAdgDefinition";
10
- export declare const UNRESOLVED = "unresolved";
11
- export declare const USER_VARIABLE_PREFIX = "user_var_";
12
- export declare const WIRE_FUNCTION = "WireFunction";
13
- export declare const GETTER_FUNCTION = "GetterFunction";
14
- export declare const ERROR_FUNCTION = "ErrorFunction";
15
- export declare const PRIMING_FUNCTION = "PrimingFunction";
16
- export declare const ID: {
17
- CONTEXT: t.Identifier;
18
- PROPS: t.Identifier;
19
- FACTORY: t.Identifier;
20
- COMPOSED_GRAPH_FUNC: t.Identifier;
21
- ITERATION_FUNC: t.Identifier;
22
- UNRESOLVED_FUNC: t.Identifier;
23
- SLOT_FUNC: t.Identifier;
24
- ELEMENT_FUNC: t.Identifier;
25
- ADG_FUNC: t.Identifier;
26
- SLOT: t.Identifier;
27
- COMPONENT_ID: t.Identifier;
28
- UNDEFINED: t.Identifier;
29
- PROP_API: t.Identifier;
30
- PROP_INIT: t.Identifier;
31
- PROP_FUNC: t.Identifier;
32
- PROP_GETTER: t.Identifier;
33
- DEFAULT_COMP: t.Identifier;
34
- FUNC_ID: t.Identifier;
35
- FUNC_NAME: t.Identifier;
36
- FUNC_TYPE: t.Identifier;
37
- FUNC_CONFIG: t.Identifier;
38
- FUNC_INPUT: t.Identifier;
39
- WIRE_TYPE: t.Identifier;
40
- UNRESOLVED_VALUE: t.Identifier;
41
- BATCH_FUNC_ID: t.Identifier;
42
- BATCH_FUNC_NAME: t.Identifier;
43
- REDUCER_FUNC_ID: t.Identifier;
44
- IMG_SRC: t.Identifier;
45
- IMG_FUNC: t.Identifier;
46
- };
47
- /**
48
- * Retrieve info about an import given its reference value (e.g. 0/names/0), while initializing/updating its usageState, as well
49
- * as updating required factory functions (if we are returning an ImportInfo for an unsupported namespace).
50
- * @param generatorState the GeneratorState
51
- * @param importRefValue import reference value, as a string. ex: "0/names/0"
52
- * @param isScriptFile boolean indicating whether we are calling the method while processing a Script (JS file)[true], or a template (HTML file)[false]
53
- * @param usageState ImportUsageState enum that represents the way the import will be used. Some example values are: composedadg, wireadapter,
54
- * getter, initialvalue, wireconfig, parentadg
55
- * @returns ImportInfo object, updated with latest usageState
56
- */
57
- export declare function updateImportInfoState(generatorState: GeneratorState, importRefValue: string, isScriptFile: boolean, usageState: ImportUsageState): ImportInfo;
58
- /**
59
- * Retrieve info about an import given its reference value (e.g. 0/names/0), without updating its state.
60
- * To update ImportInfo state, see method updateImportInfoState.
61
- * @param generatorState the current GeneratorState for this module generation
62
- * @param importRefValue the import reference value to use in looking up the ImportInfo. ex: "0/names/0"
63
- * @param isScriptFile boolean determining whether we are processing a script (JS) or template (HTML)
64
- * @returns ImportInfo corresponding to the importRefValue in the generatorState
65
- */
66
- export declare function retrieveImportInfo(generatorState: GeneratorState, importRefValue: string, isScriptFile: boolean): ImportInfo;
67
- export declare function getFactoryFuncDeclaration(generatorState: GeneratorState, isScriptFile: boolean): t.VariableDeclaration | undefined;
68
- export declare function primitiveToExpression(generatorState: GeneratorState, primitive: PrimitiveValue): t.Expression;
69
- export declare function bindingToExpression(generatorState: GeneratorState, binding: Binding | PropertyReference, isScriptFile: boolean): t.Expression;
70
- export declare function bindingRootToExpression(generatorState: GeneratorState, binding: Binding | PropertyReference): t.Expression;
71
- export declare function valueToExpression(generatorState: GeneratorState, komaciValue: Value, isScriptFile: boolean, usageState: ImportUsageState): t.Expression;
72
- /**
73
- * Retrieve the file path for the js of a component
74
- * @param generatorState GeneratorState containg the GeneratorInput, which in turn contains the JS raw source file
75
- * @returns Returns raw source code as string for the js of a component if it exists in the srcFileMap
76
- * Returns undefined if no JS source file (whose filename also matches the LWC module name) was included in the
77
- * GeneratorState.GeneratorInput by lwc-platform, other calling code.
78
- */
79
- export declare function getComponentSrcJs(generatorState: GeneratorState): string | undefined;
80
- /**
81
- * AST is traversed to see if they contain getter functions that are not valid. For each getter that is valid, we will
82
- * update contextual information about the class and pass back info about the valid getters.
83
- *
84
- * @param {ClassPropertyContexts} properties the Map of properties metadata, which tells whether a property is used by a wire functions
85
- * @param {ModuleContext} moduleContext Contextual info about the module itself
86
- * @param {ModuleLevelConsumption} moduleLevelConsumption Details about how things are consuming module level entities. NOTE: This is updated in here for import usage
87
- * @param {t.File} ast The ast for the module
88
- * @returns Information about valid getters to be added to the resolvable Module.
89
- */
90
- export declare function processGetters(properties: ClassPropertyContexts, moduleContext: ModuleContext, moduleLevelConsumption: ModuleLevelConsumption, getterMethodPaths: NodePath<t.ClassMethod>[]): ValidGetterContext[];
91
- /**
92
- * This method takes in the generatedCombinedModule AST, the original source AST, the import references map from the
93
- * GeneratorState, and the valid getter contexts (which contain the babel NodePaths to the getters) and transforms the generated
94
- * combined module AST by hoisting any passed-in valid getters into top-level getter methods, adding `props` as input paramater to the
95
- * same getter methods, replacing this.<var> references in the getter method to props.<var>, as well as replacing references of
96
- * valid imports references in the getter with corresponding Komaci module unique identifiers (i1, i2, i3,...).
97
- *
98
- * @param genCombinedModuleAST the AST of the generated combined module
99
- * @param importReferencesMap Map of importReferences (ex: "0/names/0") to ImportInfo, which comes from GeneratorState's scriptGenState.importRefsMap
100
- * @param getterImportUsage All the imports decalred in the ast
101
- * @param validGetterContexts Array of ValidGetterContext object which contain the NodePath representing the getter, as well as an Array<string> of
102
- * memberUsages within the same getter.
103
- */
104
- export declare function transformGetters(genCombinedModuleAST: t.File, importReferencesMap: {
105
- [refId: string]: ImportInfo;
106
- }, getterImportUsage: AstContext['importDeclarations'], validGetterContexts: ValidGetterContext[]): void;
107
- /**
108
- * Validate what templates are good and then uplift the metadata about how they interact with other portions of the modules.
109
- * i.e. import, property references, used in priming
110
- * @param {ClassPropertyContexts} properties the Map of properties metadata, which tells whether a property is used by a wire functions
111
- * @param {ModuleContext} moduleContext Contextual info about the module itself
112
- * @param {ModuleLevelConsumption} moduleLevelConsumption Details about how things are consuming module level entities. NOTE: This is updated in here for import usage
113
- * @param {Array<NodePath<t.ClassProperty>>} templateStrings The templates to validate
114
- * @returns Information about valid templates to be added to the resolvable Module.
115
- */
116
- export declare function processTemplateStrings(properties: ClassPropertyContexts, moduleContext: ModuleContext, moduleLevelConsumption: ModuleLevelConsumption, templateStrings: Array<NodePath<t.ClassProperty>>): ValidTemplateContext[];
117
- /**
118
- * Converts templates into single line functions, hoisted to the top level of the resolvable module.
119
- * Part of this hoisting is going through and updating references to properties or imports. similar to what we do for getters.
120
- *
121
- * @param genCombinedModuleAST the AST of the generated combined module
122
- * @param importReferencesMap Map of importReferences (ex: "0/names/0") to ImportInfo, which comes from GeneratorState's scriptGenState.importRefsMap
123
- * @param definedImports All the imports decalred in the ast
124
- * @param validGetterContexts Array of valid templates
125
- */
126
- export declare function hoistTemplateStrings(genCombinedModuleAST: t.File, importReferencesMap: {
127
- [refId: string]: ImportInfo;
128
- }, definedImports: AstContext['importDeclarations'], templateStrings: ValidTemplateContext[]): void;
129
- /**
130
- * Create a mapping from the original name of an import to the generated one made by komaci
131
- * @param importReferencesMap The inverse mapping of generated name to info
132
- * @returns A map of original import name to generated name
133
- */
134
- export declare function mapRealNameToGenerated(importReferencesMap: {
135
- [refId: string]: ImportInfo;
136
- }): Map<string, string>;
137
- /**
138
- * Take a function defined on a class and sanitize it's interactions in preparation to be hoisted into resolvable module/
139
- *
140
- * Two main things happen during sanitization:
141
- * - references to imports are updated to their generated name (i.e. i2)
142
- * - references to `this` are replaced with references to `props`
143
- *
144
- * @param nodePath The node to sanitize
145
- * @param definedImports Metadata about all the imports we know about
146
- * @param trueToGeneratedImport Mapping of real import name to generated name
147
- */
148
- export declare function sanitizeFunction(nodePath: NodePath<t.ClassProperty | t.ClassMethod>, definedImports: AstContext['importDeclarations'], trueToGeneratedImport: Map<string, string>): void;
149
- //# sourceMappingURL=shared.d.ts.map