miniread 1.16.0 → 1.17.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/dist/transforms/rename-charcode-variables/rename-charcode-variables-transform.d.ts +2 -0
- package/dist/transforms/rename-charcode-variables/rename-charcode-variables-transform.js +67 -0
- package/dist/transforms/rename-charcode-variables-v2/rename-charcode-variables-v2-transform.d.ts +2 -0
- package/dist/transforms/rename-charcode-variables-v2/rename-charcode-variables-v2-transform.js +135 -0
- package/dist/transforms/transform-registry.js +8 -4
- package/package.json +1 -1
- package/transform-manifest.json +21 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { isStableRenamed, RenameGroup } from "../../core/stable-naming.js";
|
|
3
|
+
import { getFilesToProcess, } from "../../core/types.js";
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
6
|
+
const traverse = require("@babel/traverse").default;
|
|
7
|
+
const BASE_NAME = "charCode";
|
|
8
|
+
/**
|
|
9
|
+
* Detects if the init is a call to .charCodeAt()
|
|
10
|
+
* e.g., `var a = str.charCodeAt(0)`
|
|
11
|
+
*/
|
|
12
|
+
const isCharCodeAtCall = (path) => {
|
|
13
|
+
const init = path.node.init;
|
|
14
|
+
if (init?.type !== "CallExpression")
|
|
15
|
+
return false;
|
|
16
|
+
const callee = init.callee;
|
|
17
|
+
if (callee.type !== "MemberExpression")
|
|
18
|
+
return false;
|
|
19
|
+
const property = callee.property;
|
|
20
|
+
if (property.type !== "Identifier")
|
|
21
|
+
return false;
|
|
22
|
+
if (property.name !== "charCodeAt")
|
|
23
|
+
return false;
|
|
24
|
+
// If charCodeAt is accessed via computed property, skip
|
|
25
|
+
if (callee.computed)
|
|
26
|
+
return false;
|
|
27
|
+
return true;
|
|
28
|
+
};
|
|
29
|
+
export const renameCharcodeVariablesTransform = {
|
|
30
|
+
id: "rename-charcode-variables",
|
|
31
|
+
description: "Renames variables assigned from .charCodeAt() to $charCode or charCode/charCode2/...",
|
|
32
|
+
scope: "file",
|
|
33
|
+
parallelizable: true,
|
|
34
|
+
transform(context) {
|
|
35
|
+
let nodesVisited = 0;
|
|
36
|
+
let transformationsApplied = 0;
|
|
37
|
+
for (const fileInfo of getFilesToProcess(context)) {
|
|
38
|
+
const group = new RenameGroup();
|
|
39
|
+
traverse(fileInfo.ast, {
|
|
40
|
+
VariableDeclarator(path) {
|
|
41
|
+
nodesVisited++;
|
|
42
|
+
const id = path.node.id;
|
|
43
|
+
if (id.type !== "Identifier")
|
|
44
|
+
return;
|
|
45
|
+
// Skip already-stable names
|
|
46
|
+
if (isStableRenamed(id.name))
|
|
47
|
+
return;
|
|
48
|
+
if (!isCharCodeAtCall(path))
|
|
49
|
+
return;
|
|
50
|
+
const binding = path.scope.getBinding(id.name);
|
|
51
|
+
if (!binding)
|
|
52
|
+
return;
|
|
53
|
+
// Skip if the variable is reassigned
|
|
54
|
+
if (!binding.constant)
|
|
55
|
+
return;
|
|
56
|
+
group.add({
|
|
57
|
+
scope: path.scope,
|
|
58
|
+
currentName: id.name,
|
|
59
|
+
baseName: BASE_NAME,
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
transformationsApplied += group.apply();
|
|
64
|
+
}
|
|
65
|
+
return Promise.resolve({ nodesVisited, transformationsApplied });
|
|
66
|
+
},
|
|
67
|
+
};
|
package/dist/transforms/rename-charcode-variables-v2/rename-charcode-variables-v2-transform.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { isStableRenamed, RenameGroup } from "../../core/stable-naming.js";
|
|
3
|
+
import { getFilesToProcess, } from "../../core/types.js";
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
6
|
+
const traverse = require("@babel/traverse").default;
|
|
7
|
+
/**
|
|
8
|
+
* Capitalizes the first letter of a string.
|
|
9
|
+
*/
|
|
10
|
+
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
11
|
+
const MAX_RECURSION_DEPTH = 5;
|
|
12
|
+
/**
|
|
13
|
+
* Converts a charCodeAt argument expression to a suffix for the variable name.
|
|
14
|
+
* Returns undefined if the expression is too complex to represent.
|
|
15
|
+
*
|
|
16
|
+
* Examples:
|
|
17
|
+
* - `0` → "0"
|
|
18
|
+
* - `index` → "Index"
|
|
19
|
+
* - `index + 1` → "IndexPlus1"
|
|
20
|
+
* - `i` → "I"
|
|
21
|
+
* - `pos` → "Pos"
|
|
22
|
+
*/
|
|
23
|
+
const getArgumentSuffix = (expression, depth = 0) => {
|
|
24
|
+
// Prevent stack overflow from deeply nested expressions
|
|
25
|
+
if (depth > MAX_RECURSION_DEPTH)
|
|
26
|
+
return undefined;
|
|
27
|
+
// Numeric literal: charCodeAt(0) → "0"
|
|
28
|
+
if (expression.type === "NumericLiteral") {
|
|
29
|
+
if (Number.isInteger(expression.value) && expression.value >= 0) {
|
|
30
|
+
return String(expression.value);
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
// Identifier: charCodeAt(index) → "Index"
|
|
35
|
+
if (expression.type === "Identifier") {
|
|
36
|
+
return capitalize(expression.name);
|
|
37
|
+
}
|
|
38
|
+
// Binary expression with +: charCodeAt(index + 1) → "IndexPlus1"
|
|
39
|
+
if (expression.type === "BinaryExpression") {
|
|
40
|
+
// Skip if left is a PrivateName (can happen in class fields)
|
|
41
|
+
if (expression.left.type === "PrivateName")
|
|
42
|
+
return undefined;
|
|
43
|
+
if (expression.operator === "+") {
|
|
44
|
+
const leftSuffix = getArgumentSuffix(expression.left, depth + 1);
|
|
45
|
+
const rightSuffix = getArgumentSuffix(expression.right, depth + 1);
|
|
46
|
+
if (leftSuffix !== undefined && rightSuffix !== undefined) {
|
|
47
|
+
return `${leftSuffix}Plus${rightSuffix}`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (expression.operator === "-") {
|
|
51
|
+
const leftSuffix = getArgumentSuffix(expression.left, depth + 1);
|
|
52
|
+
const rightSuffix = getArgumentSuffix(expression.right, depth + 1);
|
|
53
|
+
if (leftSuffix !== undefined && rightSuffix !== undefined) {
|
|
54
|
+
return `${leftSuffix}Minus${rightSuffix}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Detects if the init is a call to .charCodeAt() and extracts the argument.
|
|
62
|
+
* Returns the argument expression if valid, undefined otherwise.
|
|
63
|
+
*/
|
|
64
|
+
const getCharCodeAtArgument = (path) => {
|
|
65
|
+
const init = path.node.init;
|
|
66
|
+
if (init?.type !== "CallExpression")
|
|
67
|
+
return undefined;
|
|
68
|
+
const callee = init.callee;
|
|
69
|
+
if (callee.type !== "MemberExpression")
|
|
70
|
+
return undefined;
|
|
71
|
+
const property = callee.property;
|
|
72
|
+
if (property.type !== "Identifier")
|
|
73
|
+
return undefined;
|
|
74
|
+
if (property.name !== "charCodeAt")
|
|
75
|
+
return undefined;
|
|
76
|
+
// If charCodeAt is accessed via computed property, skip
|
|
77
|
+
if (callee.computed)
|
|
78
|
+
return undefined;
|
|
79
|
+
// Must have exactly one argument
|
|
80
|
+
if (init.arguments.length !== 1)
|
|
81
|
+
return undefined;
|
|
82
|
+
const firstArgument = init.arguments[0];
|
|
83
|
+
if (!firstArgument)
|
|
84
|
+
return undefined;
|
|
85
|
+
if (firstArgument.type === "SpreadElement")
|
|
86
|
+
return undefined;
|
|
87
|
+
if (firstArgument.type === "ArgumentPlaceholder")
|
|
88
|
+
return undefined;
|
|
89
|
+
return firstArgument;
|
|
90
|
+
};
|
|
91
|
+
export const renameCharcodeVariablesV2Transform = {
|
|
92
|
+
id: "rename-charcode-variables-v2",
|
|
93
|
+
description: "Renames variables assigned from .charCodeAt(arg) to $charCodeAt{Arg} based on the argument",
|
|
94
|
+
scope: "file",
|
|
95
|
+
parallelizable: true,
|
|
96
|
+
transform(context) {
|
|
97
|
+
let nodesVisited = 0;
|
|
98
|
+
let transformationsApplied = 0;
|
|
99
|
+
for (const fileInfo of getFilesToProcess(context)) {
|
|
100
|
+
const group = new RenameGroup();
|
|
101
|
+
traverse(fileInfo.ast, {
|
|
102
|
+
VariableDeclarator(path) {
|
|
103
|
+
nodesVisited++;
|
|
104
|
+
const id = path.node.id;
|
|
105
|
+
if (id.type !== "Identifier")
|
|
106
|
+
return;
|
|
107
|
+
// Skip already-stable names
|
|
108
|
+
if (isStableRenamed(id.name))
|
|
109
|
+
return;
|
|
110
|
+
const argument = getCharCodeAtArgument(path);
|
|
111
|
+
if (argument === undefined)
|
|
112
|
+
return;
|
|
113
|
+
const binding = path.scope.getBinding(id.name);
|
|
114
|
+
if (!binding)
|
|
115
|
+
return;
|
|
116
|
+
// Skip if the variable is reassigned
|
|
117
|
+
if (!binding.constant)
|
|
118
|
+
return;
|
|
119
|
+
// Try to derive a suffix from the argument
|
|
120
|
+
const suffix = getArgumentSuffix(argument);
|
|
121
|
+
if (suffix === undefined)
|
|
122
|
+
return;
|
|
123
|
+
const baseName = `charCodeAt${suffix}`;
|
|
124
|
+
group.add({
|
|
125
|
+
scope: path.scope,
|
|
126
|
+
currentName: id.name,
|
|
127
|
+
baseName,
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
transformationsApplied += group.apply();
|
|
132
|
+
}
|
|
133
|
+
return Promise.resolve({ nodesVisited, transformationsApplied });
|
|
134
|
+
},
|
|
135
|
+
};
|
|
@@ -5,12 +5,16 @@ import { expandSequenceExpressionsV5Transform } from "./expand-sequence-expressi
|
|
|
5
5
|
import { expandUndefinedLiteralsTransform } from "./expand-undefined-literals/expand-undefined-literals-transform.js";
|
|
6
6
|
import { removeRedundantElseTransform } from "./remove-redundant-else/remove-redundant-else-transform.js";
|
|
7
7
|
import { renameCatchParametersTransform } from "./rename-catch-parameters/rename-catch-parameters-transform.js";
|
|
8
|
+
import { renameCharCodeAtTransform } from "./rename-char-code-at/rename-char-code-at-transform.js";
|
|
9
|
+
import { renameCharcodeVariablesTransform } from "./rename-charcode-variables/rename-charcode-variables-transform.js";
|
|
10
|
+
import { renameCharcodeVariablesV2Transform } from "./rename-charcode-variables-v2/rename-charcode-variables-v2-transform.js";
|
|
8
11
|
import { renameComparisonFlagsTransform } from "./rename-comparison-flags/rename-comparison-flags-transform.js";
|
|
9
12
|
import { renameDestructuredAliasesTransform } from "./rename-destructured-aliases/rename-destructured-aliases-transform.js";
|
|
10
13
|
import { renameEventParametersTransform } from "./rename-event-parameters/rename-event-parameters-transform.js";
|
|
11
14
|
import { renameLoopIndexVariablesTransform } from "./rename-loop-index-variables/rename-loop-index-variables-transform.js";
|
|
12
15
|
import { renameLoopIndexVariablesV2Transform } from "./rename-loop-index-variables-v2/rename-loop-index-variables-v2-transform.js";
|
|
13
16
|
import { renameLoopIndexVariablesV3Transform } from "./rename-loop-index-variables-v3/rename-loop-index-variables-v3-transform.js";
|
|
17
|
+
import { renameParametersToMatchPropertiesTransform } from "./rename-parameters-to-match-properties/rename-parameters-to-match-properties-transform.js";
|
|
14
18
|
import { renamePromiseExecutorParametersTransform } from "./rename-promise-executor-parameters/rename-promise-executor-parameters-transform.js";
|
|
15
19
|
import { renameReplaceChildParametersTransform } from "./rename-replace-child-parameters/rename-replace-child-parameters-transform.js";
|
|
16
20
|
import { renameThisAliasesTransform } from "./rename-this-aliases/rename-this-aliases-transform.js";
|
|
@@ -18,10 +22,7 @@ import { renameTimeoutIdsTransform } from "./rename-timeout-ids/rename-timeout-i
|
|
|
18
22
|
import { renameUseReferenceGuardsTransform } from "./rename-use-reference-guards/rename-use-reference-guards-transform.js";
|
|
19
23
|
import { renameUseReferenceGuardsV2Transform } from "./rename-use-reference-guards-v2/rename-use-reference-guards-v2-transform.js";
|
|
20
24
|
import { splitVariableDeclarationsTransform } from "./split-variable-declarations/split-variable-declarations-transform.js";
|
|
21
|
-
import { renameCharCodeAtTransform } from "./rename-char-code-at/rename-char-code-at-transform.js";
|
|
22
|
-
import { renameParametersToMatchPropertiesTransform } from "./rename-parameters-to-match-properties/rename-parameters-to-match-properties-transform.js";
|
|
23
25
|
export const transformRegistry = {
|
|
24
|
-
[renameCharCodeAtTransform.id]: renameCharCodeAtTransform,
|
|
25
26
|
[expandBooleanLiteralsTransform.id]: expandBooleanLiteralsTransform,
|
|
26
27
|
[expandSpecialNumberLiteralsTransform.id]: expandSpecialNumberLiteralsTransform,
|
|
27
28
|
[expandSequenceExpressionsV4Transform.id]: expandSequenceExpressionsV4Transform,
|
|
@@ -29,12 +30,16 @@ export const transformRegistry = {
|
|
|
29
30
|
[expandUndefinedLiteralsTransform.id]: expandUndefinedLiteralsTransform,
|
|
30
31
|
[removeRedundantElseTransform.id]: removeRedundantElseTransform,
|
|
31
32
|
[renameCatchParametersTransform.id]: renameCatchParametersTransform,
|
|
33
|
+
[renameCharCodeAtTransform.id]: renameCharCodeAtTransform,
|
|
34
|
+
[renameCharcodeVariablesTransform.id]: renameCharcodeVariablesTransform,
|
|
35
|
+
[renameCharcodeVariablesV2Transform.id]: renameCharcodeVariablesV2Transform,
|
|
32
36
|
[renameComparisonFlagsTransform.id]: renameComparisonFlagsTransform,
|
|
33
37
|
[renameDestructuredAliasesTransform.id]: renameDestructuredAliasesTransform,
|
|
34
38
|
[renameEventParametersTransform.id]: renameEventParametersTransform,
|
|
35
39
|
[renameLoopIndexVariablesTransform.id]: renameLoopIndexVariablesTransform,
|
|
36
40
|
[renameLoopIndexVariablesV2Transform.id]: renameLoopIndexVariablesV2Transform,
|
|
37
41
|
[renameLoopIndexVariablesV3Transform.id]: renameLoopIndexVariablesV3Transform,
|
|
42
|
+
[renameParametersToMatchPropertiesTransform.id]: renameParametersToMatchPropertiesTransform,
|
|
38
43
|
[renamePromiseExecutorParametersTransform.id]: renamePromiseExecutorParametersTransform,
|
|
39
44
|
[renameReplaceChildParametersTransform.id]: renameReplaceChildParametersTransform,
|
|
40
45
|
[renameThisAliasesTransform.id]: renameThisAliasesTransform,
|
|
@@ -42,6 +47,5 @@ export const transformRegistry = {
|
|
|
42
47
|
[renameUseReferenceGuardsTransform.id]: renameUseReferenceGuardsTransform,
|
|
43
48
|
[renameUseReferenceGuardsV2Transform.id]: renameUseReferenceGuardsV2Transform,
|
|
44
49
|
[splitVariableDeclarationsTransform.id]: splitVariableDeclarationsTransform,
|
|
45
|
-
[renameParametersToMatchPropertiesTransform.id]: renameParametersToMatchPropertiesTransform,
|
|
46
50
|
};
|
|
47
51
|
export const allTransformIds = Object.keys(transformRegistry);
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "miniread",
|
|
3
3
|
"author": "Łukasz Jerciński",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "1.
|
|
5
|
+
"version": "1.17.0",
|
|
6
6
|
"description": "Transform minified JavaScript/TypeScript into a more readable form using deterministic AST-based transforms.",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
package/transform-manifest.json
CHANGED
|
@@ -219,6 +219,27 @@
|
|
|
219
219
|
"diffReductionImpact": 0.00003774938185385768,
|
|
220
220
|
"recommended": true,
|
|
221
221
|
"notes": "Added manually based on high-confidence heuristic."
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
"id": "rename-charcode-variables",
|
|
225
|
+
"description": "Renames variables assigned from .charCodeAt() to $charCode or charCode/charCode2/...",
|
|
226
|
+
"scope": "file",
|
|
227
|
+
"parallelizable": true,
|
|
228
|
+
"diffReductionImpact": 0,
|
|
229
|
+
"recommended": false,
|
|
230
|
+
"evaluatedAt": "2026-01-23T17:59:00.000Z",
|
|
231
|
+
"notes": "Superseded by rename-charcode-variables-v2.",
|
|
232
|
+
"supersededBy": "rename-charcode-variables-v2"
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
"id": "rename-charcode-variables-v2",
|
|
236
|
+
"description": "Renames variables assigned from .charCodeAt(arg) to $charCodeAt{Arg} based on the argument",
|
|
237
|
+
"scope": "file",
|
|
238
|
+
"parallelizable": true,
|
|
239
|
+
"diffReductionImpact": 0,
|
|
240
|
+
"recommended": true,
|
|
241
|
+
"evaluatedAt": "2026-01-23T18:10:00.000Z",
|
|
242
|
+
"notes": "Derives names from charCodeAt argument for better stability (e.g., $charCodeAtIndex, $charCodeAtIndexPlus1)."
|
|
222
243
|
}
|
|
223
244
|
],
|
|
224
245
|
"presetStats": {
|