miniread 1.13.0 → 1.15.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 renameCharCodeAtTransform: Transform;
@@ -0,0 +1,70 @@
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
+ export const renameCharCodeAtTransform = {
8
+ id: "rename-char-code-at",
9
+ description: "Renames variables used in charCodeAt calls (str.charCodeAt(index))",
10
+ scope: "file",
11
+ parallelizable: true,
12
+ transform(context) {
13
+ let nodesVisited = 0;
14
+ let transformationsApplied = 0;
15
+ for (const fileInfo of getFilesToProcess(context)) {
16
+ const group = new RenameGroup();
17
+ const processedBindings = new Set();
18
+ traverse(fileInfo.ast, {
19
+ CallExpression(path) {
20
+ nodesVisited++;
21
+ const node = path.node;
22
+ if (node.callee.type !== "MemberExpression")
23
+ return;
24
+ const callee = node.callee;
25
+ if (callee.computed)
26
+ return;
27
+ if (callee.property.type !== "Identifier")
28
+ return;
29
+ if (callee.property.name !== "charCodeAt")
30
+ return;
31
+ // Rename object (str)
32
+ if (callee.object.type === "Identifier") {
33
+ const currentName = callee.object.name;
34
+ const binding = path.scope.getBinding(currentName);
35
+ if (binding &&
36
+ !processedBindings.has(binding) &&
37
+ !isStableRenamed(currentName)) {
38
+ processedBindings.add(binding);
39
+ group.add({
40
+ scope: binding.scope,
41
+ currentName,
42
+ baseName: "str",
43
+ });
44
+ }
45
+ }
46
+ // Rename first argument (index)
47
+ if (node.arguments.length > 0) {
48
+ const firstArgument = node.arguments[0];
49
+ if (firstArgument?.type === "Identifier") {
50
+ const currentName = firstArgument.name;
51
+ const binding = path.scope.getBinding(currentName);
52
+ if (binding &&
53
+ !processedBindings.has(binding) &&
54
+ !isStableRenamed(currentName)) {
55
+ processedBindings.add(binding);
56
+ group.add({
57
+ scope: binding.scope,
58
+ currentName,
59
+ baseName: "index",
60
+ });
61
+ }
62
+ }
63
+ }
64
+ },
65
+ });
66
+ transformationsApplied += group.apply();
67
+ }
68
+ return Promise.resolve({ nodesVisited, transformationsApplied });
69
+ },
70
+ };
@@ -0,0 +1,2 @@
1
+ import { type Transform } from "../../core/types.js";
2
+ export declare const renameParametersToMatchPropertiesTransform: Transform;
@@ -0,0 +1,98 @@
1
+ import { createRequire } from "node:module";
2
+ import { isIdentifierName, isKeyword, isStrictBindReservedWord, } from "@babel/helper-validator-identifier";
3
+ import { getFilesToProcess, } from "../../core/types.js";
4
+ import { isStableRenamed } from "../../core/stable-naming.js";
5
+ const require = createRequire(import.meta.url);
6
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
7
+ const traverse = require("@babel/traverse").default;
8
+ export const renameParametersToMatchPropertiesTransform = {
9
+ id: "rename-parameters-to-match-properties",
10
+ description: "Renames parameters that are assigned to object properties of the same name",
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
+ Function(path) {
19
+ nodesVisited++;
20
+ const parameters = path.get("params");
21
+ for (const parameterPath of parameters) {
22
+ if (!parameterPath.isIdentifier())
23
+ continue;
24
+ const currentName = parameterPath.node.name;
25
+ if (isStableRenamed(currentName))
26
+ continue;
27
+ const binding = path.scope.getBinding(currentName);
28
+ if (!binding)
29
+ continue;
30
+ let candidateName;
31
+ let isConsistent = true;
32
+ // Analyze all references to this parameter
33
+ for (const referencePath of binding.referencePaths) {
34
+ const parent = referencePath.parentPath;
35
+ // Check if reference is a value in an ObjectProperty
36
+ if (parent?.isObjectProperty()) {
37
+ // Ensure the param is the value, not the key (unless computed, but standard key is non-computed identifier)
38
+ // Note: referencePath.node is the specific node instance.
39
+ if (parent.node.value !== referencePath.node)
40
+ continue;
41
+ // Get the key name
42
+ const key = parent.node.key;
43
+ if (key.type === "Identifier" && !parent.node.computed) {
44
+ const propertyName = key.name;
45
+ if (candidateName === undefined) {
46
+ candidateName = propertyName;
47
+ }
48
+ else if (candidateName !== propertyName) {
49
+ // Conflicting property names (e.g. { x: a, y: a })
50
+ isConsistent = false;
51
+ break;
52
+ }
53
+ }
54
+ }
55
+ }
56
+ if (candidateName) {
57
+ // Check global references (conservative: if file uses 'console', don't use 'console')
58
+ const programScope = path.scope.getProgramParent();
59
+ if (Object.hasOwn(programScope.globals, candidateName)) {
60
+ isConsistent = false;
61
+ }
62
+ // Check for nested scope shadowing
63
+ // If we rename `a` -> `b`, and a nested scope has `b` bound,
64
+ // and we use `a` in that nested scope, it will become `b` (referring to inner `b`).
65
+ const wouldBeShadowed = binding.referencePaths.some((referencePath) => referencePath.scope !== path.scope &&
66
+ referencePath.scope.hasBinding(candidateName));
67
+ if (wouldBeShadowed) {
68
+ isConsistent = false;
69
+ }
70
+ }
71
+ // Constraints:
72
+ // 1. New name must be valid identifier
73
+ // 2. New name must not be a keyword/reserved word
74
+ // 3. New name must be longer than current, OR current is very short (<= 2 chars)
75
+ // 4. New name must be different from current
76
+ // 5. New name must not already exist in the scope
77
+ if (isConsistent &&
78
+ candidateName &&
79
+ candidateName !== currentName &&
80
+ (candidateName.length > currentName.length ||
81
+ currentName.length <= 2) &&
82
+ isIdentifierName(candidateName) &&
83
+ !isKeyword(candidateName) &&
84
+ !isStrictBindReservedWord(candidateName, true) &&
85
+ !path.scope.hasBinding(candidateName)) {
86
+ path.scope.rename(currentName, candidateName);
87
+ transformationsApplied++;
88
+ }
89
+ }
90
+ },
91
+ });
92
+ }
93
+ return Promise.resolve({
94
+ nodesVisited,
95
+ transformationsApplied,
96
+ });
97
+ },
98
+ };
@@ -17,7 +17,10 @@ import { renameTimeoutIdsTransform } from "./rename-timeout-ids/rename-timeout-i
17
17
  import { renameUseReferenceGuardsTransform } from "./rename-use-reference-guards/rename-use-reference-guards-transform.js";
