miniread 1.20.0 → 1.21.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.
@@ -129,6 +129,10 @@ const manifestData = {
129
129
  evaluatedAt: "2026-01-23T10:29:23.279Z",
130
130
  notes: "Measured with baseline none: 0.00%. Added to recommended for readability.",
131
131
  },
132
+ "rename-typeof-variables": {
133
+ diffReductionImpact: 0,
134
+ recommended: true,
135
+ },
132
136
  "rename-use-reference-guards": {
133
137
  diffReductionImpact: 0,
134
138
  recommended: false,
@@ -21,6 +21,7 @@ import { renamePromiseExecutorParametersTransform } from "../rename-promise-exec
21
21
  import { renameReplaceChildParametersTransform } from "../rename-replace-child-parameters/rename-replace-child-parameters-transform.js";
22
22
  import { renameThisAliasesTransform } from "../rename-this-aliases/rename-this-aliases-transform.js";
23
23
  import { renameTimeoutIdsTransform } from "../rename-timeout-ids/rename-timeout-ids-transform.js";
24
+ import { renameTypeofVariablesTransform } from "../rename-typeof-variables/rename-typeof-variables-transform.js";
24
25
  import { renameUseReferenceGuardsTransform } from "../rename-use-reference-guards/rename-use-reference-guards-transform.js";
25
26
  import { renameUseReferenceGuardsV2Transform } from "../rename-use-reference-guards-v2/rename-use-reference-guards-v2-transform.js";
26
27
  import { simplifyBooleanNegationsTransform } from "../simplify-boolean-negations/simplify-boolean-negations-transform.js";
@@ -47,6 +48,7 @@ export const transformRegistry = {
47
48
  [renameReplaceChildParametersTransform.id]: renameReplaceChildParametersTransform,
48
49
  [renameThisAliasesTransform.id]: renameThisAliasesTransform,
49
50
  [renameTimeoutIdsTransform.id]: renameTimeoutIdsTransform,
51
+ [renameTypeofVariablesTransform.id]: renameTypeofVariablesTransform,
50
52
  [renameUseReferenceGuardsTransform.id]: renameUseReferenceGuardsTransform,
51
53
  [renameUseReferenceGuardsV2Transform.id]: renameUseReferenceGuardsV2Transform,
52
54
  [simplifyBooleanNegationsTransform.id]: simplifyBooleanNegationsTransform,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "recommended": {
3
- "diffReductionImpact": 0.003690002076215948,
4
- "notes": "Measured with baseline none: 0.37%."
3
+ "diffReductionImpact": 0.001470172462538888,
4
+ "notes": "Measured with baseline none: 0.15%."
5
5
  }
6
6
  }
@@ -0,0 +1,4 @@
1
+ {
2
+ "diffReductionImpact": 0,
3
+ "recommended": true
4
+ }
@@ -0,0 +1,2 @@
1
+ import { type Transform } from "../../core/types.js";
2
+ export declare const renameTypeofVariablesTransform: Transform;
@@ -0,0 +1,132 @@
1
+ import { createRequire } from "node:module";
2
+ import { isArrayPattern, isAssignmentPattern, isClassDeclaration, isExportNamedDeclaration, isExportSpecifier, isFunctionDeclaration, isIdentifier, isObjectPattern, isObjectProperty, isRestElement, isVariableDeclaration, } from "@babel/types";
3
+ import { isStableRenamed, RenameGroup } from "../../core/stable-naming.js";
4
+ import { getFilesToProcess, } from "../../core/types.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
+ const BASE_NAME = "type";
9
+ const addBindingNamesFromNode = (node, out) => {
10
+ const babelNode = node;
11
+ if (!babelNode)
12
+ return;
13
+ if (babelNode.type === "VoidPattern")
14
+ return;
15
+ if (isIdentifier(babelNode)) {
16
+ out.add(babelNode.name);
17
+ return;
18
+ }
19
+ if (isObjectPattern(babelNode)) {
20
+ for (const property of babelNode.properties) {
21
+ if (isRestElement(property)) {
22
+ addBindingNamesFromNode(property.argument, out);
23
+ continue;
24
+ }
25
+ if (!isObjectProperty(property))
26
+ continue;
27
+ addBindingNamesFromNode(property.value, out);
28
+ }
29
+ return;
30
+ }
31
+ if (isArrayPattern(babelNode)) {
32
+ for (const element of babelNode.elements) {
33
+ addBindingNamesFromNode(element, out);
34
+ }
35
+ return;
36
+ }
37
+ if (isRestElement(babelNode)) {
38
+ addBindingNamesFromNode(babelNode.argument, out);
39
+ return;
40
+ }
41
+ if (isAssignmentPattern(babelNode)) {
42
+ addBindingNamesFromNode(babelNode.left, out);
43
+ }
44
+ };
45
+ const collectExportedNames = (program) => {
46
+ const exportedNames = new Set();
47
+ for (const statement of program.body) {
48
+ if (!isExportNamedDeclaration(statement))
49
+ continue;
50
+ const declaration = statement.declaration;
51
+ if (declaration) {
52
+ if (isVariableDeclaration(declaration)) {
53
+ for (const declarator of declaration.declarations) {
54
+ addBindingNamesFromNode(declarator.id, exportedNames);
55
+ }
56
+ }
57
+ else if (isFunctionDeclaration(declaration) ||
58
+ isClassDeclaration(declaration)) {
59
+ const id = declaration.id;
60
+ if (!id)
61
+ continue;
62
+ exportedNames.add(id.name);
63
+ }
64
+ }
65
+ for (const specifier of statement.specifiers) {
66
+ if (!isExportSpecifier(specifier))
67
+ continue;
68
+ const local = specifier.local;
69
+ if (!isIdentifier(local))
70
+ continue;
71
+ exportedNames.add(local.name);
72
+ }
73
+ }
74
+ return exportedNames;
75
+ };
76
+ export const renameTypeofVariablesTransform = {
77
+ id: "rename-typeof-variables",
78
+ description: "Renames variables assigned from typeof expressions to $type (stable when unique) or type/type2/...",
79
+ scope: "file",
80
+ parallelizable: true,
81
+ transform(context) {
82
+ let nodesVisited = 0;
83
+ let transformationsApplied = 0;
84
+ for (const fileInfo of getFilesToProcess(context)) {
85
+ const group = new RenameGroup();
86
+ const exportedNames = collectExportedNames(fileInfo.ast.program);
87
+ traverse(fileInfo.ast, {
88
+ VariableDeclarator(path) {
89
+ nodesVisited++;
90
+ const id = path.node.id;
91
+ if (id.type !== "Identifier")
92
+ return;
93
+ const init = path.node.init;
94
+ if (!init)
95
+ return;
96
+ if (init.type !== "UnaryExpression")
97
+ return;
98
+ if (init.operator !== "typeof")
99
+ return;
100
+ const currentName = id.name;
101
+ // Skip already-stable names
102
+ if (isStableRenamed(currentName))
103
+ return;
104
+ // Skip non-minified names (more than 1 character)
105
+ if (currentName.length > 1)
106
+ return;
107
+ const binding = path.scope.getBinding(currentName);
108
+ if (!binding)
109
+ return;
110
+ // Skip reassigned variables (name would be misleading)
111
+ if (!binding.constant)
112
+ return;
113
+ // Skip exported names at program scope
114
+ if (binding.scope.block.type === "Program" &&
115
+ exportedNames.has(currentName)) {
116
+ return;
117
+ }
118
+ group.add({
119
+ scope: binding.scope,
120
+ currentName,
121
+ baseName: BASE_NAME,
122
+ });
123
+ },
124
+ });
125
+ transformationsApplied += group.apply();
126
+ }
127
+ return Promise.resolve({
128
+ nodesVisited,
129
+ transformationsApplied,
130
+ });
131
+ },
132
+ };
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.20.0",
5
+ "version": "1.21.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",