@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
package/build/index.js CHANGED
@@ -28,39 +28,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.processGeneratorInputAndState = exports.generateKomaciModule = void 0;
30
30
  const generator_1 = __importDefault(require("@babel/generator"));
31
- const shared_1 = require("./shared");
32
- const modGenScript_1 = require("./modGenScript");
33
- const modGenTemplate_1 = require("./modGenTemplate");
31
+ const componentAstProcessing_1 = require("./componentAstProcessing");
32
+ const functionAstGeneration_1 = require("./functionAstGeneration");
33
+ const types_1 = require("./types");
34
34
  const t = __importStar(require("@babel/types"));
35
+ const genericAstGeneration_1 = require("./genericAstGeneration");
36
+ const common_shared_1 = require("@komaci/common-shared");
35
37
  // Generate a module adhering to the Komaci Runtime Format which is used
36
38
  // by the Komaci Resolution Engine to drive dependency resolution.
37
39
  function generateKomaciModule(input) {
38
- // Initial state for this call to module generator.
39
- const generatorState = {
40
- input,
41
- adgImportSuffix: 0,
42
- importAliasSuffix: 0,
43
- scriptGenState: {
44
- importRefsMap: {},
45
- requiredFactoryFunctions: new Set(),
46
- importInfosForWireAdapter: new Set(),
47
- primingAdapterImportDeclarations: [],
48
- },
49
- templateGenState: {
50
- importRefsMap: {},
51
- requiredFactoryFunctions: new Set(),
52
- refProps: new Set(),
53
- },
54
- iterationContext: [],
55
- defaultCompImported: false,
56
- adgNameMap: {},
57
- isDefaultExportFound: false,
58
- };
59
40
  try {
60
- return processGeneratorInputAndState(input, generatorState);
41
+ return processGeneratorInputAndState(input);
61
42
  }
62
43
  catch (e) {
63
- const babelOutput = (0, generator_1.default)(t.file((0, modGenScript_1.produceErrorAdgFunction)(generatorState, e)));
44
+ const moduleMetadata = (0, common_shared_1.initModuleMetadataObject)();
45
+ const babelOutput = (0, generator_1.default)(t.file((0, functionAstGeneration_1.produceErrorAdgFunction)(moduleMetadata, e)));
64
46
  return babelOutput.code;
65
47
  }
66
48
  }
@@ -68,26 +50,26 @@ exports.generateKomaciModule = generateKomaciModule;
68
50
  /**
69
51
  * Helper function to process and build the resolvable module based off the generator input and inital generator state.
70
52
  * @param input the Generator input
71
- * @param generatorState the inital generator state
72
53
  * @returns the resolvable module.
73
54
  */
