miniread 1.13.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.
- package/dist/transforms/rename-parameters-to-match-properties/rename-parameters-to-match-properties-transform.d.ts +2 -0
- package/dist/transforms/rename-parameters-to-match-properties/rename-parameters-to-match-properties-transform.js +98 -0
- package/dist/transforms/transform-registry.js +2 -0
- package/package.json +1 -1
- package/transform-manifest.json +9 -0
|
@@ -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,6 +17,7 @@ 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 { renameParametersToMatchPropertiesTransform } from "./rename-parameters-to-match-properties/rename-parameters-to-match-properties-transform.js";
|
|
20
21
|
export const transformRegistry = {
|
|
21
22
|
[expandBooleanLiteralsTransform.id]: expandBooleanLiteralsTransform,
|
|
22
23
|
[expandSpecialNumberLiteralsTransform.id]: expandSpecialNumberLiteralsTransform,
|
|
@@ -37,5 +38,6 @@ export const transformRegistry = {
|
|
|
37
38
|
[renameUseReferenceGuardsTransform.id]: renameUseReferenceGuardsTransform,
|
|
38
39
|
[renameUseReferenceGuardsV2Transform.id]: renameUseReferenceGuardsV2Transform,
|
|
39
40
|
[splitVariableDeclarationsTransform.id]: splitVariableDeclarationsTransform,
|
|
41
|
+
[renameParametersToMatchPropertiesTransform.id]: renameParametersToMatchPropertiesTransform,
|
|
40
42
|
};
|
|
41
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.
|
|
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",
|
package/transform-manifest.json
CHANGED
|
@@ -190,6 +190,15 @@
|
|
|
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-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."
|
|
193
202
|
}
|
|
194
203
|
],
|
|
195
204
|
"presetStats": {
|