@goast/core 0.1.1 → 0.2.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 (58) hide show
  1. package/cjs/index.js +1 -0
  2. package/cjs/lib/ast/index.js +4 -0
  3. package/cjs/lib/ast/node.js +27 -0
  4. package/cjs/lib/parse/parser.js +1 -1
  5. package/cjs/lib/transform/transform-endpoint.js +1 -1
  6. package/cjs/lib/transform/transform-schema.js +4 -2
  7. package/cjs/lib/utils/array.utils.js +11 -0
  8. package/cjs/lib/utils/class.utils.js +33 -0
  9. package/cjs/lib/utils/common.utils.js +75 -1
  10. package/cjs/lib/utils/error.utils.js +7 -0
  11. package/cjs/lib/utils/index.js +4 -0
  12. package/cjs/lib/utils/object.utils.js +6 -2
  13. package/cjs/lib/utils/schema.utils.js +2 -1
  14. package/cjs/lib/utils/source-builder.js +171 -81
  15. package/cjs/lib/utils/string-builder/index.js +8 -0
  16. package/cjs/lib/utils/string-builder/options.js +7 -0
  17. package/cjs/lib/utils/string-builder/string-builder.js +142 -0
  18. package/cjs/lib/utils/string-builder/utils.js +38 -0
  19. package/cjs/lib/utils/string.utils.js +123 -107
  20. package/cjs/lib/utils/type.utils.js +9 -0
  21. package/esm/index.js +1 -0
  22. package/esm/lib/ast/index.js +1 -0
  23. package/esm/lib/ast/node.js +23 -0
  24. package/esm/lib/parse/parser.js +1 -1
  25. package/esm/lib/transform/transform-endpoint.js +1 -1
  26. package/esm/lib/transform/transform-schema.js +4 -2
  27. package/esm/lib/utils/array.utils.js +7 -0
  28. package/esm/lib/utils/class.utils.js +27 -0
  29. package/esm/lib/utils/common.utils.js +64 -0
  30. package/esm/lib/utils/error.utils.js +3 -0
  31. package/esm/lib/utils/index.js +4 -0
  32. package/esm/lib/utils/object.utils.js +6 -2
  33. package/esm/lib/utils/schema.utils.js +2 -1
  34. package/esm/lib/utils/source-builder.js +170 -80
  35. package/esm/lib/utils/string-builder/index.js +3 -0
  36. package/esm/lib/utils/string-builder/options.js +4 -0
  37. package/esm/lib/utils/string-builder/string-builder.js +138 -0
  38. package/esm/lib/utils/string-builder/utils.js +30 -0
  39. package/esm/lib/utils/string.utils.js +111 -105
  40. package/esm/lib/utils/type.utils.js +6 -1
  41. package/package.json +5 -5
  42. package/types/index.d.ts +1 -0
  43. package/types/lib/ast/index.d.ts +1 -0
  44. package/types/lib/ast/node.d.ts +14 -0
  45. package/types/lib/transform/api-types.d.ts +3 -1
  46. package/types/lib/utils/array.utils.d.ts +1 -0
  47. package/types/lib/utils/class.utils.d.ts +7 -0
  48. package/types/lib/utils/common.utils.d.ts +14 -2
  49. package/types/lib/utils/error.utils.d.ts +1 -0
  50. package/types/lib/utils/index.d.ts +4 -0
  51. package/types/lib/utils/object.utils.d.ts +1 -1
  52. package/types/lib/utils/source-builder.d.ts +55 -37
  53. package/types/lib/utils/string-builder/index.d.ts +3 -0
  54. package/types/lib/utils/string-builder/options.d.ts +8 -0
  55. package/types/lib/utils/string-builder/string-builder.d.ts +72 -0
  56. package/types/lib/utils/string-builder/utils.d.ts +21 -0
  57. package/types/lib/utils/string.utils.d.ts +20 -66
  58. package/types/lib/utils/type.utils.d.ts +44 -5
package/cjs/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./lib/ast"), exports);
4
5
  tslib_1.__exportStar(require("./lib/codegen"), exports);