74
- function processGeneratorInputAndState(input, generatorState) {
55
+ function processGeneratorInputAndState(input) {
75
56
  if (input.moduleInfo.type === 'file') {
76
57
  // Determine file type based on extension in file name.
77
58
  const segments = input.moduleInfo.fileName.split('.');
78
59
  if (segments.length < 2) {
79
- throw new Error(shared_1.ERROR_PREFIX + 'unable to determine file type');
60
+ throw new Error(types_1.ERROR_PREFIX + 'unable to determine file type');
80
61
  }
81
62
  const fileExt = segments[segments.length - 1];
82
63
  let sourceFile;
64
+ const source = (0, componentAstProcessing_1.getComponentSrcJs)(input);
83
65
  if (fileExt === 'html') {
84
- sourceFile = (0, modGenTemplate_1.generateTemplateModule)(generatorState, input.moduleInfo.komaciDoc);
66
+ sourceFile = (0, genericAstGeneration_1.generateResolvableModule)(input, undefined, input.moduleInfo.komaciDoc, source);
85
67
  }
86
68
  else if (fileExt === 'js') {
87
- sourceFile = (0, modGenScript_1.generateScriptModule)(generatorState, input.moduleInfo.komaciDoc);
69
+ sourceFile = (0, genericAstGeneration_1.generateResolvableModule)(input, input.moduleInfo.komaciDoc, undefined, source);
88
70
  }
89
71
  else {
90
- throw new Error(shared_1.ERROR_PREFIX + 'unsupported file extension: ' + fileExt);
72
+ throw new Error(types_1.ERROR_PREFIX + 'unsupported file extension: ' + fileExt);
91
73
  }
92
74
  const babelOutput = (0, generator_1.default)(sourceFile);
93
75
  return babelOutput.code;
@@ -102,7 +84,7 @@ function processGeneratorInputAndState(input, generatorState) {
102
84
  // Determine file type based on extension in file name.
103
85
  const segments = fullFilename.split('.');
104
86
  if (segments.length < 2) {
105
- throw new Error(shared_1.ERROR_PREFIX + 'unable to determine file type');
87
+ throw new Error(types_1.ERROR_PREFIX + 'unable to determine file type');
106
88
  }
107
89
  filename = segments[0]; // the filename, up to but not including, the file separator character
108
90
  fileExt = segments[segments.length - 1]; // filename extension, not including the separator
@@ -123,9 +105,10 @@ function processGeneratorInputAndState(input, generatorState) {
123
105
  filesMap['js'] = filesObj;
124
106
  }
125
107
  else {
126
- throw new Error(shared_1.ERROR_PREFIX + 'unsupported file extension: ' + fileExt);
108
+ throw new Error(types_1.ERROR_PREFIX + 'unsupported file extension: ' + fileExt);
127
109
  }
128
110
  }
111
+ const source = (0, componentAstProcessing_1.getComponentSrcJs)(input);
129
112
  const komaciJSDoc = filesMap && filesMap['js'] && filesMap['js'][input.moduleInfo.name]
130
113
  ? filesMap['js'][input.moduleInfo.name]
131
114
  : {};
@@ -133,12 +116,12 @@ function processGeneratorInputAndState(input, generatorState) {
133
116
  ? filesMap['html'][input.moduleInfo.name]
134
117
  : {};
135
118
  // eslint-disable-next-line prefer-const
136
- sourceFile = (0, modGenScript_1.generateCombinedModule)(generatorState, komaciJSDoc, komaciHTMLDoc);
119
+ sourceFile = (0, genericAstGeneration_1.generateResolvableModule)(input, komaciJSDoc, komaciHTMLDoc, source);
137
120
  // Generate source from constructed AST.
138
121
  const babelOutput = (0, generator_1.default)(sourceFile);
139
122
  return babelOutput.code;
140
123
  }
141
- throw new Error(shared_1.ERROR_PREFIX + 'unrecognized input type');
124
+ throw new Error(types_1.ERROR_PREFIX + 'unrecognized input type');
142
125
  }
143
126
  exports.processGeneratorInputAndState = processGeneratorInputAndState;
144
127
  //# sourceMappingURL=index.js.map
package/build/types.d.ts CHANGED
@@ -1,6 +1,17 @@
1
1
  import * as t from '@babel/types';
2
2
  import { KomaciDocument } from '@komaci/types';
3
- import { AstContext } from '@komaci/common-shared';
3
+ import { IdKeys } from '@komaci/common-shared';
4
+ export declare const ERROR_PREFIX = "[komaci esm-generator] ";
5
+ export declare const CONTEXT = "ctx";
6
+ export declare const PROPS = "props";
7
+ export declare const ADG_IMPORT_SUFFIX = "getAdgDefinition";
8
+ export declare const UNRESOLVED = "unresolved";
9
+ export declare const USER_VARIABLE_PREFIX = "user_var_";
10
+ export declare const WIRE_FUNCTION = "WireFunction";
11
+ export declare const GETTER_FUNCTION = "GetterFunction";
12
+ export declare const ERROR_FUNCTION = "ErrorFunction";
13
+ export declare const PRIMING_FUNCTION = "PrimingFunction";
14
+ export declare const ID: Record<IdKeys, t.Identifier>;
4
15
  export declare type GeneratorInput = {
5
16
  moduleInfo: ModuleFileInfo | ModuleBundleInfo;
6
17
  options?: GeneratorOptions;
@@ -38,7 +49,7 @@ export declare type GeneratorState = {
38
49
  importRefsMap: {
39
50
  [refId: string]: ImportInfo;
40
51
  };
41
- importMetadata?: AstContext['importDeclarations'];
52
+ importMetadata?: unknown;
42
53
  importInfosForWireAdapter: Set<ImportInfo>;
43
54
  requiredFactoryFunctions: Set<t.Identifier>;
44
55
  primingAdapterImportDeclarations: t.ImportDeclaration[];
package/build/types.js CHANGED
@@ -1,6 +1,74 @@
1
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
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ImportUsageState = void 0;
26
+ exports.ImportUsageState = exports.ID = exports.PRIMING_FUNCTION = exports.ERROR_FUNCTION = exports.GETTER_FUNCTION = exports.WIRE_FUNCTION = exports.USER_VARIABLE_PREFIX = exports.UNRESOLVED = exports.ADG_IMPORT_SUFFIX = exports.PROPS = exports.CONTEXT = exports.ERROR_PREFIX = void 0;
27
+ const t = __importStar(require("@babel/types"));
28
+ //////////////////////////////////////////////////////////////
29
+ // Types for interacting with Komaci Module Generator.
30
+ /////////////////////////////////////////////////////////////
31
+ exports.ERROR_PREFIX = '[komaci esm-generator] ';
32
+ exports.CONTEXT = 'ctx';
33
+ exports.PROPS = 'props';
34
+ exports.ADG_IMPORT_SUFFIX = 'getAdgDefinition';
35
+ exports.UNRESOLVED = 'unresolved';
36
+ exports.USER_VARIABLE_PREFIX = 'user_var_';
37
+ exports.WIRE_FUNCTION = 'WireFunction';
38
+ exports.GETTER_FUNCTION = 'GetterFunction';
39
+ exports.ERROR_FUNCTION = 'ErrorFunction';
40
+ exports.PRIMING_FUNCTION = 'PrimingFunction';
41
+ exports.ID = {
42
+ CONTEXT: t.identifier(exports.CONTEXT),
43
+ PROPS: t.identifier(exports.PROPS),
44
+ FACTORY: t.identifier('fct'),
45
+ COMPOSED_GRAPH_FUNC: t.identifier('cg'),
46
+ ITERATION_FUNC: t.identifier('i'),
47
+ UNRESOLVED_FUNC: t.identifier('u'),
48
+ SLOT_FUNC: t.identifier('s'),
49
+ ELEMENT_FUNC: t.identifier('e'),
50
+ ADG_FUNC: t.identifier('adg'),
51
+ SLOT: t.identifier('slot'),
52
+ COMPONENT_ID: t.identifier('componentId'),
53
+ UNDEFINED: t.identifier('undefined'),
54
+ PROP_API: t.identifier('a'),
55
+ PROP_INIT: t.identifier('i'),
56
+ PROP_FUNC: t.identifier('f'),
57
+ PROP_GETTER: t.identifier('g'),
58
+ DEFAULT_COMP: t.identifier('comp1'),
59
+ FUNC_ID: t.identifier('f'),
60
+ FUNC_NAME: t.identifier('fN'),
61
+ FUNC_TYPE: t.identifier('t'),
62
+ FUNC_CONFIG: t.identifier('c'),
63
+ FUNC_INPUT: t.identifier('in'),
64
+ WIRE_TYPE: t.identifier('wt'),
65
+ UNRESOLVED_VALUE: t.identifier('unresolved'),
66
+ BATCH_FUNC_ID: t.identifier('b'),
67
+ BATCH_FUNC_NAME: t.identifier('bN'),
68
+ REDUCER_FUNC_ID: t.identifier('r'),
69
+ IMG_SRC: t.identifier('src'),
70
+ IMG_FUNC: t.identifier('img'),
71
+ };
4
72
  // enum representing the different states of the way an Import is used
5
73
  var ImportUsageState;
6
74
  (function (ImportUsageState) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@komaci/esm-generator",
3
- "version": "242.0.0",
3
+ "version": "242.1.0",
4
4
  "description": "Komaci generator for ADG ES modules",
5
5
  "homepage": "https://komaci.dev/",
6
6
  "repository": {
@@ -28,13 +28,13 @@
28
28
  "dependencies": {
29
29
  "@babel/core": "^7.9.0",
30
30
  "@babel/generator": "^7.9.0",
31
- "@komaci/common-shared": "242.0.0",
32
- "@komaci/static-analyzer": "242.0.0",
31
+ "@komaci/common-shared": "242.1.0",
32
+ "@komaci/static-analyzer": "242.1.0",
33
33
  "o11y": "^240.7.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@babel/types": "^7.9.0",
37
- "@komaci/types": "242.0.0",
37
+ "@komaci/types": "242.1.0",
38
38
  "@lwc-platform/sfdc-lwc-compiler": "^2.18.0-240.2"
39
39
  }
40
40
  }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=modGenScript.spec.d.ts.map
@@ -1,110 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const fs_1 = __importDefault(require("fs"));
7
- const path_1 = require("path");
8
- const modGenScript_1 = require("../modGenScript");
9
- const generator_1 = __importDefault(require("@babel/generator"));
10
- const types_1 = require("@babel/types");
11
- const initGeneratorState = (input) => ({
12
- input,
13
- adgImportSuffix: 0,
14
- importAliasSuffix: 0,
15
- scriptGenState: {
16
- importRefsMap: {},
17
- requiredFactoryFunctions: new Set([]),
18
- importInfosForWireAdapter: new Set([]),
19
- primingAdapterImportDeclarations: [],
20
- },
21
- templateGenState: {
22
- importRefsMap: {},
23
- requiredFactoryFunctions: new Set(),
24
- refProps: new Set(),
25
- },
26
- iterationContext: [],
27
- defaultCompImported: true,
28
- adgNameMap: {},
29
- isDefaultExportFound: true,
30
- });
31
- describe('generateScriptModule', () => {
32
- it('should not process items requiring the AST if the source is not provided', () => {
33
- const komaciDoc = JSON.parse(fs_1.default.readFileSync((0, path_1.join)(__dirname, './fixtures-script-files/class-props/komaciDoc.json'), 'utf-8'));
34
- const filesMap = {};
35
- filesMap['test.js'] = komaciDoc;
36
- const input = {
37
- moduleInfo: {
38
- name: 'test',
39
- namespace: 'lightning',
40
- type: 'bundle',
41
- files: filesMap,
42
- },
43
- srcFileMap: {
44
- 'somethingElse.js': 'eval("bye")',
45
- 'test.css': 'p { text-align: center; }',
46
- 'test.html': '<html><body><h1>Hey</h1></body></html>',
47
- },
48
- };
49
- const generatorState = initGeneratorState(input);
50
- (0, modGenScript_1.generateScriptModule)(generatorState, komaciDoc);
51
- expect(komaciDoc.adgs?.[0].functions?.find((f) => f.type === 'GetterFunction')).toBeUndefined();
52
- });
53
- it('should process items requiring ast if the source is provided', () => {
54
- const src = fs_1.default.readFileSync((0, path_1.join)(__dirname, './fixtures-script-files/class-props/actual.js'), 'utf-8');
55
- const komaciDoc = JSON.parse(fs_1.default.readFileSync((0, path_1.join)(__dirname, './fixtures-script-files/class-props/komaciDoc.json'), 'utf-8'));
56
- const filesMap = {};
57
- filesMap['test.js'] = komaciDoc;
58
- const input = {
59
- moduleInfo: {
60
- name: 'test',
61
- namespace: 'lightning',
62
- type: 'bundle',
63
- files: filesMap,
64
- },
65
- srcFileMap: {
66
- 'test.js': src,
67
- 'test.css': 'p { text-align: center; }',
68
- 'test.html': '<html><body><h1>Hey</h1></body></html>',
69
- },
70
- };
71
- const generatorState = initGeneratorState(input);
72
- (0, modGenScript_1.generateScriptModule)(generatorState, komaciDoc);
73
- expect(komaciDoc.adgs?.[0].functions?.find((f) => f.type === 'GetterFunction')).not.toBeUndefined();
74
- });
75
- });
76
- describe('produceErrorAdgFunction', () => {
77
- it('should produce a resolvable module with an adg with only one node that shows what the error was', () => {
78
- const input = {
79
- moduleInfo: {
80
- name: 'test',
81
- namespace: 'lightning',
82
- type: 'bundle',
83
- files: {},
84
- },
85
- srcFileMap: {
86
- 'test.js': '',
87
- 'test.css': 'p { text-align: center; }',
88
- 'test.html': '<html><body><h1>Hey</h1></body></html>',
89
- },
90
- };
91
- const generatorState = initGeneratorState(input);
92
- const expected = `export default function (fct, ctx) {
93
- const {
94
- adg
95
- } = fct;
96
- return adg(undefined, {}, [{
97
- t: "ErrorFunction",
98
- c: {
99
- "error": "something bad happened",
100
- "stack": "full stack normally"
101
- }
102
- }], comp1, []);
103
- }`;
104
- const testError = new Error('something bad happened');
105
- testError.stack = 'full stack normally';
106
- const res = (0, modGenScript_1.produceErrorAdgFunction)(generatorState, testError);
107
- expect((0, generator_1.default)((0, types_1.file)(res)).code).toBe(expected);
108
- });
109
- });
110
- //# sourceMappingURL=modGenScript.spec.js.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=modGenTemplate.spec.d.ts.map
@@ -1,196 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const modGenTemplate_1 = require("../modGenTemplate");
4
- const types_1 = require("@babel/types");
5
- const shared_1 = require("../shared");
6
- const initGeneratorState = (input) => ({
7
- input,
8
- adgImportSuffix: 0,
9
- importAliasSuffix: 0,
10
- scriptGenState: {
11
- importRefsMap: {},
12
- requiredFactoryFunctions: new Set([]),
13
- importInfosForWireAdapter: new Set([]),
14
- primingAdapterImportDeclarations: [],
15
- },
16
- templateGenState: {
17
- importRefsMap: {},
18
- requiredFactoryFunctions: new Set(),
19
- refProps: new Set(),
20
- },
21
- iterationContext: [],
22
- defaultCompImported: true,
23
- adgNameMap: {},
24
- isDefaultExportFound: true,
25
- });
26
- const generatorInput = {
27
- moduleInfo: {
28
- name: 'test',
29
- namespace: 'lightning',
30
- komaciDoc: {},
31
- fileName: 'test.html',
32
- type: 'file',
33
- },
34
- };
35
- describe('imageToExpression', () => {
36
- it('should return an expression for a KomaciDocument Image node', () => {
37
- const komaciDocImagePrimitive = {
38
- type: 'Image',
39
- src: {
40
- type: 'PrimitiveValue',
41
- value: 'testPrimitive.jpg', // this is string literal of a relative URL
42
- },
43
- };
44
- const generatorState = initGeneratorState(generatorInput);
45
- let expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImagePrimitive);
46
- expect(expression.type).toBe('CallExpression');
47
- let callExpression = expression;
48
- expect(callExpression).not.toBeUndefined();
49
- expect(callExpression.callee).toBe(shared_1.ID.IMG_FUNC);
50
- expect(callExpression.arguments).not.toBeUndefined();
51
- expect(callExpression.arguments).toHaveLength(1);
52
- expect(callExpression.arguments[0]).not.toBeUndefined();
53
- expect(callExpression.arguments[0].type).toBe('ObjectExpression');
54
- let objExpression = callExpression
55
- .arguments[0];
56
- expect(objExpression.properties).not.toBeUndefined();
57
- expect(objExpression.properties).toHaveLength(1);
58
- expect(objExpression.properties[0].type).toBe('ObjectProperty');
59
- let objProperty = objExpression.properties[0];
60
- expect(objProperty.key).toBe(shared_1.ID.IMG_SRC);
61
- expect(objProperty.value).toEqual((0, types_1.stringLiteral)('testPrimitive.jpg')); // img({src: "testPrimitive.jpg"})
62
- const komaciDocImageBinding = {
63
- type: 'Image',
64
- src: {
65
- type: 'Binding',
66
- value: 'testBinding', // this is a string literal, but represents a variable name
67
- },
68
- };
69
- expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImageBinding);
70
- expect(expression.type).toBe('CallExpression');
71
- callExpression = expression;
72
- expect(callExpression).not.toBeUndefined();
73
- expect(callExpression.callee).toBe(shared_1.ID.IMG_FUNC);
74
- expect(callExpression.arguments).not.toBeUndefined();
75
- expect(callExpression.arguments).toHaveLength(1);
76
- expect(callExpression.arguments[0]).not.toBeUndefined();
77
- expect(callExpression.arguments[0].type).toBe('ObjectExpression');
78
- objExpression = callExpression.arguments[0];
79
- expect(objExpression).not.toBeUndefined();
80
- expect(objExpression.properties).not.toBeUndefined();
81
- expect(objExpression.properties).toHaveLength(1);
82
- expect(objExpression.properties[0].type).toBe('ObjectProperty');
83
- objProperty = objExpression.properties[0];
84
- expect(objProperty.key).toBe(shared_1.ID.IMG_SRC);
85
- let segments = ['ctx', 'props', 'testBinding'];
86
- let expr = (0, types_1.identifier)(segments[0]);
87
- for (let i = 1; i < segments.length; i++) {
88
- expr = (0, types_1.optionalMemberExpression)(expr, (0, types_1.identifier)(segments[i]), false, false);
89
- }
90
- expect(objProperty.value).toEqual(expr); // img({src: ctx.props.testBinding})
91
- const komaciDocImageNestedBinding = {
92
- type: 'Image',
93
- src: {
94
- type: 'Binding',
95
- value: 'imageObject/nested/imageName', // this is a string literal, represents nested-scoped variable names
96
- },
97
- };
98
- expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImageNestedBinding);
99
- expect(expression.type).toBe('CallExpression');
100
- callExpression = expression;
101
- expect(callExpression).not.toBeUndefined();
102
- expect(callExpression.callee).toBe(shared_1.ID.IMG_FUNC);
103
- expect(callExpression.arguments).not.toBeUndefined();
104
- expect(callExpression.arguments).toHaveLength(1);
105
- expect(callExpression.arguments[0]).not.toBeUndefined();
106
- expect(callExpression.arguments[0].type).toBe('ObjectExpression');
107
- objExpression = callExpression.arguments[0];
108
- expect(objExpression).not.toBeUndefined();
109
- expect(objExpression.properties).not.toBeUndefined();
110
- expect(objExpression.properties).toHaveLength(1);
111
- expect(objExpression.properties[0].type).toBe('ObjectProperty');
112
- objProperty = objExpression.properties[0];
113
- expect(objProperty.key).toBe(shared_1.ID.IMG_SRC);
114
- segments = ['ctx', 'props', 'imageObject', 'nested', 'imageName'];
115
- expr = (0, types_1.identifier)(segments[0]);
116
- const chainingStartIdx = 2;
117
- for (let i = 1; i < segments.length; i++) {
118
- expr = (0, types_1.optionalMemberExpression)(expr, (0, types_1.identifier)(segments[i]), false, i > chainingStartIdx);
119
- }
120
- expect(objProperty.value).toEqual(expr); // img({src: ctx.props.imageObject?.nested?.imageName})
121
- });
122
- it('should not return an expression for invalid KomaciDocument Image nodes due to invalid src.value type (boolean)', () => {
123
- const komaciDocImagePrimitive = {
124
- type: 'Image',
125
- src: {
126
- type: 'PrimitiveValue',
127
- value: false, // not a supported type (boolean)
128
- },
129
- };
130
- const generatorState = initGeneratorState(generatorInput);
131
- const expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImagePrimitive);
132
- expect(expression.type).toBe('CallExpression');
133
- const callExpression = expression;
134
- expect(callExpression).not.toBeUndefined();
135
- expect(callExpression.callee).not.toBeUndefined();
136
- expect(callExpression.callee).toBe(shared_1.ID.IMG_FUNC);
137
- expect(callExpression.arguments).not.toBeUndefined();
138
- expect(callExpression.arguments).toHaveLength(1);
139
- expect(callExpression.arguments[0].type).toBe('ObjectExpression');
140
- const objExpression = callExpression
141
- .arguments[0];
142
- expect(objExpression.properties).not.toBeUndefined();
143
- expect(objExpression.properties).toHaveLength(0); // no img() populated with a valid src attribute
144
- });
145
- it('should not return an expression for invalid KomaciDocument Image nodes due to blank src.value', () => {
146
- const komaciDocImagePrimitiveBlank = {
147
- type: 'Image',
148
- src: {
149
- type: 'PrimitiveValue',
150
- value: '', // blanks not supported
151
- },
152
- };
153
- const generatorState = initGeneratorState(generatorInput);
154
- let expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImagePrimitiveBlank);
155
- expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImagePrimitiveBlank);
156
- expect(expression.type).toBe('CallExpression');
157
- const callExpression = expression;
158
- expect(callExpression).not.toBeUndefined();
159
- expect(callExpression.callee).not.toBeUndefined();
160
- expect(callExpression.callee).toBe(shared_1.ID.IMG_FUNC);
161
- expect(callExpression.arguments).not.toBeUndefined();
162
- expect(callExpression.arguments).toHaveLength(1);
163
- expect(callExpression.arguments[0].type).toBe('ObjectExpression');
164
- const objExpression = callExpression
165
- .arguments[0];
166
- expect(objExpression.properties).not.toBeUndefined();
167
- expect(objExpression.properties).toHaveLength(0); // no img() populated with a valid src attribute
168
- });
169
- it('should not return an expression for invalid KomaciDocument Image nodes with src.type that is neither Binding nor PrimitiveType', () => {
170
- const komaciDocImageInvalidSrcValType = {
171
- type: 'Image',
172
- src: {
173
- type: 'PrimitiveValue',
174
- value: 'some random string',
175
- },
176
- };
177
- const generatorState = initGeneratorState(generatorInput);
178
- // change the src.value type to something invalid, a number
179
- komaciDocImageInvalidSrcValType.src.value = 12345;
180
- let expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImageInvalidSrcValType);
181
- expression = (0, modGenTemplate_1.imageToExpression)(generatorState, komaciDocImageInvalidSrcValType);
182
- expect(expression.type).toBe('CallExpression');
183
- const callExpression = expression;
184
- expect(callExpression).not.toBeUndefined();
185
- expect(callExpression.callee).not.toBeUndefined();
186
- expect(callExpression.callee).toBe(shared_1.ID.IMG_FUNC);
187
- expect(callExpression.arguments).not.toBeUndefined();
188
- expect(callExpression.arguments).toHaveLength(1);
189
- expect(callExpression.arguments[0].type).toBe('ObjectExpression');
190
- const objExpression = callExpression
191
- .arguments[0];
192
- expect(objExpression.properties).not.toBeUndefined();
193
- expect(objExpression.properties).toHaveLength(0);
194
- });
195
- });
196
- //# sourceMappingURL=modGenTemplate.spec.js.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=shared.spec.d.ts.map
@@ -1,66 +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
- 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 common_shared_1 = require("@komaci/common-shared");
30
- const shared_1 = require("../shared");
31
- const badImportRef_1 = require("./fixtures-source-code/badImportRef");
32
- const t = __importStar(require("@babel/types"));
33
- const generator_1 = __importDefault(require("@babel/generator"));
34
- describe('sanitizeFunction', () => {
35
- const ast = (0, common_shared_1.generateAstFromSrcCode)(badImportRef_1.sourceClass);
36
- const { getters } = (0, common_shared_1.getPropertyMetadataFromAst)(ast);
37
- const imports = new Map();
38
- imports.set('test', {
39
- identifierName: 'test',
40
- namespace: 'force/shared',
41
- isSupported: true,
42
- isDefault: true,
43
- isNamespaceSpecifier: false,
44
- });
45
- imports.set('bad', {
46
- identifierName: 'test',
47
- namespace: 'invalid/import',
48
- isSupported: false,
49
- isDefault: true,
50
- isNamespaceSpecifier: false,
51
- });
52
- const generatedNameMap = new Map();
53
- generatedNameMap.set('force/shared', 'i0');
54
- generatedNameMap.set('invalid/import', shared_1.UNRESOLVED);
55
- it('should be able to clean up and hoist a function with valid import references', () => {
56
- (0, shared_1.sanitizeFunction)(getters[0], imports, generatedNameMap);
57
- const hoisted = t.functionDeclaration(t.identifier('g0'), [t.identifier('props')], // make 'props' an input parameter to the hoisted getter
58
- getters[0].node.body);
59
- const generatedFunction = (0, generator_1.default)(t.file(t.program([hoisted])));
60
- expect(generatedFunction.code).toBe(badImportRef_1.expectedGoodHoist);
61
- });
62
- it('should throw an error if we try to reference a bad input', () => {
63
- expect(() => (0, shared_1.sanitizeFunction)(getters[1], imports, generatedNameMap)).toThrowError(new Error(`${shared_1.ERROR_PREFIX}: 'bad' does not map to valid import`));
64
- });
65
- });
66
- //# sourceMappingURL=shared.spec.js.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=templateStrings.spec.d.ts.map