@mittwald/flow-codemods 1.2.0-next.0 → 1.2.0-next.10

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 (44) hide show
  1. package/README.md +35 -6
  2. package/dist/cli/codemod.d.ts +16 -0
  3. package/dist/cli/codemod.d.ts.map +1 -1
  4. package/dist/cli/codemod.js +21 -0
  5. package/dist/cli/list.d.ts +30 -1
  6. package/dist/cli/list.d.ts.map +1 -1
  7. package/dist/cli/list.js +15 -4
  8. package/dist/cli/upgrade.d.ts +7 -0
  9. package/dist/cli/upgrade.d.ts.map +1 -1
  10. package/dist/cli/upgrade.js +31 -20
  11. package/dist/cli.js +16 -1
  12. package/dist/install.d.ts +110 -11
  13. package/dist/install.d.ts.map +1 -1
  14. package/dist/install.js +163 -50
  15. package/dist/migrations/accent-box-color-to-background-color/transform.js +175 -0
  16. package/dist/migrations/action-prop-to-on-action/transform.js +82 -0
  17. package/dist/migrations/align-to-combine/transform.js +166 -0
  18. package/dist/migrations/button-color-accent-to-success/transform.js +126 -0
  19. package/dist/migrations/button-props-interfaces/transform.js +178 -0
  20. package/dist/migrations/color-primary-to-default/transform.js +135 -0
  21. package/dist/migrations/imports-to-package-root/transform.js +93 -0
  22. package/dist/migrations/muted-action-error-to-abort-action-error/transform.js +198 -0
  23. package/dist/migrations/package.json +3 -0
  24. package/dist/migrations/password-tools-rule/transform.js +173 -0
  25. package/dist/migrations/password-tools-subpath-renamed/transform.js +51 -0
  26. package/dist/migrations.generated.d.ts.map +1 -1
  27. package/dist/migrations.generated.js +12 -3
  28. package/dist/run/jscodeshift.d.ts +4 -1
  29. package/dist/run/jscodeshift.d.ts.map +1 -1
  30. package/dist/run/jscodeshift.js +60 -22
  31. package/dist/tools/package.json +3 -0
  32. package/dist/tools/to-remote-package.js +29 -0
  33. package/package.json +5 -6
  34. package/src/migrations/accent-box-color-to-background-color/transform.ts +0 -218
  35. package/src/migrations/action-prop-to-on-action/transform.ts +0 -110
  36. package/src/migrations/align-to-combine/transform.ts +0 -212
  37. package/src/migrations/button-color-accent-to-success/transform.ts +0 -164
  38. package/src/migrations/button-props-interfaces/transform.ts +0 -229
  39. package/src/migrations/color-primary-to-default/transform.ts +0 -173
  40. package/src/migrations/imports-to-package-root/transform.ts +0 -107
  41. package/src/migrations/muted-action-error-to-abort-action-error/transform.ts +0 -260
  42. package/src/migrations/password-tools-rule/transform.ts +0 -219
  43. package/src/migrations/password-tools-subpath-renamed/transform.ts +0 -61
  44. package/src/tools/to-remote-package.ts +0 -37
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mittwald/flow-codemods",
3
- "version": "1.2.0-next.0",
3
+ "version": "1.2.0-next.10",
4
4
  "type": "module",
5
5
  "description": "Codemods and an upgrade CLI for consumers of Flow, mittwald's design system",
6
6
  "homepage": "https://flow.mittwald.de",
@@ -9,21 +9,20 @@
9
9
  "flow-codemods": "dist/cli.js"
10
10
  },
11
11
  "files": [
12
- "dist",
13
- "src/migrations/**/transform.ts",
14
- "src/tools/to-remote-package.ts"
12
+ "dist"
15
13
  ],
16
14
  "engines": {
17
15
  "node": ">=22.0.0"
18
16
  },
19
17
  "scripts": {
20
- "build": "tsx dev/generateCli.ts && tsc -p tsconfig.build.json",
18
+ "build": "tsx dev/generateCli.ts && tsc -p tsconfig.build.json && tsx dev/buildTransforms.ts",
21
19
  "test:compile": "tsc --noEmit",
22
20
  "test:unit": "vitest run"
23
21
  },
