miniread 1.15.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.
@@ -0,0 +1,2 @@
1
+ import { type Transform } from "../../core/types.js";
2
+ export declare const removeRedundantElseTransform: Transform;
@@ -0,0 +1,61 @@
1
+ import { createRequire } from "node:module";
2
+ import { getFilesToProcess, } from "../../core/types.js";
3
+ const require = createRequire(import.meta.url);
4
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
5
+ const traverse = require("@babel/traverse").default;
6
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
7
+ const t = require("@babel/types");
8
+ export const removeRedundantElseTransform = {
9
+ id: "remove-redundant-else",
10
+ description: "Removes redundant else blocks after return/throw/break/continue",
11
+ scope: "file",
12
+ parallelizable: true,
13
+ transform(context) {
14
+ let nodesVisited = 0;
15
+ let transformationsApplied = 0;
16
+ for (const fileInfo of getFilesToProcess(context)) {
17
+ traverse(fileInfo.ast, {
18
+ IfStatement(path) {
19
+ nodesVisited++;
20
+ const { consequent, alternate } = path.node;
21
+ if (!alternate)
22
+ return;
23
+ let interrupts = false;
24
+ if (isInterrupting(consequent)) {
25
+ interrupts = true;
26
+ }
27
+ else if (consequent.type === "BlockStatement" &&
28
+ consequent.body.length > 0) {
29
+ const last = consequent.body.at(-1);
30
+ if (isInterrupting(last)) {
31
+ interrupts = true;
32
+ }
33
+ }
34
+ if (interrupts) {
35
+ // Remove alternate
36
+ // eslint-disable-next-line unicorn/no-null
37
+ path.node.alternate = null;
38
+ // Insert alternate content after
39
+ path.insertAfter(alternate);
40
+ transformationsApplied++;
41
+ }
42
+ },
43
+ });
44
+ }
45
+ return Promise.resolve({
46
+ nodesVisited,
47
+ transformationsApplied,
48
+ });
49
+ },
50
+ };
51
+ function isInterrupting(node) {
52
+ return (
53
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
54
+ (t.isReturnStatement(node) ||
55
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
56
+ t.isThrowStatement(node) ||
57
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
58
+ t.isBreakStatement(node) ||
59
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
60
+ t.isContinueStatement(node)));
61
+ }
@@ -0,0 +1,2 @@
1
+ import { type Transform } from "../../core/types.js";
2
+ export declare const renameCharcodeVariablesTransform: Transform;
@@ -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
+ };
@@ -0,0 +1,2 @@
1
+ import { type Transform } from "../../core/types.js";
2
+ export declare const renameCharcodeVariablesV2Transform: Transform;
@@ -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
+ };
@@ -3,13 +3,18 @@ import { expandSpecialNumberLiteralsTransform } from "./expand-special-number-li
3
3
  import { expandSequenceExpressionsV4Transform } from "./expand-sequence-expressions-v4/expand-sequence-expressions-v4-transform.js";
4
4
  import { expandSequenceExpressionsV5Transform } from "./expand-sequence-expressions-v5/expand-sequence-expressions-v5-transform.js";
5
5
  import { expandUndefinedLiteralsTransform } from "./expand-undefined-literals/expand-undefined-literals-transform.js";
6
+ import { removeRedundantElseTransform } from "./remove-redundant-else/remove-redundant-else-transform.js";
6
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";
7
11
  import { renameComparisonFlagsTransform } from "./rename-comparison-flags/rename-comparison-flags-transform.js";
8
12
  import { renameDestructuredAliasesTransform } from "./rename-destructured-aliases/rename-destructured-aliases-transform.js";
9
13
  import { renameEventParametersTransform } from "./rename-event-parameters/rename-event-parameters-transform.js";
10
14
  import { renameLoopIndexVariablesTransform } from "./rename-loop-index-variables/rename-loop-index-variables-transform.js";
11
15
  import { renameLoopIndexVariablesV2Transform } from "./rename-loop-index-variables-v2/rename-loop-index-variables-v2-transform.js";
12
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";
13
18
  import { renamePromiseExecutorParametersTransform } from "./rename-promise-executor-parameters/rename-promise-executor-parameters-transform.js";
14
19
  import { renameReplaceChildParametersTransform } from "./rename-replace-child-parameters/rename-replace-child-parameters-transform.js";
15
20
  import { renameThisAliasesTransform } from "./rename-this-aliases/rename-this-aliases-transform.js";
