ts-codemod-lib 3.1.1 → 3.2.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.
- package/README.md +13 -0
- package/dist/functions/ast-transformers/append-as-const.d.mts +16 -0
- package/dist/functions/ast-transformers/append-as-const.d.mts.map +1 -1
- package/dist/functions/ast-transformers/append-as-const.mjs +101 -0
- package/dist/functions/ast-transformers/append-as-const.mjs.map +1 -1
- package/package.json +1 -1
- package/src/functions/ast-transformers/append-as-const.mts +251 -2
- package/src/functions/ast-transformers/append-as-const.test.mts +173 -1
package/README.md
CHANGED
|
@@ -52,6 +52,7 @@ Options:
|
|
|
52
52
|
- `'avoidInFunctionArgs'`: Avoids adding `as const` inside function call arguments
|
|
53
53
|
- `'all'`: Applies `as const` everywhere
|
|
54
54
|
- `ignorePrefixes`: Array of string prefixes for identifiers that should not have `as const` added (default: `['mut_', '#mut_', '_mut_', 'draft']`)
|
|
55
|
+
- `removeAsConstForConstTypeParameters`: Whether to remove redundant `as const` assertions from call arguments whose corresponding parameter type is exactly a `const`-modified type parameter (default: `true`). Only applies when the callee resolves to a single call signature within the transformed file itself; imported callees, overloads, calls with explicit type arguments and spread arguments are left as they are.
|
|
55
56
|
|
|
56
57
|
Example:
|
|
57
58
|
|
|
@@ -67,6 +68,18 @@ const arr2 = [1, 2, 3] as const;
|
|
|
67
68
|
const obj2 = { a: 1, b: 2 } as const;
|
|
68
69
|
```
|
|
69
70
|
|
|
71
|
+
Example (`removeAsConstForConstTypeParameters`):
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const f = <const T>(x: T): T => x;
|
|
75
|
+
|
|
76
|
+
// Before
|
|
77
|
+
const a = f([1, 2] as const);
|
|
78
|
+
|
|
79
|
+
// After (the `const` type parameter already infers `readonly [1, 2]`)
|
|
80
|
+
const a2 = f([1, 2]);
|
|
81
|
+
```
|
|
82
|
+
|
|
70
83
|
### 2. `convertToReadonlyTransformer`
|
|
71
84
|
|
|
72
85
|
Converts TypeScript type definitions to readonly types. This transformer helps in creating more type-safe code by making types readonly where appropriate. It also normalizes nested readonly types (e.g., `Readonly<Readonly<T>>` becomes `Readonly<T>`).
|
|
@@ -14,6 +14,22 @@ export type AppendAsConstTransformerOptions = DeepReadonly<{
|
|
|
14
14
|
* @default ['mut_', '#mut_', '_mut_', 'draft']
|
|
15
15
|
*/
|
|
16
16
|
ignorePrefixes?: string[];
|
|
17
|
+
/**
|
|
18
|
+
* Whether to remove redundant `as const` assertions from call arguments
|
|
19
|
+
* whose corresponding parameter type is exactly a `const`-modified type
|
|
20
|
+
* parameter.
|
|
21
|
+
*
|
|
22
|
+
* (e.g. `f([1, 2] as const)` becomes `f([1, 2])` for
|
|
23
|
+
* `function f<const T>(x: T): T`, because the `const` type parameter
|
|
24
|
+
* already makes TypeScript infer the argument as if it were annotated
|
|
25
|
+
* with `as const`.)
|
|
26
|
+
*
|
|
27
|
+
* Only applies when the callee resolves to a single call signature within
|
|
28
|
+
* the transformed file itself; imported callees are left as they are.
|
|
29
|
+
*
|
|
30
|
+
* @default true
|
|
31
|
+
*/
|
|
32
|
+
removeAsConstForConstTypeParameters?: boolean;
|
|
17
33
|
debug?: boolean;
|
|
18
34
|
}>;
|
|
19
35
|
//# sourceMappingURL=append-as-const.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"append-as-const.d.mts","sourceRoot":"","sources":["../../../src/functions/ast-transformers/append-as-const.mts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAMlD,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAItD,eAAO,MAAM,wBAAwB,aACzB,+BAA+B,KACxC,
|
|
1
|
+
{"version":3,"file":"append-as-const.d.mts","sourceRoot":"","sources":["../../../src/functions/ast-transformers/append-as-const.mts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAMlD,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAItD,eAAO,MAAM,wBAAwB,aACzB,+BAA+B,KACxC,kBAuCF,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG,YAAY,CAAC;IACzD;;OAEG;IACH,UAAU,CAAC,EAAE,KAAK,GAAG,qBAAqB,CAAC;IAE3C;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAE1B;;;;;;;;;;;;;;OAcG;IACH,mCAAmC,CAAC,EAAE,OAAO,CAAC;IAE9C,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC,CAAC"}
|
|
@@ -13,6 +13,7 @@ const appendAsConstTransformer = (options) => {
|
|
|
13
13
|
const optionsInternal = {
|
|
14
14
|
applyLevel: options?.applyLevel ?? "avoidInFunctionArgs",
|
|
15
15
|
ignoredPrefixes: ignorePrefixes,
|
|
16
|
+
removeAsConstForConstTypeParameters: options?.removeAsConstForConstTypeParameters ?? true,
|
|
16
17
|
debugPrint: options?.debug === true ? console.debug : () => {
|
|
17
18
|
},
|
|
18
19
|
replaceNode: options?.debug === true ? replaceNodeWithDebugPrint : (node, newNodeText) => node.replaceWithText(newNodeText)
|
|
@@ -32,6 +33,9 @@ const appendAsConstTransformer = (options) => {
|
|
|
32
33
|
optionsInternal
|
|
33
34
|
);
|
|
34
35
|
}
|
|
36
|
+
if (optionsInternal.removeAsConstForConstTypeParameters) {
|
|
37
|
+
removeAsConstInConstTypeParameterArgs(sourceAst, optionsInternal);
|
|
38
|
+
}
|
|
35
39
|
}
|
|
36
40
|
};
|
|
37
41
|
};
|
|
@@ -214,6 +218,103 @@ const transformNode = (node, context, options) => {
|
|
|
214
218
|
);
|
|
215
219
|
}
|
|
216
220
|
};
|
|
221
|
+
const removeAsConstInConstTypeParameterArgs = (sourceAst, options) => {
|
|
222
|
+
for (const call of sourceAst.getDescendantsOfKind(tsm.SyntaxKind.CallExpression).toReversed()) {
|
|
223
|
+
if (call.wasForgotten()) {
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const args = call.getArguments();
|
|
227
|
+
if (!args.some(isAsConstNode)) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (hasDisableNextLineComment(call, TRANSFORMER_NAME)) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
const variableStatement = call.getFirstAncestorByKind(
|
|
234
|
+
tsm.SyntaxKind.VariableStatement
|
|
235
|
+
);
|
|
236
|
+
if (variableStatement !== void 0 && hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (!Arr.isEmpty(call.getTypeArguments())) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const callTarget = resolveSingleSignatureCallTarget(call);
|
|
243
|
+
if (callTarget === void 0) {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
removeAsConstArgsOfCall(args, callTarget, options);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
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;
|
|
257
|
+
}
|
|
258
|
+
if (argument.wasForgotten() || !isAsConstNode(argument)) {
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (hasDisableNextLineComment(argument, TRANSFORMER_NAME)) {
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const parameter = getMatchedParameter(callTarget.parameters, argumentIndex);
|
|
265
|
+
if (parameter === void 0 || !isBareConstTypeParameterReference(
|
|
266
|
+
parameter,
|
|
267
|
+
callTarget.constTypeParameterNames
|
|
268
|
+
)) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
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
|
+
};
|
|
299
|
+
};
|
|
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());
|
|
317
|
+
};
|
|
217
318
|
|
|
218
319
|
export { appendAsConstTransformer };
|
|
219
320
|
//# sourceMappingURL=append-as-const.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"append-as-const.mjs","sources":["../../../src/functions/ast-transformers/append-as-const.mts"],"sourcesContent":["import { Arr, ISet } from 'ts-data-forge';\nimport * as tsm from 'ts-morph';\nimport { type DeepReadonly } from 'ts-type-forge';\nimport {\n hasDisableNextLineComment,\n isAsConstNode,\n} from '../functions/index.mjs';\nimport { replaceNodeWithDebugPrint } from '../utils/index.mjs';\nimport { type TsMorphTransformer } from './types.mjs';\n\nconst TRANSFORMER_NAME = 'append-as-const';\n\nexport const appendAsConstTransformer = (\n options?: AppendAsConstTransformerOptions,\n): TsMorphTransformer => {\n const ignorePrefixes = ISet.create(\n options?.ignorePrefixes ?? ['mut_', '#mut_', '_mut_', 'draft'],\n );\n\n const optionsInternal: AppendAsConstTransformerOptionsInternal = {\n applyLevel: options?.applyLevel ?? 'avoidInFunctionArgs',\n ignoredPrefixes: ignorePrefixes,\n\n debugPrint: options?.debug === true ? console.debug : () => {},\n replaceNode:\n options?.debug === true\n ? replaceNodeWithDebugPrint\n : (node, newNodeText) => node.replaceWithText(newNodeText),\n } as const;\n\n return {\n name: TRANSFORMER_NAME,\n transform: (sourceAst) => {\n for (const node of sourceAst.getDescendantsOfKind(\n tsm.SyntaxKind.VariableDeclaration,\n )) {\n transformNode(\n node,\n {\n isUnderConstContext: false,\n isDirectUnderConstInitializer: false,\n },\n optionsInternal,\n );\n }\n },\n };\n};\n\nexport type AppendAsConstTransformerOptions = DeepReadonly<{\n /**\n * @default \"avoidInFunctionArgs\"\n */\n applyLevel?: 'all' | 'avoidInFunctionArgs';\n\n /**\n * A mute keywords to ignore the readonly conversion.\n *\n * (e.g. `\"mut_\"`)\n *\n * @default ['mut_', '#mut_', '_mut_', 'draft']\n */\n ignorePrefixes?: string[];\n\n // TODO\n // ignoreConstTypeParameter?: boolean;\n\n debug?: boolean;\n}>;\n\ntype AppendAsConstTransformerOptionsInternal = DeepReadonly<{\n applyLevel: 'all' | 'avoidInFunctionArgs';\n ignoredPrefixes: ISet<string>;\n\n debugPrint: (...args: readonly unknown[]) => void;\n replaceNode: (node: tsm.Node, newNodeText: string) => void;\n}>;\n\ntype AsConstContext = Readonly<{\n /**\n * Whether the current node is under an `as const` context.\n *\n * (e.g. `[1, 2, {x: 3}] as const` --> `isUnderConstContext` is true for `[1, 2, {x: 3}]` and its children `{x: 3}`)\n */\n isUnderConstContext: boolean;\n\n /**\n * Whether the current node is directly under a `const` variable initializer.\n *\n * (e.g. `const foo = [1, 2, 3];` --> `isDirectUnderConstInitializer` is true for `[1, 2, 3]`)\n */\n isDirectUnderConstInitializer: boolean;\n}>;\n\nconst transformNode = (\n node: tsm.Node,\n context: AsConstContext,\n options: AppendAsConstTransformerOptionsInternal,\n): void => {\n if (node.wasForgotten()) {\n return;\n }\n\n options.debugPrint(node.getKindName(), node.getText(), { context });\n\n if (hasDisableNextLineComment(node, TRANSFORMER_NAME)) {\n options.debugPrint('skipped by disable-next-line comment');\n\n return;\n }\n\n if (\n options.applyLevel === 'avoidInFunctionArgs' &&\n tsm.Node.isCallExpression(node)\n ) {\n return;\n }\n\n if (node.isKind(tsm.SyntaxKind.VariableDeclaration)) {\n const nodeName = node.getName();\n\n // check for ignorePrefix\n if (options.ignoredPrefixes.some((p) => nodeName.startsWith(p))) {\n // Skip conversion for variable declarations with ignored prefixes\n // Example: const mut_foo: string[] = []; -> remains as is, without appending `as const`\n options.debugPrint('skipped variable declaration by ignorePrefixes');\n\n return;\n }\n\n const variableStatement = node.getVariableStatement();\n\n if (\n variableStatement !== undefined &&\n hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)\n ) {\n return;\n }\n\n const initializer = node.getInitializer();\n\n if (initializer === undefined) {\n return;\n }\n\n const declarationKindKeywords = node\n .getVariableStatement()\n ?.getDeclarationKindKeywords()\n .map((k) => k.getText());\n\n if (\n declarationKindKeywords !== undefined &&\n Arr.isFixedLengthArray(1, declarationKindKeywords)\n ) {\n transformNode(\n initializer,\n {\n isDirectUnderConstInitializer: declarationKindKeywords[0] === 'const',\n isUnderConstContext: false,\n },\n options,\n );\n\n return;\n }\n\n // const [a, b] = ...;\n // TODO: Support ignoredPrefixes in ArrayBindingPattern\n // if (ts.isArrayBindingPattern(nodeName)) {\n // // for (const [i, el] of nodeName.elements.entries())\n // }\n\n // const { x, y } = ...;\n // TODO: Support ignoredPrefixes in ObjectBindingPattern\n // if (ts.isObjectBindingPattern(nodeName)) {\n // // for (const [i, el] of nodeName.elements.entries())\n // }\n }\n\n // Skip already type asserted nodes\n if (tsm.Node.isAsExpression(node) && !isAsConstNode(node)) {\n return;\n }\n\n // pass by ([(X)] -> X)\n if (tsm.Node.isParenthesizedExpression(node)) {\n transformNode(node.getExpression(), context, options);\n\n return;\n }\n\n // pass by ([X satisfies ...] -> X)\n if (tsm.Node.isSatisfiesExpression(node)) {\n transformNode(node.getExpression(), context, options);\n\n return;\n }\n\n // pass by property initializer ([key: value] -> value)\n if (tsm.Node.isPropertyAssignment(node)) {\n const initializer = node.getInitializer();\n\n if (initializer !== undefined) {\n transformNode(initializer, context, options);\n }\n\n return;\n }\n\n // pass by arrow function body ([() => X] -> X)\n if (tsm.Node.isArrowFunction(node)) {\n const body = node.getBody();\n\n transformNode(body, context, options);\n\n return;\n }\n\n // pass by spread element ([...X] -> X)\n if (tsm.Node.isSpreadElement(node)) {\n transformNode(node.getExpression(), context, options);\n\n return;\n }\n\n if (\n node.isKind(tsm.SyntaxKind.NoSubstitutionTemplateLiteral) || // `abc`\n node.isKind(tsm.SyntaxKind.NumericLiteral) || // 123\n node.isKind(tsm.SyntaxKind.BigIntLiteral) || // 123n\n node.isKind(tsm.SyntaxKind.StringLiteral) || // 'abc'\n node.isKind(tsm.SyntaxKind.TrueKeyword) || // true\n node.isKind(tsm.SyntaxKind.FalseKeyword) // false\n ) {\n if (\n !context.isDirectUnderConstInitializer &&\n !context.isUnderConstContext\n ) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n if (node.isKind(tsm.SyntaxKind.TemplateExpression)) {\n if (!context.isUnderConstContext) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n if (tsm.Node.isArrayLiteralExpression(node)) {\n for (const el of node.getElements()) {\n transformNode(\n el,\n {\n isUnderConstContext: true, // [...] as const\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n }\n\n if (!context.isUnderConstContext) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n if (tsm.Node.isObjectLiteralExpression(node)) {\n for (const el of node.getProperties()) {\n transformNode(\n el,\n {\n isUnderConstContext: true, // {...} as const\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n }\n\n if (!context.isUnderConstContext) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n // `as const` node\n if (isAsConstNode(node)) {\n if (context.isDirectUnderConstInitializer) {\n // In const variable declarations, remove `as const` first and then re-append it later if needed\n\n transformNode(\n node.getExpression(),\n {\n isUnderConstContext: false,\n isDirectUnderConstInitializer: true,\n },\n options,\n );\n\n options.replaceNode(\n node,\n // The expression may be marked \"as const\"\n node.getExpression().getText(),\n ); // remove `as const`\n\n return;\n }\n\n if (context.isUnderConstContext) {\n transformNode(\n node.getExpression(),\n {\n isUnderConstContext: true,\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n\n options.replaceNode(\n node,\n // The expression may be marked \"as const\"\n node.getExpression().getText(),\n ); // remove `as const`\n\n return;\n }\n\n transformNode(\n node.getExpression(),\n {\n isUnderConstContext: true,\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n\n return;\n }\n\n if (tsm.Node.isConditionalExpression(node)) {\n // For conditional expressions, traverse both branches in a non-const context\n transformNode(\n node.getWhenTrue(),\n {\n isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,\n isUnderConstContext: false,\n },\n options,\n );\n\n transformNode(\n node.getWhenFalse(),\n {\n isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,\n isUnderConstContext: false,\n },\n options,\n );\n\n // return;\n }\n};\n"],"names":[],"mappings":";;;;;;;AAUA,MAAM,gBAAA,GAAmB,iBAAA;AAElB,MAAM,wBAAA,GAA2B,CACtC,OAAA,KACuB;AACvB,EAAA,MAAM,iBAAiB,IAAA,CAAK,MAAA;AAAA,IAC1B,SAAS,cAAA,IAAkB,CAAC,MAAA,EAAQ,OAAA,EAAS,SAAS,OAAO;AAAA,GAC/D;AAEA,EAAA,MAAM,eAAA,GAA2D;AAAA,IAC/D,UAAA,EAAY,SAAS,UAAA,IAAc,qBAAA;AAAA,IACnC,eAAA,EAAiB,cAAA;AAAA,IAEjB,YAAY,OAAA,EAAS,KAAA,KAAU,IAAA,GAAO,OAAA,CAAQ,QAAQ,MAAM;AAAA,IAAC,CAAA;AAAA,IAC7D,WAAA,EACE,OAAA,EAAS,KAAA,KAAU,IAAA,GACf,yBAAA,GACA,CAAC,IAAA,EAAM,WAAA,KAAgB,IAAA,CAAK,eAAA,CAAgB,WAAW;AAAA,GAC/D;AAEA,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,gBAAA;AAAA,IACN,SAAA,EAAW,CAAC,SAAA,KAAc;AACxB,MAAA,KAAA,MAAW,QAAQ,SAAA,CAAU,oBAAA;AAAA,QAC3B,IAAI,UAAA,CAAW;AAAA,OACjB,EAAG;AACD,QAAA,aAAA;AAAA,UACE,IAAA;AAAA,UACA;AAAA,YACE,mBAAA,EAAqB,KAAA;AAAA,YACrB,6BAAA,EAA+B;AAA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A,EAAc,CAAE,OAAA;AAAQ,OAC/B;AAEA,MAAA;AAAA,IACF;AAEA,IAAA,IAAI,QAAQ,mBAAA,EAAqB;AAC/B,MAAA,aAAA;AAAA,QACE,KAAK,aAAA,EAAc;AAAA,QACnB;AAAA,UACE,mBAAA,EAAqB,IAAA;AAAA,UACrB,6BAAA,EAA+B;AAAA,SACjC;AAAA,QACA;AAAA,OACF;AAEA,MAAA,OAAA,CAAQ,WAAA;AAAA,QACN,IAAA;AAAA;AAAA,QAEA,IAAA,CAAK,aAAA,EAAc,CAAE,OAAA;AAAQ,OAC/B;AAEA,MAAA;AAAA,IACF;AAEA,IAAA,aAAA;AAAA,MACE,KAAK,aAAA,EAAc;AAAA,MACnB;AAAA,QACE,mBAAA,EAAqB,IAAA;AAAA,QACrB,6BAAA,EAA+B;AAAA,OACjC;AAAA,MACA;AAAA,KACF;AAEA,IAAA;AAAA,EACF;AAEA,EAAA,IAAI,GAAA,CAAI,IAAA,CAAK,uBAAA,CAAwB,IAAI,CAAA,EAAG;AAE1C,IAAA,aAAA;AAAA,MACE,KAAK,WAAA,EAAY;AAAA,MACjB;AAAA,QACE,+BAA+B,OAAA,CAAQ,6BAAA;AAAA,QACvC,mBAAA,EAAqB;AAAA,OACvB;AAAA,MACA;AAAA,KACF;AAEA,IAAA,aAAA;AAAA,MACE,KAAK,YAAA,EAAa;AAAA,MAClB;AAAA,QACE,+BAA+B,OAAA,CAAQ,6BAAA;AAAA,QACvC,mBAAA,EAAqB;AAAA,OACvB;AAAA,MACA;AAAA,KACF;AAAA,EAGF;AACF,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"append-as-const.mjs","sources":["../../../src/functions/ast-transformers/append-as-const.mts"],"sourcesContent":["import { Arr, ISet } from 'ts-data-forge';\nimport * as tsm from 'ts-morph';\nimport { type DeepReadonly } from 'ts-type-forge';\nimport {\n hasDisableNextLineComment,\n isAsConstNode,\n} from '../functions/index.mjs';\nimport { replaceNodeWithDebugPrint } from '../utils/index.mjs';\nimport { type TsMorphTransformer } from './types.mjs';\n\nconst TRANSFORMER_NAME = 'append-as-const';\n\nexport const appendAsConstTransformer = (\n options?: AppendAsConstTransformerOptions,\n): TsMorphTransformer => {\n const ignorePrefixes = ISet.create(\n options?.ignorePrefixes ?? ['mut_', '#mut_', '_mut_', 'draft'],\n );\n\n const optionsInternal: AppendAsConstTransformerOptionsInternal = {\n applyLevel: options?.applyLevel ?? 'avoidInFunctionArgs',\n ignoredPrefixes: ignorePrefixes,\n removeAsConstForConstTypeParameters:\n options?.removeAsConstForConstTypeParameters ?? true,\n\n debugPrint: options?.debug === true ? console.debug : () => {},\n replaceNode:\n options?.debug === true\n ? replaceNodeWithDebugPrint\n : (node, newNodeText) => node.replaceWithText(newNodeText),\n } as const;\n\n return {\n name: TRANSFORMER_NAME,\n transform: (sourceAst) => {\n for (const node of sourceAst.getDescendantsOfKind(\n tsm.SyntaxKind.VariableDeclaration,\n )) {\n transformNode(\n node,\n {\n isUnderConstContext: false,\n isDirectUnderConstInitializer: false,\n },\n optionsInternal,\n );\n }\n\n if (optionsInternal.removeAsConstForConstTypeParameters) {\n removeAsConstInConstTypeParameterArgs(sourceAst, optionsInternal);\n }\n },\n };\n};\n\nexport type AppendAsConstTransformerOptions = DeepReadonly<{\n /**\n * @default \"avoidInFunctionArgs\"\n */\n applyLevel?: 'all' | 'avoidInFunctionArgs';\n\n /**\n * A mute keywords to ignore the readonly conversion.\n *\n * (e.g. `\"mut_\"`)\n *\n * @default ['mut_', '#mut_', '_mut_', 'draft']\n */\n ignorePrefixes?: string[];\n\n /**\n * Whether to remove redundant `as const` assertions from call arguments\n * whose corresponding parameter type is exactly a `const`-modified type\n * parameter.\n *\n * (e.g. `f([1, 2] as const)` becomes `f([1, 2])` for\n * `function f<const T>(x: T): T`, because the `const` type parameter\n * already makes TypeScript infer the argument as if it were annotated\n * with `as const`.)\n *\n * Only applies when the callee resolves to a single call signature within\n * the transformed file itself; imported callees are left as they are.\n *\n * @default true\n */\n removeAsConstForConstTypeParameters?: boolean;\n\n debug?: boolean;\n}>;\n\ntype AppendAsConstTransformerOptionsInternal = DeepReadonly<{\n applyLevel: 'all' | 'avoidInFunctionArgs';\n ignoredPrefixes: ISet<string>;\n removeAsConstForConstTypeParameters: boolean;\n\n debugPrint: (...args: readonly unknown[]) => void;\n replaceNode: (node: tsm.Node, newNodeText: string) => void;\n}>;\n\ntype AsConstContext = Readonly<{\n /**\n * Whether the current node is under an `as const` context.\n *\n * (e.g. `[1, 2, {x: 3}] as const` --> `isUnderConstContext` is true for `[1, 2, {x: 3}]` and its children `{x: 3}`)\n */\n isUnderConstContext: boolean;\n\n /**\n * Whether the current node is directly under a `const` variable initializer.\n *\n * (e.g. `const foo = [1, 2, 3];` --> `isDirectUnderConstInitializer` is true for `[1, 2, 3]`)\n */\n isDirectUnderConstInitializer: boolean;\n}>;\n\nconst transformNode = (\n node: tsm.Node,\n context: AsConstContext,\n options: AppendAsConstTransformerOptionsInternal,\n): void => {\n if (node.wasForgotten()) {\n return;\n }\n\n options.debugPrint(node.getKindName(), node.getText(), { context });\n\n if (hasDisableNextLineComment(node, TRANSFORMER_NAME)) {\n options.debugPrint('skipped by disable-next-line comment');\n\n return;\n }\n\n if (\n options.applyLevel === 'avoidInFunctionArgs' &&\n tsm.Node.isCallExpression(node)\n ) {\n return;\n }\n\n if (node.isKind(tsm.SyntaxKind.VariableDeclaration)) {\n const nodeName = node.getName();\n\n // check for ignorePrefix\n if (options.ignoredPrefixes.some((p) => nodeName.startsWith(p))) {\n // Skip conversion for variable declarations with ignored prefixes\n // Example: const mut_foo: string[] = []; -> remains as is, without appending `as const`\n options.debugPrint('skipped variable declaration by ignorePrefixes');\n\n return;\n }\n\n const variableStatement = node.getVariableStatement();\n\n if (\n variableStatement !== undefined &&\n hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)\n ) {\n return;\n }\n\n const initializer = node.getInitializer();\n\n if (initializer === undefined) {\n return;\n }\n\n const declarationKindKeywords = node\n .getVariableStatement()\n ?.getDeclarationKindKeywords()\n .map((k) => k.getText());\n\n if (\n declarationKindKeywords !== undefined &&\n Arr.isFixedLengthArray(1, declarationKindKeywords)\n ) {\n transformNode(\n initializer,\n {\n isDirectUnderConstInitializer: declarationKindKeywords[0] === 'const',\n isUnderConstContext: false,\n },\n options,\n );\n\n return;\n }\n\n // const [a, b] = ...;\n // TODO: Support ignoredPrefixes in ArrayBindingPattern\n // if (ts.isArrayBindingPattern(nodeName)) {\n // // for (const [i, el] of nodeName.elements.entries())\n // }\n\n // const { x, y } = ...;\n // TODO: Support ignoredPrefixes in ObjectBindingPattern\n // if (ts.isObjectBindingPattern(nodeName)) {\n // // for (const [i, el] of nodeName.elements.entries())\n // }\n }\n\n // Skip already type asserted nodes\n if (tsm.Node.isAsExpression(node) && !isAsConstNode(node)) {\n return;\n }\n\n // pass by ([(X)] -> X)\n if (tsm.Node.isParenthesizedExpression(node)) {\n transformNode(node.getExpression(), context, options);\n\n return;\n }\n\n // pass by ([X satisfies ...] -> X)\n if (tsm.Node.isSatisfiesExpression(node)) {\n transformNode(node.getExpression(), context, options);\n\n return;\n }\n\n // pass by property initializer ([key: value] -> value)\n if (tsm.Node.isPropertyAssignment(node)) {\n const initializer = node.getInitializer();\n\n if (initializer !== undefined) {\n transformNode(initializer, context, options);\n }\n\n return;\n }\n\n // pass by arrow function body ([() => X] -> X)\n if (tsm.Node.isArrowFunction(node)) {\n const body = node.getBody();\n\n transformNode(body, context, options);\n\n return;\n }\n\n // pass by spread element ([...X] -> X)\n if (tsm.Node.isSpreadElement(node)) {\n transformNode(node.getExpression(), context, options);\n\n return;\n }\n\n if (\n node.isKind(tsm.SyntaxKind.NoSubstitutionTemplateLiteral) || // `abc`\n node.isKind(tsm.SyntaxKind.NumericLiteral) || // 123\n node.isKind(tsm.SyntaxKind.BigIntLiteral) || // 123n\n node.isKind(tsm.SyntaxKind.StringLiteral) || // 'abc'\n node.isKind(tsm.SyntaxKind.TrueKeyword) || // true\n node.isKind(tsm.SyntaxKind.FalseKeyword) // false\n ) {\n if (\n !context.isDirectUnderConstInitializer &&\n !context.isUnderConstContext\n ) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n if (node.isKind(tsm.SyntaxKind.TemplateExpression)) {\n if (!context.isUnderConstContext) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n if (tsm.Node.isArrayLiteralExpression(node)) {\n for (const el of node.getElements()) {\n transformNode(\n el,\n {\n isUnderConstContext: true, // [...] as const\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n }\n\n if (!context.isUnderConstContext) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n if (tsm.Node.isObjectLiteralExpression(node)) {\n for (const el of node.getProperties()) {\n transformNode(\n el,\n {\n isUnderConstContext: true, // {...} as const\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n }\n\n if (!context.isUnderConstContext) {\n options.replaceNode(node, `${node.getText()} as const`);\n }\n\n return;\n }\n\n // `as const` node\n if (isAsConstNode(node)) {\n if (context.isDirectUnderConstInitializer) {\n // In const variable declarations, remove `as const` first and then re-append it later if needed\n\n transformNode(\n node.getExpression(),\n {\n isUnderConstContext: false,\n isDirectUnderConstInitializer: true,\n },\n options,\n );\n\n options.replaceNode(\n node,\n // The expression may be marked \"as const\"\n node.getExpression().getText(),\n ); // remove `as const`\n\n return;\n }\n\n if (context.isUnderConstContext) {\n transformNode(\n node.getExpression(),\n {\n isUnderConstContext: true,\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n\n options.replaceNode(\n node,\n // The expression may be marked \"as const\"\n node.getExpression().getText(),\n ); // remove `as const`\n\n return;\n }\n\n transformNode(\n node.getExpression(),\n {\n isUnderConstContext: true,\n isDirectUnderConstInitializer: false,\n },\n options,\n );\n\n return;\n }\n\n if (tsm.Node.isConditionalExpression(node)) {\n // For conditional expressions, traverse both branches in a non-const context\n transformNode(\n node.getWhenTrue(),\n {\n isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,\n isUnderConstContext: false,\n },\n options,\n );\n\n transformNode(\n node.getWhenFalse(),\n {\n isDirectUnderConstInitializer: context.isDirectUnderConstInitializer,\n isUnderConstContext: false,\n },\n options,\n );\n\n // return;\n }\n};\n\n/**\n * Removes redundant `as const` assertions from call arguments whose\n * corresponding parameter type is exactly a `const`-modified type parameter\n * (e.g. `f([1, 2] as const)` for `function f<const T>(x: T): T`).\n *\n * A `const` type parameter makes TypeScript treat every inference candidate\n * as if it were annotated with `as const`, so when the parameter type is the\n * bare type parameter itself, removing the assertion cannot change the\n * inferred type. Conservative bail-outs (the `as const` is kept) when:\n *\n * - the callee does not resolve to exactly one call signature (unresolved\n * imports resolve to none, since each file is transformed in an isolated\n * single-file project; overloaded functions resolve to several),\n * - the call has explicit type arguments (inference is not involved),\n * - the argument is a spread argument or follows one (its parameter position\n * is not statically known),\n * - the parameter type is not the bare type parameter (e.g. `readonly T[]`).\n */\nconst removeAsConstInConstTypeParameterArgs = (\n // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types\n sourceAst: tsm.SourceFile,\n options: AppendAsConstTransformerOptionsInternal,\n): void => {\n // Reversed so that calls nested inside another call's arguments are\n // processed before the outer call replaces (and thereby forgets) them.\n for (const call of sourceAst\n .getDescendantsOfKind(tsm.SyntaxKind.CallExpression)\n .toReversed()) {\n if (call.wasForgotten()) {\n continue;\n }\n\n const args = call.getArguments();\n\n // Cheap syntactic pre-check before touching the type checker\n if (!args.some(isAsConstNode)) {\n continue;\n }\n\n if (hasDisableNextLineComment(call, TRANSFORMER_NAME)) {\n continue;\n }\n\n const variableStatement = call.getFirstAncestorByKind(\n tsm.SyntaxKind.VariableStatement,\n );\n\n if (\n variableStatement !== undefined &&\n hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)\n ) {\n continue;\n }\n\n // With explicit type arguments no inference happens, so the `as const`\n // may be load-bearing for assignability.\n if (!Arr.isEmpty(call.getTypeArguments())) {\n continue;\n }\n\n const callTarget = resolveSingleSignatureCallTarget(call);\n\n if (callTarget === undefined) {\n continue;\n }\n\n removeAsConstArgsOfCall(args, callTarget, options);\n }\n};\n\nconst removeAsConstArgsOfCall = (\n args: readonly tsm.Node[],\n // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types\n callTarget: ResolvedCallTarget,\n options: AppendAsConstTransformerOptionsInternal,\n): void => {\n // Argument positions at or after a spread argument cannot be mapped to a\n // parameter statically.\n const firstSpreadIndex = args.findIndex((a) =>\n a.isKind(tsm.SyntaxKind.SpreadElement),\n );\n\n const mappableArgCount =\n firstSpreadIndex === -1 ? args.length : firstSpreadIndex;\n\n for (const [argumentIndex, argument] of args.entries()) {\n if (argumentIndex >= mappableArgCount) {\n return;\n }\n\n if (argument.wasForgotten() || !isAsConstNode(argument)) {\n continue;\n }\n\n if (hasDisableNextLineComment(argument, TRANSFORMER_NAME)) {\n continue;\n }\n\n const parameter = getMatchedParameter(callTarget.parameters, argumentIndex);\n\n if (\n parameter === undefined ||\n !isBareConstTypeParameterReference(\n parameter,\n callTarget.constTypeParameterNames,\n )\n ) {\n continue;\n }\n\n options.debugPrint(\n 'removing redundant as const in const type parameter argument',\n argument.getText(),\n );\n\n options.replaceNode(argument, argument.getExpression().getText());\n }\n};\n\ntype ResolvedCallTarget = Readonly<{\n parameters: readonly tsm.ParameterDeclaration[];\n constTypeParameterNames: ISet<string>;\n}>;\n\n/**\n * Resolves the callee of a call expression to the declaration of its single\n * call signature, and collects the names of that declaration's\n * `const`-modified type parameters.\n *\n * Returns `undefined` when the callee does not resolve to exactly one call\n * signature, the resolved declaration has no parameters or type parameters,\n * or none of the type parameters carry the `const` modifier.\n */\nconst resolveSingleSignatureCallTarget = (\n // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types\n call: tsm.CallExpression,\n): ResolvedCallTarget | undefined => {\n // 0 signatures: the callee is unresolved (e.g. imported, since each file\n // is transformed in an isolated single-file project).\n // 2+ signatures: overloads, where removing `as const` could change which\n // overload is selected.\n if (\n !Arr.isFixedLengthArray(\n 1,\n call.getExpression().getType().getCallSignatures(),\n )\n ) {\n return undefined;\n }\n\n const declaration = call\n .getProject()\n .getTypeChecker()\n .getResolvedSignature(call)\n ?.getDeclaration();\n\n if (\n declaration === undefined ||\n !tsm.Node.isParametered(declaration) ||\n !tsm.Node.isTypeParametered(declaration)\n ) {\n return undefined;\n }\n\n // Only type parameters declared on the call's own signature are inferred\n // at the call site; a `const` type parameter of an enclosing declaration\n // (e.g. a generic class containing this method) is already fixed there.\n const constTypeParameterNames = ISet.create(\n declaration\n .getTypeParameters()\n .filter((tp) => tp.hasModifier(tsm.SyntaxKind.ConstKeyword))\n .map((tp) => tp.getName()),\n );\n\n if (constTypeParameterNames.size === 0) {\n return undefined;\n }\n\n return {\n parameters: declaration.getParameters(),\n constTypeParameterNames,\n } as const;\n};\n\nconst getMatchedParameter = (\n // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types\n parameters: readonly tsm.ParameterDeclaration[],\n argumentIndex: number,\n): tsm.ParameterDeclaration | undefined => {\n if (argumentIndex < parameters.length) {\n return parameters[argumentIndex];\n }\n\n const lastParameter = parameters.at(-1);\n\n return lastParameter?.isRestParameter() === true ? lastParameter : undefined;\n};\n\n/**\n * Whether the parameter's declared type is exactly a bare reference to one of\n * the given `const`-modified type parameters (i.e. `x: T`, not `x: readonly\n * T[]` or `x: Wrapper<T>` — there the argument expression's own type is still\n * observable, so the `as const` is kept).\n */\nconst isBareConstTypeParameterReference = (\n // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types\n parameter: tsm.ParameterDeclaration,\n constTypeParameterNames: ISet<string>,\n): boolean => {\n const typeNode = parameter.getTypeNode();\n\n if (typeNode === undefined) {\n return false;\n }\n\n if (!typeNode.isKind(tsm.SyntaxKind.TypeReference)) {\n return false;\n }\n\n const typeName = typeNode.getTypeName();\n\n return (\n typeName.isKind(tsm.SyntaxKind.Identifier) &&\n Arr.isEmpty(typeNode.getTypeArguments()) &&\n const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AY,aAAA,EAAc;AAAA,IACtC;AAAA,GACF;AACF,CAAA;AAEA,MAAM,mBAAA,GAAsB,CAE1B,UAAA,EACA,aAAA,KACyC;AACzC,EAAA,IAAI,aAAA,GAAgB,WAAW,MAAA,EAAQ;AACrC,IAAA,OAAO,WAAW,aAAa,CAAA;AAAA,EACjC;AAEA,EAAA,MAAM,aAAA,GAAgB,UAAA,CAAW,EAAA,CAAG,EAAE,CAAA;AAEtC,EAAA,OAAO,aAAA,EAAe,eAAA,EAAgB,KAAM,IAAA,GAAO,aAAA,GAAgB,MAAA;AACrE,CAAA;AAQA,MAAM,iCAAA,GAAoC,CAExC,SAAA,EACA,uBAAA,KACY;AACZ,EAAA,MAAM,QAAA,GAAW,UAAU,WAAA,EAAY;AAEvC,EAAA,IAAI,aAAa,MAAA,EAAW;AAC1B,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,IAAI,CAAC,QAAA,CAAS,MAAA,CAAO,GAAA,CAAI,UAAA,CAAW,aAAa,CAAA,EAAG;AAClD,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,MAAM,QAAA,GAAW,SAAS,WAAA,EAAY;AAEtC,EAAA,OACE,SAAS,MAAA,CAAO,GAAA,CAAI,UAAA,CAAW,UAAU,KACzC,GAAA,CAAI,OAAA,CAAQ,QAAA,CAAS,gBAAA,EAAkB,CAAA,IACvC,uBAAA,CAAwB,GAAA,CAAI,QAAA,CAAS,SAAS,CAAA;AAElD,CAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -20,6 +20,8 @@ export const appendAsConstTransformer = (
|
|
|
20
20
|
const optionsInternal: AppendAsConstTransformerOptionsInternal = {
|
|
21
21
|
applyLevel: options?.applyLevel ?? 'avoidInFunctionArgs',
|
|
22
22
|
ignoredPrefixes: ignorePrefixes,
|
|
23
|
+
removeAsConstForConstTypeParameters:
|
|
24
|
+
options?.removeAsConstForConstTypeParameters ?? true,
|
|
23
25
|
|
|
24
26
|
debugPrint: options?.debug === true ? console.debug : () => {},
|
|
25
27
|
replaceNode:
|
|
@@ -43,6 +45,10 @@ export const appendAsConstTransformer = (
|
|
|
43
45
|
optionsInternal,
|
|
44
46
|
);
|
|
45
47
|
}
|
|
48
|
+
|
|
49
|
+
if (optionsInternal.removeAsConstForConstTypeParameters) {
|
|
50
|
+
removeAsConstInConstTypeParameterArgs(sourceAst, optionsInternal);
|
|
51
|
+
}
|
|
46
52
|
},
|
|
47
53
|
};
|
|
48
54
|
};
|
|
@@ -62,8 +68,22 @@ export type AppendAsConstTransformerOptions = DeepReadonly<{
|
|
|
62
68
|
*/
|
|
63
69
|
ignorePrefixes?: string[];
|
|
64
70
|
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Whether to remove redundant `as const` assertions from call arguments
|
|
73
|
+
* whose corresponding parameter type is exactly a `const`-modified type
|
|
74
|
+
* parameter.
|
|
75
|
+
*
|
|
76
|
+
* (e.g. `f([1, 2] as const)` becomes `f([1, 2])` for
|
|
77
|
+
* `function f<const T>(x: T): T`, because the `const` type parameter
|
|
78
|
+
* already makes TypeScript infer the argument as if it were annotated
|
|
79
|
+
* with `as const`.)
|
|
80
|
+
*
|
|
81
|
+
* Only applies when the callee resolves to a single call signature within
|
|
82
|
+
* the transformed file itself; imported callees are left as they are.
|
|
83
|
+
*
|
|
84
|
+
* @default true
|
|
85
|
+
*/
|
|
86
|
+
removeAsConstForConstTypeParameters?: boolean;
|
|
67
87
|
|
|
68
88
|
debug?: boolean;
|
|
69
89
|
}>;
|
|
@@ -71,6 +91,7 @@ export type AppendAsConstTransformerOptions = DeepReadonly<{
|
|
|
71
91
|
type AppendAsConstTransformerOptionsInternal = DeepReadonly<{
|
|
72
92
|
applyLevel: 'all' | 'avoidInFunctionArgs';
|
|
73
93
|
ignoredPrefixes: ISet<string>;
|
|
94
|
+
removeAsConstForConstTypeParameters: boolean;
|
|
74
95
|
|
|
75
96
|
debugPrint: (...args: readonly unknown[]) => void;
|
|
76
97
|
replaceNode: (node: tsm.Node, newNodeText: string) => void;
|
|
@@ -364,3 +385,231 @@ const transformNode = (
|
|
|
364
385
|
// return;
|
|
365
386
|
}
|
|
366
387
|
};
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Removes redundant `as const` assertions from call arguments whose
|
|
391
|
+
* corresponding parameter type is exactly a `const`-modified type parameter
|
|
392
|
+
* (e.g. `f([1, 2] as const)` for `function f<const T>(x: T): T`).
|
|
393
|
+
*
|
|
394
|
+
* A `const` type parameter makes TypeScript treat every inference candidate
|
|
395
|
+
* as if it were annotated with `as const`, so when the parameter type is the
|
|
396
|
+
* bare type parameter itself, removing the assertion cannot change the
|
|
397
|
+
* inferred type. Conservative bail-outs (the `as const` is kept) when:
|
|
398
|
+
*
|
|
399
|
+
* - the callee does not resolve to exactly one call signature (unresolved
|
|
400
|
+
* imports resolve to none, since each file is transformed in an isolated
|
|
401
|
+
* single-file project; overloaded functions resolve to several),
|
|
402
|
+
* - the call has explicit type arguments (inference is not involved),
|
|
403
|
+
* - the argument is a spread argument or follows one (its parameter position
|
|
404
|
+
* is not statically known),
|
|
405
|
+
* - the parameter type is not the bare type parameter (e.g. `readonly T[]`).
|
|
406
|
+
*/
|
|
407
|
+
const removeAsConstInConstTypeParameterArgs = (
|
|
408
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
409
|
+
sourceAst: tsm.SourceFile,
|
|
410
|
+
options: AppendAsConstTransformerOptionsInternal,
|
|
411
|
+
): void => {
|
|
412
|
+
// Reversed so that calls nested inside another call's arguments are
|
|
413
|
+
// processed before the outer call replaces (and thereby forgets) them.
|
|
414
|
+
for (const call of sourceAst
|
|
415
|
+
.getDescendantsOfKind(tsm.SyntaxKind.CallExpression)
|
|
416
|
+
.toReversed()) {
|
|
417
|
+
if (call.wasForgotten()) {
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const args = call.getArguments();
|
|
422
|
+
|
|
423
|
+
// Cheap syntactic pre-check before touching the type checker
|
|
424
|
+
if (!args.some(isAsConstNode)) {
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (hasDisableNextLineComment(call, TRANSFORMER_NAME)) {
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const variableStatement = call.getFirstAncestorByKind(
|
|
433
|
+
tsm.SyntaxKind.VariableStatement,
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
if (
|
|
437
|
+
variableStatement !== undefined &&
|
|
438
|
+
hasDisableNextLineComment(variableStatement, TRANSFORMER_NAME)
|
|
439
|
+
) {
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// With explicit type arguments no inference happens, so the `as const`
|
|
444
|
+
// may be load-bearing for assignability.
|
|
445
|
+
if (!Arr.isEmpty(call.getTypeArguments())) {
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const callTarget = resolveSingleSignatureCallTarget(call);
|
|
450
|
+
|
|
451
|
+
if (callTarget === undefined) {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
removeAsConstArgsOfCall(args, callTarget, options);
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const removeAsConstArgsOfCall = (
|
|
460
|
+
args: readonly tsm.Node[],
|
|
461
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
462
|
+
callTarget: ResolvedCallTarget,
|
|
463
|
+
options: AppendAsConstTransformerOptionsInternal,
|
|
464
|
+
): void => {
|
|
465
|
+
// Argument positions at or after a spread argument cannot be mapped to a
|
|
466
|
+
// parameter statically.
|
|
467
|
+
const firstSpreadIndex = args.findIndex((a) =>
|
|
468
|
+
a.isKind(tsm.SyntaxKind.SpreadElement),
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
const mappableArgCount =
|
|
472
|
+
firstSpreadIndex === -1 ? args.length : firstSpreadIndex;
|
|
473
|
+
|
|
474
|
+
for (const [argumentIndex, argument] of args.entries()) {
|
|
475
|
+
if (argumentIndex >= mappableArgCount) {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (argument.wasForgotten() || !isAsConstNode(argument)) {
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (hasDisableNextLineComment(argument, TRANSFORMER_NAME)) {
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const parameter = getMatchedParameter(callTarget.parameters, argumentIndex);
|
|
488
|
+
|
|
489
|
+
if (
|
|
490
|
+
parameter === undefined ||
|
|
491
|
+
!isBareConstTypeParameterReference(
|
|
492
|
+
parameter,
|
|
493
|
+
callTarget.constTypeParameterNames,
|
|
494
|
+
)
|
|
495
|
+
) {
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
options.debugPrint(
|
|
500
|
+
'removing redundant as const in const type parameter argument',
|
|
501
|
+
argument.getText(),
|
|
502
|
+
);
|
|
503
|
+
|
|
504
|
+
options.replaceNode(argument, argument.getExpression().getText());
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
type ResolvedCallTarget = Readonly<{
|
|
509
|
+
parameters: readonly tsm.ParameterDeclaration[];
|
|
510
|
+
constTypeParameterNames: ISet<string>;
|
|
511
|
+
}>;
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Resolves the callee of a call expression to the declaration of its single
|
|
515
|
+
* call signature, and collects the names of that declaration's
|
|
516
|
+
* `const`-modified type parameters.
|
|
517
|
+
*
|
|
518
|
+
* Returns `undefined` when the callee does not resolve to exactly one call
|
|
519
|
+
* signature, the resolved declaration has no parameters or type parameters,
|
|
520
|
+
* or none of the type parameters carry the `const` modifier.
|
|
521
|
+
*/
|
|
522
|
+
const resolveSingleSignatureCallTarget = (
|
|
523
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
524
|
+
call: tsm.CallExpression,
|
|
525
|
+
): ResolvedCallTarget | undefined => {
|
|
526
|
+
// 0 signatures: the callee is unresolved (e.g. imported, since each file
|
|
527
|
+
// is transformed in an isolated single-file project).
|
|
528
|
+
// 2+ signatures: overloads, where removing `as const` could change which
|
|
529
|
+
// overload is selected.
|
|
530
|
+
if (
|
|
531
|
+
!Arr.isFixedLengthArray(
|
|
532
|
+
1,
|
|
533
|
+
call.getExpression().getType().getCallSignatures(),
|
|
534
|
+
)
|
|
535
|
+
) {
|
|
536
|
+
return undefined;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const declaration = call
|
|
540
|
+
.getProject()
|
|
541
|
+
.getTypeChecker()
|
|
542
|
+
.getResolvedSignature(call)
|
|
543
|
+
?.getDeclaration();
|
|
544
|
+
|
|
545
|
+
if (
|
|
546
|
+
declaration === undefined ||
|
|
547
|
+
!tsm.Node.isParametered(declaration) ||
|
|
548
|
+
!tsm.Node.isTypeParametered(declaration)
|
|
549
|
+
) {
|
|
550
|
+
return undefined;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// Only type parameters declared on the call's own signature are inferred
|
|
554
|
+
// at the call site; a `const` type parameter of an enclosing declaration
|
|
555
|
+
// (e.g. a generic class containing this method) is already fixed there.
|
|
556
|
+
const constTypeParameterNames = ISet.create(
|
|
557
|
+
declaration
|
|
558
|
+
.getTypeParameters()
|
|
559
|
+
.filter((tp) => tp.hasModifier(tsm.SyntaxKind.ConstKeyword))
|
|
560
|
+
.map((tp) => tp.getName()),
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
if (constTypeParameterNames.size === 0) {
|
|
564
|
+
return undefined;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return {
|
|
568
|
+
parameters: declaration.getParameters(),
|
|
569
|
+
constTypeParameterNames,
|
|
570
|
+
} as const;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const getMatchedParameter = (
|
|
574
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
575
|
+
parameters: readonly tsm.ParameterDeclaration[],
|
|
576
|
+
argumentIndex: number,
|
|
577
|
+
): tsm.ParameterDeclaration | undefined => {
|
|
578
|
+
if (argumentIndex < parameters.length) {
|
|
579
|
+
return parameters[argumentIndex];
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
const lastParameter = parameters.at(-1);
|
|
583
|
+
|
|
584
|
+
return lastParameter?.isRestParameter() === true ? lastParameter : undefined;
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Whether the parameter's declared type is exactly a bare reference to one of
|
|
589
|
+
* the given `const`-modified type parameters (i.e. `x: T`, not `x: readonly
|
|
590
|
+
* T[]` or `x: Wrapper<T>` — there the argument expression's own type is still
|
|
591
|
+
* observable, so the `as const` is kept).
|
|
592
|
+
*/
|
|
593
|
+
const isBareConstTypeParameterReference = (
|
|
594
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
595
|
+
parameter: tsm.ParameterDeclaration,
|
|
596
|
+
constTypeParameterNames: ISet<string>,
|
|
597
|
+
): boolean => {
|
|
598
|
+
const typeNode = parameter.getTypeNode();
|
|
599
|
+
|
|
600
|
+
if (typeNode === undefined) {
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (!typeNode.isKind(tsm.SyntaxKind.TypeReference)) {
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const typeName = typeNode.getTypeName();
|
|
609
|
+
|
|
610
|
+
return (
|
|
611
|
+
typeName.isKind(tsm.SyntaxKind.Identifier) &&
|
|
612
|
+
Arr.isEmpty(typeNode.getTypeArguments()) &&
|
|
613
|
+
constTypeParameterNames.has(typeName.getText())
|
|
614
|
+
);
|
|
615
|
+
};
|
|
@@ -11,10 +11,12 @@ import { transformSourceCode } from './transform-source-code.mjs';
|
|
|
11
11
|
const testFn = async ({
|
|
12
12
|
source,
|
|
13
13
|
expected,
|
|
14
|
+
options,
|
|
14
15
|
debug,
|
|
15
16
|
}: Readonly<{
|
|
16
17
|
source: string;
|
|
17
18
|
expected: string;
|
|
19
|
+
options?: Parameters<typeof appendAsConstTransformer>[0];
|
|
18
20
|
debug?: boolean;
|
|
19
21
|
}>): Promise<void> => {
|
|
20
22
|
if (debug !== true) {
|
|
@@ -26,7 +28,9 @@ const testFn = async ({
|
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
const transformed = await formatter(
|
|
29
|
-
transformSourceCode(source, false, [
|
|
31
|
+
transformSourceCode(source, false, [
|
|
32
|
+
appendAsConstTransformer({ ...options, debug }),
|
|
33
|
+
]),
|
|
30
34
|
);
|
|
31
35
|
|
|
32
36
|
const expectedFormatted = await formatter(expected);
|
|
@@ -698,6 +702,174 @@ describe(appendAsConstTransformer, () => {
|
|
|
698
702
|
])('$name', testFn);
|
|
699
703
|
});
|
|
700
704
|
|
|
705
|
+
describe('Redundant as const in const type parameter arguments', () => {
|
|
706
|
+
test.each([
|
|
707
|
+
{
|
|
708
|
+
name: 'const type parameter (function declaration) should remove as const',
|
|
709
|
+
source: dedent`
|
|
710
|
+
function f<const T>(x: T): T { return x; }
|
|
711
|
+
const a = f([1, 2] as const);
|
|
712
|
+
`,
|
|
713
|
+
expected: dedent`
|
|
714
|
+
function f<const T>(x: T): T { return x; }
|
|
715
|
+
const a = f([1, 2]);
|
|
716
|
+
`,
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
name: 'const type parameter (expression statement call) should remove as const',
|
|
720
|
+
source: dedent`
|
|
721
|
+
function f<const T>(x: T): T { return x; }
|
|
722
|
+
f({ x: 1 } as const);
|
|
723
|
+
`,
|
|
724
|
+
expected: dedent`
|
|
725
|
+
function f<const T>(x: T): T { return x; }
|
|
726
|
+
f({ x: 1 });
|
|
727
|
+
`,
|
|
728
|
+
},
|
|
729
|
+
{
|
|
730
|
+
name: 'const type parameter (arrow function) should remove as const',
|
|
731
|
+
source: dedent`
|
|
732
|
+
const f = <const S,>(x: S): S => x;
|
|
733
|
+
const a = f([1, 2] as const);
|
|
734
|
+
`,
|
|
735
|
+
expected: dedent`
|
|
736
|
+
const f = <const S,>(x: S): S => x;
|
|
737
|
+
const a = f([1, 2]);
|
|
738
|
+
`,
|
|
739
|
+
},
|
|
740
|
+
{
|
|
741
|
+
name: 'const type parameter (object literal method) should remove as const',
|
|
742
|
+
source: dedent`
|
|
743
|
+
const Obj = { m: <const U,>(x: U): U => x } as const;
|
|
744
|
+
const a = Obj.m([1] as const);
|
|
745
|
+
`,
|
|
746
|
+
expected: dedent`
|
|
747
|
+
const Obj = { m: <const U,>(x: U): U => x } as const;
|
|
748
|
+
const a = Obj.m([1]);
|
|
749
|
+
`,
|
|
750
|
+
},
|
|
751
|
+
{
|
|
752
|
+
name: 'const rest type parameter should remove as const',
|
|
753
|
+
source: dedent`
|
|
754
|
+
function rest<const T extends readonly unknown[]>(...xs: T): T { return xs; }
|
|
755
|
+
const r = rest(1 as const, [2, 3] as const);
|
|
756
|
+
`,
|
|
757
|
+
expected: dedent`
|
|
758
|
+
function rest<const T extends readonly unknown[]>(...xs: T): T { return xs; }
|
|
759
|
+
const r = rest(1, [2, 3]);
|
|
760
|
+
`,
|
|
761
|
+
},
|
|
762
|
+
{
|
|
763
|
+
name: 'mixed const and non-const type parameters should only remove for const ones',
|
|
764
|
+
source: dedent`
|
|
765
|
+
function h<const A, B>(a: A, b: B): A { return a; }
|
|
766
|
+
const m = h([1] as const, [2] as const);
|
|
767
|
+
`,
|
|
768
|
+
expected: dedent`
|
|
769
|
+
function h<const A, B>(a: A, b: B): A { return a; }
|
|
770
|
+
const m = h([1], [2] as const);
|
|
771
|
+
`,
|
|
772
|
+
},
|
|
773
|
+
{
|
|
774
|
+
name: 'non-const type parameter should keep as const',
|
|
775
|
+
source: dedent`
|
|
776
|
+
function g<T>(x: T): T { return x; }
|
|
777
|
+
const b = g([1, 2] as const);
|
|
778
|
+
`,
|
|
779
|
+
expected: dedent`
|
|
780
|
+
function g<T>(x: T): T { return x; }
|
|
781
|
+
const b = g([1, 2] as const);
|
|
782
|
+
`,
|
|
783
|
+
},
|
|
784
|
+
{
|
|
785
|
+
name: 'imported callee should keep as const',
|
|
786
|
+
source: dedent`
|
|
787
|
+
import { f } from './other.mjs';
|
|
788
|
+
const a = f([1, 2] as const);
|
|
789
|
+
`,
|
|
790
|
+
expected: dedent`
|
|
791
|
+
import { f } from './other.mjs';
|
|
792
|
+
const a = f([1, 2] as const);
|
|
793
|
+
`,
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
name: 'overloaded function should keep as const',
|
|
797
|
+
source: dedent`
|
|
798
|
+
function ov<const T>(x: T): T;
|
|
799
|
+
function ov(x: number): number;
|
|
800
|
+
function ov(x: unknown): unknown { return x; }
|
|
801
|
+
const e = ov([1] as const);
|
|
802
|
+
`,
|
|
803
|
+
expected: dedent`
|
|
804
|
+
function ov<const T>(x: T): T;
|
|
805
|
+
function ov(x: number): number;
|
|
806
|
+
function ov(x: unknown): unknown { return x; }
|
|
807
|
+
const e = ov([1] as const);
|
|
808
|
+
`,
|
|
809
|
+
},
|
|
810
|
+
{
|
|
811
|
+
name: 'explicit type arguments should keep as const',
|
|
812
|
+
source: dedent`
|
|
813
|
+
function f<const T>(x: T): T { return x; }
|
|
814
|
+
const j = f<readonly [1, 2]>([1, 2] as const);
|
|
815
|
+
`,
|
|
816
|
+
expected: dedent`
|
|
817
|
+
function f<const T>(x: T): T { return x; }
|
|
818
|
+
const j = f<readonly [1, 2]>([1, 2] as const);
|
|
819
|
+
`,
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
name: 'non-bare type parameter reference (readonly T[]) should keep as const',
|
|
823
|
+
source: dedent`
|
|
824
|
+
function ro<const T>(xs: readonly T[]): T { return xs[0] as T; }
|
|
825
|
+
const o = ro([1, 2] as const);
|
|
826
|
+
`,
|
|
827
|
+
expected: dedent`
|
|
828
|
+
function ro<const T>(xs: readonly T[]): T { return xs[0] as T; }
|
|
829
|
+
const o = ro([1, 2] as const);
|
|
830
|
+
`,
|
|
831
|
+
},
|
|
832
|
+
{
|
|
833
|
+
name: 'argument after spread argument should keep as const',
|
|
834
|
+
source: dedent`
|
|
835
|
+
function f<const T>(a: T, b: T): T { return a; }
|
|
836
|
+
const xs = [1, 2] as const;
|
|
837
|
+
const s = f(...xs, [3] as const);
|
|
838
|
+
`,
|
|
839
|
+
expected: dedent`
|
|
840
|
+
function f<const T>(a: T, b: T): T { return a; }
|
|
841
|
+
const xs = [1, 2] as const;
|
|
842
|
+
const s = f(...xs, [3] as const);
|
|
843
|
+
`,
|
|
844
|
+
},
|
|
845
|
+
{
|
|
846
|
+
name: 'transformer-ignore-next-line should keep as const',
|
|
847
|
+
source: dedent`
|
|
848
|
+
function f<const T>(x: T): T { return x; }
|
|
849
|
+
// transformer-ignore-next-line
|
|
850
|
+
const a = f([1, 2] as const);
|
|
851
|
+
`,
|
|
852
|
+
expected: dedent`
|
|
853
|
+
function f<const T>(x: T): T { return x; }
|
|
854
|
+
// transformer-ignore-next-line
|
|
855
|
+
const a = f([1, 2] as const);
|
|
856
|
+
`,
|
|
857
|
+
},
|
|
858
|
+
{
|
|
859
|
+
name: 'removeAsConstForConstTypeParameters: false should keep as const',
|
|
860
|
+
source: dedent`
|
|
861
|
+
function f<const T>(x: T): T { return x; }
|
|
862
|
+
const a = f([1, 2] as const);
|
|
863
|
+
`,
|
|
864
|
+
expected: dedent`
|
|
865
|
+
function f<const T>(x: T): T { return x; }
|
|
866
|
+
const a = f([1, 2] as const);
|
|
867
|
+
`,
|
|
868
|
+
options: { removeAsConstForConstTypeParameters: false },
|
|
869
|
+
},
|
|
870
|
+
])('$name', testFn);
|
|
871
|
+
});
|
|
872
|
+
|
|
701
873
|
describe('ignorePrefixes option (mut_ prefix)', () => {
|
|
702
874
|
test.each([
|
|
703
875
|
{
|