@jetstreamapp/soql-parser-js 6.0.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 (50) hide show
  1. package/AUTHORS.md +1 -0
  2. package/CHANGELOG.md +959 -0
  3. package/LICENSE.txt +12 -0
  4. package/README.md +949 -0
  5. package/bin/soql-parser-js +3 -0
  6. package/dist/cjs/index.js +2 -0
  7. package/dist/cjs/index.js.LICENSE.txt +1 -0
  8. package/dist/cjs/main.licenses.txt +898 -0
  9. package/dist/cjs/src/api/api-models.d.ts +175 -0
  10. package/dist/cjs/src/api/api-models.js +2 -0
  11. package/dist/cjs/src/api/public-utils.d.ts +29 -0
  12. package/dist/cjs/src/api/public-utils.js +175 -0
  13. package/dist/cjs/src/composer/composer.d.ts +32 -0
  14. package/dist/cjs/src/composer/composer.js +295 -0
  15. package/dist/cjs/src/formatter/formatter.d.ts +42 -0
  16. package/dist/cjs/src/formatter/formatter.js +236 -0
  17. package/dist/cjs/src/index.d.ts +5 -0
  18. package/dist/cjs/src/index.js +23 -0
  19. package/dist/cjs/src/models.d.ts +266 -0
  20. package/dist/cjs/src/models.js +2 -0
  21. package/dist/cjs/src/parser/lexer.d.ts +186 -0
  22. package/dist/cjs/src/parser/lexer.js +954 -0
  23. package/dist/cjs/src/parser/parser.d.ts +89 -0
  24. package/dist/cjs/src/parser/parser.js +697 -0
  25. package/dist/cjs/src/parser/visitor.d.ts +4 -0
  26. package/dist/cjs/src/parser/visitor.js +856 -0
  27. package/dist/cjs/src/utils.d.ts +42 -0
  28. package/dist/cjs/src/utils.js +245 -0
  29. package/dist/cli/index.js +113 -0
  30. package/dist/esm/api/api-models.js +1 -0
  31. package/dist/esm/api/public-utils.js +153 -0
  32. package/dist/esm/composer/composer.js +273 -0
  33. package/dist/esm/formatter/formatter.js +223 -0
  34. package/dist/esm/index.js +4 -0
  35. package/dist/esm/models.js +1 -0
  36. package/dist/esm/parser/lexer.js +947 -0
  37. package/dist/esm/parser/parser.js +667 -0
  38. package/dist/esm/parser/visitor.js +823 -0
  39. package/dist/esm/utils.js +200 -0
  40. package/dist/src/api/api-models.js +2 -0
  41. package/dist/src/api/public-utils.js +203 -0
  42. package/dist/src/composer/composer.js +358 -0
  43. package/dist/src/formatter/formatter.js +263 -0
  44. package/dist/src/index.js +28 -0
  45. package/dist/src/models.js +2 -0
  46. package/dist/src/parser/lexer.js +997 -0
  47. package/dist/src/parser/parser.js +711 -0
  48. package/dist/src/parser/visitor.js +862 -0
  49. package/dist/src/utils.js +251 -0
  50. package/package.json +78 -0
