styled-components-to-stylex-codemod 0.0.9 → 0.0.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "styled-components-to-stylex-codemod",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "description": "Codemod to transform styled-components to StyleX",
5
5
  "keywords": [
6
6
  "codemod",
@@ -32,43 +32,47 @@
32
32
  "jscodeshift": "^17.3.0",
33
33
  "postcss-selector-parser": "^7.1.1",
34
34
  "postcss-value-parser": "^4.2.0",
35
- "stylis": "^4.3.2"
35
+ "stylis": "^4.3.6"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@codemirror/lang-javascript": "^6.2.4",
39
39
  "@emotion/is-prop-valid": "^1.4.0",
40
- "@storybook/react": "^10.1.10",
41
- "@storybook/react-vite": "^10.1.10",
42
- "@stylexjs/babel-plugin": "^0.17.4",
40
+ "@storybook/react-vite": "^10.2.7",
41
+ "@stylexjs/babel-plugin": "^0.17.5",
43
42
  "@stylexjs/eslint-plugin": "^0.17.5",
44
- "@stylexjs/stylex": "^0.17.4",
45
- "@stylexjs/unplugin": "^0.17.4",
43
+ "@stylexjs/stylex": "^0.17.5",
44
+ "@stylexjs/unplugin": "^0.17.5",
46
45
  "@types/jscodeshift": "^17.3.0",
47
- "@types/node": "^25.0.3",
48
- "@types/react": "^19.2.7",
46
+ "@types/node": "^25.2.1",
47
+ "@types/react": "^19.2.13",
49
48
  "@types/react-dom": "^19.2.3",
50
- "@types/stylis": "^4.2.5",
51
- "@typescript-eslint/parser": "^8.53.0",
49
+ "@types/stylis": "^4.2.7",
50
+ "@typescript-eslint/parser": "^8.54.0",
52
51
  "@uiw/react-codemirror": "^4.25.4",
53
- "@vitejs/plugin-react": "^5.1.2",
54
- "@vitest/coverage-v8": "^4.0.16",
52
+ "@vitejs/plugin-react": "^5.1.3",
53
+ "@vitest/coverage-v8": "^4.0.18",
55
54
  "assert": "^2.1.0",
56
55
  "concurrently": "^9.2.1",
57
56
  "eslint": "^9.39.2",
58
- "knip": "^5.81.0",
59
- "lefthook": "^2.0.13",
60
- "oxfmt": "^0.20.0",
61
- "oxlint": "^1.35.0",
62
- "oxlint-tsgolint": "^0.10.1",
57
+ "eslint-plugin-storybook": "10.2.7",
58
+ "knip": "^5.83.1",
59
+ "lefthook": "^2.1.0",
60
+ "oxfmt": "^0.28.0",
61
+ "oxlint": "^1.43.0",
62
+ "oxlint-tsgolint": "^0.11.4",
63
63
  "path-browserify": "^1.0.1",
64
- "react": "^19.2.3",
65
- "react-dom": "^19.2.3",
66
- "storybook": "^10.1.10",
67
- "styled-components": "^6.1.19",
68
- "tsdown": "^0.18.3",
64
+ "pixelmatch": "^7.1.0",
65
+ "playwright": "^1.58.2",
66
+ "pngjs": "^7.0.0",
67
+ "react": "^19.2.4",
68
+ "react-dom": "^19.2.4",
69
+ "react-select": "^5.10.2",
70
+ "storybook": "^10.2.7",
71
+ "styled-components": "^6.3.8",
72
+ "tsdown": "^0.20.3",
69
73
  "typescript": "^5.9.3",
70
- "vite": "^7.3.0",
71
- "vitest": "^4.0.16"
74
+ "vite": "^7.3.1",
75
+ "vitest": "^4.0.18"
72
76
  },
