ts-codemod-lib 3.3.0 → 3.3.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 (69) hide show
  1. package/dist/entry-point.mjs +2 -24
  2. package/dist/entry-point.mjs.map +1 -1
  3. package/dist/functions/ast-transformers/append-as-const.mjs +330 -292
  4. package/dist/functions/ast-transformers/append-as-const.mjs.map +1 -1
  5. package/dist/functions/ast-transformers/convert-interface-to-type.mjs +85 -70
  6. package/dist/functions/ast-transformers/convert-interface-to-type.mjs.map +1 -1
  7. package/dist/functions/ast-transformers/convert-to-readonly.mjs +969 -840
  8. package/dist/functions/ast-transformers/convert-to-readonly.mjs.map +1 -1
  9. package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.mjs +276 -134
  10. package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.mjs.map +1 -1
  11. package/dist/functions/ast-transformers/index.mjs +10 -12
  12. package/dist/functions/ast-transformers/index.mjs.map +1 -1
  13. package/dist/functions/ast-transformers/readonly-transformer-helpers/compare-union-types.mjs +14 -7
  14. package/dist/functions/ast-transformers/readonly-transformer-helpers/compare-union-types.mjs.map +1 -1
  15. package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.mjs +12 -15
  16. package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.mjs.map +1 -1
  17. package/dist/functions/ast-transformers/readonly-transformer-helpers/group-union-types.mjs +63 -58
  18. package/dist/functions/ast-transformers/readonly-transformer-helpers/group-union-types.mjs.map +1 -1
  19. package/dist/functions/ast-transformers/readonly-transformer-helpers/index.mjs +5 -5
  20. package/dist/functions/ast-transformers/readonly-transformer-helpers/index.mjs.map +1 -1
  21. package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mjs +15 -20
  22. package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mjs.map +1 -1
  23. package/dist/functions/ast-transformers/replace-any-with-unknown.mjs +30 -34
  24. package/dist/functions/ast-transformers/replace-any-with-unknown.mjs.map +1 -1
  25. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs +197 -156
  26. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs.map +1 -1
  27. package/dist/functions/ast-transformers/transform-source-code.mjs +74 -75
  28. package/dist/functions/ast-transformers/transform-source-code.mjs.map +1 -1
  29. package/dist/functions/ast-transformers/types.mjs +2 -2
  30. package/dist/functions/ast-transformers/types.mjs.map +1 -1
  31. package/dist/functions/constants/ignore-comment-text.mjs +11 -13
  32. package/dist/functions/constants/ignore-comment-text.mjs.map +1 -1
  33. package/dist/functions/constants/index.mjs +2 -2
  34. package/dist/functions/constants/index.mjs.map +1 -1
  35. package/dist/functions/functions/has-disable-next-line-comment.mjs +67 -38
  36. package/dist/functions/functions/has-disable-next-line-comment.mjs.map +1 -1
  37. package/dist/functions/functions/index.mjs +11 -11
  38. package/dist/functions/functions/index.mjs.map +1 -1
  39. package/dist/functions/functions/is-as-const-node.mjs +25 -20
  40. package/dist/functions/functions/is-as-const-node.mjs.map +1 -1
  41. package/dist/functions/functions/is-atomic-type-node.mjs +245 -25
  42. package/dist/functions/functions/is-atomic-type-node.mjs.map +1 -1
  43. package/dist/functions/functions/is-conditional-type-distribution-guard.mjs +34 -13
  44. package/dist/functions/functions/is-conditional-type-distribution-guard.mjs.map +1 -1
  45. package/dist/functions/functions/is-readonly-node.mjs +120 -14
  46. package/dist/functions/functions/is-readonly-node.mjs.map +1 -1
  47. package/dist/functions/functions/is-spread-parameter-node.mjs +5 -6
  48. package/dist/functions/functions/is-spread-parameter-node.mjs.map +1 -1
  49. package/dist/functions/functions/remove-parentheses.mjs +4 -4
  50. package/dist/functions/functions/remove-parentheses.mjs.map +1 -1
  51. package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs +23 -9
  52. package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs.map +1 -1
  53. package/dist/functions/functions/unwrap-readonly.mjs +3 -4
  54. package/dist/functions/functions/unwrap-readonly.mjs.map +1 -1
  55. package/dist/functions/functions/wrap-with-parentheses.d.mts.map +1 -1
  56. package/dist/functions/functions/wrap-with-parentheses.mjs +54 -29
  57. package/dist/functions/functions/wrap-with-parentheses.mjs.map +1 -1
  58. package/dist/functions/index.mjs +5 -24
  59. package/dist/functions/index.mjs.map +1 -1
  60. package/dist/functions/utils/index.mjs +2 -2
  61. package/dist/functions/utils/index.mjs.map +1 -1
  62. package/dist/functions/utils/replace-with-debug.mjs +4 -6
  63. package/dist/functions/utils/replace-with-debug.mjs.map +1 -1
  64. package/dist/index.mjs +2 -24
  65. package/dist/index.mjs.map +1 -1
  66. package/package.json +11 -12
  67. package/src/functions/ast-transformers/convert-to-readonly.test.mts +10 -1
  68. package/src/functions/ast-transformers/enable-no-unchecked-indexed-access.test.mts +76 -22
  69. package/src/functions/functions/wrap-with-parentheses.mts +5 -2
