@servicenow/sdk-build-core 2.2.9 → 3.0.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 (71) hide show
  1. package/dist/GUID.js +2 -5
  2. package/dist/GUID.js.map +1 -1
  3. package/dist/IncludePaths.d.ts +25 -0
  4. package/dist/IncludePaths.js +97 -0
  5. package/dist/IncludePaths.js.map +1 -0
  6. package/dist/Keys.d.ts +1 -0
  7. package/dist/Keys.js +3 -0
  8. package/dist/Keys.js.map +1 -1
  9. package/dist/XML.d.ts +7 -0
  10. package/dist/XML.js +19 -2
  11. package/dist/XML.js.map +1 -1
  12. package/dist/index.d.ts +1 -1
  13. package/dist/index.js +1 -1
  14. package/dist/plugins/Context.d.ts +12 -5
  15. package/dist/plugins/behaviors/Composer.d.ts +2 -1
  16. package/dist/plugins/behaviors/Composer.js.map +1 -1
  17. package/dist/plugins/behaviors/Serializer.d.ts +3 -2
  18. package/dist/plugins/behaviors/extractors/Data.d.ts +5 -1
  19. package/dist/plugins/behaviors/extractors/Data.js +11 -1
  20. package/dist/plugins/behaviors/extractors/Data.js.map +1 -1
  21. package/dist/plugins/index.d.ts +1 -1
  22. package/dist/plugins/util/CallExpression.js +15 -1
  23. package/dist/plugins/util/CallExpression.js.map +1 -1
  24. package/dist/plugins/util/CodeTransformation.d.ts +19 -7
  25. package/dist/plugins/util/CodeTransformation.js +89 -44
  26. package/dist/plugins/util/CodeTransformation.js.map +1 -1
  27. package/dist/plugins/util/index.d.ts +0 -1
  28. package/dist/plugins/util/index.js +0 -1
  29. package/dist/plugins/util/index.js.map +1 -1
  30. package/dist/util/Debug.d.ts +1 -5
  31. package/dist/util/Debug.js +0 -16
  32. package/dist/util/Debug.js.map +1 -1
  33. package/dist/util/RuntimeTableSchema.d.ts +5 -0
  34. package/dist/util/RuntimeTableSchema.js +58 -0
  35. package/dist/util/RuntimeTableSchema.js.map +1 -0
  36. package/dist/util/Util.d.ts +0 -4
  37. package/dist/util/Util.js +0 -14
  38. package/dist/util/Util.js.map +1 -1
  39. package/dist/util/index.d.ts +1 -1
  40. package/dist/util/index.js +1 -1
  41. package/dist/util/index.js.map +1 -1
  42. package/package.json +3 -3
  43. package/src/GUID.ts +1 -1
  44. package/src/IncludePaths.ts +122 -0
  45. package/src/Keys.ts +4 -0
  46. package/src/XML.ts +22 -2
  47. package/src/index.ts +1 -1
  48. package/src/plugins/Context.ts +12 -5
  49. package/src/plugins/behaviors/Composer.ts +2 -1
  50. package/src/plugins/behaviors/Serializer.ts +3 -2
  51. package/src/plugins/behaviors/extractors/Data.ts +11 -1
  52. package/src/plugins/index.ts +1 -1
  53. package/src/plugins/util/CallExpression.ts +24 -1
  54. package/src/plugins/util/CodeTransformation.ts +109 -54
  55. package/src/plugins/util/index.ts +0 -1
  56. package/src/util/Debug.ts +1 -20
  57. package/src/util/RuntimeTableSchema.ts +44 -0
  58. package/src/util/Util.ts +0 -21
  59. package/src/util/index.ts +1 -1
  60. package/dist/BuildOptions.d.ts +0 -46
  61. package/dist/BuildOptions.js +0 -45
  62. package/dist/BuildOptions.js.map +0 -1
  63. package/dist/plugins/util/ConfigurationFunction.d.ts +0 -105
  64. package/dist/plugins/util/ConfigurationFunction.js +0 -377
  65. package/dist/plugins/util/ConfigurationFunction.js.map +0 -1
  66. package/dist/util/XMLJsonBuilder.d.ts +0 -18
  67. package/dist/util/XMLJsonBuilder.js +0 -59
  68. package/dist/util/XMLJsonBuilder.js.map +0 -1
  69. package/src/BuildOptions.ts +0 -26
  70. package/src/plugins/util/ConfigurationFunction.ts +0 -468
  71. package/src/util/XMLJsonBuilder.ts +0 -64