73
77
  "engines": {
74
78
  "node": ">=22.20",
@@ -81,6 +85,7 @@
81
85
  "test:run": "vitest run",
82
86
  "test:coverage": "vitest run --coverage",
83
87
  "format": "oxfmt",
88
+ "format:check": "oxfmt --check",
84
89
  "lint": "concurrently 'pnpm:lint:oxlint' 'pnpm:lint:eslint'",
85
90
  "lint:oxlint": "oxlint --type-aware --deny-warnings",
86
91
  "lint:eslint": "eslint . --cache --max-warnings=0 --no-warn-ignored",
@@ -88,10 +93,12 @@
88
93
  "typecheck:fixtures": "tsc -p test-cases/tsconfig.json --noEmit",
89
94
  "typecheck:playground": "tsc -p playground/tsconfig.json --noEmit",
90
95
  "typecheck": "concurrently 'pnpm:typecheck:*'",
91
- "check": "concurrently 'pnpm:lint' 'pnpm:typecheck' 'pnpm:test:run' 'pnpm:knip'",
92
- "ci": "pnpm lint && pnpm typecheck && pnpm test:run && pnpm knip && pnpm build",
96
+ "check": "concurrently 'pnpm:lint' 'pnpm:typecheck' 'pnpm:test:run' 'pnpm:knip' 'pnpm:storybook:build' 'pnpm:build' 'pnpm:format:check'",
97
+ "ci": "pnpm lint && pnpm typecheck && pnpm test:run && pnpm knip && pnpm storybook:build && pnpm build && pnpm format:check",
93
98
  "knip": "knip",
99
+ "verify-rendering": "node scripts/verify-storybook-rendering.mts",
94
100
  "storybook": "storybook dev -p 6006",
101
+ "storybook:build": "storybook build",
95
102
  "playground": "vite --config playground/vite.config.ts",
96
103
  "build:playground": "vite build --config playground/vite.config.ts"
97
104
  }
