@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.
Files changed (29) hide show
  1. package/dist/migrations/accent-box-color-to-background-color/transform.js +175 -0
  2. package/dist/migrations/action-prop-to-on-action/transform.js +82 -0
  3. package/dist/migrations/align-to-combine/transform.js +166 -0
  4. package/dist/migrations/button-color-accent-to-success/transform.js +126 -0
  5. package/dist/migrations/button-props-interfaces/transform.js +178 -0
  6. package/dist/migrations/color-primary-to-default/transform.js +135 -0
  7. package/dist/migrations/imports-to-package-root/transform.js +93 -0
  8. package/dist/migrations/muted-action-error-to-abort-action-error/transform.js +198 -0
  9. package/dist/migrations/package.json +3 -0
  10. package/dist/migrations/password-tools-rule/transform.js +173 -0
  11. package/dist/migrations/password-tools-subpath-renamed/transform.js +51 -0
  12. package/dist/migrations.generated.js +1 -1
  13. package/dist/run/jscodeshift.d.ts +4 -1
  14. package/dist/run/jscodeshift.d.ts.map +1 -1
  15. package/dist/run/jscodeshift.js +60 -22
  16. package/dist/tools/package.json +3 -0
  17. package/dist/tools/to-remote-package.js +29 -0
  18. package/package.json +4 -6
  19. package/src/migrations/accent-box-color-to-background-color/transform.ts +0 -218
  20. package/src/migrations/action-prop-to-on-action/transform.ts +0 -110
  21. package/src/migrations/align-to-combine/transform.ts +0 -212
  22. package/src/migrations/button-color-accent-to-success/transform.ts +0 -164
  23. package/src/migrations/button-props-interfaces/transform.ts +0 -229
  24. package/src/migrations/color-primary-to-default/transform.ts +0 -173
  25. package/src/migrations/imports-to-package-root/transform.ts +0 -107
  26. package/src/migrations/muted-action-error-to-abort-action-error/transform.ts +0 -260
  27. package/src/migrations/password-tools-rule/transform.ts +0 -219
  28. package/src/migrations/password-tools-subpath-renamed/transform.ts +0 -61
  29. package/src/tools/to-remote-package.ts +0 -37