18
18
  import { renameUseReferenceGuardsV2Transform } from "./rename-use-reference-guards-v2/rename-use-reference-guards-v2-transform.js";
19
19
  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";
20
22
  export const transformRegistry = {
23
+ [renameCharCodeAtTransform.id]: renameCharCodeAtTransform,
21
24
  [expandBooleanLiteralsTransform.id]: expandBooleanLiteralsTransform,
22
25
  [expandSpecialNumberLiteralsTransform.id]: expandSpecialNumberLiteralsTransform,
23
26
  [expandSequenceExpressionsV4Transform.id]: expandSequenceExpressionsV4Transform,
@@ -37,5 +40,6 @@ export const transformRegistry = {
37
40
  [renameUseReferenceGuardsTransform.id]: renameUseReferenceGuardsTransform,
38
41
  [renameUseReferenceGuardsV2Transform.id]: renameUseReferenceGuardsV2Transform,
39
42
  [splitVariableDeclarationsTransform.id]: splitVariableDeclarationsTransform,
43
+ [renameParametersToMatchPropertiesTransform.id]: renameParametersToMatchPropertiesTransform,
40
44
  };
41
45
  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.13.0",
5
+ "version": "1.15.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",
@@ -190,6 +190,25 @@
190
190
  "recommended": true,
191
191
  "evaluatedAt": "2026-01-23T08:17:45.579Z",
192
192
  "notes": "Auto-added by evaluation script."
193
+ },
194
+ {
195
+ "id": "rename-char-code-at",
196
+ "description": "Renames variables used in charCodeAt calls (str.charCodeAt(index))",
197
+ "scope": "file",
198
+ "parallelizable": true,
199
+ "diffReductionImpact": 0,
200
+ "recommended": true,
201
+ "evaluatedAt": "2026-01-23T18:00:00.000Z",
202
+ "notes": "Measured with baseline none: 0.00%. Renames variables used in charCodeAt to improve readability."
203
+ },
204
+ {
205
+ "id": "rename-parameters-to-match-properties",
206
+ "description": "Renames parameters that are assigned to object properties of the same name",
207
+ "scope": "file",
208
+ "parallelizable": true,
209
+ "diffReductionImpact": 0.00003774938185385768,
210
+ "recommended": true,
211
+ "notes": "Added manually based on high-confidence heuristic."
193
212
  }
194
213
  ],
195
214
  "presetStats": {