@@ -0,0 +1,44 @@
1
+ import * as tables from '@servicenow/sdk-core/runtime/fluent'
2
+ import { Document } from '../plugins'
3
+
4
+ type Tag = 'html' | 'script' | 'css' | 'xml'
5
+
6
+ export type FieldToTagMap = Record<string, Tag>
7
+
8
+ const internalTypeToScriptTag: FieldToTagMap = {
9
+ html: 'html',
10
+ translated_html: 'html',
11
+ html_template: 'html',
12
+ message_html: 'html',
13
+ html_script: 'html',
14
+ script: 'script',
15
+ script_plain: 'script',
16
+ xml: 'xml',
17
+ }
18
+
19
+ export function maybeGetFieldToTagMap(table: string, documents: Document[]): FieldToTagMap {
20
+ return getTagsForColumnsFromSystemTables(table) || getTagsForColumnsFromBootstrapDocs(table, documents) || {}
21
+ }
22
+
23
+ function getTagsForColumnsFromSystemTables(table: string): FieldToTagMap | undefined {
24
+ const schema = (tables as any)[table]?.schema
25
+ return (
26
+ schema &&
27
+ Object.entries(schema)
28
+ .filter(([_, schema]) => internalTypeToScriptTag[(schema as any).column_type])
29
+ .map(([field, schema]) => ({ [field]: internalTypeToScriptTag[(schema as any).column_type] }))
30
+ .reduce((acc, val) => ({ ...acc, ...val }), {})
31
+ )
32
+ }
33
+
34
+ function getTagsForColumnsFromBootstrapDocs(table: string, documents: Document[]): FieldToTagMap | undefined {
35
+ const bootstrapDoc = documents.find((doc) => doc.kind === 'bootstrap' && doc.guid === table)
36
+ const columns = bootstrapDoc?.data?.['data']?.bootstrapData.columns
37
+ return (
38
+ columns &&
39
+ Object.entries(columns)
40
+ .filter(([, column]) => internalTypeToScriptTag[(column as any).type])
41
+ .map(([field, column]) => ({ [field]: internalTypeToScriptTag[(column as any).type] }))
42
+ .reduce((acc, val) => ({ ...acc, ...val }), {})
43
+ )
44
+ }
package/src/util/Util.ts CHANGED
@@ -1,24 +1,3 @@
1
- import { FileSystem, path } from '@servicenow/sdk-project'
2
-
3
- export const NOW_DIR = '.now'
4
-
5
- export function safeWriteFileToPath(
6
- fs: FileSystem,
7
- dirPath: string,
8
- fileName: string,
9
- content: string,
10
- encoding: FileSystem.Encoding = 'utf-8'
11
- ) {
12
- createDirIfNotExists(dirPath, fs)
13
- fs.writeFileSync(path.join(dirPath, fileName), content, { encoding: encoding })
14
- }
15
-
16
- export function createDirIfNotExists(dirPath: string, fs: FileSystem) {
17
- if (!FileSystem.existsSync(fs, dirPath)) {
18
- fs.mkdirSync(dirPath, { recursive: true })
19
- }
20
- }
21
-
22
1
  export function noThrow<T>(action: () => T) {
23
2
  try {
24
3
  return action()
package/src/util/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './Debug'
2
2
  export * from './Util'
3
3
  export * from './XMLUploadParser'
4
- export * from './XMLJsonBuilder'
5
4
  export * from './Scope'
6
5
  export * from './Directive'
6
+ export * from './RuntimeTableSchema'
@@ -1,46 +0,0 @@
1
- import * as z from 'zod';
2
- declare const BuildOptionsSchema: z.ZodIntersection<z.ZodObject<{
3
- mode: z.ZodString;
4
- release: z.ZodDefault<z.ZodString>;
5
- }, "strip", z.ZodTypeAny, {
6
- mode: string;
7
- release: string;
8
- }, {
9
- mode: string;
10
- release?: string | undefined;
11
- }>, z.ZodUnion<[z.ZodObject<{
12
- mode: z.ZodLiteral<"serialize">;
13
- compilerOptions: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
14
- }, "strip", z.ZodTypeAny, {
15
- mode: "serialize";
16
- compilerOptions?: Record<string, unknown> | undefined;
17
- }, {
18
- mode: "serialize";
19
- compilerOptions?: Record<string, unknown> | undefined;
20
- }>, z.ZodObject<{
21
- mode: z.ZodLiteral<"transform">;
22
- clean: z.ZodDefault<z.ZodBoolean>;
23
- transformDirectory: z.ZodDefault<z.ZodString>;
24
- }, "strip", z.ZodTypeAny, {
25
- mode: "transform";
26
- clean: boolean;
27
- transformDirectory: string;
28
- }, {
29
- mode: "transform";
30
- clean?: boolean | undefined;
31
- transformDirectory?: string | undefined;
32
- }>]>>;
33
- export type BuildOptionsInput = z.input<typeof BuildOptionsSchema>;
34
- export type BuildOptions = z.output<typeof BuildOptionsSchema>;
35
- export declare const parseBuildOptions: (data: unknown, params?: Partial<z.ParseParams>) => {
36
- mode: string;
37
- release: string;
38
- } & ({
39
- mode: "serialize";
40
- compilerOptions?: Record<string, unknown> | undefined;
41
- } | {
42
- mode: "transform";
43
- clean: boolean;
44
- transformDirectory: string;
45
- });
46
- export {};
@@ -1,45 +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.parseBuildOptions = void 0;
27
- const sdk_project_1 = require("@servicenow/sdk-project");
28
- const z = __importStar(require("zod"));
29
- const BuildOptionsSchema = z
30
- .object({
31
- mode: z.string(),
32
- release: z.string().default('default'),
33
- })
34
- .and(z
35
- .object({
36
- mode: z.literal('serialize'),
37
- compilerOptions: z.record(z.string(), z.unknown()).optional(),
38
- })
39
- .or(z.object({
40
- mode: z.literal('transform'),
41
- clean: z.boolean().default(false),
42
- transformDirectory: z.string().default(sdk_project_1.path.resolve(process.cwd(), 'incoming')),
43
- })));
44
- exports.parseBuildOptions = BuildOptionsSchema.parse;
45
- //# sourceMappingURL=BuildOptions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BuildOptions.js","sourceRoot":"","sources":["../src/BuildOptions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yDAA8C;AAC9C,uCAAwB;AAExB,MAAM,kBAAkB,GAAG,CAAC;KACvB,MAAM,CAAC;IACJ,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC;CACzC,CAAC;KACD,GAAG,CACA,CAAC;KACI,MAAM,CAAC;IACJ,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC;IAC5B,eAAe,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,QAAQ,EAAE;CAChE,CAAC;KACD,EAAE,CACC,CAAC,CAAC,MAAM,CAAC;IACL,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC;IAC5B,KAAK,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC;IACjC,kBAAkB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,kBAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,UAAU,CAAC,CAAC;CAClF,CAAC,CACL,CACR,CAAA;AAIQ,QAAA,iBAAiB,GAAG,kBAAkB,CAAC,KAAK,CAAA"}
@@ -1,105 +0,0 @@
1
- import { Logger, ts } from '@servicenow/sdk-project';
2
- import { Context } from '../Context';
3
- import { EntityData } from '../behaviors';
4
- export type ConfigEntity = {
5
- inputObject: InputObject;
6
- configEntries: ConfigEntries;
7
- };
8
- export type InputObject = {
9
- $id: string | number;
10
- [name: string]: any;
11
- };
12
- export type ConfigEntries = {
13
- [index: `${number}`]: ConfigEntry;
14
- };
15
- export type ConfigEntry = {
16
- name: string;
17
- info: {
18
- [name: string]: any;
19
- };
20
- };
21
- export type ComposerClass = {
22
- prototype: any;
23
- [key: string]: any;
24
- };
25
- export declare class ConfigurationFunctionEntityData extends EntityData {
26
- private readonly config;
27
- constructor(config: ConfigEntity, node: ts.CallExpression);
28
- getValue(): ConfigEntity;
29
- }
30
- /**
31
- * This is an extractor used to extract configuration functions.
32
- * This is an example of a configuration function:
33
- * ```typescript
34
- * import { table } from 'place'
35
- * PlaceHolderFunction('Unique ID', (param) => {
36
- * param.foo({arg1: 'hey', arg2: table});
37
- * const output = param.bar({placeholder: 'value'});
38
- * param.baz({arg1: output.value1})
39
- * })
40
- * ```
41
- */
42
- export declare class ConfigurationFunctionExtractor {
43
- private rootNode;
44
- private context;
45
- private composerClass;
46
- private identifiers;
47
- /**
48
- *
49
- * @param rootNode rootNode of AST where extraction begins
50
- * @param context context from the plugin this is called in
51
- * @param composerClass A class whose prototype will be used to validate the allowed functions within the configuration function
52
- */
53
- constructor(rootNode: ts.CallExpression, context: Context, composerClass: ComposerClass);
54
- /**
55
- * Grabs the property access expression chain of a call expression, and protects against accidentally grabbing property access expressions in arguments of the call expression
56
- */
57
- private getPropChain;
58
- /**
59
- * Returns the first element of a property access chain
60
- * v
61
- * a.b.c.d({})
62
- */
63
- private getFirstProp;
64
- /**
65
- * Returns the last element of a property access chain
66
- *
67
- * v
68
- * a.b.c.d({})
69
- */
70
- private getLastProp;
71
- /**
72
- * Validates that a call expression is valid. Will error if the call expression is inappropriately constructed for the configuration function
73
- * @param ce
74
- * @param parameterName
75
- */
76
- private isValidCallExpression;
77
- /**
78
- * Returns the step number of the step containing a identifier which matches the type of the queried identfier
79
- *
80
- * If it cannot find the identifier, it returns -1
81
- * @param identifier
82
- * @param data
83
- */
84
- private findReferencedIdentifier;
85
- private getRootIdentifier;
86
- private parseValue;
87
- private getKVPair;
88
- private objNodeToJSON;
89
- private getName;
90
- private getConfigFunctionComponents;
91
- private extractCallExpression;
92
- extractConfigFunction(kind: string, // Name of function for now
93
- logger: Logger): EntityData | undefined;
94
- /**
95
- * Returns a single entity of the kind specified which contains all data of nodes within the configuration function
96
- *
97
- * Will attempt to generalize this for use by flow team
98
- * @param kind
99
- * @param node
100
- * @param context
101
- * @returns
102
- */
103
- extractConfigFunctionUnsafe(kind: string, // Name of function for now,
104
- logger: Logger): EntityData | undefined;
105
- }
@@ -1,377 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConfigurationFunctionExtractor = exports.ConfigurationFunctionEntityData = void 0;
4
- const sdk_project_1 = require("@servicenow/sdk-project");
5
- const behaviors_1 = require("../behaviors");
6
- const CallExpression_1 = require("./CallExpression");
7
- // TODO: Just created this to avoid refactoring all of the ATF stuff. Should be removed
8
- // and the EntityData class should be used instead, if possible.
9
- class ConfigurationFunctionEntityData extends behaviors_1.EntityData {
10
- config;
11
- constructor(config, node) {
12
- super('test', 'NO_GUID_GENERATED', // TODO: Generate GUID?
13
- new behaviors_1.ObjectData({}, node), node);
14
- this.config = config;
15
- }
16
- getValue() {
17
- return this.config;
18
- }
19
- }
20
- exports.ConfigurationFunctionEntityData = ConfigurationFunctionEntityData;
21
- /**
22
- * This is an extractor used to extract configuration functions.
23
- * This is an example of a configuration function:
24
- * ```typescript
25
- * import { table } from 'place'
26
- * PlaceHolderFunction('Unique ID', (param) => {
27
- * param.foo({arg1: 'hey', arg2: table});
28
- * const output = param.bar({placeholder: 'value'});
29
- * param.baz({arg1: output.value1})
30
- * })
31
- * ```
32
- */
33
- class ConfigurationFunctionExtractor {
34
- rootNode;
35
- context;
36
- composerClass;
37
- identifiers;
38
- /**
39
- *
40
- * @param rootNode rootNode of AST where extraction begins
41
- * @param context context from the plugin this is called in
42
- * @param composerClass A class whose prototype will be used to validate the allowed functions within the configuration function
43
- */
44
- constructor(rootNode, context, composerClass) {
45
- this.rootNode = rootNode;
46
- this.context = context;
47
- this.composerClass = composerClass;
48
- this.identifiers = [];
49
- }
50
- /**
51
- * Grabs the property access expression chain of a call expression, and protects against accidentally grabbing property access expressions in arguments of the call expression
52
- */
53
- getPropChain(ce) {
54
- // Do this to ensure we are only considering property access expressions that are apart of the chain and not potentially in the arguments of the function
55
- const props = [ce.getFirstDescendantByKindOrThrow(sdk_project_1.ts.SyntaxKind.PropertyAccessExpression)];
56
- const childProps = props[0].getDescendantsOfKind(sdk_project_1.ts.SyntaxKind.PropertyAccessExpression);
57
- if (childProps.length > 0) {
58
- props.push(...childProps);
59
- }
60
- return props;
61
- }
62
- /**
63
- * Returns the first element of a property access chain
64
- * v
65
- * a.b.c.d({})
66
- */
67
- getFirstProp(ce) {
68
- // Need to use this instead of just grabbing descendents cause the arguments of the call expression can also potentially have property access expressions which we don't want to consider
69
- const props = this.getPropChain(ce);
70
- if (props.length === 1) {
71
- const ids = props[0]?.getChildrenOfKind(sdk_project_1.ts.SyntaxKind.Identifier);
72
- return ids[0];
73
- }
74
- const lastProp = props[props.length - 1];
75
- return lastProp.getChildrenOfKind(sdk_project_1.ts.SyntaxKind.Identifier)[0];
76
- }
77
- /**
78
- * Returns the last element of a property access chain
79
- *
80
- * v
81
- * a.b.c.d({})
82
- */
83
- getLastProp(ce) {
84
- // Do not need special chain handling here like in getFirstProp cause we are always grabbing the property access expression from the top level regardless of chain length
85
- const props = ce.getFirstDescendantByKindOrThrow(sdk_project_1.ts.SyntaxKind.PropertyAccessExpression);
86
- const ids = props.getChildrenOfKind(sdk_project_1.ts.SyntaxKind.Identifier);
87
- return ids[ids.length - 1]; // Identifier is either the only one present or is the last of 2 so this should fetch it in either scenario
88
- }
89
- /**
90
- * Validates that a call expression is valid. Will error if the call expression is inappropriately constructed for the configuration function
91
- * @param ce
92
- * @param parameterName
93
- */
94
- isValidCallExpression(ce, paramId) {
95
- if (!paramId) {
96
- throw Error('No parameter provided to configuration function');
97
- }
98
- // Ensure the line of the function call starts with the argument of the configuration function
99
- const firstId = this.getFirstProp(ce);
100
- if (firstId?.getText() !== paramId?.getText()) {
101
- throw Error(`Call expression does not begin with "${paramId.getText()}", instead began with "${firstId.getText()}"`);
102
- }
103
- // Verify that the function call is actually a valid function call based on the composer class this will be used with
104
- const lastId = this.getLastProp(ce);
105
- const functionName = lastId.getText();
106
- const nameIsValid = Object.getOwnPropertyNames(this.composerClass.prototype).some((funcName) => {
107
- return funcName != 'constructor' && functionName.startsWith(funcName);
108
- });
109
- if (!nameIsValid) {
110
- throw Error(`Call expression function does not exist on the object ${paramId.getText()}`);
111
- }
112
- // Verify that call expression has the expected number of arguments
113
- const funcArgs = ce.getFirstDescendantByKind(sdk_project_1.ts.SyntaxKind.SyntaxList)?.getChildren();
114
- if (!funcArgs) {
115
- throw Error('Call expression had no arguments at all');
116
- }
117
- if (funcArgs.length != 1) {
118
- throw Error('Call expressions only support a single argument');
119
- }
120
- if (funcArgs[0].getKind() != sdk_project_1.ts.SyntaxKind.ObjectLiteralExpression) {
121
- throw Error('Call expression must have an object as its argument');
122
- }
123
- }
124
- /**
125
- * Returns the step number of the step containing a identifier which matches the type of the queried identfier
126
- *
127
- * If it cannot find the identifier, it returns -1
128
- * @param identifier
129
- * @param data
130
- */
131
- findReferencedIdentifier(identifier) {
132
- const idName = identifier.getSymbol()?.getName();
133
- // Identifier index lines up with step index, removed from step object so identifiers are not returned in extracted object
134
- for (let i = 0; i < this.identifiers.length; i++) {
135
- const identifier = this.identifiers[i];
136
- // Maybe there's another way to more accurately identify but this should be fine?
137
- const name = identifier?.getSymbol()?.getName();
138
- if (name && idName && name == idName) {
139
- return i;
140
- }
141
- }
142
- return -1;
143
- }
144
- getRootIdentifier(prop) {
145
- const children = prop.getChildrenOfKind(sdk_project_1.ts.SyntaxKind.PropertyAccessExpression);
146
- if (children.length == 0) {
147
- const id = prop.getFirstChildByKind(sdk_project_1.ts.SyntaxKind.Identifier);
148
- if (!id) {
149
- throw Error('Could not find identifier in property access expression');
150
- }
151
- return id;
152
- }
153
- return this.getRootIdentifier(children[0]);
154
- }
155
- parseValue(valueNode) {
156
- if (valueNode.getKind() == sdk_project_1.ts.SyntaxKind.ArrayLiteralExpression) {
157
- const values = [];
158
- const syntaxList = valueNode.getFirstDescendantByKind(sdk_project_1.ts.SyntaxKind.SyntaxList);
159
- if (!syntaxList) {
160
- return;
161
- }
162
- const children = syntaxList.getChildren();
163
- for (const child of children) {
164
- if (child.getKind() == sdk_project_1.ts.SyntaxKind.CommaToken) {
165
- continue;
166
- }
167
- const value = this.parseValue(child);
168
- values.push(value);
169
- }
170
- return values;
171
- }
172
- if (valueNode.getKind() == sdk_project_1.ts.SyntaxKind.ObjectLiteralExpression) {
173
- const objNode = valueNode;
174
- return this.objNodeToJSON(objNode);
175
- }
176
- if (valueNode.getKind() == sdk_project_1.ts.SyntaxKind.PropertyAccessExpression) {
177
- const propNode = valueNode;
178
- const identifier = this.getRootIdentifier(propNode);
179
- const refIdentifierIndex = this.findReferencedIdentifier(identifier);
180
- if (refIdentifierIndex == -1) {
181
- throw Error('Step corresponding to property access expression could not be found');
182
- }
183
- const parsedValue = valueNode.getText().split('.');
184
- // Replace it just with a string here. Parsing into a real value will happen in the composer
185
- return { __references: refIdentifierIndex, __value: parsedValue[parsedValue.length - 1] };
186
- }
187
- if (valueNode.getKind() == sdk_project_1.ts.SyntaxKind.TemplateExpression) {
188
- // console.info(`literal: ${(valueNode as ts.TemplateExpression).getVal}`)
189
- const templateNode = valueNode;
190
- const templateValue = [templateNode.getHead().getLiteralText()];
191
- templateNode.getTemplateSpans().forEach((s) => {
192
- templateValue.push(this.parseValue(s.getExpression()));
193
- templateValue.push(s.getLiteral().getLiteralText());
194
- });
195
- return { __template: templateValue };
196
- }
197
- // A variety of Identifiers. Will be handled here
198
- // TODO: This is swallowing diagnostics and not handling the case where no data was extracted
199
- return this.context.extractAst(valueNode)['data']?.getValue();
200
- }
201
- getKVPair(prop) {
202
- const props = prop.getChildren();
203
- const keyNode = props[0];
204
- const valueNode = props[2];
205
- const key = keyNode.getLiteralValue ? keyNode.getLiteralValue() : keyNode.getText();
206
- const value = this.parseValue(valueNode);
207
- if (typeof key !== 'undefined' && typeof value !== 'undefined') {
208
- return { key, value };
209
- }
210
- return { key: undefined, value: undefined };
211
- }
212
- objNodeToJSON(obj) {
213
- if (!obj) {
214
- throw Error('objNodeToJSON received an undefined object node');
215
- }
216
- const syntaxList = obj.getFirstDescendantByKind(sdk_project_1.ts.SyntaxKind.SyntaxList);
217
- if (!syntaxList) {
218
- throw Error('objNodeToJSON could not find a syntax list in the ObjectLiteralExpression');
219
- }
220
- const json = {};
221
- syntaxList.getChildrenOfKind(sdk_project_1.ts.SyntaxKind.PropertyAssignment).forEach((arg) => {
222
- const propArg = arg;
223
- const { key, value } = this.getKVPair(propArg);
224
- // TODO: What if the value is actually undefined?
225
- if (key !== undefined && value !== undefined) {
226
- json[key] = value;
227
- }
228
- });
229
- return json;
230
- }
231
- getName(node) {
232
- // This may not be robust but should be a good enough
233
- const lastProp = this.getLastProp(node);
234
- return lastProp.getText();
235
- }
236
- getConfigFunctionComponents(kind, node) {
237
- kind;
238
- const configArgs = node.getArguments();
239
- if (configArgs.length != 2) {
240
- throw Error('The plugin must have exactly 2 arguments');
241
- }
242
- if (configArgs[0].getKind() != sdk_project_1.ts.SyntaxKind.ObjectLiteralExpression) {
243
- throw Error("The plugin's first argument was not an object");
244
- }
245
- if (configArgs[1].getKind() != sdk_project_1.ts.SyntaxKind.ArrowFunction &&
246
- configArgs[1].getKind() != sdk_project_1.ts.SyntaxKind.FunctionExpression) {
247
- throw Error("The plugin's second argument was not a function");
248
- }
249
- const objectArg = configArgs[0];
250
- const configFunction = configArgs[1];
251
- // TODO: This is swallowing diagnostics and not handling the case where no data was extracted
252
- const objectArgValue = this.context.extractAst(objectArg)['data']?.getValue();
253
- if (!('$id' in objectArgValue)) {
254
- throw Error('No $id defined in first argument');
255
- }
256
- const parameters = configFunction.getParameters();
257
- // Checking the type is parameter is probably redundant but can not hurt
258
- if (parameters.length != 1 || !parameters[0] || parameters[0].getKind() != sdk_project_1.ts.SyntaxKind.Parameter) {
259
- throw Error('Configuration function must have an parameter as its only argument');
260
- }
261
- const paramId = parameters[0].getChildrenOfKind(sdk_project_1.ts.SyntaxKind.Identifier)[0];
262
- const block = configFunction.getFirstChildByKind(sdk_project_1.ts.SyntaxKind.Block);
263
- if (!block) {
264
- throw Error(`Failed to find block in configuration function`);
265
- }
266
- const syntaxList = block.getFirstChildByKind(sdk_project_1.ts.SyntaxKind.SyntaxList);
267
- if (!syntaxList) {
268
- throw Error(`Failed to find syntax list in configuration function`);
269
- }
270
- return { paramId, syntaxList, objectArgValue };
271
- }
272
- extractCallExpression(node, paramId) {
273
- const callExpressions = node.getDescendantsOfKind(sdk_project_1.ts.SyntaxKind.CallExpression);
274
- if (callExpressions.length == 0) {
275
- throw Error(`Expression statement did not have any call expression`);
276
- }
277
- if (callExpressions.length > 1) {
278
- throw Error(`Found more than one call expression inside of an expression statement`);
279
- }
280
- const callExpression = callExpressions[0];
281
- this.isValidCallExpression(callExpression, paramId);
282
- return callExpression;
283
- }
284
- extractConfigFunction(kind, // Name of function for now
285
- logger) {
286
- try {
287
- return this.extractConfigFunctionUnsafe(kind, logger);
288
- }
289
- catch (e) {
290
- logger.error('Could not extract configuration function: ', e);
291
- // swallows error, maybe fully re-echo but probably should just swallow silently to behave as expected
292
- return undefined;
293
- }
294
- }
295
- /**
296
- * Returns a single entity of the kind specified which contains all data of nodes within the configuration function
297
- *
298
- * Will attempt to generalize this for use by flow team
299
- * @param kind
300
- * @param node
301
- * @param context
302
- * @returns
303
- */
304
- extractConfigFunctionUnsafe(kind, // Name of function for now,
305
- logger) {
306
- // If this node is not the correct kind, return undefined. Some other extractor will handle it
307
- const name = (0, CallExpression_1.getCallExpressionName)(this.rootNode).toLowerCase();
308
- if (name !== kind) {
309
- return undefined;
310
- }
311
- // Below here we assume anything that makes it this far is being extracted by the right type of extractor so failing to get any other part will result in an error stopping compilation instead
312
- const { paramId, syntaxList, objectArgValue } = this.getConfigFunctionComponents(kind, this.rootNode);
313
- const config = { inputObject: objectArgValue, configEntries: {} };
314
- const data = config.configEntries;
315
- let stepNumber = 0;
316
- syntaxList.getChildren().forEach((statement) => {
317
- try {
318
- switch (statement.getKind()) {
319
- case sdk_project_1.ts.SyntaxKind.ExpressionStatement: {
320
- /**
321
- * (param) => {
322
- * param.foo({arg: 'value'}) <- Expression statement
323
- * }
324
- */
325
- // This formatting of casting is used throughout the class. Try to keep casting close to where the value is determined and then save it to a cast variable and use
326
- // that below. This prevents accidentally using a variable as multiple different types by forgetting to cast and also keeps casting near where type is determined
327
- // so its more clear why it is being cast
328
- const ce = this.extractCallExpression(statement, paramId);
329
- data[stepNumber++] = {
330
- name: this.getName(ce),
331
- info: this.objNodeToJSON(ce.getFirstDescendantByKind(sdk_project_1.ts.SyntaxKind.ObjectLiteralExpression)),
332
- };
333
- this.identifiers.push(null);
334
- break;
335
- }
336
- case sdk_project_1.ts.SyntaxKind.VariableStatement: {
337
- /**
338
- * (param) => {
339
- * const output = param.foo({arg: 'value'}) <- Variable statement
340
- * }
341
- */
342
- const ce = this.extractCallExpression(statement, paramId);
343
- const variableDeclaration = statement.getFirstDescendantByKind(sdk_project_1.ts.SyntaxKind.VariableDeclaration);
344
- if (!variableDeclaration) {
345
- throw Error('Could not find variable declaration in variable statement');
346
- }
347
- const assignment = variableDeclaration.getChildren(); // Format: identifier, =, value
348
- const identifier = assignment[0];
349
- const value = assignment[2]; // Already been validated above to be a valid call expression
350
- data[stepNumber++] = {
351
- name: this.getName(ce),
352
- info: this.objNodeToJSON(value.getFirstDescendantByKind(sdk_project_1.ts.SyntaxKind.ObjectLiteralExpression)),
353
- };
354
- this.identifiers.push(identifier);
355
- break;
356
- }
357
- case sdk_project_1.ts.SyntaxKind.MultiLineCommentTrivia:
358
- /** This is a multiline comment */
359
- break;
360
- case sdk_project_1.ts.SyntaxKind.SingleLineCommentTrivia:
361
- // This is a single line comment
362
- break;
363
- default:
364
- throw Error(`Found an invalid node in the configuration function block, found: ${statement?.getKindName() ? statement.getKindName() : 'Kind name undefined'}`);
365
- }
366
- }
367
- catch (e) {
368
- // Provides easy context w/o having to worry about having blah.getText() in all errors
369
- logger.error(`Encountered an error while parsing statement: ${statement.getText()}`);
370
- throw e;
371
- }
372
- });
373
- return new ConfigurationFunctionEntityData(config, this.rootNode);
374
- }
375
- }
376
- exports.ConfigurationFunctionExtractor = ConfigurationFunctionExtractor;
377
- //# sourceMappingURL=ConfigurationFunction.js.map