ts-codemod-lib 3.2.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.
- package/README.md +68 -0
- package/dist/entry-point.mjs +2 -23
- package/dist/entry-point.mjs.map +1 -1
- package/dist/functions/ast-transformers/append-as-const.mjs +330 -292
- package/dist/functions/ast-transformers/append-as-const.mjs.map +1 -1
- package/dist/functions/ast-transformers/convert-interface-to-type.mjs +85 -70
- package/dist/functions/ast-transformers/convert-interface-to-type.mjs.map +1 -1
- package/dist/functions/ast-transformers/convert-to-readonly.mjs +969 -840
- package/dist/functions/ast-transformers/convert-to-readonly.mjs.map +1 -1
- package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.d.mts +65 -0
- package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.d.mts.map +1 -0
- package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.mjs +297 -0
- package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.mjs.map +1 -0
- package/dist/functions/ast-transformers/index.d.mts +1 -0
- package/dist/functions/ast-transformers/index.d.mts.map +1 -1
- package/dist/functions/ast-transformers/index.mjs +10 -11
- package/dist/functions/ast-transformers/index.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/compare-union-types.mjs +14 -7
- package/dist/functions/ast-transformers/readonly-transformer-helpers/compare-union-types.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.mjs +12 -15
- package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/group-union-types.mjs +63 -58
- package/dist/functions/ast-transformers/readonly-transformer-helpers/group-union-types.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/index.mjs +5 -5
- package/dist/functions/ast-transformers/readonly-transformer-helpers/index.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mjs +15 -20
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mjs.map +1 -1
- package/dist/functions/ast-transformers/replace-any-with-unknown.mjs +30 -34
- package/dist/functions/ast-transformers/replace-any-with-unknown.mjs.map +1 -1
- package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs +197 -156
- package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs.map +1 -1
- package/dist/functions/ast-transformers/transform-source-code.mjs +74 -75
- package/dist/functions/ast-transformers/transform-source-code.mjs.map +1 -1
- package/dist/functions/ast-transformers/types.mjs +2 -2
- package/dist/functions/ast-transformers/types.mjs.map +1 -1
- package/dist/functions/constants/ignore-comment-text.mjs +11 -13
- package/dist/functions/constants/ignore-comment-text.mjs.map +1 -1
- package/dist/functions/constants/index.mjs +2 -2
- package/dist/functions/constants/index.mjs.map +1 -1
- package/dist/functions/functions/has-disable-next-line-comment.mjs +67 -38
- package/dist/functions/functions/has-disable-next-line-comment.mjs.map +1 -1
- package/dist/functions/functions/index.mjs +11 -11
- package/dist/functions/functions/index.mjs.map +1 -1
- package/dist/functions/functions/is-as-const-node.mjs +25 -20
- package/dist/functions/functions/is-as-const-node.mjs.map +1 -1
- package/dist/functions/functions/is-atomic-type-node.mjs +245 -25
- package/dist/functions/functions/is-atomic-type-node.mjs.map +1 -1
- package/dist/functions/functions/is-conditional-type-distribution-guard.mjs +34 -13
- package/dist/functions/functions/is-conditional-type-distribution-guard.mjs.map +1 -1
- package/dist/functions/functions/is-readonly-node.mjs +120 -14
- package/dist/functions/functions/is-readonly-node.mjs.map +1 -1
- package/dist/functions/functions/is-spread-parameter-node.mjs +5 -6
- package/dist/functions/functions/is-spread-parameter-node.mjs.map +1 -1
- package/dist/functions/functions/remove-parentheses.mjs +4 -4
- package/dist/functions/functions/remove-parentheses.mjs.map +1 -1
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs +23 -9
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs.map +1 -1
- package/dist/functions/functions/unwrap-readonly.mjs +3 -4
- package/dist/functions/functions/unwrap-readonly.mjs.map +1 -1
- package/dist/functions/functions/wrap-with-parentheses.d.mts.map +1 -1
- package/dist/functions/functions/wrap-with-parentheses.mjs +54 -29
- package/dist/functions/functions/wrap-with-parentheses.mjs.map +1 -1
- package/dist/functions/index.mjs +5 -23
- package/dist/functions/index.mjs.map +1 -1
- package/dist/functions/utils/index.mjs +2 -2
- package/dist/functions/utils/index.mjs.map +1 -1
- package/dist/functions/utils/replace-with-debug.mjs +4 -6
- package/dist/functions/utils/replace-with-debug.mjs.map +1 -1
- package/dist/index.mjs +2 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -12
- package/src/functions/ast-transformers/convert-to-readonly.test.mts +10 -1
- package/src/functions/ast-transformers/enable-no-unchecked-indexed-access.mts +438 -0
- package/src/functions/ast-transformers/enable-no-unchecked-indexed-access.test.mts +619 -0
- package/src/functions/ast-transformers/index.mts +1 -0
- package/src/functions/functions/wrap-with-parentheses.mts +5 -2
|
@@ -1,320 +1,358 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Arr, ISet } from 'ts-data-forge';
|
|
2
2
|
import * as tsm from 'ts-morph';
|
|
3
|
-
import { hasDisableNextLineComment } from '../functions/
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
44
|
-
|
|
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
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
41
|
+
if (options.applyLevel === 'avoidInFunctionArgs' &&
|
|
42
|
+
tsm.Node.isCallExpression(node)) {
|
|
43
|
+
return;
|
|
67
44
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
138
|
-
|
|
95
|
+
|
|
96
|
+
if (tsm.Node.isSatisfiesExpression(node)) {
|
|
97
|
+
transformNode(node.getExpression(), context, options);
|
|
98
|
+
return;
|
|
139
99
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
155
|
-
|
|
108
|
+
|
|
109
|
+
if (tsm.Node.isArrowFunction(node)) {
|
|
110
|
+
const body = node.getBody();
|
|
111
|
+
transformNode(body, context, options);
|
|
112
|
+
return;
|
|
156
113
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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 (
|
|
177
|
-
|
|
178
|
-
node.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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 (
|
|
231
|
-
|
|
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
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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 (
|
|
240
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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,
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
259
|
-
|
|
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
|
-
|
|
262
|
-
|
|
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
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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 = (
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
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
|
-
|
|
320
|
-
|
|
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
|