@@ -0,0 +1,273 @@
1
+ import * as utils from '../utils';
2
+ import { Formatter } from '../formatter/formatter';
3
+ import { parseQuery } from '../parser/visitor';
4
+ export function formatQuery(soql, formatOptions, parseOptions) {
5
+ return composeQuery(parseQuery(soql, parseOptions), { format: true, formatOptions });
6
+ }
7
+ export function composeQuery(soql, config = {}) {
8
+ if (!soql) {
9
+ return '';
10
+ }
11
+ config = config || {};
12
+ config.format = config.format ? true : false;
13
+ if (config.logging) {
14
+ console.time('composer');
15
+ console.log('Composing Query:', soql);
16
+ console.log('Format output:', config.format);
17
+ }
18
+ const query = new Compose(soql, config).query;
19
+ if (config.logging) {
20
+ console.timeEnd('composer');
21
+ }
22
+ return query;
23
+ }
24
+ export class Compose {
25
+ soql;
26
+ logging = false;
27
+ format = false;
28
+ query;
29
+ formatter;
30
+ constructor(soql, config = {}) {
31
+ this.soql = soql;
32
+ config = { autoCompose: true, ...config };
33
+ const { logging, format } = config;
34
+ this.logging = !!logging;
35
+ this.format = !!format;
36
+ this.query = '';
37
+ this.formatter = new Formatter(this.format, {
38
+ logging: this.logging,
39
+ ...config.formatOptions,
40
+ });
41
+ if (config.autoCompose) {
42
+ this.start();
43
+ }
44
+ }
45
+ start() {
46
+ this.query = this.parseQuery(this.soql);
47
+ }
48
+ log(soql) {
49
+ if (this.logging) {
50
+ console.log('Current SOQL:', soql);
51
+ }
52
+ }
53
+ parseFn(fn) {
54
+ let output;
55
+ if (fn.rawValue) {
56
+ output = fn.rawValue;
57
+ }
58
+ else {
59
+ output = fn.functionName;
60
+ output += `(${(fn.parameters || []).map(param => (utils.isString(param) ? param : this.parseFn(param))).join(', ')})`;
61
+ }
62
+ if (fn.alias) {
63
+ output += ` ${fn.alias}`;
64
+ }
65
+ return output;
66
+ }
67
+ parseQuery(query) {
68
+ const fieldData = {
69
+ fields: this.parseFields(query.fields || []).map(field => ({
70
+ text: field.text,
71
+ typeOfClause: field.typeOfClause,
72
+ isSubquery: field.text.startsWith('('),
73
+ prefix: '',
74
+ suffix: '',
75
+ })),
76
+ isSubquery: utils.isSubquery(query),
77
+ lineBreaks: [],
78
+ };
79
+ let output = '';
80
+ if (query.fields) {
81
+ output += this.formatter.formatClause('SELECT').trimStart();
82
+ }
83
+ this.formatter.formatFields(fieldData);
84
+ let fieldsOutput = '';
85
+ fieldData.fields.forEach(field => {
86
+ if (Array.isArray(field.typeOfClause)) {
87
+ fieldsOutput += `${field.prefix}${this.formatter.formatTyeOfField(field.text, field.typeOfClause)}${field.suffix}`;
88
+ }
89
+ else {
90
+ fieldsOutput += `${field.prefix}${field.text}${field.suffix}`;
91
+ }
92
+ });
93
+ output += this.formatter.formatText(fieldsOutput);
94
+ if (!!(utils.isSubquery(query) ? query.relationshipName : query.sObject)) {
95
+ output += this.formatter.formatClause('FROM');
96
+ }
97
+ if (utils.isSubquery(query)) {
98
+ const sObjectPrefix = query.sObjectPrefix || [];
99
+ sObjectPrefix.push(query.relationshipName);
100
+ output += this.formatter.formatText(`${sObjectPrefix.join('.')}${utils.get(query.sObjectAlias, '', ' ')}`);
101
+ }
102
+ else if (query.sObject) {
103
+ output += this.formatter.formatText(`${query.sObject}${utils.get(query.sObjectAlias, '', ' ')}`);
104
+ }
105
+ this.log(output);
106
+ if (query.usingScope) {
107
+ output += this.formatter.formatClause('USING SCOPE');
108
+ output += this.formatter.formatText(query.usingScope);
109
+ this.log(output);
110
+ }
111
+ if (query.where) {
112
+ output += this.formatter.formatClause('WHERE');
113
+ output += this.formatter.formatText(this.parseWhereOrHavingClause(query.where));
114
+ this.log(output);
115
+ }
116
+ if (query.groupBy) {
117
+ output += this.formatter.formatClause('GROUP BY');
118
+ output += this.formatter.formatText(this.parseGroupByClause(query.groupBy));
119
+ this.log(output);
120
+ if (query.having) {
121
+ output += this.formatter.formatClause('HAVING');
122
+ output += this.formatter.formatText(this.parseWhereOrHavingClause(query.having));
123
+ this.log(output);
124
+ }
125
+ }
126
+ if (query.orderBy && (!Array.isArray(query.orderBy) || query.orderBy.length > 0)) {
127
+ output += this.formatter.formatClause('ORDER BY');
128
+ output += this.formatter.formatText(this.parseOrderBy(query.orderBy));
129
+ this.log(output);
130
+ }
131
+ if (utils.isNumber(query.limit)) {
132
+ output += this.formatter.formatClause('LIMIT');
133
+ output += this.formatter.formatText(`${query.limit}`);
134
+ this.log(output);
135
+ }
136
+ if (utils.isNumber(query.offset)) {
137
+ output += this.formatter.formatClause('OFFSET');
138
+ output += this.formatter.formatText(`${query.offset}`);
139
+ this.log(output);
140
+ }
141
+ if (query.withDataCategory) {
142
+ output += this.formatter.formatClause('WITH DATA CATEGORY');
143
+ output += this.formatter.formatText(this.parseWithDataCategory(query.withDataCategory));
144
+ this.log(output);
145
+ }
146
+ if (query.withSecurityEnforced) {
147
+ output += this.formatter.formatClause('WITH SECURITY_ENFORCED');
148
+ this.log(output);
149
+ }
150
+ if (query.withAccessLevel) {
151
+ output += this.formatter.formatClause(`WITH ${query.withAccessLevel}`);
152
+ this.log(output);
153
+ }
154
+ if (query.for) {
155
+ output += this.formatter.formatClause('FOR');
156
+ output += this.formatter.formatText(query.for);
157
+ this.log(output);
158
+ }
159
+ if (query.update) {
160
+ output += this.formatter.formatClause('UPDATE');
161
+ output += this.formatter.formatText(query.update);
162
+ this.log(output);
163
+ }
164
+ return output.trim();
165
+ }
166
+ parseFields(fields) {
167
+ return fields.map(field => {
168
+ let text = '';
169
+ let typeOfClause;
170
+ const objPrefix = field.objectPrefix ? `${field.objectPrefix}.` : '';
171
+ switch (field.type) {
172
+ case 'Field': {
173
+ text = `${objPrefix}${field.field}${field.alias ? ` ${field.alias}` : ''}`;
174
+ break;
175
+ }
176
+ case 'FieldFunctionExpression': {
177
+ let params = '';
178
+ if (field.parameters) {
179
+ params = field.parameters
180
+ .map(param => (utils.isString(param) ? param : this.parseFields([param]).map(param => param.text)))
181
+ .join(', ');
182
+ }
183
+ text = `${field.functionName}(${params})${field.alias ? ` ${field.alias}` : ''}`;
184
+ break;
185
+ }
186
+ case 'FieldRelationship': {
187
+ text = `${objPrefix}${field.relationships.join('.')}.${field.field}${utils.hasAlias(field) ? ` ${field.alias}` : ''}`;
188
+ break;
189
+ }
190
+ case 'FieldSubquery': {
191
+ text = this.formatter.formatSubquery(this.parseQuery(field.subquery));
192
+ break;
193
+ }
194
+ case 'FieldTypeof': {
195
+ typeOfClause = this.parseTypeOfField(field);
196
+ text = typeOfClause.join(' ');
197
+ break;
198
+ }
199
+ default:
200
+ break;
201
+ }
202
+ return { text, typeOfClause };
203
+ });
204
+ }
205
+ parseTypeOfField(typeOfField) {
206
+ const output = [`TYPEOF ${typeOfField.field}`].concat(typeOfField.conditions.map(condition => this.formatter.formatTypeofFieldCondition(condition)));
207
+ output.push(`END`);
208
+ return output;
209
+ }
210
+ parseWhereOrHavingClause(whereOrHaving, tabOffset = 0, priorConditionIsNegation = false) {
211
+ let output = '';
212
+ const left = whereOrHaving.left;
213
+ let trimPrecedingOutput = false;
214
+ if (left) {
215
+ output += this.formatter.formatParens(left.openParen, '(', utils.isNegationCondition(left));
216
+ if (!utils.isNegationCondition(left)) {
217
+ tabOffset = tabOffset + (left.openParen || 0) - (left.closeParen || 0);
218
+ if (priorConditionIsNegation) {
219
+ tabOffset++;
220
+ }
221
+ let expression = '';
222
+ expression += utils.isValueFunctionCondition(left) ? this.parseFn(left.fn) : left.field;
223
+ expression += ` ${left.operator} `;
224
+ if (utils.isValueQueryCondition(left)) {
225
+ expression += this.formatter.formatSubquery(this.parseQuery(left.valueQuery), 1, true);
226
+ }
227
+ else {
228
+ expression += utils.getAsArrayStr(utils.getWhereValue(left.value, left.literalType, left.operator));
229
+ }
230
+ output += this.formatter.formatWithIndent(expression);
231
+ output += this.formatter.formatParens(left.closeParen, ')', priorConditionIsNegation);
232
+ }
233
+ }
234
+ if (utils.isWhereOrHavingClauseWithRightCondition(whereOrHaving)) {
235
+ const operator = utils.get(whereOrHaving.operator);
236
+ trimPrecedingOutput = operator === 'NOT';
237
+ const formattedData = this.formatter.formatWhereClauseOperators(operator, this.parseWhereOrHavingClause(whereOrHaving.right, tabOffset, utils.isNegationCondition(left)), tabOffset);
238
+ return `${trimPrecedingOutput ? output.trimRight() : output}${formattedData}`.trim();
239
+ }
240
+ else {
241
+ return output.trim();
242
+ }
243
+ }
244
+ parseGroupByClause(groupBy) {
245
+ return (Array.isArray(groupBy) ? groupBy : [groupBy])
246
+ .map(clause => (utils.isGroupByField(clause) ? clause.field : this.parseFn(clause.fn)))
247
+ .join(', ');
248
+ }
249
+ parseOrderBy(orderBy) {
250
+ if (Array.isArray(orderBy)) {
251
+ return this.formatter.formatOrderByArray(orderBy.map(ob => this.parseOrderBy(ob)));
252
+ }
253
+ else {
254
+ let output = '';
255
+ if (utils.isOrderByField(orderBy)) {
256
+ output = `${utils.get(orderBy.field, ' ')}`;
257
+ }
258
+ else {
259
+ output += `${this.parseFn(orderBy.fn)} `;
260
+ }
261
+ output += `${utils.get(orderBy.order, ' ')}${utils.get(orderBy.nulls, '', 'NULLS ')}`;
262
+ return output.trim();
263
+ }
264
+ }
265
+ parseWithDataCategory(withDataCategory) {
266
+ return withDataCategory.conditions
267
+ .map(condition => {
268
+ const params = condition.parameters.length > 1 ? `(${condition.parameters.join(', ')})` : `${condition.parameters.join(', ')}`;
269
+ return `${condition.groupName} ${condition.selector} ${params}`;
270
+ })
271
+ .join(' AND ');
272
+ }
273
+ }
@@ -0,0 +1,223 @@
1
+ import { isNumber, generateParens } from '../utils';
2
+ export class Formatter {
3
+ enabled;
4
+ options;
5
+ currIndent = 1;
6
+ constructor(enabled, options) {
7
+ this.enabled = enabled;
8
+ this.options = {
9
+ numIndent: options.numIndent ?? 1,
10
+ fieldMaxLineLength: options.fieldMaxLineLength ?? 60,
11
+ fieldSubqueryParensOnOwnLine: options.fieldSubqueryParensOnOwnLine ?? true,
12
+ whereClauseOperatorsIndented: true,
13
+ newLineAfterKeywords: options.newLineAfterKeywords ?? false,
14
+ logging: options.logging ?? false,
15
+ };
16
+ if (this.options.newLineAfterKeywords) {
17
+ this.options.fieldSubqueryParensOnOwnLine = true;
18
+ }
19
+ }
20
+ log(data) {
21
+ if (this.options.logging) {
22
+ console.log(data);
23
+ }
24
+ }
25
+ getIndent(additionalIndent = 0) {
26
+ return this.repeatChar((this.currIndent + additionalIndent) * (this.options.numIndent || 1), '\t');
27
+ }
28
+ repeatChar(numTimes, char) {
29
+ return new Array(numTimes).fill(char).join('');
30
+ }
31
+ setSubquery(isSubquery) {
32
+ this.currIndent = isSubquery ? (this.currIndent += 1) : (this.currIndent -= 1);
33
+ }
34
+ stepCurrIndex(num) {
35
+ this.currIndent += num;
36
+ }
37
+ formatFields(fieldData) {
38
+ function trimPrevSuffix(currIdx) {
39
+ if (fieldData.fields[currIdx - 1]) {
40
+ fieldData.fields[currIdx - 1].suffix = fieldData.fields[currIdx - 1].suffix.trim();
41
+ }
42
+ }
43
+ fieldData.fields.forEach((field, i) => {
44
+ field.suffix = fieldData.fields.length - 1 === i ? '' : ', ';
45
+ });
46
+ if (this.enabled) {
47
+ let lineLen = 0;
48
+ let newLineAndIndentNext = false;
49
+ fieldData.fields.forEach((field, i) => {
50
+ if (field.isSubquery) {
51
+ trimPrevSuffix(i);
52
+ field.prefix = `\n${this.getIndent()}`;
53
+ field.suffix = fieldData.fields.length - 1 === i ? '' : ', ';
54
+ lineLen = 0;
55
+ newLineAndIndentNext = true;
56
+ }
57
+ else if (Array.isArray(field.typeOfClause)) {
58
+ trimPrevSuffix(i);
59
+ field.prefix = `\n${this.getIndent()}`;
60
+ newLineAndIndentNext = true;
61
+ }
62
+ else if (isNumber(this.options.fieldMaxLineLength)) {
63
+ lineLen += field.text.length + field.suffix.length;
64
+ if (lineLen > this.options.fieldMaxLineLength || newLineAndIndentNext) {
65
+ trimPrevSuffix(i);
66
+ if (!this.options.newLineAfterKeywords || i > 0) {
67
+ field.prefix += `\n${this.getIndent()}`;
68
+ }
69
+ lineLen = 0;
70
+ newLineAndIndentNext = false;
71
+ }
72
+ }
73
+ this.log(field);
74
+ });
75
+ }
76
+ }
77
+ formatTyeOfField(text, typeOfClause) {
78
+ if (this.enabled && this.options.newLineAfterKeywords) {
79
+ return typeOfClause
80
+ .map((part, i) => {
81
+ if (i === 0) {
82
+ return part;
83
+ }
84
+ else if (i === typeOfClause.length - 1) {
85
+ return `${this.getIndent()}${part}`;
86
+ }
87
+ else {
88
+ return `${this.getIndent()}\t${part}`;
89
+ }
90
+ })
91
+ .join('\n');
92
+ }
93
+ return text;
94
+ }
95
+ formatTypeofFieldCondition(condition) {
96
+ let output = '';
97
+ const fields = condition.fieldList.join(', ');
98
+ if (this.enabled && this.options.newLineAfterKeywords) {
99
+ const indent = this.getIndent();
100
+ output = `${condition.type}`;
101
+ if (condition.objectType) {
102
+ output += `\n${indent}\t\t${condition.objectType}\n${indent}\tTHEN\n${indent}\t\t${fields}`;
103
+ }
104
+ else {
105
+ output += `\n${indent}\t\t${fields}`;
106
+ }
107
+ }
108
+ else {
109
+ output = condition.type;
110
+ if (condition.objectType) {
111
+ output += ` ${condition.objectType} THEN ${fields}`;
112
+ }
113
+ else {
114
+ output += ` ${fields}`;
115
+ }
116
+ }
117
+ return output;
118
+ }
119
+ formatSubquery(queryStr, numTabs = 2, incrementTabsWhereClauseOpIndent = false) {
120
+ if (incrementTabsWhereClauseOpIndent) {
121
+ numTabs++;
122
+ }
123
+ let leftParen = '(';
124
+ let rightParen = ')';
125
+ if (this.enabled) {
126
+ if (this.options.fieldSubqueryParensOnOwnLine || this.options.newLineAfterKeywords) {
127
+ queryStr = queryStr.replace(/\n/g, `\n${this.repeatChar(numTabs, '\t')}`);
128
+ leftParen = `(\n${this.repeatChar(numTabs, '\t')}`;
129
+ rightParen = `\n${this.repeatChar(numTabs - 1, '\t')})`;
130
+ }
131
+ else {
132
+ queryStr = queryStr.replace(/\n/g, '\n\t');
133
+ }
134
+ }
135
+ return `${leftParen}${queryStr}${rightParen}`;
136
+ }
137
+ formatClause(clause) {
138
+ if (this.enabled) {
139
+ return this.options.newLineAfterKeywords ? `\n${clause}\n\t` : `\n${clause}`;
140
+ }
141
+ return ` ${clause}`;
142
+ }
143
+ formatText(text) {
144
+ return this.enabled && (this.options.newLineAfterKeywords || text.startsWith('\n')) ? text : ` ${text}`;
145
+ }
146
+ formatWithIndent(text) {
147
+ return this.enabled ? `${this.getIndent()}${text}` : text;
148
+ }
149
+ formatOrderByArray(groupBy) {
150
+ if (this.enabled) {
151
+ let currLen = 0;
152
+ let output = '';
153
+ groupBy.forEach((token, i) => {
154
+ const nextToken = groupBy[i + 1];
155
+ currLen += token.length;
156
+ if (nextToken && (currLen + nextToken.length > (this.options.fieldMaxLineLength || 0) || this.options.newLineAfterKeywords)) {
157
+ output += `${token},\n\t`;
158
+ currLen = 0;
159
+ }
160
+ else {
161
+ output += `${token}${nextToken ? ', ' : ''}`;
162
+ }
163
+ });
164
+ return output;
165
+ }
166
+ else {
167
+ return groupBy.join(', ');
168
+ }
169
+ }
170
+ formatParens(count, character, leadingParenInline = false) {
171
+ let output = '';
172
+ if (isNumber(count) && count > 0) {
173
+ if (this.enabled) {
174
+ if (character === '(') {
175
+ for (let i = 0; i < count; i++) {
176
+ if (leadingParenInline && i === count - 1) {
177
+ output += '(';
178
+ }
179
+ else {
180
+ if (i === 0) {
181
+ output += '(\n';
182
+ }
183
+ else {
184
+ this.currIndent++;
185
+ output += `${this.getIndent()}(\n`;
186
+ }
187
+ }
188
+ }
189
+ if (!leadingParenInline || count > 1) {
190
+ this.currIndent++;
191
+ }
192
+ }
193
+ else {
194
+ for (let i = count - 1; i >= 0; i--) {
195
+ if (leadingParenInline && i === count - 1) {
196
+ output += ')';
197
+ }
198
+ else {
199
+ this.currIndent--;
200
+ output += `\n${this.getIndent()})`;
201
+ }
202
+ }
203
+ }
204
+ }
205
+ else {
206
+ output += generateParens(count, character);
207
+ }
208
+ }
209
+ return output;
210
+ }
211
+ formatWhereClauseOperators(operator, whereClause, additionalIndent = 0) {
212
+ const skipNewLineAndIndent = operator === 'NOT';
213
+ if (this.enabled && !skipNewLineAndIndent) {
214
+ return `\n${this.getIndent(additionalIndent)}${operator} ${whereClause}`;
215
+ }
216
+ else {
217
+ return `${skipNewLineAndIndent ? '' : ' '}${operator} ${whereClause}`;
218
+ }
219
+ }
220
+ formatAddNewLine(alt = ' ', skipNewLineAndIndent) {
221
+ return this.enabled && !skipNewLineAndIndent ? `\n` : alt;
222
+ }
223
+ }
@@ -0,0 +1,4 @@
1
+ export { parseQuery, isQueryValid } from './parser/visitor';
2
+ export * from './api/api-models';
3
+ export * from './api/public-utils';
4
+ export * from './composer/composer';
@@ -0,0 +1 @@
1
+ export {};