miniread 1.12.0 → 1.14.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 renameComparisonFlagsTransform: Transform;
@@ -0,0 +1,86 @@
1
+ import { createRequire } from "node:module";
2
+ import { RenameGroup, isStableRenamed } 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 renameComparisonFlagsTransform = {
8
+ id: "rename-comparison-flags",
9
+ description: "Rename boolean variables assigned from string comparisons to descriptive flag names",
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 renameGroup = new RenameGroup();
17
+ traverse(fileInfo.ast, {
18
+ VariableDeclarator(path) {
19
+ nodesVisited++;
20
+ const { id, init } = path.node;
21
+ if (!init || id.type !== "Identifier") {
22
+ return;
23
+ }
24
+ if (init.type !== "BinaryExpression" ||
25
+ (init.operator !== "===" &&
26
+ init.operator !== "!==" &&
27
+ init.operator !== "==" &&
28
+ init.operator !== "!=")) {
29
+ return;
30
+ }
31
+ let stringValue;
32
+ if (init.right.type === "StringLiteral" &&
33
+ init.left.type === "Identifier") {
34
+ stringValue = init.right.value;
35
+ }
36
+ else if (init.left.type === "StringLiteral" &&
37
+ init.right.type === "Identifier") {
38
+ stringValue = init.left.value;
39
+ }
40
+ if (stringValue === undefined) {
41
+ return;
42
+ }
43
+ const currentName = id.name;
44
+ if (isStableRenamed(currentName)) {
45
+ return;
46
+ }
47
+ const operator = init.operator;
48
+ const parts = stringValue.split(/[^\dA-Za-z]+/u).filter(Boolean);
49
+ const capitalizedValue = parts.length === 0
50
+ ? stringValue === ""
51
+ ? "EmptyString"
52
+ : "Flag"
53
+ : parts
54
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
55
+ .join("");
56
+ let baseName;
57
+ switch (operator) {
58
+ case "===": {
59
+ baseName = `is${capitalizedValue}`;
60
+ break;
61
+ }
62
+ case "!==": {
63
+ baseName = `isNot${capitalizedValue}`;
64
+ break;
65
+ }
66
+ case "==": {
67
+ baseName = `equals${capitalizedValue}`;
68
+ break;
69
+ }
70
+ case "!=": {
71
+ baseName = `notEquals${capitalizedValue}`;
72
+ break;
73
+ }
74
+ }
75
+ renameGroup.add({
76
+ scope: path.scope,
77
+ currentName,
78
+ baseName,
79
+ });
80
+ },
81
+ });
82
+ transformationsApplied += renameGroup.apply();
83
+ }
84
+ return Promise.resolve({ nodesVisited, transformationsApplied });
85
+ },
86
+ };
@@ -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
+ };
@@ -4,6 +4,7 @@ import { expandSequenceExpressionsV4Transform } from "./expand-sequence-expressi
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
6
  import { renameCatchParametersTransform } from "./rename-catch-parameters/rename-catch-parameters-transform.js";
7
+ import { renameComparisonFlagsTransform } from "./rename-comparison-flags/rename-comparison-flags-transform.js";
7
8
  import { renameDestructuredAliasesTransform } from "./rename-destructured-aliases/rename-destructured-aliases-transform.js";
8
9
  import { renameEventParametersTransform } from "./rename-event-parameters/rename-event-parameters-transform.js";
9
10
  import { renameLoopIndexVariablesTransform } from "./rename-loop-index-variables/rename-loop-index-variables-transform.js";
@@ -16,6 +17,7 @@ import { renameTimeoutIdsTransform } from "./rename-timeout-ids/rename-timeout-i
16
17
  import { renameUseReferenceGuardsTransform } from "./rename-use-reference-guards/rename-use-reference-guards-transform.js";
17
18
  import { renameUseReferenceGuardsV2Transform } from "./rename-use-reference-guards-v2/rename-use-reference-guards-v2-transform.js";
18
19
  import { splitVariableDeclarationsTransform } from "./split-variable-declarations/split-variable-declarations-transform.js";
20
+ import { renameParametersToMatchPropertiesTransform } from "./rename-parameters-to-match-properties/rename-parameters-to-match-properties-transform.js";
19
21
  export const transformRegistry = {
20
22
  [expandBooleanLiteralsTransform.id]: expandBooleanLiteralsTransform,
21
23
  [expandSpecialNumberLiteralsTransform.id]: expandSpecialNumberLiteralsTransform,
@@ -23,6 +25,7 @@ export const transformRegistry = {
23
25
  [expandSequenceExpressionsV5Transform.id]: expandSequenceExpressionsV5Transform,
24
26
  [expandUndefinedLiteralsTransform.id]: expandUndefinedLiteralsTransform,
25
27
  [renameCatchParametersTransform.id]: renameCatchParametersTransform,
28
+ [renameComparisonFlagsTransform.id]: renameComparisonFlagsTransform,
26
29
  [renameDestructuredAliasesTransform.id]: renameDestructuredAliasesTransform,
27
30
  [renameEventParametersTransform.id]: renameEventParametersTransform,
28
31
  [renameLoopIndexVariablesTransform.id]: renameLoopIndexVariablesTransform,
@@ -35,5 +38,6 @@ export const transformRegistry = {
35
38
  [renameUseReferenceGuardsTransform.id]: renameUseReferenceGuardsTransform,
36
39
  [renameUseReferenceGuardsV2Transform.id]: renameUseReferenceGuardsV2Transform,
37
40
  [splitVariableDeclarationsTransform.id]: splitVariableDeclarationsTransform,
41
+ [renameParametersToMatchPropertiesTransform.id]: renameParametersToMatchPropertiesTransform,
38
42
  };
39
43
  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.12.0",
5
+ "version": "1.14.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",
@@ -141,6 +141,16 @@
141
141
  "evaluatedAt": "2026-01-23T10:29:23.279Z",
142
142
  "notes": "Measured with baseline none: 0.00%. Added to recommended for readability."
143
143
  },
144
+ {
145
+ "id": "rename-comparison-flags",
146
+ "description": "Rename boolean variables assigned from string comparisons to descriptive flag names",
147
+ "scope": "file",
148
+ "parallelizable": true,
149
+ "diffReductionImpact": 0,
150
+ "recommended": true,
151
+ "evaluatedAt": "2026-01-23T17:54:14.000Z",
152
+ "notes": "Measured with baseline none: 0%. Improves readability by renaming minified variables like 'saA === \"darwin\"' to '$isDarwin'."
153
+ },
144
154
  {
145
155
  "id": "split-variable-declarations",
146
156
  "description": "Splits multi-declarator variable declarations into separate statements",
@@ -180,6 +190,15 @@
180
190
  "recommended": true,
181
191
  "evaluatedAt": "2026-01-23T08:17:45.579Z",
182
192
  "notes": "Auto-added by evaluation script."
193
+ },
194
+ {
195
+ "id": "rename-parameters-to-match-properties",
196
+ "description": "Renames parameters that are assigned to object properties of the same name",
197
+ "scope": "file",
198
+ "parallelizable": true,
199
+ "diffReductionImpact": 0.00003774938185385768,
200
+ "recommended": true,
201
+ "notes": "Added manually based on high-confidence heuristic."
183
202
  }
184
203
  ],
185
204
  "presetStats": {