@@ -1,110 +0,0 @@
1
- import type { Transform } from "jscodeshift";
2
-
3
- /**
4
- * Renames the `action` prop to `onAction` on `Action`.
5
- *
6
- * The scope is deliberately narrow. Only JSX elements that resolve to `Action`
7
- * — imported (named or as a namespace) from `@mittwald/flow-react-components`
8
- * or `@mittwald/flow-remote-react-components`, including their subpath entries
9
- * — are touched. `Action` is not one of the generated remote components, but
10
- * the remote package re-exports the `flr-universal` surface, which carries it.
11
- * Same-named components from other packages are left untouched, and so is the
12
- * `action` attribute of a plain `<form>`.
13
- *
14
- * An element that already carries `onAction` keeps it and only loses the stale
15
- * `action` prop, which mirrors what the runtime fallback does: an explicit
16
- * `onAction` wins.
17
- */
18
- const actionPropToOnActionTransform: Transform = (fileInfo, { j }) => {
19
- const flowPackages = [
20
- "@mittwald/flow-react-components",
21
- "@mittwald/flow-remote-react-components",
22
- ];
23
- const affectedComponents = new Set(["Action"]);
24
-
25
- const isFlowImport = (source: string): boolean =>
26
- flowPackages.some((pkg) => source === pkg || source.startsWith(`${pkg}/`));
27
-
28
- const root = j(fileInfo.source, { parser: "tsx" });
29
-
30
- // Local JSX identifier -> canonical component name (resolves `as` aliases).
31
- const localToComponent = new Map<string, string>();
32
- // Local names of `import * as Flow` namespace imports from a Flow package.
33
- const flowNamespaces = new Set<string>();
34
-
35
- root
36
- .find(j.ImportDeclaration)
37
- .filter((path) => isFlowImport(String(path.node.source.value)))
38
- .forEach((path) => {
39
- for (const specifier of path.node.specifiers ?? []) {
40
- if (
41
- specifier.type === "ImportSpecifier" &&
42
- specifier.imported.type === "Identifier" &&
43
- affectedComponents.has(specifier.imported.name)
44
- ) {
45
- localToComponent.set(
46
- String(specifier.local?.name ?? specifier.imported.name),
47
- String(specifier.imported.name),
48
- );
49
- } else if (
50
- specifier.type === "ImportNamespaceSpecifier" &&
51
- specifier.local
52
- ) {
53
- flowNamespaces.add(String(specifier.local.name));
54
- }
55
- }
56
- });
57
-
58
- if (localToComponent.size === 0 && flowNamespaces.size === 0) {
59
- return fileInfo.source;
60
- }
61
-
62
- root.find(j.JSXOpeningElement).forEach((path) => {
63
- const name = path.node.name;
64
-
65
- let isAffected = false;
66
- if (name.type === "JSXIdentifier") {
67
- isAffected = localToComponent.has(name.name);
68
- } else if (
69
- name.type === "JSXMemberExpression" &&
70
- name.object.type === "JSXIdentifier" &&
71
- name.property.type === "JSXIdentifier"
72
- ) {
73
- isAffected =
74
- flowNamespaces.has(name.object.name) &&
75
- affectedComponents.has(name.property.name);
76
- }
77
-
78
- if (!isAffected) {
79
- return;
80
- }
81
-
82
- const attributes = path.node.attributes ?? [];
83
-
84
- const isNamed = (attribute: (typeof attributes)[number], key: string) =>
85
- attribute.type === "JSXAttribute" &&
86
- attribute.name.type === "JSXIdentifier" &&
87
- attribute.name.name === key;
88
-
89
- const hasOnAction = attributes.some((attribute) =>
90
- isNamed(attribute, "onAction"),
91
- );
92
-
93
- if (hasOnAction) {
94
- path.node.attributes = attributes.filter(
95
- (attribute) => !isNamed(attribute, "action"),
96
- );
97
- return;
98
- }
99
-
100
- for (const attribute of attributes) {
101
- if (isNamed(attribute, "action") && attribute.type === "JSXAttribute") {
102
- attribute.name.name = "onAction";
103
- }
104
- }
105
- });
106
-
107
- return root.toSource();
108
- };
109
-
110
- export default actionPropToOnActionTransform;
@@ -1,212 +0,0 @@
1
- import type { Transform } from "jscodeshift";
2
-
3
- /**
4
- * Renames the `Align` component to `Combine`, and `AlignProps` to
5
- * `CombineProps`.
6
- *
7
- * Only names imported from `@mittwald/flow-react-components` or
8
- * `@mittwald/flow-remote-react-components` (including their subpath entries)
9
- * are touched, so a same-named import from another package is left alone. An
10
- * `Align` imported under a local alias (`import { Align as Row }`) keeps its
11
- * alias — only the imported name changes. Namespace usages (`<Flow.Align />`,
12
- * `Flow.AlignProps`) are rewritten as well.
13
- */
14
- const alignToCombineTransform: Transform = (fileInfo, { j }) => {
15
- const flowPackages = [
16
- "@mittwald/flow-react-components",
17
- "@mittwald/flow-remote-react-components",
18
- ];
19
- const renames = new Map([
20
- ["Align", "Combine"],
21
- ["AlignProps", "CombineProps"],
22
- ]);
23
-
24
- const isFlowImport = (source: string): boolean =>
25
- flowPackages.some((pkg) => source === pkg || source.startsWith(`${pkg}/`));
26
-
27
- // ast-types models `importKind` on the declaration only, while babel also
28
- // puts it on the specifier — which is where a per-specifier `type X` lives.
29
- const isTypeOnly = (specifier: object): boolean =>
30
- (specifier as { importKind?: string }).importKind === "type";
31
- const makeValueImport = (specifier: object): void => {
32
- (specifier as { importKind?: string }).importKind = "value";
33
- };
34
-
35
- const root = j(fileInfo.source, { parser: "tsx" });
36
-
37
- /** Local identifiers that refer to a renamed export, mapped to the new name. */
38
- const localRenames = new Map<string, string>();
39
- /**
40
- * Whether any import specifier was rewritten. An aliased `Align as Row` adds
41
- * nothing to `localRenames` — its local name stays `Row` — so counting those
42
- * alone would treat an alias-only file as untouched and discard the rewrite.
43
- */
44
- let renamedAnImport = false;
45
- /** Local names of `import * as Flow` namespace imports from a Flow package. */
46
- const flowNamespaces = new Set<string>();
47
-
48
- const flowImports = root
49
- .find(j.ImportDeclaration)
50
- .filter((path) => isFlowImport(String(path.node.source.value)));
51
-
52
- /**
53
- * `imported:local` pairs the file already binds without a rename. A rename
54
- * that lands on one of these must not add a second specifier for it — the
55
- * name is available from the import that already carries it, which is also
56
- * the entry that really exports it.
57
- */
58
- const claimed = new Set<string>();
59
-
60
- flowImports.forEach((path) => {
61
- for (const specifier of path.node.specifiers ?? []) {
62
- if (
63
- specifier.type === "ImportNamespaceSpecifier" &&
64
- specifier.local?.name
65
- ) {
66
- flowNamespaces.add(String(specifier.local.name));
67
- continue;
68
- }
69
-
70
- if (
71
- specifier.type !== "ImportSpecifier" ||
72
- specifier.imported.type !== "Identifier" ||
73
- renames.has(specifier.imported.name)
74
- ) {
75
- continue;
76
- }
77
-
78
- claimed.add(
79
- `${specifier.imported.name}:${String(
80
- specifier.local?.name ?? specifier.imported.name,
81
- )}`,
82
- );
83
- }
84
- });
85
-
86
- flowImports.forEach((path) => {
87
- const specifiers = path.node.specifiers ?? [];
88
- const survivors: typeof specifiers = [];
89
-
90
- for (const specifier of specifiers) {
91
- if (
92
- specifier.type !== "ImportSpecifier" ||
93
- specifier.imported.type !== "Identifier"
94
- ) {
95
- survivors.push(specifier);
96
- continue;
97
- }
98
-
99
- const imported = specifier.imported.name;
100
- const renamed = renames.get(imported);
101
- if (!renamed) {
102
- survivors.push(specifier);
103
- continue;
104
- }
105
-
106
- const local = String(specifier.local?.name ?? imported);
107
- const isAlias = local !== imported;
108
- const newLocal = isAlias ? local : renamed;
109
- const key = `${renamed}:${newLocal}`;
110
-
111
- renamedAnImport = true;
112
- if (!isAlias) {
113
- // An aliased import keeps its local name and needs no further change.
114
- localRenames.set(local, renamed);
115
- }
116
-
117
- // Renaming can collide with a name the file already binds — directly, or
118
- // because a second old name maps onto the same new one. Either way the
119
- // binding already exists, so this specifier goes away instead of
120
- // producing a duplicate declaration, which would not parse.
121
- const collision = claimed.has(key);
122
- claimed.add(key);
123
- if (collision) {
124
- // A value import must not lose out to a type-only one.
125
- if (!isTypeOnly(specifier)) {
126
- for (const kept of survivors) {
127
- if (
128
- kept.type === "ImportSpecifier" &&
129
- kept.imported.type === "Identifier" &&
130
- `${kept.imported.name}:${String(
131
- kept.local?.name ?? kept.imported.name,
132
- )}` === key
133
- ) {
134
- makeValueImport(kept);
135
- }
136
- }
137
- }
138
- continue;
139
- }
140
-
141
- // Mutate in place so an `import type` / `type X` modifier survives.
142
- specifier.imported.name = renamed;
143
- if (!isAlias && specifier.local) {
144
- specifier.local.name = renamed;
145
- }
146
- survivors.push(specifier);
147
- }
148
-
149
- if (survivors.length === specifiers.length) {
150
- return;
151
- }
152
-
153
- // Every specifier moved to an import that already binds the name. Leaving
154
- // the declaration behind would turn it into a side-effect import of an
155
- // entry the file no longer uses.
156
- if (survivors.length === 0) {
157
- j(path).remove();
158
- return;
159
- }
160
-
161
- path.node.specifiers = survivors;
162
- });
163
-
164
- if (!renamedAnImport && flowNamespaces.size === 0) {
165
- return fileInfo.source;
166
- }
167
-
168
- /**
169
- * `JSXIdentifier` extends `Identifier`, so this one pass covers JSX tags,
170
- * value references and type references alike. Positions where the name is not
171
- * a reference to the import — an object key, a member's property, a JSX
172
- * attribute name — are skipped.
173
- */
174
- root.find(j.Identifier).forEach((path) => {
175
- const parent = path.parent.node;
176
-
177
- const isNamespaceMember =
178
- (parent.type === "MemberExpression" ||
179
- parent.type === "JSXMemberExpression") &&
180
- parent.property === path.node;
181
-
182
- if (isNamespaceMember) {
183
- const object = parent.object;
184
- if (object.type === "Identifier" || object.type === "JSXIdentifier") {
185
- const renamed = renames.get(path.node.name);
186
- if (renamed && flowNamespaces.has(String(object.name))) {
187
- path.node.name = renamed;
188
- }
189
- }
190
- return;
191
- }
192
-
193
- if (
194
- parent.type === "ImportSpecifier" ||
195
- parent.type === "JSXAttribute" ||
196
- ((parent.type === "ObjectProperty" || parent.type === "Property") &&
197
- parent.key === path.node &&
198
- !parent.computed)
199
- ) {
200
- return;
201
- }
202
-
203
- const renamed = localRenames.get(path.node.name);
204
- if (renamed) {
205
- path.node.name = renamed;
206
- }
207
- });
208
-
209
- return root.toSource();
210
- };
211
-
212
- export default alignToCombineTransform;
@@ -1,164 +0,0 @@
1
- import type { ConditionalExpression, Transform } from "jscodeshift";
2
-
3
- /**
4
- * An expression that can be a prop's value. Deliberately not
5
- * `JSXExpressionContainer["expression"]`: that also admits `JSXEmptyExpression`
6
- * (`color={/* a comment *\/}`), which cannot be written back into a ternary
7
- * branch. The call site filters it out instead.
8
- */
9
- type ValueNode = ConditionalExpression["consequent"];
10
-
11
- /**
12
- * Renames the `color="accent"` prop value to `color="success"` on `Button` and
13
- * `SubmitButton`.
14
- *
15
- * The scope is deliberately narrow. Only JSX elements that resolve to one of
16
- * those components — imported (named or as a namespace) from
17
- * `@mittwald/flow-react-components` or
18
- * `@mittwald/flow-remote-react-components`, including their subpath entries —
19
- * are touched. Same-named components from other packages are left untouched.
20
- *
21
- * Only literal `"accent"` values are rewritten — either as the whole value
22
- * (`color="accent"`, `color={"accent"}`) or in a value position inside a
23
- * dynamic one (`color={cond ? "secondary" : "accent"}`, `color={override ??
24
- * "accent"}`). A value the transform cannot see into (`color={someVariable}`)
25
- * is left alone.
26
- */
27
- const buttonColorAccentToSuccessTransform: Transform = (fileInfo, { j }) => {
28
- const flowPackages = [
29
- "@mittwald/flow-react-components",
30
- "@mittwald/flow-remote-react-components",
31
- ];
32
- const affectedComponents = new Set(["Button", "SubmitButton"]);
33
-
34
- const isFlowImport = (source: string): boolean =>
35
- flowPackages.some((pkg) => source === pkg || source.startsWith(`${pkg}/`));
36
-
37
- const isAccentLiteral = (node: unknown): boolean => {
38
- if (!node || typeof node !== "object" || !("type" in node)) {
39
- return false;
40
- }
41
- const { type } = node as { type: string };
42
- return (
43
- (type === "StringLiteral" || type === "Literal") &&
44
- (node as { value?: unknown }).value === "accent"
45
- );
46
- };
47
-
48
- /**
49
- * Rewrites every `"accent"` sitting in a position whose value can reach the
50
- * prop: the expression itself, both branches of a ternary, and the operands
51
- * of `??`/`||` — plus the right operand of `&&`, since `&&` yields its left
52
- * operand only when that operand is falsy and a non-empty string never is. A
53
- * `"accent"` anywhere else is not a value that reaches the prop (an object
54
- * key, an index into a lookup table) and stays as it is.
55
- *
56
- * This cannot move into a shared module: the CLI loads this file straight out
57
- * of the published package, which ships only
58
- * `src/migrations/**\/transform.ts` — see this package's AGENTS.md.
59
- */
60
- const rewriteValuePositions = (node: ValueNode): ValueNode => {
61
- if (isAccentLiteral(node)) {
62
- return j.stringLiteral("success");
63
- }
64
- if (node?.type === "ConditionalExpression") {
65
- node.consequent = rewriteValuePositions(node.consequent);
66
- node.alternate = rewriteValuePositions(node.alternate);
67
- return node;
68
- }
69
- if (node?.type === "LogicalExpression") {
70
- if (node.operator !== "&&") {
71
- node.left = rewriteValuePositions(node.left);
72
- }
73
- node.right = rewriteValuePositions(node.right);
74
- return node;
75
- }
76
- return node;
77
- };
78
-
79
- const root = j(fileInfo.source, { parser: "tsx" });
80
-
81
- // Local JSX identifier -> canonical component name (resolves `as` aliases).
82
- const localToComponent = new Map<string, string>();
83
- // Local names of `import * as Flow` namespace imports from a Flow package.
84
- const flowNamespaces = new Set<string>();
85
-
86
- root
87
- .find(j.ImportDeclaration)
88
- .filter((path) => isFlowImport(String(path.node.source.value)))
89
- .forEach((path) => {
90
- for (const specifier of path.node.specifiers ?? []) {
91
- if (
92
- specifier.type === "ImportSpecifier" &&
93
- specifier.imported.type === "Identifier" &&
94
- affectedComponents.has(specifier.imported.name)
95
- ) {
96
- localToComponent.set(
97
- String(specifier.local?.name ?? specifier.imported.name),
98
- String(specifier.imported.name),
99
- );
100
- } else if (
101
- specifier.type === "ImportNamespaceSpecifier" &&
102
- specifier.local
103
- ) {
104
- flowNamespaces.add(String(specifier.local.name));
105
- }
106
- }
107
- });
108
-
109
- if (localToComponent.size === 0 && flowNamespaces.size === 0) {
110
- return fileInfo.source;
111
- }
112
-
113
- root.find(j.JSXOpeningElement).forEach((path) => {
114
- const name = path.node.name;
115
-
116
- let isAffected = false;
117
- if (name.type === "JSXIdentifier") {
118
- isAffected = localToComponent.has(name.name);
119
- } else if (
120
- name.type === "JSXMemberExpression" &&
121
- name.object.type === "JSXIdentifier" &&
122
- name.property.type === "JSXIdentifier"
123
- ) {
124
- isAffected =
125
- flowNamespaces.has(name.object.name) &&
126
- affectedComponents.has(name.property.name);
127
- }
128
-
129
- if (!isAffected) {
130
- return;
131
- }
132
-
133
- for (const attribute of path.node.attributes ?? []) {
134
- if (
135
- attribute.type !== "JSXAttribute" ||
136
- attribute.name.type !== "JSXIdentifier" ||
137
- attribute.name.name !== "color"
138
- ) {
139
- continue;
140
- }
141
-
142
- const value = attribute.value;
143
-
144
- // color="accent"
145
- if (isAccentLiteral(value)) {
146
- attribute.value = j.stringLiteral("success");
147
- continue;
148
- }
149
-
150
- // color={"accent"}, and every value position inside a dynamic value:
151
- // color={cond ? "accent" : "x"}, color={fallback ?? "accent"}.
152
- if (
153
- value?.type === "JSXExpressionContainer" &&
154
- value.expression.type !== "JSXEmptyExpression"
155
- ) {
156
- value.expression = rewriteValuePositions(value.expression);
157
- }
158
- }
159
- });
160
-
161
- return root.toSource();
162
- };
163
-
164
- export default buttonColorAccentToSuccessTransform;