5
6
  tslib_1.__exportStar(require("./lib/parse"), exports);
6
7
  tslib_1.__exportStar(require("./lib/transform"), exports);
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./node"), exports);
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AstNode = void 0;
4
+ const NODE_HISTORY_SYMBOL = Symbol('node-history');
5
+ class AstNode {
6
+ constructor(options) {
7
+ var _a;
8
+ this.inject = (_a = options.inject) !== null && _a !== void 0 ? _a : {};
9
+ }
10
+ write(builder) {
11
+ let history = builder[NODE_HISTORY_SYMBOL];
12
+ if (!history) {
13
+ history = [];
14
+ builder[NODE_HISTORY_SYMBOL] = history;
15
+ }
16
+ history.push(this);
17
+ builder.append(this.inject.before);
18
+ this.onWrite(builder);
19
+ builder.append(this.inject.after);
20
+ history.pop();
21
+ }
22
+ getParentNode(builder) {
23
+ const history = builder[NODE_HISTORY_SYMBOL];
24
+ return history === null || history === void 0 ? void 0 : history.at(-2);
25
+ }
26
+ }
27
+ exports.AstNode = AstNode;
@@ -56,7 +56,7 @@ class OpenApiParser {
56
56
  });
57
57
  existingDocument.dereferencedComponents.set(path, result);
58
58
  if (value.$ref) {
59
- result.$ref = yield this.resolveReference(file, value);
59
+ result.$ref = (yield this.resolveReference(file, value));
60
60
  }