@@ -17,22 +22,24 @@ import { renameTimeoutIdsTransform } from "./rename-timeout-ids/rename-timeout-i
17
22
  import { renameUseReferenceGuardsTransform } from "./rename-use-reference-guards/rename-use-reference-guards-transform.js";
18
23
  import { renameUseReferenceGuardsV2Transform } from "./rename-use-reference-guards-v2/rename-use-reference-guards-v2-transform.js";
19
24
  import { splitVariableDeclarationsTransform } from "./split-variable-declarations/split-variable-declarations-transform.js";
20
- import { renameCharCodeAtTransform } from "./rename-char-code-at/rename-char-code-at-transform.js";
21
- import { renameParametersToMatchPropertiesTransform } from "./rename-parameters-to-match-properties/rename-parameters-to-match-properties-transform.js";
22
25
  export const transformRegistry = {
23
- [renameCharCodeAtTransform.id]: renameCharCodeAtTransform,
24
26
  [expandBooleanLiteralsTransform.id]: expandBooleanLiteralsTransform,
25
27
  [expandSpecialNumberLiteralsTransform.id]: expandSpecialNumberLiteralsTransform,
26
28
  [expandSequenceExpressionsV4Transform.id]: expandSequenceExpressionsV4Transform,
27
29
  [expandSequenceExpressionsV5Transform.id]: expandSequenceExpressionsV5Transform,
28
30
  [expandUndefinedLiteralsTransform.id]: expandUndefinedLiteralsTransform,
31
+ [removeRedundantElseTransform.id]: removeRedundantElseTransform,
29
32
  [renameCatchParametersTransform.id]: renameCatchParametersTransform,
33
+ [renameCharCodeAtTransform.id]: renameCharCodeAtTransform,
34
+ [renameCharcodeVariablesTransform.id]: renameCharcodeVariablesTransform,
35
+ [renameCharcodeVariablesV2Transform.id]: renameCharcodeVariablesV2Transform,
30
36
  [renameComparisonFlagsTransform.id]: renameComparisonFlagsTransform,
31
37
  [renameDestructuredAliasesTransform.id]: renameDestructuredAliasesTransform,
32
38
  [renameEventParametersTransform.id]: renameEventParametersTransform,
33
39
  [renameLoopIndexVariablesTransform.id]: renameLoopIndexVariablesTransform,
34
40
  [renameLoopIndexVariablesV2Transform.id]: renameLoopIndexVariablesV2Transform,
35
41
  [renameLoopIndexVariablesV3Transform.id]: renameLoopIndexVariablesV3Transform,
42
+ [renameParametersToMatchPropertiesTransform.id]: renameParametersToMatchPropertiesTransform,
36
43
  [renamePromiseExecutorParametersTransform.id]: renamePromiseExecutorParametersTransform,
37
44
  [renameReplaceChildParametersTransform.id]: renameReplaceChildParametersTransform,
38
45
  [renameThisAliasesTransform.id]: renameThisAliasesTransform,
@@ -40,6 +47,5 @@ export const transformRegistry = {
40
47
  [renameUseReferenceGuardsTransform.id]: renameUseReferenceGuardsTransform,
41
48
  [renameUseReferenceGuardsV2Transform.id]: renameUseReferenceGuardsV2Transform,
42
49
  [splitVariableDeclarationsTransform.id]: splitVariableDeclarationsTransform,
43
- [renameParametersToMatchPropertiesTransform.id]: renameParametersToMatchPropertiesTransform,
44
50
  };
45
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.15.0",
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",
@@ -191,6 +191,16 @@
191
191
  "evaluatedAt": "2026-01-23T08:17:45.579Z",
192
192
  "notes": "Auto-added by evaluation script."
193
193
  },
194
+ {
195
+ "id": "remove-redundant-else",
196
+ "description": "Removes redundant else blocks after return/throw/break/continue",
197
+ "scope": "file",
198
+ "parallelizable": true,
199
+ "diffReductionImpact": 0,
200
+ "recommended": true,
201
+ "evaluatedAt": "2026-01-23T18:15:00.000Z",
202
+ "notes": "Added manually; improves readability by reducing nesting."
203
+ },
194
204
  {
195
205
  "id": "rename-char-code-at",
196
206
  "description": "Renames variables used in charCodeAt calls (str.charCodeAt(index))",
@@ -209,6 +219,27 @@
209
219
  "diffReductionImpact": 0.00003774938185385768,
210
220
  "recommended": true,
211
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)."
212
243
  }
213
244
  ],
214
245
  "presetStats": {