miniread 1.0.0 → 1.2.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/README.md +2 -2
- package/dist/scripts/evaluate/check-expected-evaluations.d.ts +17 -0
- package/dist/scripts/evaluate/check-expected-evaluations.js +35 -0
- package/dist/scripts/evaluate/check-snapshots.d.ts +14 -0
- package/dist/scripts/evaluate/check-snapshots.js +111 -0
- package/dist/scripts/evaluate/clone-metrics-report.d.ts +2 -0
- package/dist/scripts/evaluate/clone-metrics-report.js +6 -0
- package/dist/scripts/evaluate/create-evaluate-command.js +5 -20
- package/dist/scripts/evaluate/evaluate-help-text.d.ts +1 -0
- package/dist/scripts/evaluate/evaluate-help-text.js +21 -0
- package/dist/scripts/evaluate/pair-evaluator.d.ts +1 -0
- package/dist/scripts/evaluate/pair-evaluator.js +14 -3
- package/dist/scripts/evaluate/parse-evaluate-cli-options.d.ts +2 -0
- package/dist/scripts/evaluate/parse-evaluate-cli-options.js +3 -1
- package/dist/scripts/evaluate/parse-transform-manifest.d.ts +2 -2
- package/dist/scripts/evaluate/parse-transform-manifest.js +2 -2
- package/dist/scripts/evaluate/relative-patch-path.d.ts +7 -0
- package/dist/scripts/evaluate/relative-patch-path.js +22 -0
- package/dist/scripts/evaluate/run-check-mode.d.ts +8 -0
- package/dist/scripts/evaluate/run-check-mode.js +116 -0
- package/dist/scripts/evaluate/run-evaluate-cli.js +11 -3
- package/dist/scripts/evaluate/run-evaluations.js +1 -0
- package/dist/scripts/evaluate/transform-content.d.ts +14 -0
- package/dist/scripts/evaluate/transform-content.js +31 -0
- package/dist/scripts/evaluate/transform-manifest.d.ts +0 -2
- package/dist/scripts/evaluate/transform-manifest.js +16 -35
- package/dist/transforms/expand-return-sequence/expand-return-sequence-transform.d.ts +2 -0
- package/dist/transforms/expand-return-sequence/expand-return-sequence-transform.js +91 -0
- package/dist/transforms/expand-sequence-expressions/expand-expression-statement-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions/expand-expression-statement-sequence.js +57 -0
- package/dist/transforms/expand-sequence-expressions/expand-sequence-expressions-transform.d.ts +2 -0
- package/dist/transforms/expand-sequence-expressions/expand-sequence-expressions-transform.js +34 -0
- package/dist/transforms/expand-sequence-expressions/expand-variable-declaration-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions/expand-variable-declaration-sequence.js +93 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-expression-statement-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-expression-statement-sequence.js +55 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-return-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-return-sequence.js +86 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-sequence-expressions-v2-transform.d.ts +2 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-sequence-expressions-v2-transform.js +41 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-variable-declaration-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions-v2/expand-variable-declaration-sequence.js +93 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-expression-statement-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-expression-statement-sequence.js +64 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-return-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-return-sequence.js +91 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-sequence-expressions-v3-transform.d.ts +2 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-sequence-expressions-v3-transform.js +48 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-throw-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-throw-sequence.js +101 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-variable-declaration-sequence.d.ts +3 -0
- package/dist/transforms/expand-sequence-expressions-v3/expand-variable-declaration-sequence.js +99 -0
- package/dist/transforms/expand-throw-sequence/expand-throw-sequence-transform.d.ts +2 -0
- package/dist/transforms/expand-throw-sequence/expand-throw-sequence-transform.js +117 -0
- package/dist/transforms/rename-binding/get-target-name.d.ts +4 -0
- package/dist/transforms/rename-binding/get-target-name.js +25 -0
- package/dist/transforms/rename-binding/is-valid-binding-identifier.d.ts +1 -0
- package/dist/transforms/rename-binding/is-valid-binding-identifier.js +10 -0
- package/dist/transforms/rename-destructured-aliases/binding-context.d.ts +7 -0
- package/dist/transforms/rename-destructured-aliases/binding-context.js +83 -0
- package/dist/transforms/rename-destructured-aliases/rename-destructured-aliases-transform.d.ts +2 -0
- package/dist/transforms/rename-destructured-aliases/rename-destructured-aliases-transform.js +118 -0
- package/dist/transforms/rename-promise-executor-parameters/is-valid-binding-identifier.d.ts +1 -0
- package/dist/transforms/rename-promise-executor-parameters/is-valid-binding-identifier.js +10 -0
- package/dist/transforms/rename-promise-executor-parameters/promise-executor-heuristics.d.ts +6 -0
- package/dist/transforms/rename-promise-executor-parameters/promise-executor-heuristics.js +88 -0
- package/dist/transforms/rename-promise-executor-parameters/rename-binding-if-safe.d.ts +3 -0
- package/dist/transforms/rename-promise-executor-parameters/rename-binding-if-safe.js +40 -0
- package/dist/transforms/rename-promise-executor-parameters/rename-promise-executor-parameters-transform.d.ts +2 -0
- package/dist/transforms/rename-promise-executor-parameters/rename-promise-executor-parameters-transform.js +76 -0
- package/dist/transforms/rename-timeout-ids/rename-timeout-ids-transform.d.ts +2 -0
- package/dist/transforms/rename-timeout-ids/rename-timeout-ids-transform.js +86 -0
- package/dist/transforms/rename-use-reference-guards/get-member-expression-for-reference.d.ts +3 -0
- package/dist/transforms/rename-use-reference-guards/get-member-expression-for-reference.js +10 -0
- package/dist/transforms/rename-use-reference-guards/get-reference-usage.d.ts +9 -0
- package/dist/transforms/rename-use-reference-guards/get-reference-usage.js +47 -0
- package/dist/transforms/rename-use-reference-guards/get-target-name.d.ts +2 -0
- package/dist/transforms/rename-use-reference-guards/get-target-name.js +23 -0
- package/dist/transforms/rename-use-reference-guards/is-use-reference-false-initializer.d.ts +2 -0
- package/dist/transforms/rename-use-reference-guards/is-use-reference-false-initializer.js +25 -0
- package/dist/transforms/rename-use-reference-guards/is-valid-binding-identifier.d.ts +1 -0
- package/dist/transforms/rename-use-reference-guards/is-valid-binding-identifier.js +10 -0
- package/dist/transforms/rename-use-reference-guards/rename-use-reference-guards-transform.d.ts +3 -0
- package/dist/transforms/rename-use-reference-guards/rename-use-reference-guards-transform.js +55 -0
- package/dist/transforms/split-variable-declarations/create-variable-declaration.d.ts +2 -0
- package/dist/transforms/split-variable-declarations/create-variable-declaration.js +28 -0
- package/dist/transforms/split-variable-declarations/is-safe-to-split-variable-declaration.d.ts +3 -0
- package/dist/transforms/split-variable-declarations/is-safe-to-split-variable-declaration.js +52 -0
- package/dist/transforms/split-variable-declarations/split-variable-declarations-transform.d.ts +2 -0
- package/dist/transforms/split-variable-declarations/split-variable-declarations-transform.js +81 -0
- package/dist/transforms/transform-presets.js +2 -2
- package/dist/transforms/transform-registry.js +20 -4
- package/package.json +29 -23
- package/transform-manifest.json +110 -30
- package/dist/transforms/add-prefix/add-prefix-transform.d.ts +0 -2
- package/dist/transforms/add-prefix/add-prefix-transform.js +0 -40
- package/dist/transforms/add-suffix/add-suffix-transform.d.ts +0 -2
- package/dist/transforms/add-suffix/add-suffix-transform.js +0 -40
package/dist/transforms/expand-sequence-expressions-v2/expand-variable-declaration-sequence.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
4
|
+
const t = require("@babel/types");
|
|
5
|
+
const createExpressionStatement = (expression) => {
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
7
|
+
return t.expressionStatement(
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
9
|
+
t.cloneNode(expression, true));
|
|
10
|
+
};
|
|
11
|
+
const createVariableDeclarationStatement = (kind, declarator) => {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
13
|
+
return t.variableDeclaration(kind, [declarator]);
|
|
14
|
+
};
|
|
15
|
+
const createVariableDeclarator = (id, init) => {
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
17
|
+
return t.variableDeclarator(
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
19
|
+
t.cloneNode(id, true), init
|
|
20
|
+
? // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
21
|
+
t.cloneNode(init, true)
|
|
22
|
+
: undefined);
|
|
23
|
+
};
|
|
24
|
+
const isSupportedStatementContainer = (path) => {
|
|
25
|
+
const parentPath = path.parentPath;
|
|
26
|
+
if (!parentPath)
|
|
27
|
+
return false;
|
|
28
|
+
return (parentPath.isProgram() ||
|
|
29
|
+
parentPath.isBlockStatement() ||
|
|
30
|
+
parentPath.isSwitchCase());
|
|
31
|
+
};
|
|
32
|
+
const isDirectiveProloguePosition = (path) => {
|
|
33
|
+
const parentPath = path.parentPath;
|
|
34
|
+
if (!parentPath)
|
|
35
|
+
return false;
|
|
36
|
+
if (!parentPath.isProgram() && !parentPath.isBlockStatement())
|
|
37
|
+
return false;
|
|
38
|
+
const siblings = parentPath.get("body");
|
|
39
|
+
if (!Array.isArray(siblings))
|
|
40
|
+
return false;
|
|
41
|
+
const index = siblings.findIndex((sibling) => sibling.node === path.node);
|
|
42
|
+
if (index === -1)
|
|
43
|
+
return false;
|
|
44
|
+
for (let bodyIndex = 0; bodyIndex < index; bodyIndex++) {
|
|
45
|
+
const sibling = siblings[bodyIndex];
|
|
46
|
+
if (!sibling)
|
|
47
|
+
return false;
|
|
48
|
+
if (!sibling.isExpressionStatement())
|
|
49
|
+
return false;
|
|
50
|
+
if (sibling.node.expression.type !== "StringLiteral")
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
};
|
|
55
|
+
export const tryExpandVariableDeclarationSequenceInitializers = (path) => {
|
|
56
|
+
if (!isSupportedStatementContainer(path))
|
|
57
|
+
return false;
|
|
58
|
+
if (path.node.kind !== "var" && path.node.declarations.length > 1) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (isDirectiveProloguePosition(path)) {
|
|
62
|
+
const wouldInsertStringLiteral = path.node.declarations.some((declarator) => declarator.init?.type === "SequenceExpression" &&
|
|
63
|
+
declarator.init.expressions[0]?.type === "StringLiteral");
|
|
64
|
+
if (wouldInsertStringLiteral)
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const hasAnySequence = path.node.declarations.some((declarator) => declarator.init?.type === "SequenceExpression" &&
|
|
68
|
+
declarator.init.expressions.length >= 2);
|
|
69
|
+
if (!hasAnySequence)
|
|
70
|
+
return false;
|
|
71
|
+
const kind = path.node.kind;
|
|
72
|
+
const statements = [];
|
|
73
|
+
for (const declarator of path.node.declarations) {
|
|
74
|
+
const init = declarator.init;
|
|
75
|
+
if (init?.type !== "SequenceExpression") {
|
|
76
|
+
statements.push(createVariableDeclarationStatement(kind, declarator));
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const leadingExpressions = init.expressions.slice(0, -1);
|
|
80
|
+
const lastExpression = init.expressions.at(-1);
|
|
81
|
+
if (!lastExpression) {
|
|
82
|
+
statements.push(createVariableDeclarationStatement(kind, declarator));
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
for (const expression of leadingExpressions) {
|
|
86
|
+
statements.push(createExpressionStatement(expression));
|
|
87
|
+
}
|
|
88
|
+
const newDeclarator = createVariableDeclarator(declarator.id, lastExpression);
|
|
89
|
+
statements.push(createVariableDeclarationStatement(kind, newDeclarator));
|
|
90
|
+
}
|
|
91
|
+
path.replaceWithMultiple(statements);
|
|
92
|
+
return true;
|
|
93
|
+
};
|
package/dist/transforms/expand-sequence-expressions-v3/expand-expression-statement-sequence.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
4
|
+
const t = require("@babel/types");
|
|
5
|
+
const isSupportedStatementContainer = (path) => {
|
|
6
|
+
const parentPath = path.parentPath;
|
|
7
|
+
if (!parentPath)
|
|
8
|
+
return false;
|
|
9
|
+
return (parentPath.isProgram() ||
|
|
10
|
+
parentPath.isBlockStatement() ||
|
|
11
|
+
parentPath.isStaticBlock() ||
|
|
12
|
+
parentPath.isSwitchCase());
|
|
13
|
+
};
|
|
14
|
+
const isDirectiveProloguePosition = (path) => {
|
|
15
|
+
const parentPath = path.parentPath;
|
|
16
|
+
if (parentPath.isBlockStatement()) {
|
|
17
|
+
const grandParent = parentPath.parentPath;
|
|
18
|
+
if (parentPath.key !== "body")
|
|
19
|
+
return false;
|
|
20
|
+
if (!grandParent.isFunction())
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
else if (!parentPath.isProgram()) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const siblings = parentPath.get("body");
|
|
27
|
+
if (!Array.isArray(siblings))
|
|
28
|
+
return false;
|
|
29
|
+
const index = siblings.findIndex((sibling) => sibling.node === path.node);
|
|
30
|
+
if (index === -1)
|
|
31
|
+
return false;
|
|
32
|
+
for (let bodyIndex = 0; bodyIndex < index; bodyIndex++) {
|
|
33
|
+
const sibling = siblings[bodyIndex];
|
|
34
|
+
if (!sibling)
|
|
35
|
+
return false;
|
|
36
|
+
if (!sibling.isExpressionStatement())
|
|
37
|
+
return false;
|
|
38
|
+
if (sibling.node.expression.type !== "StringLiteral")
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
};
|
|
43
|
+
export const tryExpandExpressionStatementSequence = (path) => {
|
|
44
|
+
if (!isSupportedStatementContainer(path))
|
|
45
|
+
return false;
|
|
46
|
+
const expression = path.node.expression;
|
|
47
|
+
if (expression.type !== "SequenceExpression")
|
|
48
|
+
return false;
|
|
49
|
+
if (expression.expressions.length < 2)
|
|
50
|
+
return false;
|
|
51
|
+
const firstExpression = expression.expressions[0];
|
|
52
|
+
if (firstExpression?.type === "StringLiteral" &&
|
|
53
|
+
isDirectiveProloguePosition(path)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
const statements = expression.expressions.map((expr) => {
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
58
|
+
return t.expressionStatement(
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
60
|
+
t.cloneNode(expr, true));
|
|
61
|
+
});
|
|
62
|
+
path.replaceWithMultiple(statements);
|
|
63
|
+
return true;
|
|
64
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
4
|
+
const t = require("@babel/types");
|
|
5
|
+
const canReplaceWithMultiple = (path) => {
|
|
6
|
+
return path.parentPath.isBlockStatement() || path.parentPath.isSwitchCase();
|
|
7
|
+
};
|
|
8
|
+
const canWrapInBlock = (path) => {
|
|
9
|
+
const parentPath = path.parentPath;
|
|
10
|
+
if (parentPath.isIfStatement() &&
|
|
11
|
+
(path.key === "consequent" || path.key === "alternate")) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if ((parentPath.isForStatement() ||
|
|
15
|
+
parentPath.isForInStatement() ||
|
|
16
|
+
parentPath.isForOfStatement() ||
|
|
17
|
+
parentPath.isWhileStatement() ||
|
|
18
|
+
parentPath.isDoWhileStatement() ||
|
|
19
|
+
parentPath.isWithStatement() ||
|
|
20
|
+
parentPath.isLabeledStatement()) &&
|
|
21
|
+
path.key === "body") {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
};
|
|
26
|
+
const replaceReturnWithStatements = (path, statements) => {
|
|
27
|
+
if (canReplaceWithMultiple(path)) {
|
|
28
|
+
path.replaceWithMultiple(statements);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if (canWrapInBlock(path)) {
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
33
|
+
path.replaceWith(t.blockStatement(statements));
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
};
|
|
38
|
+
const isDirectiveProloguePosition = (path) => {
|
|
39
|
+
if (!path.parentPath.isBlockStatement())
|
|
40
|
+
return false;
|
|
41
|
+
if (path.parentPath.key !== "body")
|
|
42
|
+
return false;
|
|
43
|
+
if (!path.parentPath.parentPath.isFunction())
|
|
44
|
+
return false;
|
|
45
|
+
const siblings = path.parentPath.get("body");
|
|
46
|
+
if (!Array.isArray(siblings))
|
|
47
|
+
return false;
|
|
48
|
+
const index = siblings.findIndex((sibling) => sibling.node === path.node);
|
|
49
|
+
if (index === -1)
|
|
50
|
+
return false;
|
|
51
|
+
for (let bodyIndex = 0; bodyIndex < index; bodyIndex++) {
|
|
52
|
+
const sibling = siblings[bodyIndex];
|
|
53
|
+
if (!sibling)
|
|
54
|
+
return false;
|
|
55
|
+
if (!sibling.isExpressionStatement())
|
|
56
|
+
return false;
|
|
57
|
+
if (sibling.node.expression.type !== "StringLiteral")
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
export const tryExpandReturnSequence = (path) => {
|
|
63
|
+
if (!canReplaceWithMultiple(path) && !canWrapInBlock(path))
|
|
64
|
+
return false;
|
|
65
|
+
const argument = path.node.argument;
|
|
66
|
+
if (!argument)
|
|
67
|
+
return false;
|
|
68
|
+
if (argument.type !== "SequenceExpression")
|
|
69
|
+
return false;
|
|
70
|
+
if (argument.expressions.length < 2)
|
|
71
|
+
return false;
|
|
72
|
+
const leadingExpressions = argument.expressions.slice(0, -1);
|
|
73
|
+
const lastExpression = argument.expressions.at(-1);
|
|
74
|
+
if (!lastExpression)
|
|
75
|
+
return false;
|
|
76
|
+
const firstExpression = leadingExpressions[0];
|
|
77
|
+
if (firstExpression?.type === "StringLiteral" &&
|
|
78
|
+
isDirectiveProloguePosition(path)) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const statements = leadingExpressions.map((expression) => {
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
83
|
+
return t.expressionStatement(
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
85
|
+
t.cloneNode(expression, true));
|
|
86
|
+
});
|
|
87
|
+
statements.push(
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
89
|
+
t.returnStatement(t.cloneNode(lastExpression, true)));
|
|
90
|
+
return replaceReturnWithStatements(path, statements);
|
|
91
|
+
};
|
package/dist/transforms/expand-sequence-expressions-v3/expand-sequence-expressions-v3-transform.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { tryExpandExpressionStatementSequence } from "./expand-expression-statement-sequence.js";
|
|
3
|
+
import { tryExpandReturnSequence } from "./expand-return-sequence.js";
|
|
4
|
+
import { tryExpandThrowSequence } from "./expand-throw-sequence.js";
|
|
5
|
+
import { tryExpandVariableDeclarationSequenceInitializers } from "./expand-variable-declaration-sequence.js";
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
8
|
+
const traverse = require("@babel/traverse").default;
|
|
9
|
+
export const expandSequenceExpressionsV3Transform = {
|
|
10
|
+
id: "expand-sequence-expressions-v3",
|
|
11
|
+
description: "Expands comma operator sequences in returns, throws, statements, and variable initializers",
|
|
12
|
+
scope: "file",
|
|
13
|
+
parallelizable: true,
|
|
14
|
+
transform(context) {
|
|
15
|
+
const { projectGraph } = context;
|
|
16
|
+
let nodesVisited = 0;
|
|
17
|
+
let transformationsApplied = 0;
|
|
18
|
+
for (const [, fileInfo] of projectGraph.files) {
|
|
19
|
+
traverse(fileInfo.ast, {
|
|
20
|
+
ReturnStatement(path) {
|
|
21
|
+
nodesVisited++;
|
|
22
|
+
if (!tryExpandReturnSequence(path))
|
|
23
|
+
return;
|
|
24
|
+
transformationsApplied += 1;
|
|
25
|
+
},
|
|
26
|
+
ThrowStatement(path) {
|
|
27
|
+
nodesVisited++;
|
|
28
|
+
if (!tryExpandThrowSequence(path))
|
|
29
|
+
return;
|
|
30
|
+
transformationsApplied += 1;
|
|
31
|
+
},
|
|
32
|
+
ExpressionStatement(path) {
|
|
33
|
+
nodesVisited++;
|
|
34
|
+
if (!tryExpandExpressionStatementSequence(path))
|
|
35
|
+
return;
|
|
36
|
+
transformationsApplied += 1;
|
|
37
|
+
},
|
|
38
|
+
VariableDeclaration(path) {
|
|
39
|
+
nodesVisited++;
|
|
40
|
+
if (!tryExpandVariableDeclarationSequenceInitializers(path))
|
|
41
|
+
return;
|
|
42
|
+
transformationsApplied += 1;
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return Promise.resolve({ nodesVisited, transformationsApplied });
|
|
47
|
+
},
|
|
48
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
4
|
+
const t = require("@babel/types");
|
|
5
|
+
const canReplaceWithMultiple = (path) => {
|
|
6
|
+
return (path.parentPath.isProgram() ||
|
|
7
|
+
path.parentPath.isBlockStatement() ||
|
|
8
|
+
path.parentPath.isStaticBlock() ||
|
|
9
|
+
path.parentPath.isSwitchCase());
|
|
10
|
+
};
|
|
11
|
+
const canWrapInBlock = (path) => {
|
|
12
|
+
const parentPath = path.parentPath;
|
|
13
|
+
if (parentPath.isIfStatement() &&
|
|
14
|
+
(path.key === "consequent" || path.key === "alternate")) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
if ((parentPath.isForStatement() ||
|
|
18
|
+
parentPath.isForInStatement() ||
|
|
19
|
+
parentPath.isForOfStatement() ||
|
|
20
|
+
parentPath.isWhileStatement() ||
|
|
21
|
+
parentPath.isDoWhileStatement() ||
|
|
22
|
+
parentPath.isWithStatement() ||
|
|
23
|
+
parentPath.isLabeledStatement()) &&
|
|
24
|
+
path.key === "body") {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
29
|
+
const replaceThrowWithStatements = (path, statements) => {
|
|
30
|
+
if (canReplaceWithMultiple(path)) {
|
|
31
|
+
path.replaceWithMultiple(statements);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (canWrapInBlock(path)) {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
36
|
+
path.replaceWith(t.blockStatement(statements));
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
};
|
|
41
|
+
const isDirectiveProloguePosition = (path) => {
|
|
42
|
+
const parentPath = path.parentPath;
|
|
43
|
+
if (parentPath.isBlockStatement()) {
|
|
44
|
+
const grandParent = parentPath.parentPath;
|
|
45
|
+
if (parentPath.key !== "body")
|
|
46
|
+
return false;
|
|
47
|
+
if (!grandParent.isFunction())
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
else if (!parentPath.isProgram()) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const siblings = parentPath.get("body");
|
|
54
|
+
if (!Array.isArray(siblings))
|
|
55
|
+
return false;
|
|
56
|
+
const index = siblings.findIndex((sibling) => sibling.node === path.node);
|
|
57
|
+
if (index === -1)
|
|
58
|
+
return false;
|
|
59
|
+
for (let bodyIndex = 0; bodyIndex < index; bodyIndex++) {
|
|
60
|
+
const sibling = siblings[bodyIndex];
|
|
61
|
+
if (!sibling)
|
|
62
|
+
return false;
|
|
63
|
+
if (!sibling.isExpressionStatement())
|
|
64
|
+
return false;
|
|
65
|
+
if (sibling.node.expression.type !== "StringLiteral")
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
};
|
|
70
|
+
export const tryExpandThrowSequence = (path) => {
|
|
71
|
+
if (!canReplaceWithMultiple(path) && !canWrapInBlock(path))
|
|
72
|
+
return false;
|
|
73
|
+
const argument = path.node.argument;
|
|
74
|
+
if (argument.type !== "SequenceExpression")
|
|
75
|
+
return false;
|
|
76
|
+
if (argument.expressions.length < 2)
|
|
77
|
+
return false;
|
|
78
|
+
const leadingExpressions = argument.expressions.slice(0, -1);
|
|
79
|
+
const lastExpression = argument.expressions.at(-1);
|
|
80
|
+
if (!lastExpression)
|
|
81
|
+
return false;
|
|
82
|
+
const firstExpression = leadingExpressions[0];
|
|
83
|
+
const shouldWrapFirstStringLiteral = firstExpression?.type === "StringLiteral" &&
|
|
84
|
+
isDirectiveProloguePosition(path);
|
|
85
|
+
const statements = leadingExpressions.map((expression) => {
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
87
|
+
let clonedExpression = t.cloneNode(expression, true);
|
|
88
|
+
if (shouldWrapFirstStringLiteral && expression === firstExpression) {
|
|
89
|
+
// Prevent inserting a directive prologue statement like `"use strict";`.
|
|
90
|
+
// `("use strict");` is not treated as a directive.
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
92
|
+
clonedExpression = t.parenthesizedExpression(clonedExpression);
|
|
93
|
+
}
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
95
|
+
return t.expressionStatement(clonedExpression);
|
|
96
|
+
});
|
|
97
|
+
statements.push(
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
99
|
+
t.throwStatement(t.cloneNode(lastExpression, true)));
|
|
100
|
+
return replaceThrowWithStatements(path, statements);
|
|
101
|
+
};
|
package/dist/transforms/expand-sequence-expressions-v3/expand-variable-declaration-sequence.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
4
|
+
const t = require("@babel/types");
|
|
5
|
+
const createExpressionStatement = (expression) => {
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
7
|
+
return t.expressionStatement(
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
9
|
+
t.cloneNode(expression, true));
|
|
10
|
+
};
|
|
11
|
+
const createVariableDeclarationStatement = (kind, declarator) => {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
13
|
+
return t.variableDeclaration(kind, [declarator]);
|
|
14
|
+
};
|
|
15
|
+
const createVariableDeclarator = (id, init) => {
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
17
|
+
return t.variableDeclarator(
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
19
|
+
t.cloneNode(id, true), init
|
|
20
|
+
? // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
21
|
+
t.cloneNode(init, true)
|
|
22
|
+
: undefined);
|
|
23
|
+
};
|
|
24
|
+
const isSupportedStatementContainer = (path) => {
|
|
25
|
+
const parentPath = path.parentPath;
|
|
26
|
+
if (!parentPath)
|
|
27
|
+
return false;
|
|
28
|
+
return (parentPath.isProgram() ||
|
|
29
|
+
parentPath.isBlockStatement() ||
|
|
30
|
+
parentPath.isStaticBlock() ||
|
|
31
|
+
parentPath.isSwitchCase());
|
|
32
|
+
};
|
|
33
|
+
const isDirectiveProloguePosition = (path) => {
|
|
34
|
+
const parentPath = path.parentPath;
|
|
35
|
+
if (!parentPath)
|
|
36
|
+
return false;
|
|
37
|
+
if (parentPath.isBlockStatement()) {
|
|
38
|
+
const grandParent = parentPath.parentPath;
|
|
39
|
+
if (parentPath.key !== "body")
|
|
40
|
+
return false;
|
|
41
|
+
if (!grandParent.isFunction())
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
else if (!parentPath.isProgram()) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const siblings = parentPath.get("body");
|
|
48
|
+
if (!Array.isArray(siblings))
|
|
49
|
+
return false;
|
|
50
|
+
const index = siblings.findIndex((sibling) => sibling.node === path.node);
|
|
51
|
+
if (index === -1)
|
|
52
|
+
return false;
|
|
53
|
+
for (let bodyIndex = 0; bodyIndex < index; bodyIndex++) {
|
|
54
|
+
const sibling = siblings[bodyIndex];
|
|
55
|
+
if (!sibling)
|
|
56
|
+
return false;
|
|
57
|
+
if (!sibling.isExpressionStatement())
|
|
58
|
+
return false;
|
|
59
|
+
if (sibling.node.expression.type !== "StringLiteral")
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
};
|
|
64
|
+
export const tryExpandVariableDeclarationSequenceInitializers = (path) => {
|
|
65
|
+
if (!isSupportedStatementContainer(path))
|
|
66
|
+
return false;
|
|
67
|
+
if (isDirectiveProloguePosition(path)) {
|
|
68
|
+
const wouldInsertStringLiteral = path.node.declarations.some((declarator) => declarator.init?.type === "SequenceExpression" &&
|
|
69
|
+
declarator.init.expressions[0]?.type === "StringLiteral");
|
|
70
|
+
if (wouldInsertStringLiteral)
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const hasAnySequence = path.node.declarations.some((declarator) => declarator.init?.type === "SequenceExpression" &&
|
|
74
|
+
declarator.init.expressions.length >= 2);
|
|
75
|
+
if (!hasAnySequence)
|
|
76
|
+
return false;
|
|
77
|
+
const kind = path.node.kind;
|
|
78
|
+
const statements = [];
|
|
79
|
+
for (const declarator of path.node.declarations) {
|
|
80
|
+
const init = declarator.init;
|
|
81
|
+
if (init?.type !== "SequenceExpression") {
|
|
82
|
+
statements.push(createVariableDeclarationStatement(kind, declarator));
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const leadingExpressions = init.expressions.slice(0, -1);
|
|
86
|
+
const lastExpression = init.expressions.at(-1);
|
|
87
|
+
if (!lastExpression) {
|
|
88
|
+
statements.push(createVariableDeclarationStatement(kind, declarator));
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
for (const expression of leadingExpressions) {
|
|
92
|
+
statements.push(createExpressionStatement(expression));
|
|
93
|
+
}
|
|
94
|
+
const newDeclarator = createVariableDeclarator(declarator.id, lastExpression);
|
|
95
|
+
statements.push(createVariableDeclarationStatement(kind, newDeclarator));
|
|
96
|
+
}
|
|
97
|
+
path.replaceWithMultiple(statements);
|
|
98
|
+
return true;
|
|
99
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
4
|
+
const traverse = require("@babel/traverse").default;
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
6
|
+
const t = require("@babel/types");
|
|
7
|
+
const canReplaceWithMultiple = (path) => {
|
|
8
|
+
return (path.parentPath.isProgram() ||
|
|
9
|
+
path.parentPath.isBlockStatement() ||
|
|
10
|
+
path.parentPath.isStaticBlock() ||
|
|
11
|
+
path.parentPath.isSwitchCase());
|
|
12
|
+
};
|
|
13
|
+
const canWrapInBlock = (path) => {
|
|
14
|
+
const parentPath = path.parentPath;
|
|
15
|
+
if (parentPath.isIfStatement() &&
|
|
16
|
+
(path.key === "consequent" || path.key === "alternate")) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
if ((parentPath.isForStatement() ||
|
|
20
|
+
parentPath.isForInStatement() ||
|
|
21
|
+
parentPath.isForOfStatement() ||
|
|
22
|
+
parentPath.isWhileStatement() ||
|
|
23
|
+
parentPath.isDoWhileStatement() ||
|
|
24
|
+
parentPath.isWithStatement() ||
|
|
25
|
+
parentPath.isLabeledStatement()) &&
|
|
26
|
+
path.key === "body") {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
};
|
|
31
|
+
const replaceThrowWithStatements = (path, statements) => {
|
|
32
|
+
if (canReplaceWithMultiple(path)) {
|
|
33
|
+
path.replaceWithMultiple(statements);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (canWrapInBlock(path)) {
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
38
|
+
path.replaceWith(t.blockStatement(statements));
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
};
|
|
43
|
+
const isDirectiveProloguePosition = (path) => {
|
|
44
|
+
const parentPath = path.parentPath;
|
|
45
|
+
if (!parentPath.isProgram() && !parentPath.isBlockStatement())
|
|
46
|
+
return false;
|
|
47
|
+
const siblings = parentPath.get("body");
|
|
48
|
+
if (!Array.isArray(siblings))
|
|
49
|
+
return false;
|
|
50
|
+
const index = siblings.findIndex((sibling) => sibling.node === path.node);
|
|
51
|
+
if (index === -1)
|
|
52
|
+
return false;
|
|
53
|
+
for (let bodyIndex = 0; bodyIndex < index; bodyIndex++) {
|
|
54
|
+
const sibling = siblings[bodyIndex];
|
|
55
|
+
if (!sibling)
|
|
56
|
+
return false;
|
|
57
|
+
if (!sibling.isExpressionStatement())
|
|
58
|
+
return false;
|
|
59
|
+
if (sibling.node.expression.type !== "StringLiteral")
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
};
|
|
64
|
+
const tryExpandThrowSequence = (path) => {
|
|
65
|
+
if (!canReplaceWithMultiple(path) && !canWrapInBlock(path))
|
|
66
|
+
return false;
|
|
67
|
+
const argument = path.node.argument;
|
|
68
|
+
if (argument.type !== "SequenceExpression")
|
|
69
|
+
return false;
|
|
70
|
+
if (argument.expressions.length < 2)
|
|
71
|
+
return false;
|
|
72
|
+
const leadingExpressions = argument.expressions.slice(0, -1);
|
|
73
|
+
const lastExpression = argument.expressions.at(-1);
|
|
74
|
+
if (!lastExpression)
|
|
75
|
+
return false;
|
|
76
|
+
const firstExpression = leadingExpressions[0];
|
|
77
|
+
const shouldWrapFirstStringLiteral = firstExpression?.type === "StringLiteral" &&
|
|
78
|
+
isDirectiveProloguePosition(path);
|
|
79
|
+
const statements = leadingExpressions.map((expression) => {
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
81
|
+
let clonedExpression = t.cloneNode(expression, true);
|
|
82
|
+
if (shouldWrapFirstStringLiteral && expression === firstExpression) {
|
|
83
|
+
// Prevent inserting a directive prologue statement like `"use strict";`.
|
|
84
|
+
// `("use strict");` is not treated as a directive.
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
86
|
+
clonedExpression = t.parenthesizedExpression(clonedExpression);
|
|
87
|
+
}
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
89
|
+
return t.expressionStatement(clonedExpression);
|
|
90
|
+
});
|
|
91
|
+
statements.push(
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
93
|
+
t.throwStatement(t.cloneNode(lastExpression, true)));
|
|
94
|
+
return replaceThrowWithStatements(path, statements);
|
|
95
|
+
};
|
|
96
|
+
export const expandThrowSequenceTransform = {
|
|
97
|
+
id: "expand-throw-sequence",
|
|
98
|
+
description: "Expands throw sequences like `throw (a(), b)`",
|
|
99
|
+
scope: "file",
|
|
100
|
+
parallelizable: true,
|
|
101
|
+
transform(context) {
|
|
102
|
+
const { projectGraph } = context;
|
|
103
|
+
let nodesVisited = 0;
|
|
104
|
+
let transformationsApplied = 0;
|
|
105
|
+
for (const [, fileInfo] of projectGraph.files) {
|
|
106
|
+
traverse(fileInfo.ast, {
|
|
107
|
+
ThrowStatement(path) {
|
|
108
|
+
nodesVisited++;
|
|
109
|
+
if (!tryExpandThrowSequence(path))
|
|
110
|
+
return;
|
|
111
|
+
transformationsApplied += 1;
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return Promise.resolve({ nodesVisited, transformationsApplied });
|
|
116
|
+
},
|
|
117
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isValidBindingIdentifier } from "./is-valid-binding-identifier.js";
|
|
2
|
+
// Cap retries to avoid pathological loops while handling large bundles.
|
|
3
|
+
const MAX_CANDIDATES = 1000;
|
|
4
|
+
export const getTargetName = (bindingScope, binding, options) => {
|
|
5
|
+
const { baseName } = options;
|
|
6
|
+
if (baseName.length === 0)
|
|
7
|
+
return;
|
|
8
|
+
const programScope = bindingScope.getProgramParent();
|
|
9
|
+
for (let index = 0; index < MAX_CANDIDATES; index++) {
|
|
10
|
+
const candidate = index === 0 ? baseName : `${baseName}${index + 1}`;
|
|
11
|
+
if (!isValidBindingIdentifier(candidate))
|
|
12
|
+
continue;
|
|
13
|
+
// Avoid shadowing bindings in parent scopes.
|
|
14
|
+
if (bindingScope.hasBinding(candidate))
|
|
15
|
+
continue;
|
|
16
|
+
if (Object.hasOwn(programScope.globals, candidate))
|
|
17
|
+
continue;
|
|
18
|
+
const wouldBeShadowed = binding.referencePaths.some((referencePath) => referencePath.scope !== bindingScope &&
|
|
19
|
+
referencePath.scope.hasBinding(candidate));
|
|
20
|
+
if (wouldBeShadowed)
|
|
21
|
+
continue;
|
|
22
|
+
return candidate;
|
|
23
|
+
}
|
|
24
|
+
return;
|
|
25
|
+
};
|