@komaci/esm-generator 240.1.5 → 242.1.1

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 (50) 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 +396 -0
  5. package/build/__tests__/fixtures-combined-files/exclude-partial-classProps-indirect-importRef-getter/actual.js +5 -1
  6. package/build/__tests__/fixtures-combined-files/include-all-indirect-importRef-getter-complex/actual.js +5 -1
  7. package/build/__tests__/fixtures-combined-files/include-class-prop-indirect-importRef-getter/actual.js +5 -1
  8. package/build/__tests__/fixtures-combined-files/only-named-export-with-template/actual.d.ts +1 -0
  9. package/build/__tests__/fixtures-combined-files/only-named-export-with-template/actual.js +2 -0
  10. package/build/__tests__/fixtures-script-files/imports/actual.js +5 -1
  11. package/build/__tests__/fixtures-script-files/imports-namespaced/actual.js +5 -1
  12. package/build/__tests__/fixtures-script-files/wire-imports/actual.js +5 -1
  13. package/build/__tests__/functionAstGeneration.spec.d.ts +2 -0
  14. package/build/__tests__/functionAstGeneration.spec.js +416 -0
  15. package/build/__tests__/general-komaci-docs/import-types/source.js +5 -1
  16. package/build/__tests__/general-komaci-docs/template-component/source.js +3 -3
  17. package/build/__tests__/genericAstGeneration.spec.d.ts +2 -0
  18. package/build/__tests__/genericAstGeneration.spec.js +877 -0
  19. package/build/__tests__/index.spec.js +34 -18
  20. package/build/__tests__/komaci-ast-unit.spec.js +240 -448
  21. package/build/__tests__/komaci-bundle-module.fixtures.spec.js +1 -0
  22. package/build/__tests__/komaci-script-module.fixtures.spec.js +5 -1
  23. package/build/__tests__/komaci-template-module.fixtures.spec.js +2 -1
  24. package/build/componentAstProcessing.d.ts +34 -0
  25. package/build/componentAstProcessing.js +105 -0
  26. package/build/compositionAstGeneration.d.ts +76 -0
  27. package/build/compositionAstGeneration.js +299 -0
  28. package/build/functionAstGeneration.d.ts +81 -0
  29. package/build/functionAstGeneration.js +345 -0
  30. package/build/genericAstGeneration.d.ts +115 -0
  31. package/build/genericAstGeneration.js +644 -0
  32. package/build/index.d.ts +6 -0
  33. package/build/index.js +77 -89
  34. package/build/types.d.ts +13 -2
  35. package/build/types.js +69 -1
  36. package/package.json +4 -4
  37. package/build/__tests__/modGenScript.spec.d.ts +0 -2
  38. package/build/__tests__/modGenScript.spec.js +0 -110
  39. package/build/__tests__/modGenTemplate.spec.d.ts +0 -2
  40. package/build/__tests__/modGenTemplate.spec.js +0 -196
  41. package/build/__tests__/shared.spec.d.ts +0 -2
  42. package/build/__tests__/shared.spec.js +0 -62
  43. package/build/__tests__/templateStrings.spec.d.ts +0 -2
  44. package/build/__tests__/templateStrings.spec.js +0 -130
  45. package/build/modGenScript.d.ts +0 -38
  46. package/build/modGenScript.js +0 -1027
  47. package/build/modGenTemplate.d.ts +0 -37
  48. package/build/modGenTemplate.js +0 -402
  49. package/build/shared.d.ts +0 -170
  50. package/build/shared.js +0 -588