@@ -1,250 +0,0 @@
1
- //#region src/adapter.d.ts
2
- /**
3
- * Adapter - Single user entry point for customizing the codemod.
4
- */
5
- type ThemeResolveContext = {
6
- kind: "theme";
7
- path: string;
8
- /**
9
- * Absolute path of the file currently being transformed.
10
- * Useful for adapter logic that wants to branch by caller file.
11
- */
12
- filePath?: string;
13
- };
14
- type CssVariableResolveContext = {
15
- kind: "cssVariable";
16
- name: string;
17
- fallback?: string;
18
- definedValue?: string;
19
- /**
20
- * Absolute path of the file currently being transformed.
21
- * Useful for adapter logic that wants to branch by caller file.
22
- */
23
- filePath?: string;
24
- };
25
- type CallResolveContext = {
26
- /**
27
- * Absolute path of the file currently being transformed.
28
- * Useful for adapter logic that wants to branch by caller file.
29
- */
30
- callSiteFilePath: string;
31
- /**
32
- * Imported name when the callee is a named import (including aliases).
33
- * Example: `import { transitionSpeed as ts } ...; ts("x")` -> "transitionSpeed"
34
- */
35
- calleeImportedName: string;
36
- /**
37
- * Import source for this call: either an absolute file path (relative imports)
38
- * or the module specifier (package imports).
39
- */
40
- calleeSource: {
41
- kind: "absolutePath";
42
- value: string;
43
- } | {
44
- kind: "specifier";
45
- value: string;
46
- };
47
- /**
48
- * Call arguments (only literals are surfaced precisely; everything else is `unknown`).
49
- */
50
- args: Array<{
51
- kind: "literal";
52
- value: string | number | boolean | null;
53
- } | {
54
- kind: "unknown";
55
- }>;
56
- };
57
- /**
58
- * Context for `adapter.resolveValue(...)` (theme + css variables).
59
- *
60
- * Helper calls are handled separately via `adapter.resolveCall(...)`.
61
- */
62
- type ResolveValueContext = ThemeResolveContext | CssVariableResolveContext;
63
- /**
64
- * Result for `adapter.resolveValue(...)` (theme + css variables).
65
- */
66
- type ResolveValueResult = {
67
- /**
68
- * JS expression string to inline into generated output.
69
- * Example: `vars.spacingSm` or `calcVars.baseSize`
70
- */
71
- expr: string;
72
- /**
73
- * Import statements required by `expr`.
74
- * These are rendered and merged into the file by the codemod.
75
- */
76
- imports: ImportSpec[];
77
- /**
78
- * If true, the transformer should drop the corresponding `--name: ...` definition
79
- * from the emitted style object (useful when replacing with StyleX vars).
80
- *
81
- * Note: Only meaningful for `{ kind: "cssVariable" }`.
82
- */
83
- dropDefinition?: boolean;
84
- };
85
- type CallResolveResult = {
86
- /**
87
- * Disambiguates how the resolved expression is used:
88
- * - "props": a StyleX style object suitable for passing to `stylex.props(...)`.
89
- * - "create": a value that can be used inside `stylex.create(...)` (e.g. tokens/vars).
90
- */
91
- usage: "props" | "create";
92
- /**
93
- * JS expression string to inline into generated output.
94
- * Example (value): `vars.spacingSm`
95
- * Example (styles): `borders.labelMuted`
96
- */
97
- expr: string;
98
- /**
99
- * Import statements required by `expr`.
100
- * These are rendered and merged into the file by the codemod.
101
- */
102
- imports: ImportSpec[];
103
- };
104
- type ImportSource = {
105
- kind: "absolutePath";
106
- value: string;
107
- } | {
108
- kind: "specifier";
109
- value: string;
110
- };
111
- type ImportSpec = {
112
- from: ImportSource;
113
- names: Array<{
114
- imported: string;
115
- local?: string;
116
- }>;
117
- };
118
- interface ExternalStylesContext {
119
- /** Absolute path of the file being transformed */
120
- filePath: string;
121
- /** Local name of the styled component */
122
- componentName: string;
123
- /** The export name (may differ from componentName for renamed exports) */
124
- exportName: string;
125
- /** Whether it's a default export */
126
- isDefaultExport: boolean;
127
- }
128
- /**
129
- * Configuration for a custom style merger function that combines stylex.props()
130
- * results with external className/style props.
131
- *
132
- * When configured, generates cleaner output:
133
- * `{...stylexProps(styles.foo, className, style)}`
134
- * instead of the verbose pattern:
135
- * `{...sx} className={[sx.className, className].filter(Boolean).join(" ")} style={{...sx.style, ...style}}`
136
- */
137
- interface StyleMergerConfig {
138
- /**
139
- * Function name to use for merging (e.g., "stylexProps" or "mergeStylexProps").
140
- */
141
- functionName: string;
142
- /**
143
- * Import source for the merger function.
144
- * Example: `{ kind: "specifier", value: "@company/ui-utils" }`
145
- */
146
- importSource: ImportSource;
147
- }
148
- interface Adapter {
149
- /**
150
- * Resolver for theme paths + CSS variables.
151
- *
152
- * Notes:
153
- * - Return `{ expr, imports }` for both theme + css variables.
154
- * - Optionally return `{ dropDefinition: true }` for css variables to remove the local `--x: ...` definition.
155
- * - Return `null` to leave a value unresolved.
156
- */
157
- resolveValue: (context: ResolveValueContext) => ResolveValueResult | null;
158
- /**
159
- * Resolver for helper calls found inside template interpolations.
160
- *
161
- * Return:
162
- * - `{ usage: "props", expr, imports }` when the call resolves to a StyleX style object
163
- * (usable as an argument to `stylex.props(...)`).
164
- * - `{ usage: "create", expr, imports }` when the call resolves to a single CSS value
165
- * (usable inside `stylex.create(...)` declarations).
166
- * - `null` to leave the call unresolved (the file may bail with a warning depending on context).
167
- */
168
- resolveCall: (context: CallResolveContext) => CallResolveResult | null;
169
- /**
170
- * Called for exported styled components to determine if they should support
171
- * external className/style extension. Return true to generate wrapper with
172
- * className/style/rest merging.
173
- */
174
- shouldSupportExternalStyling: (context: ExternalStylesContext) => boolean;
175
- /**
176
- * Custom merger function for className/style combining.
177
- * When provided, generates cleaner output using this function instead of
178
- * the verbose className/style merging pattern.
179
- * Set to `null` to use the verbose pattern (default).
180
- *
181
- * Expected merger function signature:
182
- * ```typescript
183
- * function merger(
184
- * styles: StyleXStyles | StyleXStyles[],
185
- * className?: string,
186
- * style?: React.CSSProperties
187
- * ): { className?: string; style?: React.CSSProperties }
188
- * ```
189
- */
190
- styleMerger: StyleMergerConfig | null;
191
- }
192
- /**
193
- * Helper for nicer user authoring + type inference.
194
- *
195
- * `defineAdapter(...)` also performs runtime validation (helpful for JS consumers)
196
- * and will throw a descriptive error message if the adapter shape is invalid.
197
- *
198
- * Usage:
199
- * export default defineAdapter({
200
- * resolveValue(ctx) {
201
- * if (ctx.kind === "theme") {
202
- * return {
203
- * expr: `tokens.${ctx.path}`,
204
- * imports: [
205
- * { from: { kind: "specifier", value: "./tokens" }, names: [{ imported: "tokens" }] },
206
- * ],
207
- * };
208
- * }
209
- * return null;
210
- * },
211
- *
212
- * resolveCall(ctx) {
213
- * // Resolve helper calls inside template interpolations.
214
- * // Return:
215
- * // - { usage: "props", expr, imports } for StyleX styles (usable in stylex.props)
216
- * // - { usage: "create", expr, imports } for a single value (usable in stylex.create)
217
- * // - null to leave the call unresolved
218
- * void ctx;
219
- * return null;
220
- * },
221
- *
222
- * // Enable className/style/rest support for exported components
223
- * shouldSupportExternalStyling(ctx) {
224
- * // Example: Enable for all exported components in a shared components folder
225
- * return ctx.filePath.includes("/shared/components/");
226
- * },
227
- *
228
- * // Optional: provide a custom merger, or use `null` for the default verbose merge output
229
- * styleMerger: null,
230
- * });
231
- */
232
- declare function defineAdapter(adapter: Adapter): Adapter;
233
- //#endregion
234
- //#region src/internal/logger.d.ts
235
- type Severity = "info" | "warning" | "error";
236
- interface WarningLog {
237
- severity: Severity;
238
- type: "unsupported-feature" | "dynamic-node";
239
- message: string;
240
- loc?: {
241
- line: number;
242
- column: number;
243
- };
244
- context?: unknown;
245
- }
246
- interface CollectedWarning extends WarningLog {
247
- filePath: string;
248
- }
249
- //#endregion
250
- export { defineAdapter as i, WarningLog as n, Adapter as r, CollectedWarning as t };
@@ -1,177 +0,0 @@
1
- //#region src/internal/public-api-validation.ts
2
- function describeValue(value) {
3
- if (value === null) return "null";
4
- if (value === void 0) return "undefined";
5
- if (Array.isArray(value)) return `Array(${value.length})`;
6
- if (typeof value === "string") return `"${value}"`;
7
- if (typeof value === "number" || typeof value === "boolean") return String(value);
8
- if (typeof value === "bigint") return value.toString();
9
- if (typeof value === "symbol") return value.description ? `Symbol(${value.description})` : "Symbol()";
10
- if (typeof value === "function") return "[Function]";
11
- if (typeof value === "object") {
12
- const ctor = value?.constructor?.name ?? "Object";
13
- let keys = [];
14
- try {
15
- keys = Object.keys(value);
16
- } catch {}
17
- const preview = keys.slice(0, 5).join(", ");
18
- const suffix = keys.length > 5 ? ", ..." : "";
19
- return keys.length ? `${ctor} { ${preview}${suffix} }` : ctor;
20
- }
21
- return "[Unknown]";
22
- }
23
- function assertValidAdapter(candidate, where) {
24
- const obj = candidate;
25
- const resolveValue = obj?.resolveValue;
26
- const resolveCall = obj?.resolveCall;
27
- const shouldSupportExternalStyling = obj?.shouldSupportExternalStyling;
28
- if (!candidate || typeof candidate !== "object") throw new Error([
29
- `${where}: expected an adapter object.`,
30
- `Received: ${describeValue(candidate)}`,
31
- "",
32
- "Adapter requirements:",
33
- " - adapter.resolveValue(context) is required",
34
- " - adapter.resolveCall(context) is required",
35
- " - adapter.shouldSupportExternalStyling(context) is required",
36
- "",
37
- "resolveValue(context) is called with one of these shapes:",
38
- " - { kind: \"theme\", path }",
39
- " - { kind: \"cssVariable\", name, fallback?, definedValue? }",
40
- "",
41
- "resolveCall(context) is called with:",
42
- " - { callSiteFilePath, calleeImportedName, calleeSource, args }",
43
- "",
44
- `Docs/examples: ${ADAPTER_DOCS_URL}`
45
- ].join("\n"));
46
- if (typeof resolveValue !== "function") throw new Error([
47
- `${where}: adapter.resolveValue must be a function.`,
48
- `Received: resolveValue=${describeValue(resolveValue)}`,
49
- "",
50
- "Adapter shape:",
51
- " {",
52
- " resolveValue(context) {",
53
- " // theme/cssVariable -> { expr, imports, dropDefinition? } | null",
54
- " }",
55
- " resolveCall(context) { return { usage: \"props\" | \"create\", expr, imports } | null }",
56
- " }",
57
- "",
58
- `Docs/examples: ${ADAPTER_DOCS_URL}`
59
- ].join("\n"));
60
- if (typeof resolveCall !== "function") throw new Error([
61
- `${where}: adapter.resolveCall must be a function.`,
62
- `Received: resolveCall=${describeValue(resolveCall)}`,
63
- "",
64
- "Adapter shape:",
65
- " {",
66
- " resolveCall(context) { return { usage: \"props\" | \"create\", expr: string, imports: ImportSpec[] } | null }",
67
- " }",
68
- "",
69
- `Docs/examples: ${ADAPTER_DOCS_URL}`
70
- ].join("\n"));
71
- if (typeof shouldSupportExternalStyling !== "function") throw new Error([`${where}: adapter.shouldSupportExternalStyling must be a function.`, `Received: shouldSupportExternalStyling=${describeValue(shouldSupportExternalStyling)}`].join("\n"));
72
- const styleMerger = obj?.styleMerger;
73
- if (styleMerger !== null && styleMerger !== void 0) {
74
- if (typeof styleMerger !== "object") throw new Error([
75
- `${where}: adapter.styleMerger must be null or an object.`,
76
- `Received: styleMerger=${describeValue(styleMerger)}`,
77
- "",
78
- "Expected shape:",
79
- " {",
80
- " functionName: \"stylexProps\",",
81
- " importSource: { kind: \"specifier\", value: \"@company/ui-utils\" }",
82
- " }"
83
- ].join("\n"));
84
- const { functionName, importSource } = styleMerger;
85
- if (typeof functionName !== "string" || !functionName.trim()) throw new Error([`${where}: adapter.styleMerger.functionName must be a non-empty string.`, `Received: functionName=${describeValue(functionName)}`].join("\n"));
86
- if (!importSource || typeof importSource !== "object") throw new Error([
87
- `${where}: adapter.styleMerger.importSource must be an object.`,
88
- `Received: importSource=${describeValue(importSource)}`,
89
- "",
90
- "Expected shape:",
91
- " { kind: \"specifier\", value: \"@company/ui-utils\" }",
92
- " or",
93
- " { kind: \"absolutePath\", value: \"/path/to/module.ts\" }"
94
- ].join("\n"));
95
- const { kind, value } = importSource;
96
- if (kind !== "specifier" && kind !== "absolutePath") throw new Error([`${where}: adapter.styleMerger.importSource.kind must be "specifier" or "absolutePath".`, `Received: kind=${describeValue(kind)}`].join("\n"));
97
- if (typeof value !== "string" || !value.trim()) throw new Error([`${where}: adapter.styleMerger.importSource.value must be a non-empty string.`, `Received: value=${describeValue(value)}`].join("\n"));
98
- }
99
- }
100
- const ADAPTER_DOCS_URL = `https://github.com/skovhus/styled-components-to-stylex-codemod#adapter`;
101
-
102
- //#endregion
103
- //#region src/internal/logger.ts
104
- var Logger = class Logger {
105
- static flushWarnings() {
106
- const result = collected;
107
- collected = [];
108
- return result;
109
- }
110
- /**
111
- * Log a warning message to stderr.
112
- * All codemod warnings go through this so tests can mock it.
113
- */
114
- static warn(message, context) {
115
- Logger.writeWithSpacing(message, context);
116
- }
117
- /**
118
- * Log an error message to stderr with file path and optional location.
119
- * Formats like warnings: "Error filepath:line:column\nmessage"
120
- */
121
- static logError(message, filePath, loc, context) {
122
- const location = loc ? `${filePath}:${loc.line}:${loc.column}` : filePath;
123
- const label = Logger.colorizeErrorLabel("Error");
124
- Logger.writeWithSpacing(`${label} ${location}\n${message}`, context);
125
- }
126
- /**
127
- * Log transform warnings to stderr and collect them.
128
- */
129
- static logWarnings(warnings, filePath) {
130
- for (const warning of warnings) {
131
- collected.push({
132
- ...warning,
133
- filePath
134
- });
135
- const location = warning.loc ? `${filePath}:${warning.loc.line}:${warning.loc.column}` : `${filePath}`;
136
- const label = Logger.colorizeSeverityLabel(warning.severity);
137
- Logger.writeWithSpacing(`${label} ${location}\n${warning.message}`, warning.context);
138
- }
139
- }
140
- static writeWithSpacing(message, context) {
141
- const trimmed = message.replace(/\s+$/u, "");
142
- const serialized = Logger.formatContext(context);
143
- process.stderr.write(`${trimmed}${serialized ? `\n${serialized}` : ""}\n\n`);
144
- }
145
- static colorizeSeverityLabel(severity) {
146
- if (severity === "error") return Logger.colorizeErrorLabel("Error");
147
- if (severity === "info") return Logger.colorizeInfoLabel("Info");
148
- return Logger.colorizeWarnLabel("Warning");
149
- }
150
- static colorizeWarnLabel(label) {
151
- if (!process.stderr.isTTY) return label;
152
- return `${WARN_BG_COLOR}${WARN_TEXT_COLOR}${label}${RESET_COLOR}`;
153
- }
154
- static colorizeErrorLabel(label) {
155
- if (!process.stderr.isTTY) return label;
156
- return `${ERROR_BG_COLOR}${ERROR_TEXT_COLOR}${label}${RESET_COLOR}`;
157
- }
158
- static colorizeInfoLabel(label) {
159
- if (!process.stderr.isTTY) return label;
160
- return `${INFO_BG_COLOR}${INFO_TEXT_COLOR}${label}${RESET_COLOR}`;
161
- }
162
- static formatContext(context) {
163
- if (typeof context === "undefined") return null;
164
- return JSON.stringify(context, null, 2);
165
- }
166
- };
167
- let collected = [];
168
- const WARN_BG_COLOR = "\x1B[43m";
169
- const WARN_TEXT_COLOR = "\x1B[30m";
170
- const ERROR_BG_COLOR = "\x1B[41m";
171
- const ERROR_TEXT_COLOR = "\x1B[37m";
172
- const INFO_BG_COLOR = "\x1B[44m";
173
- const INFO_TEXT_COLOR = "\x1B[37m";
174
- const RESET_COLOR = "\x1B[0m";
175
-
176
- //#endregion
177
- export { assertValidAdapter as n, describeValue as r, Logger as t };