24
22
  "dependencies": {
25
23
  "@inquirer/prompts": "^7.9.0",
26
24
  "jscodeshift": "^17.3.0",
25
+ "package-manager-detector": "^1.8.0",
27
26
  "picocolors": "^1.1.1",
28
27
  "registry-url": "^7.2.0",
29
28
  "semver": "^7.8.5"
@@ -38,5 +37,5 @@
38
37
  "vitest": "^4.1.11",
39
38
  "yaml": "^2.8.1"
40
39
  },
41
- "gitHead": "d43b17699970312ffb6c1eaafde8c3438bfc7f72"
40
+ "gitHead": "1a8de0c9ddcce49e78f0ca8a0c217e4d33a48615"
42
41
  }
@@ -1,218 +0,0 @@
1
- import type { Transform } from "jscodeshift";
2
-
3
- /**
4
- * Moves the background value of `AccentBox` from `color` to `backgroundColor`
5
- * (alpha.786).
6
- *
7
- * `color` did not go away, it changed meaning: it used to set the background
8
- * (`"blue" | "green" | "gradient" | "neutral"`) and now sets the content color
9
- * (`"default" | "dark" | "light" | "dark-static" | "light-static"`). A blanket
10
- * rename would therefore break every element that already uses the new meaning.
11
- * The transform decides per value instead: a value from the new content-color
12
- * union stays on `color`, every other literal moves to `backgroundColor`.
13
- *
14
- * The scope is deliberately narrow. Only JSX elements that resolve to
15
- * `AccentBox` — imported (named or as a namespace) from
16
- * `@mittwald/flow-react-components` or
17
- * `@mittwald/flow-remote-react-components`, including their subpath entries —
18
- * are touched.
19
- *
20
- * Two cases are left alone, because both are undecidable without knowing the
21
- * value:
22
- *
23
- * - Values it cannot read (`color={expression}`), and expressions that mix both
24
- * meanings (`color={flag ? "blue" : "dark"}`). The same expression means the
25
- * background in old code and the content color in new code, and a mix has no
26
- * single correct answer. An expression whose every value position is a
27
- * literal on the same side of the split _is_ decidable and gets renamed:
28
- * `color={flag ? "blue" : "green"}`.
29
- * - An element that already carries `backgroundColor`. Moving `color` there would
30
- * silently overwrite the explicit value.
31
- *
32
- * Both keep their `color` prop and need a look by hand.
33
- */
34
- const accentBoxColorToBackgroundColorTransform: Transform = (
35
- fileInfo,
36
- { j },
37
- ) => {
38
- const flowPackages = [
39
- "@mittwald/flow-react-components",
40
- "@mittwald/flow-remote-react-components",
41
- ];
42
- const affectedComponents = new Set(["AccentBox"]);
43
- /** Values `color` still accepts — everything else was a background color. */
44
- const contentColors = new Set([
45
- "default",
46
- "dark",
47
- "light",
48
- "dark-static",
49
- "light-static",
50
- ]);
51
-
52
- const isFlowImport = (source: string): boolean =>
53
- flowPackages.some((pkg) => source === pkg || source.startsWith(`${pkg}/`));
54
-
55
- /** The value of a string literal, or `undefined` for anything dynamic. */
56
- const literalValue = (node: unknown): string | undefined => {
57
- if (!node || typeof node !== "object" || !("type" in node)) {
58
- return undefined;
59
- }
60
- const { type } = node as { type: string };
61
- if (type !== "StringLiteral" && type !== "Literal") {
62
- return undefined;
63
- }
64
- const { value } = node as { value?: unknown };
65
- return typeof value === "string" ? value : undefined;
66
- };
67
-
68
- /**
69
- * Every literal that could become this attribute's value, or `undefined` when
70
- * any of those positions is something we cannot read. The positions are the
71
- * expression itself, both ternary branches, and the operands of `??`/`||` —
72
- * plus only the right operand of `&&`, since `&&` yields its left operand
73
- * only when that operand is falsy and a non-empty string never is.
74
- *
75
- * A list, not a single value, because this transform decides per attribute,
76
- * not per literal: `color={flag ? "blue" : "dark"}` mixes a background with a
77
- * content colour, and no single rename is right for both.
78
- */
79
- const valueLiterals = (node: unknown): string[] | undefined => {
80
- const literal = literalValue(node);
81
- if (literal !== undefined) {
82
- return [literal];
83
- }
84
- if (!node || typeof node !== "object" || !("type" in node)) {
85
- return undefined;
86
- }
87
- const typed = node as {
88
- type: string;
89
- operator?: string;
90
- consequent?: unknown;
91
- alternate?: unknown;
92
- left?: unknown;
93
- right?: unknown;
94
- };
95
- if (typed.type === "ConditionalExpression") {
96
- const consequent = valueLiterals(typed.consequent);
97
- const alternate = valueLiterals(typed.alternate);
98
- return consequent && alternate
99
- ? [...consequent, ...alternate]
100
- : undefined;
101
- }
102
- if (typed.type === "LogicalExpression") {
103
- const right = valueLiterals(typed.right);
104
- if (right === undefined) {
105
- return undefined;
106
- }
107
- if (typed.operator === "&&") {
108
- return right;
109
- }
110
- const left = valueLiterals(typed.left);
111
- return left ? [...left, ...right] : undefined;
112
- }
113
- return undefined;
114
- };
115
-
116
- const root = j(fileInfo.source, { parser: "tsx" });
117
-
118
- // Local JSX identifier -> canonical component name (resolves `as` aliases).
119
- const localToComponent = new Map<string, string>();
120
- // Local names of `import * as Flow` namespace imports from a Flow package.
121
- const flowNamespaces = new Set<string>();
122
-
123
- root
124
- .find(j.ImportDeclaration)
125
- .filter((path) => isFlowImport(String(path.node.source.value)))
126
- .forEach((path) => {
127
- for (const specifier of path.node.specifiers ?? []) {
128
- if (
129
- specifier.type === "ImportSpecifier" &&
130
- specifier.imported.type === "Identifier" &&
131
- affectedComponents.has(specifier.imported.name)
132
- ) {
133
- localToComponent.set(
134
- String(specifier.local?.name ?? specifier.imported.name),
135
- String(specifier.imported.name),
136
- );
137
- } else if (
138
- specifier.type === "ImportNamespaceSpecifier" &&
139
- specifier.local
140
- ) {
141
- flowNamespaces.add(String(specifier.local.name));
142
- }
143
- }
144
- });
145
-
146
- if (localToComponent.size === 0 && flowNamespaces.size === 0) {
147
- return fileInfo.source;
148
- }
149
-
150
- root.find(j.JSXOpeningElement).forEach((path) => {
151
- const name = path.node.name;
152
-
153
- let isAffected = false;
154
- if (name.type === "JSXIdentifier") {
155
- isAffected = localToComponent.has(name.name);
156
- } else if (
157
- name.type === "JSXMemberExpression" &&
158
- name.object.type === "JSXIdentifier" &&
159
- name.property.type === "JSXIdentifier"
160
- ) {
161
- isAffected =
162
- flowNamespaces.has(name.object.name) &&
163
- affectedComponents.has(name.property.name);
164
- }
165
-
166
- if (!isAffected) {
167
- return;
168
- }
169
-
170
- const attributes = path.node.attributes ?? [];
171
-
172
- const isNamed = (attribute: unknown, key: string): boolean =>
173
- !!attribute &&
174
- typeof attribute === "object" &&
175
- (attribute as { type?: string }).type === "JSXAttribute" &&
176
- (attribute as { name?: { type?: string; name?: string } }).name?.type ===
177
- "JSXIdentifier" &&
178
- (attribute as { name?: { name?: string } }).name?.name === key;
179
-
180
- // An explicit `backgroundColor` is the new API already — never overwrite it.
181
- if (attributes.some((attribute) => isNamed(attribute, "backgroundColor"))) {
182
- return;
183
- }
184
-
185
- for (const attribute of attributes) {
186
- if (!isNamed(attribute, "color") || attribute.type !== "JSXAttribute") {
187
- continue;
188
- }
189
-
190
- const value = attribute.value;
191
-
192
- const literals =
193
- value?.type === "JSXExpressionContainer"
194
- ? valueLiterals(value.expression)
195
- : valueLiterals(value);
196
-
197
- if (literals === undefined || literals.length === 0) {
198
- continue;
199
- }
200
-
201
- // All-or-nothing. Every value has to be a background colour for the
202
- // rename to be right; all content colours means the element already uses
203
- // the new meaning, and a mix has no single correct answer.
204
- const backgrounds = literals.filter(
205
- (literal) => !contentColors.has(literal),
206
- );
207
- if (backgrounds.length !== literals.length) {
208
- continue;
209
- }
210
-
211
- attribute.name = j.jsxIdentifier("backgroundColor");
212
- }
213
- });
214
-
215
- return root.toSource();
216
- };
217
-
218
- export default accentBoxColorToBackgroundColorTransform;
@@ -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;