@@ -0,0 +1,877 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const generator_1 = __importDefault(require("@babel/generator"));
30
+ const common_shared_1 = require("@komaci/common-shared");
31
+ const genericAstGeneration_1 = require("../genericAstGeneration");
32
+ const t = __importStar(require("@babel/types"));
33
+ describe('valueToExpression', () => {
34
+ it('composes proper statement for complex object. Includes ObjectValue, ArrayValue, PropertyReference, and Primitive Value', () => {
35
+ const wireFunctionObject = {
36
+ type: 'ObjectValue',
37
+ value: {
38
+ fields: {
39
+ type: 'ArrayValue',
40
+ value: [
41
+ { type: 'PrimitiveValue', value: 'Account.Id' },
42
+ { type: 'PrimitiveValue', value: 'Account.Name' },
43
+ { type: 'PrimitiveValue', value: 'Account.CreatedById' },
44
+ { type: 'PrimitiveValue', value: 'Account.CreatedDate' },
45
+ { type: 'PrimitiveValue', value: 'Account.OwnerId' },
46
+ { type: 'PrimitiveValue', value: 'Account.ParentId' },
47
+ ],
48
+ },
49
+ recordId: { type: 'PropertyReference', value: 'recordId' },
50
+ },
51
+ };
52
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
53
+ const result = (0, genericAstGeneration_1.valueToExpression)(wireFunctionObject, moduleMetadata);
54
+ expect(result).not.toBeUndefined();
55
+ expect(result.properties).toHaveLength(2);
56
+ const objExprProperties = result.properties;
57
+ expect(objExprProperties[0].key.value).toEqual('fields');
58
+ expect(objExprProperties[0].value.elements).toHaveLength(6);
59
+ expect(objExprProperties[0].value
60
+ .elements[0].value).toEqual('Account.Id');
61
+ expect(objExprProperties[0].value
62
+ .elements[1].value).toEqual('Account.Name');
63
+ expect(objExprProperties[0].value
64
+ .elements[2].value).toEqual('Account.CreatedById');
65
+ expect(objExprProperties[0].value
66
+ .elements[3].value).toEqual('Account.CreatedDate');
67
+ expect(objExprProperties[0].value
68
+ .elements[4].value).toEqual('Account.OwnerId');
69
+ expect(objExprProperties[0].value
70
+ .elements[5].value).toEqual('Account.ParentId');
71
+ expect(objExprProperties[1].key.value).toEqual('recordId');
72
+ expect(objExprProperties[1].value
73
+ .property.name).toEqual('recordId');
74
+ });
75
+ it('composes proper statement when komaci value is an importReference', () => {
76
+ const komaciValue = {
77
+ type: 'ImportReference',
78
+ value: '0/names/0',
79
+ namespacedMember: 'p1',
80
+ };
81
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
82
+ const komaciDoc = {
83
+ imports: [
84
+ { resourceName: 'force/shared', names: ['*'] },
85
+ { resourceName: 'laf/transformWire', names: ['*'] },
86
+ { resourceName: 'lightning/uiRecordApi', names: ['getRecord'] },
87
+ ],
88
+ };
89
+ (0, common_shared_1.initializeImportMetdataFromKomaciDocument)(komaciDoc, moduleMetadata);
90
+ const result = (0, genericAstGeneration_1.valueToExpression)(komaciValue, moduleMetadata);
91
+ expect(result).not.toBeUndefined();
92
+ expect(result.object.name).toEqual('i0');
93
+ expect(result.property.name).toEqual('p1');
94
+ });
95
+ it('composes proper statement when komaci value is an unsupported importReference', () => {
96
+ const komaciValue = {
97
+ type: 'ImportReference',
98
+ value: '0/names/0',
99
+ namespacedMember: 'p1',
100
+ };
101
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
102
+ const komaciDoc = {
103
+ imports: [
104
+ { resourceName: 'unsuported/shared', names: ['*'] },
105
+ { resourceName: 'laf/transformWire', names: ['*'] },
106
+ { resourceName: 'lightning/uiRecordApi', names: ['getRecord'] },
107
+ ],
108
+ };
109
+ (0, common_shared_1.initializeImportMetdataFromKomaciDocument)(komaciDoc, moduleMetadata);
110
+ const result = (0, genericAstGeneration_1.valueToExpression)(komaciValue, moduleMetadata);
111
+ expect(result).not.toBeUndefined();
112
+ expect(result.type).toBe('Identifier');
113
+ expect(result.name).toBe('unresolved');
114
+ });
115
+ it('marks invalid import reference as unresolved', () => {
116
+ const komaciValue = {
117
+ type: 'ImportReference',
118
+ value: '7/names/0',
119
+ namespacedMember: 'p1',
120
+ };
121
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
122
+ const komaciDoc = {
123
+ imports: [
124
+ { resourceName: 'unsuported/shared', names: ['*'] },
125
+ { resourceName: 'laf/transformWire', names: ['*'] },
126
+ { resourceName: 'lightning/uiRecordApi', names: ['getRecord'] },
127
+ ],
128
+ };
129
+ (0, common_shared_1.initializeImportMetdataFromKomaciDocument)(komaciDoc, moduleMetadata);
130
+ const result = (0, genericAstGeneration_1.valueToExpression)(komaciValue, moduleMetadata);
131
+ expect(result).not.toBeUndefined();
132
+ expect(result.type).toBe('Identifier');
133
+ expect(result.name).toBe('unresolved');
134
+ });
135
+ });
136
+ describe('functionToExpression', () => {
137
+ it('composes proper object expression for a valid getter function', () => {
138
+ //need to make a hoistedGetterFunction object
139
+ // module metadata based on komaci document.
140
+ const functionMeta = {
141
+ type: common_shared_1.FunctionTypes.GETTER_FUNCTION,
142
+ componentAstNode: undefined,
143
+ requiredProperties: ['tempProp'],
144
+ generatedName: `g${0}`,
145
+ };
146
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
147
+ const result = (0, genericAstGeneration_1.functionToExpression)(moduleMetadata, functionMeta);
148
+ expect(result).not.toBeUndefined();
149
+ expect(result?.type).toEqual('ObjectExpression');
150
+ expect(result?.properties).toHaveLength(3);
151
+ const properties = result?.properties;
152
+ expect(properties[0].key.name).toEqual('f');
153
+ expect(properties[0].value.name).toEqual('g0');
154
+ expect(properties[1].key.name).toEqual('t');
155
+ expect(properties[1].value.value).toEqual('GetterFunction');
156
+ expect(properties[2].key.name).toEqual('in');
157
+ expect(properties[2].value.elements).toHaveLength(1);
158
+ });
159
+ it('composes proper object expression for a valid template string', () => {
160
+ //need to make a hoistedGetterFunction object
161
+ // module metadata based on komaci document.
162
+ const functionMeta = {
163
+ type: common_shared_1.FunctionTypes.TEMPLATE_STRING,
164
+ componentAstNode: undefined,
165
+ requiredProperties: ['tempProp'],
166
+ generatedName: `t${0}`,
167
+ };
168
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
169
+ const result = (0, genericAstGeneration_1.functionToExpression)(moduleMetadata, functionMeta);
170
+ expect(result).not.toBeUndefined();
171
+ expect(result?.type).toEqual('ObjectExpression');
172
+ expect(result?.properties).toHaveLength(3);
173
+ const properties = result?.properties;
174
+ expect(properties[0].key.name).toEqual('f');
175
+ expect(properties[0].value.name).toEqual('t0');
176
+ expect(properties[1].key.name).toEqual('t');
177
+ expect(properties[1].value.value).toEqual('TemplatedStringFunction');
178
+ expect(properties[2].key.name).toEqual('in');
179
+ expect(properties[2].value.elements).toHaveLength(1);
180
+ });
181
+ });
182
+ describe('propertyToExpression', () => {
183
+ it('composes proper object expression when input is property with inital value', () => {
184
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
185
+ const propertyConext = {
186
+ type: common_shared_1.PropertyTypes.BASE,
187
+ inital: { type: 'PrimitiveValue', value: 5 },
188
+ usedInPriming: false,
189
+ isPublic: true,
190
+ };
191
+ const result = (0, genericAstGeneration_1.propertyToExpression)(moduleMetadata, propertyConext);
192
+ expect(result).not.toBeUndefined();
193
+ expect(result?.type).toEqual('ObjectExpression');
194
+ expect(result?.properties).toHaveLength(2);
195
+ const properties = result?.properties;
196
+ expect(properties).toHaveLength(2);
197
+ expect(properties[0].key.name).toEqual('a');
198
+ expect(properties[0].value.value).toBeTruthy();
199
+ expect(properties[1].key.name).toEqual('i');
200
+ expect(properties[1].value.value).toEqual(5);
201
+ });
202
+ it('composes proper object expression when property is a getter', () => {
203
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
204
+ const propertyConext = {
205
+ type: common_shared_1.PropertyTypes.GETTER,
206
+ usedInPriming: false,
207
+ functionIndex: 0,
208
+ isPublic: false,
209
+ };
210
+ const result = (0, genericAstGeneration_1.propertyToExpression)(moduleMetadata, propertyConext);
211
+ expect(result).not.toBeUndefined();
212
+ expect(result?.type).toEqual('ObjectExpression');
213
+ expect(result?.properties).toHaveLength(2);
214
+ const properties = result?.properties;
215
+ expect(properties).toHaveLength(2);
216
+ expect(properties[0].key.name).toEqual('a');
217
+ expect(properties[0].value.value).toBeFalsy();
218
+ expect(properties[1].key.name).toEqual('f');
219
+ expect(properties[1].value.value).toEqual(0);
220
+ });
221
+ it('composes proper object expression when property is a Template string', () => {
222
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
223
+ const propertyConext = {
224
+ type: common_shared_1.PropertyTypes.TEMPLATE_STRING,
225
+ usedInPriming: false,
226
+ functionIndex: 0,
227
+ isPublic: false,
228
+ };
229
+ const result = (0, genericAstGeneration_1.propertyToExpression)(moduleMetadata, propertyConext);
230
+ expect(result).not.toBeUndefined();
231
+ expect(result?.type).toEqual('ObjectExpression');
232
+ expect(result?.properties).toHaveLength(2);
233
+ const properties = result?.properties;
234
+ expect(properties).toHaveLength(2);
235
+ expect(properties[0].key.name).toEqual('a');
236
+ expect(properties[0].value.value).toBeFalsy();
237
+ expect(properties[1].key.name).toEqual('f');
238
+ expect(properties[1].value.value).toEqual(0);
239
+ });
240
+ it('composes proper object expression when property is a wire', () => {
241
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
242
+ const propertyConext = {
243
+ type: common_shared_1.PropertyTypes.WIRE,
244
+ usedInPriming: false,
245
+ functionIndex: 1,
246
+ isPublic: false,
247
+ };
248
+ const result = (0, genericAstGeneration_1.propertyToExpression)(moduleMetadata, propertyConext);
249
+ expect(result).not.toBeUndefined();
250
+ expect(result?.type).toEqual('ObjectExpression');
251
+ expect(result?.properties).toHaveLength(2);
252
+ const properties = result?.properties;
253
+ expect(properties).toHaveLength(2);
254
+ expect(properties[0].key.name).toEqual('a');
255
+ expect(properties[0].value.value).toBeFalsy();
256
+ expect(properties[1].key.name).toEqual('f');
257
+ expect(properties[1].value.value).toEqual(1);
258
+ });
259
+ it('composes proper object expression when property is a wire that is unresolvabe', () => {
260
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
261
+ const propertyConext = {
262
+ type: common_shared_1.PropertyTypes.WIRE,
263
+ usedInPriming: false,
264
+ functionIndex: common_shared_1.UNSUPPORTED_FUNCTION,
265
+ isPublic: false,
266
+ };
267
+ const result = (0, genericAstGeneration_1.propertyToExpression)(moduleMetadata, propertyConext);
268
+ expect(result).not.toBeUndefined();
269
+ expect(result?.type).toEqual('ObjectExpression');
270
+ expect(result?.properties).toHaveLength(2);
271
+ const properties = result?.properties;
272
+ expect(properties).toHaveLength(2);
273
+ expect(properties[0].key.name).toEqual('a');
274
+ expect(properties[0].value.value).toBeFalsy();
275
+ expect(properties[1].key.name).toEqual('g');
276
+ expect(properties[1].value.value).toEqual(undefined);
277
+ });
278
+ });
279
+ describe('processWireFunctionType', () => {
280
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
281
+ (0, common_shared_1.composeImportMetadata)('randomWire', 'laf/transformWire', true, '0/names/0', moduleMetadata);
282
+ (0, common_shared_1.composeImportMetadata)('apexWire', '@salesforce/apex/recordApi', true, '1/names/0', moduleMetadata);
283
+ (0, common_shared_1.composeImportMetadata)('getRecord', 'lightning/uiRecordApi', true, '2/names/0', moduleMetadata);
284
+ (0, common_shared_1.composeImportMetadata)('bacth_getRecord', 'lightning/uiRecordApi', true, '2/names/1', moduleMetadata);
285
+ (0, common_shared_1.composeImportMetadata)('reducer', 'lightning/uiRecordApi', true, '2/names/2', moduleMetadata);
286
+ (0, common_shared_1.composeImportMetadata)('CurrentPageReference', 'laf/transformWire', true, '3/names/0', moduleMetadata);
287
+ it('throws an error when a function that isnt a wire is passed in', () => {
288
+ const throwingFunc = () => (0, genericAstGeneration_1.processWireFunctionType)({
289
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
290
+ type: common_shared_1.FunctionTypes.GETTER_FUNCTION,
291
+ requiredProperties: [],
292
+ importReference: { functionName: '' },
293
+ }, moduleMetadata);
294
+ expect(throwingFunc).toThrow();
295
+ });
296
+ it('does nothing when function refers to a non existent import', () => {
297
+ const astStatements = (0, genericAstGeneration_1.processWireFunctionType)({
298
+ type: common_shared_1.FunctionTypes.WIRE_FUNCTION,
299
+ requiredProperties: [],
300
+ importReference: { functionName: 'test' },
301
+ }, moduleMetadata);
302
+ expect(astStatements).toBeUndefined();
303
+ });
304
+ it('effectively builds metadata for a non priming adapter wire', () => {
305
+ const astStatements = (0, genericAstGeneration_1.processWireFunctionType)({
306
+ type: common_shared_1.FunctionTypes.WIRE_FUNCTION,
307
+ requiredProperties: [],
308
+ input: [
309
+ {
310
+ type: 'ObjectValue',
311
+ value: {
312
+ fields: {
313
+ type: 'ArrayValue',
314
+ value: [
315
+ {
316
+ type: 'PrimitiveValue',
317
+ value: 'Account.Name',
318
+ },
319
+ ],
320
+ },
321
+ resourceId: {
322
+ type: 'PropertyReference',
323
+ value: 'recordId',
324
+ },
325
+ recordType: {
326
+ type: 'PropertyReference',
327
+ value: 'recordType',
328
+ },
329
+ },
330
+ },
331
+ ],
332
+ importReference: { functionName: 'randomWire' },
333
+ }, moduleMetadata);
334
+ expect(astStatements).not.toBeUndefined();
335
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
336
+ const { code } = (0, generator_1.default)(t.objectExpression(astStatements));
337
+ expect(code).toBe(`{
338
+ f: i0,
339
+ fN: "randomWire",
340
+ t: "WireFunction",
341
+ c: ctx => ({
342
+ "fields": ["Account.Name"],
343
+ "resourceId": ctx.props.recordId,
344
+ "recordType": ctx.props.recordType
345
+ }),
346
+ in: ["recordId", "recordType"]
347
+ }`);
348
+ });
349
+ it('effectively builds metadata for an apex wire', () => {
350
+ const astStatements = (0, genericAstGeneration_1.processWireFunctionType)({
351
+ type: common_shared_1.FunctionTypes.WIRE_FUNCTION,
352
+ requiredProperties: [],
353
+ importReference: { functionName: 'apexWire' },
354
+ }, moduleMetadata);
355
+ expect(astStatements).not.toBeUndefined();
356
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
357
+ const { code } = (0, generator_1.default)(t.objectExpression(astStatements));
358
+ expect(code).toBe(`{
359
+ f: i1,
360
+ fN: "recordApi",
361
+ t: "WireFunction",
362
+ wt: "apex",
363
+ c: ctx => ({})
364
+ }`);
365
+ });
366
+ it('effectively builds metadata for a context wire', () => {
367
+ const astStatements = (0, genericAstGeneration_1.processWireFunctionType)({
368
+ type: common_shared_1.FunctionTypes.WIRE_FUNCTION,
369
+ requiredProperties: [],
370
+ importReference: { functionName: 'CurrentPageReference' },
371
+ }, moduleMetadata);
372
+ expect(astStatements).not.toBeUndefined();
373
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
374
+ const { code } = (0, generator_1.default)(t.objectExpression(astStatements));
375
+ expect(code).toBe(`{
376
+ f: i5,
377
+ fN: "CurrentPageReference",
378
+ t: "WireFunction",
379
+ wt: "cpr",
380
+ c: ctx => ({})
381
+ }`);
382
+ });
383
+ it('effectively builds metadata for a priming adapter wire with neither a batcher or reducer', () => {
384
+ const astStatements = (0, genericAstGeneration_1.processWireFunctionType)({
385
+ type: common_shared_1.FunctionTypes.PRIMING_FUNCTION,
386
+ requiredProperties: [],
387
+ importReference: { functionName: 'getRecord' },
388
+ }, moduleMetadata);
389
+ expect(astStatements).not.toBeUndefined();
390
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
391
+ const { code } = (0, generator_1.default)(t.objectExpression(astStatements));
392
+ expect(code).toBe(`{
393
+ f: i2,
394
+ fN: "getRecord",
395
+ t: "PrimingFunction",
396
+ c: ctx => ({})
397
+ }`);
398
+ });
399
+ it('effectively builds metadata for a priming adapter wire without a reducer', () => {
400
+ const astStatements = (0, genericAstGeneration_1.processWireFunctionType)({
401
+ type: common_shared_1.FunctionTypes.PRIMING_FUNCTION,
402
+ requiredProperties: [],
403
+ importReference: {
404
+ functionName: 'getRecord',
405
+ batchingName: 'bacth_getRecord',
406
+ },
407
+ }, moduleMetadata);
408
+ expect(astStatements).not.toBeUndefined();
409
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
410
+ const { code } = (0, generator_1.default)(t.objectExpression(astStatements));
411
+ expect(code).toBe(`{
412
+ f: i2,
413
+ fN: "getRecord",
414
+ b: i3,
415
+ bN: "bacth_getRecord",
416
+ t: "PrimingFunction",
417
+ c: ctx => ({})
418
+ }`);
419
+ });
420
+ it('effectively builds metadata for a priming adapter wire', () => {
421
+ const astStatements = (0, genericAstGeneration_1.processWireFunctionType)({
422
+ type: common_shared_1.FunctionTypes.PRIMING_FUNCTION,
423
+ requiredProperties: [],
424
+ importReference: {
425
+ functionName: 'getRecord',
426
+ batchingName: 'bacth_getRecord',
427
+ reducer: 'reducer',
428
+ },
429
+ }, moduleMetadata);
430
+ expect(astStatements).not.toBeUndefined();
431
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
432
+ const { code } = (0, generator_1.default)(t.objectExpression(astStatements));
433
+ expect(code).toBe(`{
434
+ f: i2,
435
+ fN: "getRecord",
436
+ b: i3,
437
+ bN: "bacth_getRecord",
438
+ r: i4,
439
+ t: "PrimingFunction",
440
+ c: ctx => ({})
441
+ }`);
442
+ });
443
+ });
444
+ describe('processErrorFunction', () => {
445
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
446
+ it('should compose ast nodes properly for error function', () => {
447
+ const res = (0, genericAstGeneration_1.processErrorFunction)({
448
+ type: common_shared_1.FunctionTypes.ERROR,
449
+ input: [
450
+ {
451
+ type: 'ObjectValue',
452
+ value: {
453
+ error: {
454
+ type: 'PrimitiveValue',
455
+ value: common_shared_1.ERROR_PREFIX +
456
+ 'Something that is not an error was thrown',
457
+ },
458
+ },
459
+ },
460
+ ],
461
+ }, moduleMetadata);
462
+ expect(res).not.toBeUndefined();
463
+ const { code } = (0, generator_1.default)(t.objectExpression(res));
464
+ expect(code).toBe(`{
465
+ t: "ErrorFunction",
466
+ c: {
467
+ "error": "[komaci] Something that is not an error was thrown"
468
+ }
469
+ }`);
470
+ });
471
+ });
472
+ describe('addImportStatements', () => {
473
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
474
+ (0, common_shared_1.composeImportMetadata)('default', 'lightning/uiTestingApi', true, '0/names/0', moduleMetadata, 'test');
475
+ (0, common_shared_1.composeImportMetadata)('regularImport', 'lightning/uiTestingApi', true, '0/names/1', moduleMetadata);
476
+ (0, common_shared_1.composeImportMetadata)('tes1', 'badImport', false, '1/names/0', moduleMetadata);
477
+ (0, common_shared_1.composeImportMetadata)('tes2', 'badImport', false, '1/names/1', moduleMetadata);
478
+ (0, common_shared_1.composeImportMetadata)('*', '@salesforce/labels', true, '2/names/0', moduleMetadata, 'labels');
479
+ (0, common_shared_1.composeImportMetadata)('tes3', 'lightning/uiRecordApi', true, '3/names/0', moduleMetadata);
480
+ (0, common_shared_1.composeImportMetadata)('tes4', 'lightning/uiRecordApi', true, '3/names/1', moduleMetadata, 'changed');
481
+ (0, common_shared_1.composeImportMetadata)('tes5', 'lightning/uiRecordApi', true, '3/names/2', moduleMetadata);
482
+ (0, common_shared_1.composeImportMetadata)('adg', '@salesforce/komaci/c__foo__foo.js', true, '4/names/0', moduleMetadata);
483
+ moduleMetadata.importsByNamespace.set('lightning/uiTestingApi', {
484
+ names: new Set(['default', 'regularImport']),
485
+ isDefaultSpecifier: true,
486
+ isSupported: true,
487
+ isNamespaceSpecifier: false,
488
+ resourceName: 'lightning/uiTestingApi',
489
+ });
490
+ moduleMetadata.importsByNamespace.set('badImport', {
491
+ names: new Set(['tes1', 'tes2']),
492
+ isDefaultSpecifier: false,
493
+ isSupported: false,
494
+ isNamespaceSpecifier: false,
495
+ resourceName: 'badImport',
496
+ });
497
+ moduleMetadata.importsByNamespace.set('@salesforce/labels', {
498
+ names: new Set(['*']),
499
+ isDefaultSpecifier: false,
500
+ isSupported: true,
501
+ isNamespaceSpecifier: true,
502
+ resourceName: '@salesforce/labels',
503
+ });
504
+ moduleMetadata.importsByNamespace.set('lightning/uiRecordApi', {
505
+ names: new Set(['tes3', 'tes4', 'tes5']),
506
+ isDefaultSpecifier: false,
507
+ isSupported: true,
508
+ isNamespaceSpecifier: false,
509
+ resourceName: 'lightning/uiRecordApi',
510
+ });
511
+ moduleMetadata.importsByNamespace.set('@salesforce/komaci/c__foo__foo.js', {
512
+ names: new Set(['adg']),
513
+ isDefaultSpecifier: false,
514
+ isSupported: true,
515
+ isNamespaceSpecifier: false,
516
+ resourceName: '@salesforce/komaci/c__foo__foo.js',
517
+ });
518
+ // lazy way to say almost all are used in priming
519
+ for (const importMetadata of moduleMetadata.importsByName.values()) {
520
+ importMetadata.usedInPriming =
521
+ importMetadata.name !== 'tes5' && importMetadata.name !== 'tes2';
522
+ }
523
+ const findImportDeclaration = (statements, resourceName) => statements.find((statement) => statement.source.value === resourceName);
524
+ const validateForEachList = (statements, cb) => statements.forEach(cb);
525
+ const scriptModuleOnlyStatements = [];
526
+ const combinedModuleStatements = [];
527
+ (0, genericAstGeneration_1.addImportStatements)(moduleMetadata, scriptModuleOnlyStatements, {
528
+ calledFromGenerateCombinedModule: false,
529
+ });
530
+ (0, genericAstGeneration_1.addImportStatements)(moduleMetadata, combinedModuleStatements, {
531
+ calledFromGenerateCombinedModule: true,
532
+ });
533
+ it('can compose an import with a default and a normal import together', () => {
534
+ validateForEachList([scriptModuleOnlyStatements, combinedModuleStatements], (statements) => {
535
+ const importStatement = findImportDeclaration(statements, 'lightning/uiTestingApi');
536
+ expect(importStatement).not.toBeUndefined();
537
+ expect(importStatement.specifiers).toHaveLength(2);
538
+ expect(importStatement.specifiers[0].type).toBe('ImportDefaultSpecifier');
539
+ expect(importStatement
540
+ .specifiers[0].local.name).toBe(moduleMetadata.importsByName.get('test')?.generatorAlias);
541
+ expect(importStatement.specifiers[1].type).toBe('ImportSpecifier');
542
+ expect(importStatement
543
+ .specifiers[1].local.name).toBe(moduleMetadata.importsByName.get('regularImport')?.generatorAlias);
544
+ expect(importStatement
545
+ .specifiers[1].imported.name).toBe('regularImport');
546
+ });
547
+ });
548
+ it('will not compose a bad import', () => {
549
+ validateForEachList([scriptModuleOnlyStatements, combinedModuleStatements], (statements) => {
550
+ const importStatement = findImportDeclaration(statements, 'badImport');
551
+ expect(importStatement).toBeUndefined();
552
+ });
553
+ });
554
+ it('can compose an import with a namespace import', () => {
555
+ validateForEachList([scriptModuleOnlyStatements, combinedModuleStatements], (statements) => {
556
+ const importStatement = findImportDeclaration(statements, '@salesforce/labels');
557
+ expect(importStatement).not.toBeUndefined();
558
+ expect(importStatement.specifiers).toHaveLength(1);
559
+ expect(importStatement.specifiers[0].type).toBe('ImportNamespaceSpecifier');
560
+ expect(importStatement
561
+ .specifiers[0].local.name).toBe(moduleMetadata.importsByName.get('labels')?.generatorAlias);
562
+ });
563
+ });
564
+ it('will leave out an import not used in priming from the composed specifiers', () => {
565
+ validateForEachList([scriptModuleOnlyStatements, combinedModuleStatements], (statements) => {
566
+ const importStatement = findImportDeclaration(statements, 'lightning/uiRecordApi');
567
+ expect(importStatement).not.toBeUndefined();
568
+ expect(importStatement.specifiers).toHaveLength(2);
569
+ expect(importStatement.specifiers[0].type).toBe('ImportSpecifier');
570
+ expect(importStatement
571
+ .specifiers[0].imported.name).toBe('tes3');
572
+ expect(importStatement.specifiers[1].type).toBe('ImportSpecifier');
573
+ expect(importStatement
574
+ .specifiers[1].local.name).toBe(moduleMetadata.importsByName.get('changed')?.generatorAlias);
575
+ expect(importStatement
576
+ .specifiers[1].imported.name).toBe('tes4');
577
+ });
578
+ });
579
+ it('will lop the last portion of a @salesforce/komaci import off if its a combined module only', () => {
580
+ const scriptImport = findImportDeclaration(scriptModuleOnlyStatements, '@salesforce/komaci/c__foo__foo.js');
581
+ expect(scriptImport).not.toBeUndefined();
582
+ const combinedImport = findImportDeclaration(combinedModuleStatements, '@salesforce/komaci/c__foo');
583
+ expect(combinedImport).not.toBeUndefined();
584
+ });
585
+ });
586
+ describe('collectMetadataForNonDefaultTemplates', () => {
587
+ const templateMap = {};
588
+ beforeEach(() => {
589
+ // fixture-template-files/component-refernece
590
+ templateMap['component-reference'] = {
591
+ compositions: [
592
+ {
593
+ type: 'Container',
594
+ compositions: [
595
+ {
596
+ type: 'ComposedAdg',
597
+ input: {
598
+ type: 'ImportReference',
599
+ value: '0/names/0',
600
+ },
601
+ properties: {},
602
+ },
603
+ {
604
+ type: 'ComposedAdg',
605
+ input: {
606
+ type: 'ImportReference',
607
+ value: '0/names/0',
608
+ },
609
+ properties: {
610
+ attribute1: {
611
+ type: 'PrimitiveValue',
612
+ value: 'literalString1',
613
+ },
614
+ attribute2: {
615
+ type: 'PrimitiveValue',
616
+ value: 'lteralString2',
617
+ },
618
+ },
619
+ },
620
+ {
621
+ type: 'ComposedAdg',
622
+ input: {
623
+ type: 'ImportReference',
624
+ value: '0/names/0',
625
+ },
626
+ properties: {
627
+ attribute1: {
628
+ type: 'Binding',
629
+ value: 'record',
630
+ },
631
+ attribute2: {
632
+ type: 'Binding',
633
+ value: 'recordId',
634
+ },
635
+ attribute3: {
636
+ type: 'Binding',
637
+ value: 'record/data/Name',
638
+ },
639
+ },
640
+ },
641
+ {
642
+ type: 'ComposedAdg',
643
+ input: {
644
+ type: 'ImportReference',
645
+ value: '0/names/0',
646
+ },
647
+ properties: {
648
+ attribute1: {
649
+ type: 'PrimitiveValue',
650
+ value: 'literalString1',
651
+ },
652
+ attribute2: {
653
+ type: 'Binding',
654
+ value: 'recordId',
655
+ },
656
+ attribute3: {
657
+ type: 'PrimitiveValue',
658
+ value: true,
659
+ },
660
+ },
661
+ },
662
+ {
663
+ type: 'ComposedAdg',
664
+ input: {
665
+ type: 'ImportReference',
666
+ value: '0/names/0',
667
+ },
668
+ properties: {
669
+ attributeUpperCase: {
670
+ type: 'PrimitiveValue',
671
+ value: 'convert kebab to camel',
672
+ },
673
+ },
674
+ },
675
+ {
676
+ type: 'ComposedAdg',
677
+ input: {
678
+ type: 'ImportReference',
679
+ value: '0/names/0',
680
+ },
681
+ properties: {
682
+ StartsWithUpperCase: {
683
+ type: 'PrimitiveValue',
684
+ value: 'should be StartsWithUpperCase',
685
+ },
686
+ },
687
+ },
688
+ {
689
+ type: 'ComposedAdg',
690
+ input: {
691
+ type: 'ImportReference',
692
+ value: '1/names/0',
693
+ },
694
+ properties: {},
695
+ },
696
+ {
697
+ type: 'ComposedAdg',
698
+ input: {
699
+ type: 'ImportReference',
700
+ value: '2/names/0',
701
+ },
702
+ properties: {
703
+ label: {
704
+ type: 'PrimitiveValue',
705
+ value: 'Base',
706
+ },
707
+ title: {
708
+ type: 'PrimitiveValue',
709
+ value: 'Looks like a link',
710
+ },
711
+ variant: {
712
+ type: 'PrimitiveValue',
713
+ value: 'base',
714
+ },
715
+ },
716
+ },
717
+ ],
718
+ },
719
+ ],
720
+ imports: [
721
+ {
722
+ resourceName: '@salesforce/komaci/c__child1__child1.js',
723
+ names: ['default'],
724
+ },
725
+ {
726
+ resourceName: '@salesforce/komaci/c__upperCaseInName__upperCaseInName.js',
727
+ names: ['default'],
728
+ },
729
+ {
730
+ resourceName: '@salesforce/komaci/lightning__button__button.js',
731
+ names: ['default'],
732
+ },
733
+ ],
734
+ exports: {
735
+ default: {
736
+ type: 'CompositionReference',
737
+ value: 0,
738
+ },
739
+ },
740
+ };
741
+ //fixture-template-files/conditional-simple
742
+ templateMap['conditional-simple'] = {
743
+ compositions: [
744
+ {
745
+ type: 'Container',
746
+ compositions: [
747
+ {
748
+ type: 'Container',
749
+ isActive: {
750
+ input: {
751
+ type: 'Binding',
752
+ value: 'someCondition',
753
+ },
754
+ negate: false,
755
+ },
756
+ compositions: [
757
+ {
758
+ type: 'ComposedAdg',
759
+ input: {
760
+ type: 'ImportReference',
761
+ value: '0/names/0',
762
+ },
763
+ properties: {},
764
+ },
765
+ {
766
+ type: 'ComposedAdg',
767
+ input: {
768
+ type: 'ImportReference',
769
+ value: '1/names/0',
770
+ },
771
+ properties: {},
772
+ },
773
+ ],
774
+ },
775
+ ],
776
+ },
777
+ ],
778
+ exports: {
779
+ default: {
780
+ type: 'CompositionReference',
781
+ value: 0,
782
+ },
783
+ },
784
+ imports: [
785
+ {
786
+ resourceName: '@salesforce/komaci/c__child1__child1.js',
787
+ names: ['default'],
788
+ },
789
+ {
790
+ resourceName: '@salesforce/komaci/c__child2__child2.js',
791
+ names: ['default'],
792
+ },
793
+ ],
794
+ };
795
+ });
796
+ it('collects metadata info for non default templates', () => {
797
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
798
+ moduleMetadata.templateKomaciDocMap = templateMap;
799
+ moduleMetadata.adgGeneratorIndex = 1;
800
+ const defaultAdg = (0, common_shared_1.initAdgMetadataObject)('adg0');
801
+ moduleMetadata.adgs.set(0, defaultAdg);
802
+ const templateImport = {
803
+ name: 'default',
804
+ komaciDocImportRef: '0/names/0',
805
+ importAlias: 'child1',
806
+ generatorAlias: 'i0',
807
+ resourceName: './conditional-simple',
808
+ staticallyAnalyzable: true,
809
+ templateKomaciDoc: templateMap['conditional-simple'],
810
+ usedInPriming: true,
811
+ isFromInternalNamespace: true,
812
+ isFromSupportedLibrary: true,
813
+ };
814
+ moduleMetadata.importsByName.set('child1', templateImport);
815
+ //assuming that component-reference is the default.
816
+ (0, genericAstGeneration_1.collectMetadataForNonDefaultTemplates)(moduleMetadata, 'component-reference');
817
+ expect(moduleMetadata.adgGeneratorIndex).toBe(2);
818
+ expect(moduleMetadata.importGeneratorIndex).toBe(2);
819
+ expect(moduleMetadata.importIndexReference.get('t1-0/names/0')).not.toBeUndefined();
820
+ expect(moduleMetadata.importIndexReference.get('t1-1/names/0')).not.toBeUndefined();
821
+ });
822
+ it('collects metadata info for non default templates, multiple non default templates', () => {
823
+ templateMap['default-template'] = {};
824
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
825
+ moduleMetadata.templateKomaciDocMap = templateMap;
826
+ moduleMetadata.adgGeneratorIndex = 1;
827
+ const defaultAdg = (0, common_shared_1.initAdgMetadataObject)('adg0');
828
+ moduleMetadata.adgs.set(0, defaultAdg);
829
+ const templateImport = {
830
+ name: 'default',
831
+ komaciDocImportRef: '0/names/0',
832
+ importAlias: 'child1',
833
+ generatorAlias: 'i0',
834
+ resourceName: './component-reference',
835
+ staticallyAnalyzable: true,
836
+ templateKomaciDoc: templateMap['component-reference'],
837
+ usedInPriming: true,
838
+ isFromInternalNamespace: true,
839
+ isFromSupportedLibrary: true,
840
+ };
841
+ moduleMetadata.importsByName.set('child1', templateImport);
842
+ const templateImport2 = {
843
+ name: 'default',
844
+ komaciDocImportRef: '1/names/0',
845
+ importAlias: 'child2',
846
+ generatorAlias: 'i1',
847
+ resourceName: './conditional-simple',
848
+ staticallyAnalyzable: true,
849
+ templateKomaciDoc: templateMap['conditional-simple'],
850
+ usedInPriming: true,
851
+ isFromInternalNamespace: true,
852
+ isFromSupportedLibrary: true,
853
+ };
854
+ moduleMetadata.importsByName.set('child2', templateImport2);
855
+ //assuming that component-reference is the default.
856
+ (0, genericAstGeneration_1.collectMetadataForNonDefaultTemplates)(moduleMetadata, 'default-template');
857
+ expect(moduleMetadata.adgGeneratorIndex).toBe(3);
858
+ expect(moduleMetadata.importGeneratorIndex).toBe(5);
859
+ expect(moduleMetadata.importIndexReference.get('t1-0/names/0')).not.toBeUndefined();
860
+ expect(moduleMetadata.importIndexReference.get('t1-1/names/0')).not.toBeUndefined();
861
+ expect(moduleMetadata.importIndexReference.get('t1-2/names/0')).not.toBeUndefined();
862
+ expect(moduleMetadata.importIndexReference.get('t2-0/names/0')).not.toBeUndefined();
863
+ expect(moduleMetadata.importIndexReference.get('t2-1/names/0')).not.toBeUndefined();
864
+ });
865
+ it('no changes when only looking at the default', () => {
866
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
867
+ moduleMetadata.templateKomaciDocMap = templateMap;
868
+ moduleMetadata.adgGeneratorIndex = 1;
869
+ const defaultAdg = (0, common_shared_1.initAdgMetadataObject)('adg0');
870
+ moduleMetadata.adgs.set(0, defaultAdg);
871
+ //assuming that component-reference is the default.
872
+ (0, genericAstGeneration_1.collectMetadataForNonDefaultTemplates)(moduleMetadata, 'component-reference');
873
+ expect(moduleMetadata.adgGeneratorIndex).toBe(1);
874
+ expect(moduleMetadata.importGeneratorIndex).toBe(0);
875
+ });
876
+ });
877
+ //# sourceMappingURL=genericAstGeneration.spec.js.map