@mittwald/flow-codemods 1.1.0 → 1.1.2
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/migrations/accent-box-color-to-background-color/transform.js +175 -0
- package/dist/migrations/action-prop-to-on-action/transform.js +82 -0
- package/dist/migrations/align-to-combine/transform.js +166 -0
- package/dist/migrations/button-color-accent-to-success/transform.js +126 -0
- package/dist/migrations/button-props-interfaces/transform.js +178 -0
- package/dist/migrations/color-primary-to-default/transform.js +135 -0
- package/dist/migrations/imports-to-package-root/transform.js +93 -0
- package/dist/migrations/muted-action-error-to-abort-action-error/transform.js +198 -0
- package/dist/migrations/package.json +3 -0
- package/dist/migrations/password-tools-rule/transform.js +173 -0
- package/dist/migrations/password-tools-subpath-renamed/transform.js +51 -0
- package/dist/migrations.generated.js +1 -1
- package/dist/run/jscodeshift.d.ts +4 -1
- package/dist/run/jscodeshift.d.ts.map +1 -1
- package/dist/run/jscodeshift.js +60 -22
- package/dist/tools/package.json +3 -0
- package/dist/tools/to-remote-package.js +29 -0
- package/package.json +4 -6
- package/src/migrations/accent-box-color-to-background-color/transform.ts +0 -218
- package/src/migrations/action-prop-to-on-action/transform.ts +0 -110
- package/src/migrations/align-to-combine/transform.ts +0 -212
- package/src/migrations/button-color-accent-to-success/transform.ts +0 -164
- package/src/migrations/button-props-interfaces/transform.ts +0 -229
- package/src/migrations/color-primary-to-default/transform.ts +0 -173
- package/src/migrations/imports-to-package-root/transform.ts +0 -107
- package/src/migrations/muted-action-error-to-abort-action-error/transform.ts +0 -260
- package/src/migrations/password-tools-rule/transform.ts +0 -219
- package/src/migrations/password-tools-subpath-renamed/transform.ts +0 -61
- package/src/tools/to-remote-package.ts +0 -37
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Replaces the removed `Button` props interfaces with `ButtonProps`
|
|
5
|
+
* (alpha.646).
|
|
6
|
+
*
|
|
7
|
+
* `RemoteButtonElementProps`, `ResetButtonProps` and `SubmitButtonProps` were
|
|
8
|
+
* removed; all three are `ButtonProps`. There is no alias for the old names, so
|
|
9
|
+
* a codebase still using them does not compile.
|
|
10
|
+
*
|
|
11
|
+
* This is more than a rename, because the names do not share an entry.
|
|
12
|
+
* `SubmitButtonProps` and `ResetButtonProps` came from the `react-hook-form`
|
|
13
|
+
* entry, which does not export `ButtonProps` — renaming them in place would
|
|
14
|
+
* swap one import error for another. The specifier therefore moves to the
|
|
15
|
+
* package root of whichever Flow package it came from, joining an existing
|
|
16
|
+
* import from that root or getting a new one.
|
|
17
|
+
*
|
|
18
|
+
* The scope is `@mittwald/flow-react-components` and its subpath entries only.
|
|
19
|
+
* The remote package re-exports the `flr-universal` surface, so it does carry
|
|
20
|
+
* some prop types — `ActionProps`, `ModalProps` and the like — but
|
|
21
|
+
* `ButtonProps` is not among them. There is nothing to move a remote import
|
|
22
|
+
* onto, so a remote codebase has to pick its own source for the type.
|
|
23
|
+
* `RemoteButtonElementProps` is left alone for the same reason, and because
|
|
24
|
+
* `@mittwald/flow-remote-elements` still exports that name today.
|
|
25
|
+
*
|
|
26
|
+
* A same-named import from another package is left alone. A name imported under
|
|
27
|
+
* a local alias (`import { SubmitButtonProps as P }`) keeps its alias — it
|
|
28
|
+
* becomes `ButtonProps as P`. Namespace usages (`Flow.SubmitButtonProps`) are
|
|
29
|
+
* rewritten as well.
|
|
30
|
+
*
|
|
31
|
+
* All three collapse onto `ButtonProps`, so a file importing more than one of
|
|
32
|
+
* them — or one of them next to `ButtonProps` itself — ends up with a single
|
|
33
|
+
* specifier instead of a duplicate declaration, which would not parse.
|
|
34
|
+
*/
|
|
35
|
+
const buttonPropsInterfacesTransform = (fileInfo, { j }) => {
|
|
36
|
+
const flowPackages = ["@mittwald/flow-react-components"];
|
|
37
|
+
const removed = new Set(["ResetButtonProps", "SubmitButtonProps"]);
|
|
38
|
+
const newName = "ButtonProps";
|
|
39
|
+
/** The Flow package a module specifier belongs to, or `undefined`. */
|
|
40
|
+
const rootPackageOf = (source) => flowPackages.find((pkg) => source === pkg || source.startsWith(`${pkg}/`));
|
|
41
|
+
// ast-types models `importKind` on the declaration only, while babel also
|
|
42
|
+
// puts it on the specifier — which is where a per-specifier `type X` lives.
|
|
43
|
+
const isTypeOnly = (node) => node.importKind === "type";
|
|
44
|
+
const root = j(fileInfo.source, { parser: "tsx" });
|
|
45
|
+
/** Local identifiers that have to be renamed (no alias in play). */
|
|
46
|
+
const localRenames = new Set();
|
|
47
|
+
/** Local names of `import * as Flow` namespace imports from a Flow package. */
|
|
48
|
+
const flowNamespaces = new Set();
|
|
49
|
+
/** `package:local` pairs that already import `ButtonProps` from a root. */
|
|
50
|
+
const bound = new Set();
|
|
51
|
+
/** `package:local` pairs that still need importing, and whether type-only. */
|
|
52
|
+
const wanted = new Map();
|
|
53
|
+
const flowImports = root
|
|
54
|
+
.find(j.ImportDeclaration)
|
|
55
|
+
.filter((path) => !!rootPackageOf(String(path.node.source.value)));
|
|
56
|
+
flowImports.forEach((path) => {
|
|
57
|
+
const source = String(path.node.source.value);
|
|
58
|
+
for (const specifier of path.node.specifiers ?? []) {
|
|
59
|
+
if (specifier.type === "ImportNamespaceSpecifier" &&
|
|
60
|
+
specifier.local?.name) {
|
|
61
|
+
flowNamespaces.add(String(specifier.local.name));
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (specifier.type === "ImportSpecifier" &&
|
|
65
|
+
specifier.imported.type === "Identifier" &&
|
|
66
|
+
specifier.imported.name === newName &&
|
|
67
|
+
source === rootPackageOf(source)) {
|
|
68
|
+
bound.add(`${source}:${String(specifier.local?.name ?? newName)}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
flowImports.forEach((path) => {
|
|
73
|
+
const source = String(path.node.source.value);
|
|
74
|
+
const pkg = rootPackageOf(source) ?? source;
|
|
75
|
+
const declarationIsType = isTypeOnly(path.node);
|
|
76
|
+
const specifiers = path.node.specifiers ?? [];
|
|
77
|
+
const survivors = [];
|
|
78
|
+
for (const specifier of specifiers) {
|
|
79
|
+
if (specifier.type !== "ImportSpecifier" ||
|
|
80
|
+
specifier.imported.type !== "Identifier" ||
|
|
81
|
+
!removed.has(specifier.imported.name)) {
|
|
82
|
+
survivors.push(specifier);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const imported = specifier.imported.name;
|
|
86
|
+
const local = String(specifier.local?.name ?? imported);
|
|
87
|
+
const isAlias = local !== imported;
|
|
88
|
+
const newLocal = isAlias ? local : newName;
|
|
89
|
+
const key = `${pkg}:${newLocal}`;
|
|
90
|
+
if (!isAlias) {
|
|
91
|
+
localRenames.add(local);
|
|
92
|
+
}
|
|
93
|
+
if (!bound.has(key)) {
|
|
94
|
+
const existing = wanted.get(key);
|
|
95
|
+
const type = declarationIsType || isTypeOnly(specifier);
|
|
96
|
+
wanted.set(key, {
|
|
97
|
+
pkg,
|
|
98
|
+
local: newLocal,
|
|
99
|
+
// A value import must not lose out to a type-only one.
|
|
100
|
+
type: existing ? existing.type && type : type,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (survivors.length === specifiers.length) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Leaving an emptied declaration behind would turn it into a side-effect
|
|
108
|
+
// import of an entry the file no longer uses.
|
|
109
|
+
if (survivors.length === 0) {
|
|
110
|
+
j(path).remove();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
path.node.specifiers = survivors;
|
|
114
|
+
});
|
|
115
|
+
if (wanted.size === 0 &&
|
|
116
|
+
localRenames.size === 0 &&
|
|
117
|
+
flowNamespaces.size === 0) {
|
|
118
|
+
return fileInfo.source;
|
|
119
|
+
}
|
|
120
|
+
for (const { pkg, local, type } of wanted.values()) {
|
|
121
|
+
const specifier = j.importSpecifier(j.identifier(newName), j.identifier(local));
|
|
122
|
+
// Join an import from the package root when the file already has one and
|
|
123
|
+
// it can carry the specifier; otherwise add a declaration of its own.
|
|
124
|
+
const host = root
|
|
125
|
+
.find(j.ImportDeclaration)
|
|
126
|
+
.filter((path) => String(path.node.source.value) === pkg &&
|
|
127
|
+
isTypeOnly(path.node) === type)
|
|
128
|
+
.paths()[0];
|
|
129
|
+
if (host) {
|
|
130
|
+
host.node.specifiers = [...(host.node.specifiers ?? []), specifier];
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const declaration = j.importDeclaration([specifier], j.literal(pkg));
|
|
134
|
+
if (type) {
|
|
135
|
+
declaration.importKind = "type";
|
|
136
|
+
}
|
|
137
|
+
const firstImport = root.find(j.ImportDeclaration).paths()[0];
|
|
138
|
+
if (firstImport) {
|
|
139
|
+
j(firstImport).insertBefore(declaration);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
root.get().node.program.body.unshift(declaration);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* `JSXIdentifier` extends `Identifier`, so this one pass covers value
|
|
147
|
+
* references and type references alike. Positions where the name is not a
|
|
148
|
+
* reference to the import — an object key, a member's property, a JSX
|
|
149
|
+
* attribute name — are skipped.
|
|
150
|
+
*/
|
|
151
|
+
root.find(j.Identifier).forEach((path) => {
|
|
152
|
+
const parent = path.parent.node;
|
|
153
|
+
const isNamespaceMember = (parent.type === "MemberExpression" ||
|
|
154
|
+
parent.type === "JSXMemberExpression") &&
|
|
155
|
+
parent.property === path.node;
|
|
156
|
+
if (isNamespaceMember) {
|
|
157
|
+
const object = parent.object;
|
|
158
|
+
if ((object.type === "Identifier" || object.type === "JSXIdentifier") &&
|
|
159
|
+
removed.has(path.node.name) &&
|
|
160
|
+
flowNamespaces.has(String(object.name))) {
|
|
161
|
+
path.node.name = newName;
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (parent.type === "ImportSpecifier" ||
|
|
166
|
+
parent.type === "JSXAttribute" ||
|
|
167
|
+
((parent.type === "ObjectProperty" || parent.type === "Property") &&
|
|
168
|
+
parent.key === path.node &&
|
|
169
|
+
!parent.computed)) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (localRenames.has(path.node.name)) {
|
|
173
|
+
path.node.name = newName;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
return root.toSource();
|
|
177
|
+
};
|
|
178
|
+
exports.default = buttonPropsInterfacesTransform;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Renames the `color="primary"` prop value to `color="default"` on the five
|
|
5
|
+
* components changed by the alpha.837 design update: `Breadcrumb`,
|
|
6
|
+
* `HeaderNavigation`, `Heading`, `IllustratedMessage` and `Link`.
|
|
7
|
+
*
|
|
8
|
+
* The scope is deliberately narrow. Only JSX elements that resolve to one of
|
|
9
|
+
* those components — imported (named or as a namespace) from
|
|
10
|
+
* `@mittwald/flow-react-components` or
|
|
11
|
+
* `@mittwald/flow-remote-react-components`, including their subpath entries —
|
|
12
|
+
* are touched. Components that still accept `color="primary"` (e.g. `Button`)
|
|
13
|
+
* and same-named elements from other packages (e.g. a router `Link`) are left
|
|
14
|
+
* untouched.
|
|
15
|
+
*
|
|
16
|
+
* Only literal `"primary"` values are rewritten — either as the whole value
|
|
17
|
+
* (`color="primary"`, `color={"primary"}`) or in a value position inside a
|
|
18
|
+
* dynamic one (`color={cond ? "secondary" : "primary"}`, `color={override ??
|
|
19
|
+
* "primary"}`). A value the transform cannot see into (`color={someVariable}`)
|
|
20
|
+
* is left alone.
|
|
21
|
+
*/
|
|
22
|
+
const colorPrimaryToDefaultTransform = (fileInfo, { j }) => {
|
|
23
|
+
const flowPackages = [
|
|
24
|
+
"@mittwald/flow-react-components",
|
|
25
|
+
"@mittwald/flow-remote-react-components",
|
|
26
|
+
];
|
|
27
|
+
const affectedComponents = new Set([
|
|
28
|
+
"Breadcrumb",
|
|
29
|
+
"HeaderNavigation",
|
|
30
|
+
"Heading",
|
|
31
|
+
"IllustratedMessage",
|
|
32
|
+
"Link",
|
|
33
|
+
]);
|
|
34
|
+
const isFlowImport = (source) => flowPackages.some((pkg) => source === pkg || source.startsWith(`${pkg}/`));
|
|
35
|
+
const isPrimaryLiteral = (node) => {
|
|
36
|
+
if (!node || typeof node !== "object" || !("type" in node)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
const { type } = node;
|
|
40
|
+
return ((type === "StringLiteral" || type === "Literal") &&
|
|
41
|
+
node.value === "primary");
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Rewrites every `"primary"` sitting in a position whose value can reach the
|
|
45
|
+
* prop: the expression itself, both branches of a ternary, and the operands
|
|
46
|
+
* of `??`/`||` — plus the right operand of `&&`, since `&&` yields its left
|
|
47
|
+
* operand only when that operand is falsy and a non-empty string never is. A
|
|
48
|
+
* `"primary"` anywhere else is not a value that reaches the prop (an object
|
|
49
|
+
* key, an index into a lookup table) and stays as it is.
|
|
50
|
+
*
|
|
51
|
+
* This cannot move into a shared module: the CLI loads this file straight out
|
|
52
|
+
* of the published package, which ships only
|
|
53
|
+
* `src/migrations/**\/transform.ts` — see this package's AGENTS.md.
|
|
54
|
+
*/
|
|
55
|
+
const rewriteValuePositions = (node) => {
|
|
56
|
+
if (isPrimaryLiteral(node)) {
|
|
57
|
+
return j.stringLiteral("default");
|
|
58
|
+
}
|
|
59
|
+
if (node?.type === "ConditionalExpression") {
|
|
60
|
+
node.consequent = rewriteValuePositions(node.consequent);
|
|
61
|
+
node.alternate = rewriteValuePositions(node.alternate);
|
|
62
|
+
return node;
|
|
63
|
+
}
|
|
64
|
+
if (node?.type === "LogicalExpression") {
|
|
65
|
+
if (node.operator !== "&&") {
|
|
66
|
+
node.left = rewriteValuePositions(node.left);
|
|
67
|
+
}
|
|
68
|
+
node.right = rewriteValuePositions(node.right);
|
|
69
|
+
return node;
|
|
70
|
+
}
|
|
71
|
+
return node;
|
|
72
|
+
};
|
|
73
|
+
const root = j(fileInfo.source, { parser: "tsx" });
|
|
74
|
+
// Local JSX identifier -> canonical component name (resolves `as` aliases).
|
|
75
|
+
const localToComponent = new Map();
|
|
76
|
+
// Local names of `import * as Flow` namespace imports from a Flow package.
|
|
77
|
+
const flowNamespaces = new Set();
|
|
78
|
+
root
|
|
79
|
+
.find(j.ImportDeclaration)
|
|
80
|
+
.filter((path) => isFlowImport(String(path.node.source.value)))
|
|
81
|
+
.forEach((path) => {
|
|
82
|
+
for (const specifier of path.node.specifiers ?? []) {
|
|
83
|
+
if (specifier.type === "ImportSpecifier" &&
|
|
84
|
+
specifier.imported.type === "Identifier" &&
|
|
85
|
+
affectedComponents.has(specifier.imported.name)) {
|
|
86
|
+
localToComponent.set(String(specifier.local?.name ?? specifier.imported.name), String(specifier.imported.name));
|
|
87
|
+
}
|
|
88
|
+
else if (specifier.type === "ImportNamespaceSpecifier" &&
|
|
89
|
+
specifier.local) {
|
|
90
|
+
flowNamespaces.add(String(specifier.local.name));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
if (localToComponent.size === 0 && flowNamespaces.size === 0) {
|
|
95
|
+
return fileInfo.source;
|
|
96
|
+
}
|
|
97
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
98
|
+
const name = path.node.name;
|
|
99
|
+
let isAffected = false;
|
|
100
|
+
if (name.type === "JSXIdentifier") {
|
|
101
|
+
isAffected = localToComponent.has(name.name);
|
|
102
|
+
}
|
|
103
|
+
else if (name.type === "JSXMemberExpression" &&
|
|
104
|
+
name.object.type === "JSXIdentifier" &&
|
|
105
|
+
name.property.type === "JSXIdentifier") {
|
|
106
|
+
isAffected =
|
|
107
|
+
flowNamespaces.has(name.object.name) &&
|
|
108
|
+
affectedComponents.has(name.property.name);
|
|
109
|
+
}
|
|
110
|
+
if (!isAffected) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
for (const attribute of path.node.attributes ?? []) {
|
|
114
|
+
if (attribute.type !== "JSXAttribute" ||
|
|
115
|
+
attribute.name.type !== "JSXIdentifier" ||
|
|
116
|
+
attribute.name.name !== "color") {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const value = attribute.value;
|
|
120
|
+
// color="primary"
|
|
121
|
+
if (isPrimaryLiteral(value)) {
|
|
122
|
+
attribute.value = j.stringLiteral("default");
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
// color={"primary"}, and every value position inside a dynamic value:
|
|
126
|
+
// color={cond ? "primary" : "x"}, color={fallback ?? "primary"}.
|
|
127
|
+
if (value?.type === "JSXExpressionContainer" &&
|
|
128
|
+
value.expression.type !== "JSXEmptyExpression") {
|
|
129
|
+
value.expression = rewriteValuePositions(value.expression);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
return root.toSource();
|
|
134
|
+
};
|
|
135
|
+
exports.default = colorPrimaryToDefaultTransform;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Subpaths this transform must leave alone. Two groups, and they are different
|
|
5
|
+
* kinds of fact.
|
|
6
|
+
*
|
|
7
|
+
* **Subpaths the package still exports.** alpha.28 collapsed 94 subpath exports
|
|
8
|
+
* onto the package root, and performing that collapse is this transform's whole
|
|
9
|
+
* job — but it did it with a catch-all `else`, so every subpath introduced
|
|
10
|
+
* _after_ alpha.28 was collateral. That stayed invisible while selection had a
|
|
11
|
+
* lower bound; it no longer has one (see `selectEntries`), so the transform now
|
|
12
|
+
* reaches current code, where flattening `all-layered.css` silently turns a
|
|
13
|
+
* stylesheet import into a JS one and flattening `mittwald-password-tools-js`
|
|
14
|
+
* moves `Rule` onto a root that does not export it.
|
|
15
|
+
*
|
|
16
|
+
* **Subpaths another entry owns.** `password-tools` is renamed to
|
|
17
|
+
* `mittwald-password-tools-js` by `password-tools-subpath-renamed`, whose
|
|
18
|
+
* `since` sorts it _after_ this entry. Flattening it first leaves that
|
|
19
|
+
* migration nothing to find, and both report success.
|
|
20
|
+
*
|
|
21
|
+
* Matched exactly, not by prefix: `react-hook-form/useFoo` still collapses onto
|
|
22
|
+
* `react-hook-form`, which is what alpha.28 did to it.
|
|
23
|
+
*
|
|
24
|
+
* `transform.test.ts` beside this file holds the first group against the
|
|
25
|
+
* package's real `exports` map, so a new subpath cannot quietly become
|
|
26
|
+
* collateral again.
|
|
27
|
+
*/
|
|
28
|
+
const keptSubpaths = new Set([
|
|
29
|
+
// the current export surface
|
|
30
|
+
"internal",
|
|
31
|
+
"flr-universal",
|
|
32
|
+
"nextjs",
|
|
33
|
+
"react-hook-form",
|
|
34
|
+
"mittwald-password-tools-js",
|
|
35
|
+
"all.css",
|
|
36
|
+
"all-layered.css",
|
|
37
|
+
"component-index",
|
|
38
|
+
"doc-properties",
|
|
39
|
+
// owned by password-tools-subpath-renamed
|
|
40
|
+
"password-tools",
|
|
41
|
+
]);
|
|
42
|
+
const importsToPackageRootTransform = (fileInfo, { j }) => {
|
|
43
|
+
const flowPackage = "@mittwald/flow-react-components";
|
|
44
|
+
const root = j(fileInfo.source, {
|
|
45
|
+
parser: "ts",
|
|
46
|
+
});
|
|
47
|
+
root
|
|
48
|
+
.find(j.ImportDeclaration)
|
|
49
|
+
.filter((i) => String(i.node.source.value).startsWith(`${flowPackage}/`))
|
|
50
|
+
.forEach((i) => {
|
|
51
|
+
const specifiers = i.node.specifiers ?? [];
|
|
52
|
+
const importPath = String(i.node.source.value);
|
|
53
|
+
const importRelativePath = importPath.slice(flowPackage.length + 1);
|
|
54
|
+
if (keptSubpaths.has(importRelativePath)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (importRelativePath === "all.css" ||
|
|
58
|
+
importRelativePath === "globals.css" ||
|
|
59
|
+
importRelativePath === "global.css") {
|
|
60
|
+
i.node.source.value = `${flowPackage}/all.css`;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (importRelativePath.startsWith("react-hook-form")) {
|
|
64
|
+
i.node.source.value = `${flowPackage}/react-hook-form`;
|
|
65
|
+
}
|
|
66
|
+
else if (importRelativePath.startsWith("nextjs")) {
|
|
67
|
+
i.node.source.value = `${flowPackage}/nextjs`;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
i.node.source.value = flowPackage;
|
|
71
|
+
}
|
|
72
|
+
specifiers.forEach((s, i) => {
|
|
73
|
+
if (s.type === "ImportDefaultSpecifier" ||
|
|
74
|
+
s.type === "ImportSpecifier") {
|
|
75
|
+
// `name` is typed `string | Identifier` in ast-types; for the
|
|
76
|
+
// specifiers handled here it is always the plain name.
|
|
77
|
+
const name = String((s.type === "ImportDefaultSpecifier"
|
|
78
|
+
? s.local?.name
|
|
79
|
+
: s.imported?.name) ?? "");
|
|
80
|
+
specifiers[i] = {
|
|
81
|
+
...s,
|
|
82
|
+
type: "ImportSpecifier",
|
|
83
|
+
imported: {
|
|
84
|
+
type: "Identifier",
|
|
85
|
+
name,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
return root.toSource();
|
|
92
|
+
};
|
|
93
|
+
exports.default = importsToPackageRootTransform;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Renames `MutedActionError` to `AbortActionError`, along with its two static
|
|
5
|
+
* helpers (alpha.712).
|
|
6
|
+
*
|
|
7
|
+
* ```diff
|
|
8
|
+
* -throw new MutedActionError();
|
|
9
|
+
* +throw new AbortActionError();
|
|
10
|
+
* -MutedActionError.isMutedActionError(error);
|
|
11
|
+
* +AbortActionError.isAbortActionError(error);
|
|
12
|
+
* -MutedActionError.rethrowIfNotMuted(error);
|
|
13
|
+
* +AbortActionError.rethrowIfNotAborted(error);
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* There is no alias for the old name, so a codebase still using it does not
|
|
17
|
+
* compile.
|
|
18
|
+
*
|
|
19
|
+
* The thrown error's `name` changed too, so a `error.name ===
|
|
20
|
+
* "MutedActionError"` comparison is rewritten as well — but only in a file that
|
|
21
|
+
* imports the class, and only where the string is compared with `==`, `===`,
|
|
22
|
+
* `!=` or `!==`. A check living in a file that never imports the class cannot
|
|
23
|
+
* be recognised; grep for the string once when you are done.
|
|
24
|
+
*
|
|
25
|
+
* Only names imported from `@mittwald/flow-react-components` (including its
|
|
26
|
+
* subpath entries) are touched. `@mittwald/flow-remote-react-components`
|
|
27
|
+
* exports no error classes, so it cannot be in scope. A local alias (`import {
|
|
28
|
+
* MutedActionError as Muted }`) keeps its alias — the static helpers on it are
|
|
29
|
+
* renamed all the same. Namespace usages (`Flow.MutedActionError`) are
|
|
30
|
+
* rewritten too.
|
|
31
|
+
*/
|
|
32
|
+
const mutedActionErrorToAbortActionErrorTransform = (fileInfo, { j }) => {
|
|
33
|
+
const flowPackages = ["@mittwald/flow-react-components"];
|
|
34
|
+
const oldName = "MutedActionError";
|
|
35
|
+
const newName = "AbortActionError";
|
|
36
|
+
const memberRenames = new Map([
|
|
37
|
+
["isMutedActionError", "isAbortActionError"],
|
|
38
|
+
["rethrowIfNotMuted", "rethrowIfNotAborted"],
|
|
39
|
+
]);
|
|
40
|
+
const isFlowImport = (source) => flowPackages.some((pkg) => source === pkg || source.startsWith(`${pkg}/`));
|
|
41
|
+
// ast-types models `importKind` on the declaration only, while babel also
|
|
42
|
+
// puts it on the specifier — which is where a per-specifier `type X` lives.
|
|
43
|
+
const isTypeOnly = (specifier) => specifier.importKind === "type";
|
|
44
|
+
const makeValueImport = (specifier) => {
|
|
45
|
+
specifier.importKind = "value";
|
|
46
|
+
};
|
|
47
|
+
const root = j(fileInfo.source, { parser: "tsx" });
|
|
48
|
+
/**
|
|
49
|
+
* Local names bound to the class, alias or not — the static helpers hang off
|
|
50
|
+
* these.
|
|
51
|
+
*/
|
|
52
|
+
const classLocals = new Set();
|
|
53
|
+
/** Local names whose identifier itself has to be renamed (no alias in play). */
|
|
54
|
+
const localRenames = new Set();
|
|
55
|
+
/** Local names of `import * as Flow` namespace imports from a Flow package. */
|
|
56
|
+
const flowNamespaces = new Set();
|
|
57
|
+
const flowImports = root
|
|
58
|
+
.find(j.ImportDeclaration)
|
|
59
|
+
.filter((path) => isFlowImport(String(path.node.source.value)));
|
|
60
|
+
/**
|
|
61
|
+
* Local names the file already binds to the new name. Renaming onto one of
|
|
62
|
+
* them must not add a second specifier, which would not parse.
|
|
63
|
+
*/
|
|
64
|
+
const claimed = new Set();
|
|
65
|
+
flowImports.forEach((path) => {
|
|
66
|
+
for (const specifier of path.node.specifiers ?? []) {
|
|
67
|
+
if (specifier.type === "ImportNamespaceSpecifier" &&
|
|
68
|
+
specifier.local?.name) {
|
|
69
|
+
flowNamespaces.add(String(specifier.local.name));
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (specifier.type === "ImportSpecifier" &&
|
|
73
|
+
specifier.imported.type === "Identifier" &&
|
|
74
|
+
specifier.imported.name === newName) {
|
|
75
|
+
claimed.add(String(specifier.local?.name ?? newName));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
flowImports.forEach((path) => {
|
|
80
|
+
const specifiers = path.node.specifiers ?? [];
|
|
81
|
+
const survivors = [];
|
|
82
|
+
for (const specifier of specifiers) {
|
|
83
|
+
if (specifier.type !== "ImportSpecifier" ||
|
|
84
|
+
specifier.imported.type !== "Identifier" ||
|
|
85
|
+
specifier.imported.name !== oldName) {
|
|
86
|
+
survivors.push(specifier);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const local = String(specifier.local?.name ?? oldName);
|
|
90
|
+
const isAlias = local !== oldName;
|
|
91
|
+
const newLocal = isAlias ? local : newName;
|
|
92
|
+
classLocals.add(local);
|
|
93
|
+
if (!isAlias) {
|
|
94
|
+
localRenames.add(local);
|
|
95
|
+
}
|
|
96
|
+
if (claimed.has(newLocal)) {
|
|
97
|
+
// The name is already bound by another import — drop this specifier
|
|
98
|
+
// instead of declaring it twice.
|
|
99
|
+
if (!isTypeOnly(specifier)) {
|
|
100
|
+
for (const kept of survivors) {
|
|
101
|
+
if (kept.type === "ImportSpecifier" &&
|
|
102
|
+
kept.imported.type === "Identifier" &&
|
|
103
|
+
String(kept.local?.name ?? kept.imported.name) === newLocal) {
|
|
104
|
+
makeValueImport(kept);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
claimed.add(newLocal);
|
|
111
|
+
// Mutate in place so an `import type` / `type X` modifier survives.
|
|
112
|
+
specifier.imported.name = newName;
|
|
113
|
+
if (!isAlias && specifier.local) {
|
|
114
|
+
specifier.local.name = newName;
|
|
115
|
+
}
|
|
116
|
+
survivors.push(specifier);
|
|
117
|
+
}
|
|
118
|
+
if (survivors.length === specifiers.length) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// Leaving an emptied declaration behind would turn it into a side-effect
|
|
122
|
+
// import of an entry the file no longer uses.
|
|
123
|
+
if (survivors.length === 0) {
|
|
124
|
+
j(path).remove();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
path.node.specifiers = survivors;
|
|
128
|
+
});
|
|
129
|
+
if (classLocals.size === 0 && flowNamespaces.size === 0) {
|
|
130
|
+
return fileInfo.source;
|
|
131
|
+
}
|
|
132
|
+
/** Does this expression denote the class — `Muted` or `Flow.MutedActionError`? */
|
|
133
|
+
const isClassReference = (node) => {
|
|
134
|
+
if (!node) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
if (node.type === "Identifier") {
|
|
138
|
+
return classLocals.has(String(node.name));
|
|
139
|
+
}
|
|
140
|
+
return (node.type === "MemberExpression" &&
|
|
141
|
+
node.object?.type === "Identifier" &&
|
|
142
|
+
flowNamespaces.has(String(node.object.name)) &&
|
|
143
|
+
node.property?.type === "Identifier" &&
|
|
144
|
+
String(node.property.name) === oldName);
|
|
145
|
+
};
|
|
146
|
+
// Static helpers first: the check reads the pre-rename object name.
|
|
147
|
+
root.find(j.MemberExpression).forEach((path) => {
|
|
148
|
+
const { object, property } = path.node;
|
|
149
|
+
if (property.type !== "Identifier" || !isClassReference(object)) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const renamed = memberRenames.get(property.name);
|
|
153
|
+
if (renamed) {
|
|
154
|
+
property.name = renamed;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
root.find(j.Identifier).forEach((path) => {
|
|
158
|
+
const parent = path.parent.node;
|
|
159
|
+
const isNamespaceMember = (parent.type === "MemberExpression" ||
|
|
160
|
+
parent.type === "JSXMemberExpression") &&
|
|
161
|
+
parent.property === path.node;
|
|
162
|
+
if (isNamespaceMember) {
|
|
163
|
+
const object = parent.object;
|
|
164
|
+
if ((object.type === "Identifier" || object.type === "JSXIdentifier") &&
|
|
165
|
+
path.node.name === oldName &&
|
|
166
|
+
flowNamespaces.has(String(object.name))) {
|
|
167
|
+
path.node.name = newName;
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (parent.type === "ImportSpecifier" ||
|
|
172
|
+
parent.type === "JSXAttribute" ||
|
|
173
|
+
((parent.type === "ObjectProperty" || parent.type === "Property") &&
|
|
174
|
+
parent.key === path.node &&
|
|
175
|
+
!parent.computed)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (localRenames.has(path.node.name)) {
|
|
179
|
+
path.node.name = newName;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
// `error.name === "MutedActionError"` — a comparison is the only place where
|
|
183
|
+
// the string can be recognised as the error's name rather than free text.
|
|
184
|
+
const comparisons = new Set(["===", "!==", "==", "!="]);
|
|
185
|
+
root.find(j.BinaryExpression).forEach((path) => {
|
|
186
|
+
if (!comparisons.has(path.node.operator)) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
for (const side of [path.node.left, path.node.right]) {
|
|
190
|
+
if ((side.type === "StringLiteral" || side.type === "Literal") &&
|
|
191
|
+
side.value === oldName) {
|
|
192
|
+
side.value = newName;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return root.toSource();
|
|
197
|
+
};
|
|
198
|
+
exports.default = mutedActionErrorToAbortActionErrorTransform;
|