@@ -1,320 +1,358 @@
1
- import { ISet, Arr } from 'ts-data-forge';
1
+ import { Arr, ISet } from 'ts-data-forge';
2
2
  import * as tsm from 'ts-morph';
3
- import { hasDisableNextLineComment } from '../functions/has-disable-next-line-comment.mjs';
4
- import { isAsConstNode } from '../functions/is-as-const-node.mjs';
5
- import '../functions/is-atomic-type-node.mjs';
6
- import { replaceNodeWithDebugPrint } from '../utils/replace-with-debug.mjs';
7
-
8
- const TRANSFORMER_NAME = "append-as-const";
9
- const appendAsConstTransformer = (options) => {
10
- const ignorePrefixes = ISet.create(
11
- options?.ignorePrefixes ?? ["mut_", "#mut_", "_mut_", "draft"]
12
- );
13
- const optionsInternal = {
14
- applyLevel: options?.applyLevel ?? "avoidInFunctionArgs",
15
- ignoredPrefixes: ignorePrefixes,
16
- removeAsConstForConstTypeParameters: options?.removeAsConstForConstTypeParameters ?? true,
17
- debugPrint: options?.debug === true ? console.debug : () => {
18
- },
19
- replaceNode: options?.debug === true ? replaceNodeWithDebugPrint : (node, newNodeText) => node.replaceWithText(newNodeText)
20
- };
21
- return {
22
- name: TRANSFORMER_NAME,
23
- transform: (sourceAst) => {
24
- for (const node of sourceAst.getDescendantsOfKind(
25
- tsm.SyntaxKind.VariableDeclaration
26
- )) {
27
- transformNode(
28
- node,
29
- {
30
- isUnderConstContext: false,
31
- isDirectUnderConstInitializer: false
32
- },
33
- optionsInternal
34
- );
35
- }
36
- if (optionsInternal.removeAsConstForConstTypeParameters) {
37
- removeAsConstInConstTypeParameterArgs(sourceAst, optionsInternal);
38
- }
39
- }
40
- };
3
+ import { hasDisableNextLineComment, isAsConstNode, } from '../functions/index.mjs';
4
+ import { replaceNodeWithDebugPrint } from '../utils/index.mjs';
5
+ const TRANSFORMER_NAME = 'append-as-const';
6
+ export const appendAsConstTransformer = (options) => {
7
+ const ignorePrefixes = ISet.create(options?.ignorePrefixes ?? ['mut_', '#mut_', '_mut_', 'draft']);
8
+ const optionsInternal = {
9
+ applyLevel: options?.applyLevel ?? 'avoidInFunctionArgs',
10
+ ignoredPrefixes: ignorePrefixes,
11
+ removeAsConstForConstTypeParameters: options?.removeAsConstForConstTypeParameters ?? true,
12
+ debugPrint: options?.debug === true ? console.debug : () => { },
13
+ replaceNode: options?.debug === true
14
+ ? replaceNodeWithDebugPrint
15
+ : (node, newNodeText) => node.replaceWithText(newNodeText),
16
+ };
17
+ return {
18
+ name: TRANSFORMER_NAME,
19
+ transform: (sourceAst) => {
20
+ for (const node of sourceAst.getDescendantsOfKind(tsm.SyntaxKind.VariableDeclaration)) {
21
+ transformNode(node, {
22
+ isUnderConstContext: false,
23
+ isDirectUnderConstInitializer: false,
24
+ }, optionsInternal);
25
+ }
26
+ if (optionsInternal.removeAsConstForConstTypeParameters) {
27
+ removeAsConstInConstTypeParameterArgs(sourceAst, optionsInternal);
28
+ }
29
+ },
30
+ };
41
31
  };
42
32
  const transformNode = (node, context, options) => {
43
- if (node.wasForgotten()) {
44
- return;
45
- }
46
- options.debugPrint(node.getKindName(), node.getText(), { context });
47
- if (hasDisableNextLineComment(node, TRANSFORMER_NAME)) {
48
- options.debugPrint("skipped by disable-next-line comment");
49
- return;
50
- }
51
- if (options.applyLevel === "avoidInFunctionArgs" && tsm.Node.isCallExpression(node)) {
52
- return;
53
- }
54
- if (node.isKind(tsm.SyntaxKind.VariableDeclaration)) {
55
- const nodeName = node.getName();
56
- if (options.ignoredPrefixes.some((p) => nodeName.startsWith(p))) {
57
- options.debugPrint("skipped variable declaration by ignorePrefixes");
58
- return;
33
+ if (node.wasForgotten()) {
34
+ return;
59
35
  }
60
- const variableStatement = node.getVariableStatement();
61
- if (variableStatement !== void 0 && hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)) {
62
- return;
36
+ options.debugPrint(node.getKindName(), node.getText(), { context });
37
+ if (hasDisableNextLineComment(node, TRANSFORMER_NAME)) {
38
+ options.debugPrint('skipped by disable-next-line comment');
39
+ return;
63
40
  }
64
- const initializer = node.getInitializer();
65
- if (initializer === void 0) {
66
- return;
41
+ if (options.applyLevel === 'avoidInFunctionArgs' &&
42
+ tsm.Node.isCallExpression(node)) {
43
+ return;
67
44
  }
68
- const declarationKindKeywords = node.getVariableStatement()?.getDeclarationKindKeywords().map((k) => k.getText());
69
- if (declarationKindKeywords !== void 0 && Arr.isFixedLengthArray(1, declarationKindKeywords)) {
70
- transformNode(
71
- initializer,
72
- {
73
- isDirectUnderConstInitializer: declarationKindKeywords[0] === "const",
74
- isUnderConstContext: false
75
- },
76
- options
77
- );
78
- return;
79
- }
80
- }
81
- if (tsm.Node.isAsExpression(node) && !isAsConstNode(node)) {
82
- return;
83
- }
84
- if (tsm.Node.isParenthesizedExpression(node)) {
85
- transformNode(node.getExpression(), context, options);
86
- return;
87
- }
88
- if (tsm.Node.isSatisfiesExpression(node)) {
89
- transformNode(node.getExpression(), context, options);
90
- return;
91
- }
92
- if (tsm.Node.isPropertyAssignment(node)) {
93
- const initializer = node.getInitializer();
94
- if (initializer !== void 0) {
95
- transformNode(initializer, context, options);
96
- }
97
- return;
98
- }
99
- if (tsm.Node.isArrowFunction(node)) {
100
- const body = node.getBody();
101
- transformNode(body, context, options);
102
- return;
103
- }
104
- if (tsm.Node.isSpreadElement(node)) {
105
- transformNode(node.getExpression(), context, options);
106
- return;
107
- }
108
- if (node.isKind(tsm.SyntaxKind.NoSubstitutionTemplateLiteral) || // `abc`
109
- node.isKind(tsm.SyntaxKind.NumericLiteral) || // 123
110
- node.isKind(tsm.SyntaxKind.BigIntLiteral) || // 123n
111
- node.isKind(tsm.SyntaxKind.StringLiteral) || // 'abc'
112
- node.isKind(tsm.SyntaxKind.TrueKeyword) || // true
113
- node.isKind(tsm.SyntaxKind.FalseKeyword)) {
114
- if (!context.isDirectUnderConstInitializer && !context.isUnderConstContext) {
115
- options.replaceNode(node, `${node.getText()} as const`);
45
+ if (node.isKind(tsm.SyntaxKind.VariableDeclaration)) {
46
+ const nodeName = node.getName();
47
+
48
+ if (options.ignoredPrefixes.some((p) => nodeName.startsWith(p))) {
49
+
50
+
51
+ options.debugPrint('skipped variable declaration by ignorePrefixes');
52
+ return;
53
+ }
54
+ const variableStatement = node.getVariableStatement();
55
+ if (variableStatement !== undefined &&
56
+ hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)) {
57
+ return;
58
+ }
59
+ const initializer = node.getInitializer();
60
+ if (initializer === undefined) {
61
+ return;
62
+ }
63
+ const declarationKindKeywords = node
64
+ .getVariableStatement()
65
+ ?.getDeclarationKindKeywords()
66
+ .map((k) => k.getText());
67
+ if (declarationKindKeywords !== undefined &&
68
+ Arr.isFixedLengthArray(1, declarationKindKeywords)) {
69
+ transformNode(initializer, {
70
+ isDirectUnderConstInitializer: declarationKindKeywords[0] === 'const',
71
+ isUnderConstContext: false,
72
+ }, options);
73
+ return;
74
+ }
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
116
85
  }
117
- return;
118
- }
119
- if (node.isKind(tsm.SyntaxKind.TemplateExpression)) {
120
- if (!context.isUnderConstContext) {
121
- options.replaceNode(node, `${node.getText()} as const`);
86
+
87
+ if (tsm.Node.isAsExpression(node) && !isAsConstNode(node)) {
88
+ return;
122
89
  }
123
- return;
124
- }
125
- if (tsm.Node.isArrayLiteralExpression(node)) {
126
- for (const el of node.getElements()) {
127
- transformNode(
128
- el,
129
- {
130
- isUnderConstContext: true,
131
- // [...] as const
132
- isDirectUnderConstInitializer: false
133
- },
134
- options
135
- );
90
+
91
+ if (tsm.Node.isParenthesizedExpression(node)) {
92
+ transformNode(node.getExpression(), context, options);
93
+ return;
136
94
  }
137
- if (!context.isUnderConstContext) {
138
- options.replaceNode(node, `${node.getText()} as const`);
95
+
96
+ if (tsm.Node.isSatisfiesExpression(node)) {
97
+ transformNode(node.getExpression(), context, options);
98
+ return;
139
99
  }
140
- return;
141
- }
142
- if (tsm.Node.isObjectLiteralExpression(node)) {
143
- for (const el of node.getProperties()) {
144
- transformNode(
145
- el,
146
- {
147
- isUnderConstContext: true,
148
- // {...} as const
149
- isDirectUnderConstInitializer: false
150
- },
151
- options
152
- );
100
+
101
+ if (tsm.Node.isPropertyAssignment(node)) {
102
+ const initializer = node.getInitializer();
103
+ if (initializer !== undefined) {
104
+ transformNode(initializer, context, options);
105
+ }
106
+ return;
153
107
  }
154
- if (!context.isUnderConstContext) {
155
- options.replaceNode(node, `${node.getText()} as const`);
108
+
109
+ if (tsm.Node.isArrowFunction(node)) {
110
+ const body = node.getBody();
111
+ transformNode(body, context, options);
112
+ return;
156
113
  }
157
- return;
158
- }
159
- if (isAsConstNode(node)) {
160
- if (context.isDirectUnderConstInitializer) {
161
- transformNode(
162
- node.getExpression(),
163
- {
164
- isUnderConstContext: false,
165
- isDirectUnderConstInitializer: true
166
- },
167
- options
168
- );
169
- options.replaceNode(
170
- node,
171
- // The expression may be marked "as const"
172
- node.getExpression().getText()
173
- );
174
- return;
114
+
115
+ if (tsm.Node.isSpreadElement(node)) {
116
+ transformNode(node.getExpression(), context, options);
117
+ return;
175
118
  }
176
- if (context.isUnderConstContext) {
177
- transformNode(
178
- node.getExpression(),
179
- {
180
- isUnderConstContext: true,
181
- isDirectUnderConstInitializer: false
182
- },
183
- options
184
- );
185
- options.replaceNode(
186
- node,
187
- // The expression may be marked "as const"
188
- node.getExpression().getText()
189
- );
190
- return;
119
+ if (node.isKind(tsm.SyntaxKind.NoSubstitutionTemplateLiteral) ||
120
+ node.isKind(tsm.SyntaxKind.NumericLiteral) ||
121
+ node.isKind(tsm.SyntaxKind.BigIntLiteral) ||
122
+ node.isKind(tsm.SyntaxKind.StringLiteral) ||
123
+ node.isKind(tsm.SyntaxKind.TrueKeyword) ||
124
+ node.isKind(tsm.SyntaxKind.FalseKeyword)
125
+ ) {
126
+ if (!context.isDirectUnderConstInitializer &&
127
+ !context.isUnderConstContext) {
128
+ options.replaceNode(node, `${node.getText()} as const`);
129
+ }
130
+ return;
191
131
  }
192
- transformNode(
193
- node.getExpression(),
194
- {
195
- isUnderConstContext: true,
196
- isDirectUnderConstInitializer: false
197
- },
198
- options
199
- );
200
- return;
201
- }
202
- if (tsm.Node.isConditionalExpression(node)) {
203
- transformNode(
204
- node.getWhenTrue(),
205
- {
206
- isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,
207
- isUnderConstContext: false
208
- },
209
- options
210
- );
211
- transformNode(
212
- node.getWhenFalse(),
213
- {
214
- isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,
215
- isUnderConstContext: false
216
- },
217
- options
218
- );
219
- }
220
- };
221
- const removeAsConstInConstTypeParameterArgs = (sourceAst, options) => {
222
- for (const call of sourceAst.getDescendantsOfKind(tsm.SyntaxKind.CallExpression).toReversed()) {
223
- if (call.wasForgotten()) {
224
- continue;
132
+ if (node.isKind(tsm.SyntaxKind.TemplateExpression)) {
133
+ if (!context.isUnderConstContext) {
134
+ options.replaceNode(node, `${node.getText()} as const`);
135
+ }
136
+ return;
225
137
  }
226
- const args = call.getArguments();
227
- if (!args.some(isAsConstNode)) {
228
- continue;
138
+ if (tsm.Node.isArrayLiteralExpression(node)) {
139
+ for (const el of node.getElements()) {
140
+ transformNode(el, {
141
+ isUnderConstContext: true,
142
+ isDirectUnderConstInitializer: false,
143
+ }, options);
144
+ }
145
+ if (!context.isUnderConstContext) {
146
+ options.replaceNode(node, `${node.getText()} as const`);
147
+ }
148
+ return;
229
149
  }
230
- if (hasDisableNextLineComment(call, TRANSFORMER_NAME)) {
231
- continue;
150
+ if (tsm.Node.isObjectLiteralExpression(node)) {
151
+ for (const el of node.getProperties()) {
152
+ transformNode(el, {
153
+ isUnderConstContext: true,
154
+ isDirectUnderConstInitializer: false,
155
+ }, options);
156
+ }
157
+ if (!context.isUnderConstContext) {
158
+ options.replaceNode(node, `${node.getText()} as const`);
159
+ }
160
+ return;
232
161
  }
233
- const variableStatement = call.getFirstAncestorByKind(
234
- tsm.SyntaxKind.VariableStatement
235
- );
236
- if (variableStatement !== void 0 && hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)) {
237
- continue;
162
+
163
+ if (isAsConstNode(node)) {
164
+ if (context.isDirectUnderConstInitializer) {
165
+
166
+ transformNode(node.getExpression(), {
167
+ isUnderConstContext: false,
168
+ isDirectUnderConstInitializer: true,
169
+ }, options);
170
+ options.replaceNode(node,
171
+
172
+ node.getExpression().getText());
173
+ return;
174
+ }
175
+ if (context.isUnderConstContext) {
176
+ transformNode(node.getExpression(), {
177
+ isUnderConstContext: true,
178
+ isDirectUnderConstInitializer: false,
179
+ }, options);
180
+ options.replaceNode(node,
181
+
182
+ node.getExpression().getText());
183
+ return;
184
+ }
185
+ transformNode(node.getExpression(), {
186
+ isUnderConstContext: true,
187
+ isDirectUnderConstInitializer: false,
188
+ }, options);
189
+ return;
238
190
  }
239
- if (!Arr.isEmpty(call.getTypeArguments())) {
240
- continue;
191
+ if (tsm.Node.isConditionalExpression(node)) {
192
+
193
+ transformNode(node.getWhenTrue(), {
194
+ isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,
195
+ isUnderConstContext: false,
196
+ }, options);
197
+ transformNode(node.getWhenFalse(), {
198
+ isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,
199
+ isUnderConstContext: false,
200
+ }, options);
201
+
241
202
  }
242
- const callTarget = resolveSingleSignatureCallTarget(call);
243
- if (callTarget === void 0) {
244
- continue;
203
+ };
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+ const removeAsConstInConstTypeParameterArgs = (
223
+
224
+ sourceAst, options) => {
225
+
226
+
227
+ for (const call of sourceAst
228
+ .getDescendantsOfKind(tsm.SyntaxKind.CallExpression)
229
+ .toReversed()) {
230
+ if (call.wasForgotten()) {
231
+ continue;
232
+ }
233
+ const args = call.getArguments();
234
+
235
+ if (!args.some(isAsConstNode)) {
236
+ continue;
237
+ }
238
+ if (hasDisableNextLineComment(call, TRANSFORMER_NAME)) {
239
+ continue;
240
+ }
241
+ const variableStatement = call.getFirstAncestorByKind(tsm.SyntaxKind.VariableStatement);
242
+ if (variableStatement !== undefined &&
243
+ hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)) {
244
+ continue;
245
+ }
246
+
247
+
248
+ if (!Arr.isEmpty(call.getTypeArguments())) {
249
+ continue;
250
+ }
251
+ const callTarget = resolveSingleSignatureCallTarget(call);
252
+ if (callTarget === undefined) {
253
+ continue;
254
+ }
255
+ removeAsConstArgsOfCall(args, callTarget, options);
245
256
  }
246
- removeAsConstArgsOfCall(args, callTarget, options);
247
- }
248
257
  };
249
- const removeAsConstArgsOfCall = (args, callTarget, options) => {
250
- const firstSpreadIndex = args.findIndex(
251
- (a) => a.isKind(tsm.SyntaxKind.SpreadElement)
252
- );
253
- const mappableArgCount = firstSpreadIndex === -1 ? args.length : firstSpreadIndex;
254
- for (const [argumentIndex, argument] of args.entries()) {
255
- if (argumentIndex >= mappableArgCount) {
256
- return;
258
+ const removeAsConstArgsOfCall = (args,
259
+
260
+ callTarget, options) => {
261
+
262
+
263
+ const firstSpreadIndex = args.findIndex((a) => a.isKind(tsm.SyntaxKind.SpreadElement));
264
+ const mappableArgCount = firstSpreadIndex === -1 ? args.length : firstSpreadIndex;
265
+ for (const [argumentIndex, argument] of args.entries()) {
266
+ if (argumentIndex >= mappableArgCount) {
267
+ return;
268
+ }
269
+ if (argument.wasForgotten() || !isAsConstNode(argument)) {
270
+ continue;
271
+ }
272
+ if (hasDisableNextLineComment(argument, TRANSFORMER_NAME)) {
273
+ continue;
274
+ }
275
+ const parameter = getMatchedParameter(callTarget.parameters, argumentIndex);
276
+ if (parameter === undefined ||
277
+ !isBareConstTypeParameterReference(parameter, callTarget.constTypeParameterNames)) {
278
+ continue;
279
+ }
280
+ options.debugPrint('removing redundant as const in const type parameter argument', argument.getText());
281
+ options.replaceNode(argument, argument.getExpression().getText());
257
282
  }
258
- if (argument.wasForgotten() || !isAsConstNode(argument)) {
259
- continue;
283
+ };
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+ const resolveSingleSignatureCallTarget = (
294
+
295
+ call) => {
296
+
297
+
298
+
299
+
300
+ if (!Arr.isFixedLengthArray(1, call.getExpression().getType().getCallSignatures())) {
301
+ return undefined;
260
302
  }
261
- if (hasDisableNextLineComment(argument, TRANSFORMER_NAME)) {
262
- continue;
303
+ const declaration = call
304
+ .getProject()
305
+ .getTypeChecker()
306
+ .getResolvedSignature(call)
307
+ ?.getDeclaration();
308
+ if (declaration === undefined ||
309
+ !tsm.Node.isParametered(declaration) ||
310
+ !tsm.Node.isTypeParametered(declaration)) {
311
+ return undefined;
263
312
  }
264
- const parameter = getMatchedParameter(callTarget.parameters, argumentIndex);
265
- if (parameter === void 0 || !isBareConstTypeParameterReference(
266
- parameter,
267
- callTarget.constTypeParameterNames
268
- )) {
269
- continue;
313
+
314
+
315
+
316
+ const constTypeParameterNames = ISet.create(declaration
317
+ .getTypeParameters()
318
+ .filter((tp) => tp.hasModifier(tsm.SyntaxKind.ConstKeyword))
319
+ .map((tp) => tp.getName()));
320
+ if (constTypeParameterNames.size === 0) {
321
+ return undefined;
270
322
  }
271
- options.debugPrint(
272
- "removing redundant as const in const type parameter argument",
273
- argument.getText()
274
- );
275
- options.replaceNode(argument, argument.getExpression().getText());
276
- }
277
- };
278
- const resolveSingleSignatureCallTarget = (call) => {
279
- if (!Arr.isFixedLengthArray(
280
- 1,
281
- call.getExpression().getType().getCallSignatures()
282
- )) {
283
- return void 0;
284
- }
285
- const declaration = call.getProject().getTypeChecker().getResolvedSignature(call)?.getDeclaration();
286
- if (declaration === void 0 || !tsm.Node.isParametered(declaration) || !tsm.Node.isTypeParametered(declaration)) {
287
- return void 0;
288
- }
289
- const constTypeParameterNames = ISet.create(
290
- declaration.getTypeParameters().filter((tp) => tp.hasModifier(tsm.SyntaxKind.ConstKeyword)).map((tp) => tp.getName())
291
- );
292
- if (constTypeParameterNames.size === 0) {
293
- return void 0;
294
- }
295
- return {
296
- parameters: declaration.getParameters(),
297
- constTypeParameterNames
298
- };
323
+ return {
324
+ parameters: declaration.getParameters(),
325
+ constTypeParameterNames,
326
+ };
299
327
  };
300
- const getMatchedParameter = (parameters, argumentIndex) => {
301
- if (argumentIndex < parameters.length) {
302
- return parameters[argumentIndex];
303
- }
304
- const lastParameter = parameters.at(-1);
305
- return lastParameter?.isRestParameter() === true ? lastParameter : void 0;
306
- };
307
- const isBareConstTypeParameterReference = (parameter, constTypeParameterNames) => {
308
- const typeNode = parameter.getTypeNode();
309
- if (typeNode === void 0) {
310
- return false;
311
- }
312
- if (!typeNode.isKind(tsm.SyntaxKind.TypeReference)) {
313
- return false;
314
- }
315
- const typeName = typeNode.getTypeName();
316
- return typeName.isKind(tsm.SyntaxKind.Identifier) && Arr.isEmpty(typeNode.getTypeArguments()) && constTypeParameterNames.has(typeName.getText());
328
+ const getMatchedParameter = (
329
+
330
+ parameters, argumentIndex) => {
331
+ if (argumentIndex < parameters.length) {
332
+ return parameters[argumentIndex];
333
+ }
334
+ const lastParameter = parameters.at(-1);
335
+ return lastParameter?.isRestParameter() === true ? lastParameter : undefined;
317
336
  };
318
337
 
319
- export { appendAsConstTransformer };
320
- //# sourceMappingURL=append-as-const.mjs.map
338
+
339
+
340
+
341
+
342
+
343
+ const isBareConstTypeParameterReference = (
344
+
345
+ parameter, constTypeParameterNames) => {
346
+ const typeNode = parameter.getTypeNode();
347
+ if (typeNode === undefined) {
348
+ return false;
349
+ }
350
+ if (!typeNode.isKind(tsm.SyntaxKind.TypeReference)) {
351
+ return false;
352
+ }
353
+ const typeName = typeNode.getTypeName();
354
+ return (typeName.isKind(tsm.SyntaxKind.Identifier) &&
355
+ Arr.isEmpty(typeNode.getTypeArguments()) &&
356
+ constTypeParameterNames.has(typeName.getText()));
357
+ };
358
+ //# sourceMappingURL=append-as-const.mjs.map