61
61
  for (const key of Object.keys(value)) {
62
62
  if (key === '$ref')
@@ -145,7 +145,7 @@ function transformResponse(context, response, status) {
145
145
  $src: Object.assign(Object.assign({}, response.$src), { component: response }),
146
146
  $ref: ref,
147
147
  id: context.idGenerator.generateId('response'),
148
- statusCode: Number(status),
148
+ statusCode: Number(status) || undefined,
149
149
  description: response.description,
150
150
  headers: transformHeaders(context, response.headers),
151
151
  contentOptions: transformContent(context, response.content),
@@ -157,7 +157,9 @@ function resolveDescriminatorMapping(context, schema) {
157
157
  for (const key of Object.keys(discriminator.mapping)) {
158
158
  const mappedSchemaRef = discriminator.mapping[key];
159
159
  const mappedSchema = transformSchema(context, mappedSchemaRef);
160
- schema.discriminator.mapping[key] = mappedSchema;
161
- mappedSchema.inheritedSchemas.push(schema);
160
+ if (schema.discriminator) {
161
+ schema.discriminator.mapping[key] = mappedSchema;
162
+ mappedSchema.inheritedSchemas.push(schema);
163
+ }
162
164
  }
163
165
  }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeItems = void 0;
4
+ function removeItems(array, predicate) {
5
+ for (let i = array.length - 1; i >= 0; i--) {
6
+ if (predicate(array[i])) {
7
+ array.splice(i, 1);
8
+ }
9
+ }
10
+ }
11
+ exports.removeItems = removeItems;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getIsInstanceOf = exports.isInstanceOf = exports.classifyArray = void 0;
4
+ function classifyArray(items, classification) {
5
+ const result = { rest: [] };
6
+ for (const key in classification) {
7
+ result[key] = [];
8
+ }
9
+ for (const item of items) {
10
+ let classified = false;
11
+ for (const key in classification) {
12
+ const type = classification[key];
13
+ if (item instanceof type) {
14
+ result[key].push(item);
15
+ classified = true;
16
+ break;
17
+ }
18
+ }
19
+ if (!classified) {
20
+ result.rest.push(item);
21
+ }
22
+ }
23
+ return result;
24
+ }
25
+ exports.classifyArray = classifyArray;
26
+ function isInstanceOf(item, type) {
27
+ return item instanceof type;
28
+ }
29
+ exports.isInstanceOf = isInstanceOf;
30
+ function getIsInstanceOf(type) {
31
+ return (item) => item instanceof type;
32
+ }
33
+ exports.getIsInstanceOf = getIsInstanceOf;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.notNullish = exports.isNullish = void 0;
3
+ exports.asVoid = exports.transformEach = exports.runEach = exports.modifyEach = exports.transform = exports.run = exports.modify = exports.toArray = exports.isIterable = exports.concatSingleOrMultiple = exports.notNullish = exports.isNullish = void 0;
4
4
  function isNullish(value) {
5
5
  return value === null || value === undefined;
6
6
  }
@@ -9,3 +9,77 @@ function notNullish(value) {
9
9
  return !isNullish(value);
10
10
  }
11
11
  exports.notNullish = notNullish;
12
+ function concatSingleOrMultiple(...values) {
13
+ if (values.length === 1) {
14
+ return values[0];
15
+ }
16
+ const result = [];
17
+ for (const value of values) {
18
+ if (Array.isArray(value)) {
19
+ result.push(...value);
20
+ }
21
+ else {
22
+ result.push(value);
23
+ }
24
+ }
25
+ return result;
26
+ }
27
+ exports.concatSingleOrMultiple = concatSingleOrMultiple;
28
+ function isIterable(value) {
29
+ return typeof value === 'object' && value !== null && Symbol.iterator in value;
30
+ }
31
+ exports.isIterable = isIterable;
32
+ function toArray(value) {
33
+ if (Array.isArray(value)) {
34
+ return value;
35
+ }
36
+ if (isIterable(value)) {
37
+ return Array.from(value);
38
+ }
39
+ return [value];
40
+ }
41
+ exports.toArray = toArray;
42
+ function modify(value, callback) {
43
+ if (notNullish(value)) {
44
+ callback(value);
45
+ }
46
+ return value;
47
+ }
48
+ exports.modify = modify;
49
+ function run(value, callback) {
50
+ callback(value);
51
+ return value;
52
+ }
53
+ exports.run = run;
54
+ function transform(value, callback) {
55
+ return callback(value);
56
+ }
57
+ exports.transform = transform;
58
+ function modifyEach(values, callback) {
59
+ for (const value of values) {
60
+ if (notNullish(value)) {
61
+ callback(value);
62
+ }
63
+ }
64
+ return values;
65
+ }
66
+ exports.modifyEach = modifyEach;
67
+ function runEach(values, callback) {
68
+ for (const value of values) {
69
+ callback(value);
70
+ }
71
+ return values;
72
+ }
73
+ exports.runEach = runEach;
74
+ function transformEach(values, callback) {
75
+ const result = [];
76
+ for (const value of values) {
77
+ result.push(callback(value));
78
+ }
79
+ return result;
80
+ }
81
+ exports.transformEach = transformEach;
82
+ function asVoid(_) {
83
+ // Do nothing
84
+ }
85
+ exports.asVoid = asVoid;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.throwExp = void 0;
4
+ function throwExp(message) {
5
+ throw new Error(message);
6
+ }
7
+ exports.throwExp = throwExp;
@@ -1,13 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./array.utils"), exports);
5
+ tslib_1.__exportStar(require("./class.utils"), exports);
4
6
  tslib_1.__exportStar(require("./common.utils"), exports);
5
7
  tslib_1.__exportStar(require("./endpoint.utils"), exports);
8
+ tslib_1.__exportStar(require("./error.utils"), exports);
6
9
  tslib_1.__exportStar(require("./factory"), exports);
7
10
  tslib_1.__exportStar(require("./generator.utils"), exports);
8
11
  tslib_1.__exportStar(require("./object.utils"), exports);
9
12
  tslib_1.__exportStar(require("./schema.utils"), exports);
10
13
  tslib_1.__exportStar(require("./source-builder"), exports);
14
+ tslib_1.__exportStar(require("./string-builder"), exports);
11
15
  tslib_1.__exportStar(require("./string.utils"), exports);
12
16
  tslib_1.__exportStar(require("./string.utils.types"), exports);
13
17
  tslib_1.__exportStar(require("./type.utils"), exports);
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createOverwriteProxy = void 0;
4
- function createOverwriteProxy(target) {
5
- return new Proxy(target, new OverwriteProxyHandler());
4
+ function createOverwriteProxy(target, values) {
5
+ const proxy = new Proxy(target, new OverwriteProxyHandler());
6
+ if (values) {
7
+ Object.assign(proxy, values);
8
+ }
9
+ return proxy;
6
10
  }
7
11
  exports.createOverwriteProxy = createOverwriteProxy;
8
12
  class OverwriteProxyHandler {
@@ -37,7 +37,8 @@ function collectSubSchemaProperties(subSchemas, properties, required, optional)
37
37
  }
38
38
  }
39
39
  function hasInvalidSubSchema(subSchemas) {
40
- return subSchemas.some((x) => x.kind !== 'object' && (x.kind !== 'combined' || (!hasInvalidSubSchema(x.allOf) && !hasInvalidSubSchema(x.anyOf))));
40
+ return subSchemas.some((x) => x.kind !== 'object' &&
41
+ (x.kind !== 'combined' || (!hasInvalidSubSchema(x.allOf) && !hasInvalidSubSchema(x.anyOf))));
41
42
  }
42
43
  /**
43
44
  * Retrieves the first schema reference and the reference chain of a given schema that
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SourceBuilder = exports.defaultSourceBuilderOptions = void 0;
3
+ exports.builderTemplate = exports.SourceBuilder = exports.defaultSourceBuilderOptions = void 0;
4
4
  const os_1 = require("os");
5
+ const common_utils_1 = require("./common.utils");
5
6
  const condition_1 = require("./condition");
7
+ const string_builder_1 = require("./string-builder");
6
8
  const string_utils_1 = require("./string.utils");
7
9
  exports.defaultSourceBuilderOptions = {
8
10
  indent: { type: 'spaces', count: 2 },
9
11
  newLine: os_1.EOL,
10
- charsTreatedAsEmptyLine: ['{'],
11
12
  };
12
13
  /**
13
14
  * Represents an in-memory source file.
14
15
  */
15
- class SourceBuilder extends string_utils_1.StringBuilder {
16
+ class SourceBuilder extends string_builder_1.StringBuilder {
16
17
  /**
17
18
  * Gets the options used by this instance.
18
19
  */
@@ -25,16 +26,11 @@ class SourceBuilder extends string_utils_1.StringBuilder {
25
26
  */
26
27
  constructor(options) {
27
28
  super(options);
28
- this._linePrefix = '';
29
29
  this._isLineIndented = false;
30
30
  this._isLastLineEmpty = true;
31
31
  this._isCurrentLineEmpty = true;
32
- /**
33
- * Gets or sets the current indentation level.
34
- */
35
- this.currentIndentLevel = 0;
32
+ this._linePrefixNodes = [];
36
33
  this.__options = Object.assign(Object.assign({}, exports.defaultSourceBuilderOptions), options);
37
- this._emptyLineCharRegex = new RegExp(`[\\s${this.__options.charsTreatedAsEmptyLine.join('')}]`);
38
34
  this._indentString = this.__options.indent.type === 'tabs' ? '\t' : ' '.repeat(this.__options.indent.count);
39
35
  }
40
36
  /**
@@ -56,61 +52,66 @@ class SourceBuilder extends string_utils_1.StringBuilder {
56
52
  */
57
53
  static build(buildAction, options) {
58
54
  const builder = new SourceBuilder(options);
59
- buildAction(builder);
55
+ builder.append(buildAction);
60
56
  return builder.toString();
61
57
  }
62
- /**
63
- * Appends one or more strings to the end of the current SourceBuilder.
64
- * @param value The string(s) to append.
65
- * @returns The current SourceBuilder.
66
- */
67
- append(...value) {
68
- for (const str of value) {
69
- if (!str)
70
- continue;
71
- if (typeof str === 'function') {
72
- str(this);
73
- continue;
58
+ appendSingle(value) {
59
+ var _a;
60
+ if ((0, common_utils_1.isNullish)(value))
61
+ return;
62
+ if (Array.isArray(value)) {
63
+ for (const part of value) {
64
+ this.appendSingle(part);
74
65
  }
75
- let lineStartIndex = 0;
76
- for (let i = 0; i < str.length; i++) {
77
- if (str[i] === '\r')
78
- continue;
79
- if (str[i] === '\n') {
80
- let lineLength = i - lineStartIndex;
81
- for (let j = i; j > 0 && str[j - 1] === '\r'; j--) {
82
- lineLength--;
83
- }
84
- if (lineLength > 0) {
85
- if (!this._isLineIndented) {
86
- this.appendIndent();
87
- }
88
- super.append(str.substring(lineStartIndex, i).replace(/\r/g, ''));
89
- }
90
- this._isLastLineEmpty = this._isCurrentLineEmpty;
91
- super.append(this.__options.newLine);
92
- if (this._linePrefix) {
66
+ return;
67
+ }
68
+ if (typeof value === 'function') {
69
+ value(this);
70
+ return;
71
+ }
72
+ if (typeof value === 'number' || typeof value === 'boolean') {
73
+ value = value.toString();
74
+ }
75
+ if (typeof value === 'object' && value !== null && '__type' in value && value.__type === 'append-value-group') {
76
+ this.forEach(value.values, (builder, part) => builder.append(part), { separator: (_a = value.separator) !== null && _a !== void 0 ? _a : undefined });
77
+ return;
78
+ }
79
+ if (typeof value !== 'string') {
80
+ return;
81
+ }
82
+ let lineStartIndex = 0;
83
+ for (let i = 0; i < value.length; i++) {
84
+ if (value[i] === '\r')
85
+ continue;
86
+ if (value[i] === '\n') {
87
+ let lineLength = i - lineStartIndex;
88
+ for (let j = i; j > 0 && value[j - 1] === '\r'; j--) {
89
+ lineLength--;
90
+ }
91
+ if (lineLength > 0) {
92
+ if (!this._isLineIndented) {
93
93
  this.appendIndent();
94
- this._isLineIndented = true;
95
- this._isCurrentLineEmpty = false;
96
- }
97
- else {
98
- this._isLineIndented = false;
99
- this._isCurrentLineEmpty = true;
100
94
  }
101
- lineStartIndex = i + 1;
95
+ this.str += (0, string_utils_1.removeStr)(value.substring(lineStartIndex, i), '\r');
102
96
  }
103
- if (!this._emptyLineCharRegex.test(str[i])) {
104
- this._isCurrentLineEmpty = false;
97
+ this._isLastLineEmpty = this._isCurrentLineEmpty;
98
+ if (this._isCurrentLineEmpty && this._linePrefixNodes.some((x) => x.kind !== 'indent')) {
99
+ this.appendIndent();
105
100
  }
101
+ this.str = (0, string_utils_1.trimEnd)(this.str, false) + '\n';
102
+ this._isLineIndented = false;
103
+ this._isCurrentLineEmpty = true;
104
+ lineStartIndex = i + 1;
106
105
  }
107
- if (!this._isLineIndented && (lineStartIndex < str.length || this._linePrefix)) {
108
- this.appendIndent();
109
- this._isLineIndented = true;
106
+ if (value[i] !== '\r' && value[i] !== '\n' && value[i] !== ' ' && value[i] !== '\t') {
107
+ this._isCurrentLineEmpty = false;
110
108
  }
111
- super.append(str.substring(lineStartIndex).replace(/\r/g, ''));
112
109
  }
113
- return this;
110
+ if (!this._isLineIndented && lineStartIndex < value.length) {
111
+ this.appendIndent();
112
+ this._isLineIndented = true;
113
+ }
114
+ this.str += (0, string_utils_1.removeStr)(value.substring(lineStartIndex), '\r');
114
115
  }
115
116
  /**
116
117
  * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
@@ -119,7 +120,10 @@ class SourceBuilder extends string_utils_1.StringBuilder {
119
120
  * @returns The current SourceBuilder.
120
121
  */
121
122
  appendIf(condition, ...value) {
122
- return this.if(condition, (builder) => builder.append(...value));
123
+ if ((0, condition_1.evalCondition)(condition)) {
124
+ return this.append(...value);
125
+ }
126
+ return this;
123
127
  }
124
128
  /**
125
129
  * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
@@ -136,7 +140,10 @@ class SourceBuilder extends string_utils_1.StringBuilder {
136
140
  * @returns The current SourceBuilder.
137
141
  */
138
142
  appendLineIf(condition, ...value) {
139
- return this.if(condition, (builder) => builder.appendLine(...value));
143
+ if ((0, condition_1.evalCondition)(condition)) {
144
+ return this.appendLine(...value);
145
+ }
146
+ return this;
140
147
  }
141
148
  /**
142
149
  * Appends the specified values to this source builder with the specified line prefix.
@@ -145,13 +152,12 @@ class SourceBuilder extends string_utils_1.StringBuilder {
145
152
  * @returns {SourceBuilder} This source builder instance.
146
153
  */
147
154
  appendWithLinePrefix(prefix, ...value) {
148
- const previousLinePrefix = this._linePrefix;
149
- this._linePrefix += prefix;
155
+ this._linePrefixNodes.push({ chars: prefix, kind: 'prefix' });
150
156
  try {
151
157
  this.append(...value);
152
158
  }
153
159
  finally {
154
- this._linePrefix = previousLinePrefix;
160
+ this._linePrefixNodes.pop();
155
161
  }
156
162
  return this;
157
163
  }
@@ -171,7 +177,10 @@ class SourceBuilder extends string_utils_1.StringBuilder {
171
177
  * @returns The current SourceBuilder.
172
178
  */
173
179
  prependIf(condition, ...value) {
174
- return this.if(condition, (builder) => builder.prepend(...value));
180
+ if ((0, condition_1.evalCondition)(condition)) {
181
+ this.prepend(...value);
182
+ }
183
+ return this;
175
184
  }
176
185
  /**
177
186
  * Prepends one or more strings to the beginning of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
@@ -180,10 +189,13 @@ class SourceBuilder extends string_utils_1.StringBuilder {
180
189
  * @returns The current SourceBuilder.
181
190
  */
182
191
  prependLineIf(condition, ...value) {
183
- return this.if(condition, (builder) => builder.prependLine(...value));
192
+ if ((0, condition_1.evalCondition)(condition)) {
193
+ this.prependLine(...value);
194
+ }
195
+ return this;
184
196
  }
185
197
  /**
186
- * Ensures that the current line is empty or consists only of whitespace characters.
198
+ * Ensures that the current line and the one before is empty or consists only of whitespace characters.
187
199
  * @returns A reference to this instance.
188
200
  */
189
201
  ensurePreviousLineEmpty() {
@@ -205,6 +217,23 @@ class SourceBuilder extends string_utils_1.StringBuilder {
205
217
  }
206
218
  return this;
207
219
  }
220
+ /**
221
+ * Pretends that the current line and the one before is empty or consists only of whitespace characters.
222
+ * @returns A reference to this instance.
223
+ */
224
+ pretendPreviousLineEmpty() {
225
+ this._isLastLineEmpty = true;
226
+ this._isCurrentLineEmpty = true;
227
+ return this;
228
+ }
229
+ /**
230
+ * Pretends that the current line is empty or consists only of whitespace characters.
231
+ * @returns A reference to this instance.
232
+ */
233
+ pretendCurrentLineEmpty() {
234
+ this._isCurrentLineEmpty = true;
235
+ return this;
236
+ }
208
237
  /**
209
238
  * Inserts specified content if the specified condition is true.
210
239
  * @param condition The condition to check before inserting content.
@@ -213,11 +242,20 @@ class SourceBuilder extends string_utils_1.StringBuilder {
213
242
  * @returns A reference to this instance.
214
243
  */
215
244
  if(condition, builderFn, elseBuilderFn) {
216
- if ((0, condition_1.evalCondition)(condition)) {
217
- builderFn(this);
245
+ return (0, condition_1.evalCondition)(condition) ? this.append(builderFn) : this.append(elseBuilderFn);
246
+ }
247
+ switch(value, cases, defaultBuilderFn, equals) {
248
+ var _a;
249
+ if ((typeof value === 'string' || typeof value === 'number') && !Array.isArray(cases)) {
250
+ return this.append((_a = cases[value]) !== null && _a !== void 0 ? _a : defaultBuilderFn);
218
251
  }
219
- else {
220
- elseBuilderFn === null || elseBuilderFn === void 0 ? void 0 : elseBuilderFn(this);
252
+ else if (Array.isArray(cases)) {
253
+ for (const c of cases) {
254
+ if (equals ? equals(value, c.case) : value === c.case) {
255
+ return this.append(c.value);
256
+ }
257
+ }
258
+ return this.append(defaultBuilderFn);
221
259
  }
222
260
  return this;
223
261
  }
@@ -228,12 +266,12 @@ class SourceBuilder extends string_utils_1.StringBuilder {
228
266
  * @returns A reference to this instance.
229
267
  */
230
268
  indent(value) {
231
- this.currentIndentLevel++;
269
+ this._linePrefixNodes.push({ chars: this._indentString, kind: 'indent' });
232
270
  try {
233
271
  this.append(value);
234
272
  }
235
273
  finally {
236
- this.currentIndentLevel--;
274
+ this._linePrefixNodes.pop();
237
275
  }
238
276
  return this;
239
277
  }
@@ -244,7 +282,7 @@ class SourceBuilder extends string_utils_1.StringBuilder {
244
282
  * @returns A reference to this instance.
245
283
  */
246
284
  indentIf(condition, value) {
247
- return this.if(condition, (builder) => builder.indent(value), (builder) => builder.append(value));
285
+ return (0, condition_1.evalCondition)(condition) ? this.indent(value) : this.append(value);
248
286
  }
249
287
  /**
250
288
  * Adds parentheses around the specified content.
@@ -254,11 +292,28 @@ class SourceBuilder extends string_utils_1.StringBuilder {
254
292
  * @returns A reference to this instance.
255
293
  */
256
294
  parenthesize(brackets, value, options) {
257
- var _a, _b, _c, _d;
258
- return this.if((_a = options === null || options === void 0 ? void 0 : options.multiline) !== null && _a !== void 0 ? _a : false, (b) => { var _a; return b.appendLine((_a = brackets[0]) !== null && _a !== void 0 ? _a : ''); }, (b) => { var _a; return b.append((_a = brackets[0]) !== null && _a !== void 0 ? _a : ''); })
259
- .indentIf((_b = options === null || options === void 0 ? void 0 : options.indent) !== null && _b !== void 0 ? _b : true, value)
260
- .if((_c = options === null || options === void 0 ? void 0 : options.multiline) !== null && _c !== void 0 ? _c : false, (b) => b.ensureCurrentLineEmpty())
261
- .append((_d = brackets[1]) !== null && _d !== void 0 ? _d : '');
295
+ var _a, _b, _c, _d, _e, _f;
296
+ const multiline = (_a = options === null || options === void 0 ? void 0 : options.multiline) !== null && _a !== void 0 ? _a : false;
297
+ const indent = (_b = options === null || options === void 0 ? void 0 : options.indent) !== null && _b !== void 0 ? _b : true;
298
+ const pretendEmpty = (_c = options === null || options === void 0 ? void 0 : options.pretendEmpty) !== null && _c !== void 0 ? _c : true;
299
+ if (multiline) {
300
+ this.appendLine((_d = brackets[0]) !== null && _d !== void 0 ? _d : '');
301
+ }
302
+ else {
303
+ this.append((_e = brackets[0]) !== null && _e !== void 0 ? _e : '');
304
+ }
305
+ if (pretendEmpty)
306
+ this.pretendPreviousLineEmpty();
307
+ if (indent !== false) {
308
+ this.indent(value);
309
+ }
310
+ else {
311
+ this.append(value);
312
+ }
313
+ if (multiline)
314
+ this.ensureCurrentLineEmpty();
315
+ this.append((_f = brackets[1]) !== null && _f !== void 0 ? _f : '');
316
+ return this;
262
317
  }
263
318
  /**
264
319
  * Adds parentheses around the specified content if the specified condition is true.
@@ -269,7 +324,15 @@ class SourceBuilder extends string_utils_1.StringBuilder {
269
324
  * @returns A reference to this instance.
270
325
  */
271
326
  parenthesizeIf(condition, brackets, value, options) {
272
- return this.if(condition, (builder) => builder.parenthesize(brackets, value, options), (builder) => builder.indentIf((options === null || options === void 0 ? void 0 : options.indent) === true, value));
327
+ if ((0, condition_1.evalCondition)(condition)) {
328
+ return this.parenthesize(brackets, value, options);
329
+ }
330
+ else if (options === null || options === void 0 ? void 0 : options.indent) {
331
+ return this.indent(value);
332
+ }
333
+ else {
334
+ return this.append(value);
335
+ }
273
336
  }
274
337
  /**
275
338
  * Adds content for each item in the specified array.
@@ -307,19 +370,46 @@ class SourceBuilder extends string_utils_1.StringBuilder {
307
370
  return this;
308
371
  }
309
372
  forEachIf(condition, items, builderFn, options) {
310
- return this.if(condition, (b) => b.forEach(items, builderFn, options));
373
+ if ((0, condition_1.evalCondition)(condition)) {
374
+ return this.forEach(items, builderFn, options);
375
+ }
376
+ return this;
311
377
  }
312
378
  appendSeparated(items, separator) {
313
379
  return this.forEach(items, (builder, item) => builder.append(item), { separator });
314
380
  }
315
381
  appendSeparatedIf(condition, items, separator) {
316
- return this.forEachIf(condition, items, (builder, item) => builder.append(item), { separator });
382
+ if ((0, condition_1.evalCondition)(condition)) {
383
+ return this.appendSeparated(items, separator);
384
+ }
385
+ return this;
317
386
  }
318
387
  appendIndent() {
319
- super.append(this._indentString.repeat(this.currentIndentLevel));
320
- if (this._linePrefix) {
321
- super.append(this._linePrefix);
322
- }
388
+ this.str += this._linePrefixNodes.map((n) => n.chars).join('');
323
389
  }
324
390
  }
325
391
  exports.SourceBuilder = SourceBuilder;
392
+ function _builderTemplate(template, ...substitutions) {
393
+ return (b) => {
394
+ var _a, _b;
395
+ let lastLine = undefined;
396
+ for (let i = template.length - 1; i >= 0; i--) {
397
+ if (template[i].includes('\n')) {
398
+ lastLine = template[i];
399
+ break;
400
+ }
401
+ }
402
+ const indent = (_b = (_a = lastLine === null || lastLine === void 0 ? void 0 : lastLine.match(/\n( *).*$/)) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : '';
403
+ const regex = new RegExp(`\\n {0,${indent.length}}`, 'g');
404
+ for (let i = 0; i < template.length; i++) {
405
+ b.append(template[i].replace(regex, '\n'));
406
+ if (i < substitutions.length)
407
+ b.append(substitutions[i]);
408
+ }
409
+ };
410
+ }
411
+ exports.builderTemplate = Object.assign(_builderTemplate, {
412
+ indent: (template, ...substitutions) => {
413
+ return (b) => b.indent(_builderTemplate(template, ...substitutions));
414
+ },
415
+ });
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringBuilder = void 0;
4
+ const tslib_1 = require("tslib");
5
+ tslib_1.__exportStar(require("./options"), exports);
6
+ var string_builder_1 = require("./string-builder");
7
+ Object.defineProperty(exports, "StringBuilder", { enumerable: true, get: function () { return string_builder_1.StringBuilder; } });
8
+ tslib_1.__exportStar(require("./utils"), exports);