styled-components-to-stylex-codemod 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Transform styled-components to StyleX.
4
4
 
5
+ **[Try it in the online playground](https://skovhus.github.io/styled-components-to-stylex-codemod/)** — experiment with the transform in your browser.
6
+
7
+ > [!WARNING]
8
+ >
9
+ > **Very much under construction (alpha):** this codemod is still early in development — expect rough edges! 🚧
10
+
5
11
  ## Installation
6
12
 
7
13
  ```bash
@@ -140,6 +146,82 @@ Adapters are the main extension point. They let you control:
140
146
  - how theme paths and CSS variables are turned into StyleX-compatible JS values (`resolveValue`)
141
147
  - what extra imports to inject into transformed files (returned from `resolveValue`)
142
148
  - how helper calls are resolved (via `resolveValue({ kind: "call", ... })`)
149
+ - which exported components should support external className/style extension (`shouldSupportExternalStyling`)
150
+ - how className/style merging is handled for components accepting external styling (`styleMerger`)
151
+
152
+ #### Style Merger
153
+
154
+ When a component accepts external `className` and/or `style` props (e.g., via `shouldSupportExternalStyling`, or when wrapping a base component that already accepts these props), the generated code needs to merge StyleX styles with externally passed values.
155
+
156
+ > **Note:** Allowing external className/style props is generally discouraged in StyleX as it bypasses the type-safe styling system. However, it can be useful during migration to maintain compatibility with existing code that passes these props.
157
+
158
+ By default, this generates verbose inline merging code. You can provide a `styleMerger` to use a helper function instead for cleaner output:
159
+
160
+ ```ts
161
+ const adapter = defineAdapter({
162
+ resolveValue(ctx) {
163
+ // ... value resolution logic
164
+ return null;
165
+ },
166
+
167
+ shouldSupportExternalStyling(ctx) {
168
+ return ctx.filePath.includes("/shared/components/");
169
+ },
170
+
171
+ // Use a custom merger function for cleaner output
172
+ styleMerger: {
173
+ functionName: "mergedSx",
174
+ importSource: { kind: "specifier", value: "./lib/mergedSx" },
175
+ },
176
+ });
177
+ ```
178
+
179
+ The merger function should have this signature:
180
+
181
+ ```ts
182
+ function mergedSx(
183
+ styles: StyleXStyles,
184
+ className?: string,
185
+ style?: React.CSSProperties
186
+ ): ReturnType<typeof stylex.props>;
187
+ ```
188
+
189
+ See [`test-cases/lib/mergedSx.ts`](./test-cases/lib/mergedSx.ts) for a reference implementation.
190
+
191
+ #### External Styles Support
192
+
193
+ Transformed components are "closed" by default — they don't accept external `className` or `style` props. Use `shouldSupportExternalStyling` to control which exported components should support external styling:
194
+
195
+ ```ts
196
+ const adapter = defineAdapter({
197
+ resolveValue(ctx) {
198
+ // ... value resolution logic
199
+ return null;
200
+ },
201
+
202
+ shouldSupportExternalStyling(ctx) {
203
+ // ctx: { filePath, componentName, exportName, isDefaultExport }
204
+
205
+ // Example: Enable for all exports in shared components folder
206
+ if (ctx.filePath.includes("/shared/components/")) {
207
+ return true;
208
+ }
209
+
210
+ // Example: Enable for specific component names
211
+ if (ctx.componentName === "Button" || ctx.componentName === "Card") {
212
+ return true;
213
+ }
214
+
215
+ return false;
216
+ },
217
+ });
218
+ ```
219
+
220
+ When `shouldSupportExternalStyling` returns `true`, the generated component will:
221
+
222
+ - Accept `className` and `style` props
223
+ - Merge them with the StyleX-generated styles
224
+ - Forward remaining props via `...rest`
143
225
 
144
226
  #### Dynamic interpolations
145
227
 
@@ -154,23 +236,12 @@ If the pipeline can’t resolve an interpolation:
154
236
  - for `withConfig({ shouldForwardProp })` wrappers, the transform preserves the value as an inline style so output keeps visual parity
155
237
  - otherwise, the declaration containing that interpolation is **dropped** and a warning is produced (manual follow-up required)
156
238
 
157
- ### Notes / Limitations
239
+ ### Limitations
158
240
 
241
+ - **Flow** type generation is non-existing, works best with TypeScript or plain JS right now. Contributions more than welcome!
159
242
  - **ThemeProvider**: if a file imports and uses `ThemeProvider` from `styled-components`, the transform **skips the entire file** (theming strategy is project-specific).
160
243
  - **createGlobalStyle**: detected usage is reported as an **unsupported-feature** warning (StyleX does not support global styles in the same way).
161
244
 
162
- ### Transform Result
163
-
164
- ```ts
165
- interface RunTransformResult {
166
- errors: number; // Files that had errors
167
- unchanged: number; // Files that were unchanged
168
- skipped: number; // Files that were skipped
169
- transformed: number; // Files that were transformed
170
- timeElapsed: number; // Total time in seconds
171
- }
172
- ```
173
-
174
245
  ## License
175
246
 
176
247
  MIT
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as defineAdapter, i as Adapter, r as TransformWarning } from "./transform-types-CCX_WVPh.mjs";
1
+ import { a as defineAdapter, i as Adapter, r as TransformWarning } from "./transform-types-t2Vk9Bka.mjs";
2
2
 
3
3
  //#region src/internal/logger.d.ts
4
4
  interface CollectedWarning extends TransformWarning {
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as logWarning, t as flushWarnings } from "./logger-D09HOyqF.mjs";
1
+ import { a as describeValue, i as assertValidAdapter, n as logWarning, t as flushWarnings } from "./logger-Dhb8r1Ry.mjs";
2
2
  import { run } from "jscodeshift/src/Runner.js";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import { dirname, join } from "node:path";
@@ -8,6 +8,9 @@ import { spawn } from "node:child_process";
8
8
 
9
9
  //#region src/adapter.ts
10
10
  /**
11
+ * Adapter - Single user entry point for customizing the codemod.
12
+ */
13
+ /**
11
14
  * Helper for nicer user authoring + type inference.
12
15
  *
13
16
  * Usage:
@@ -24,14 +27,15 @@ import { spawn } from "node:child_process";
24
27
  * return null;
25
28
  * },
26
29
  *
27
- * // Optional: Enable className/style/rest support for exported components
28
- * shouldSupportExternalStyles(ctx) {
30
+ * // Enable className/style/rest support for exported components
31
+ * shouldSupportExternalStyling(ctx) {
29
32
  * // Example: Enable for all exported components in a shared components folder
30
33
  * return ctx.filePath.includes("/shared/components/");
31
34
  * },
32
35
  * });
33
36
  */
34
37
  function defineAdapter(adapter) {
38
+ assertValidAdapter(adapter, "defineAdapter(adapter)");
35
39
  return adapter;
36
40
  }
37
41
 
@@ -64,19 +68,46 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
64
68
  * ```
65
69
  */
66
70
  async function runTransform(options) {
71
+ if (!options || typeof options !== "object") throw new Error([
72
+ "[styled-components-to-stylex] runTransform(options) was called with an invalid argument.",
73
+ "Expected: runTransform({ files: string | string[], adapter: Adapter, ... })",
74
+ `Received: ${describeValue(options)}`,
75
+ "",
76
+ "Example (plain JS):",
77
+ " import { runTransform, defineAdapter } from \"styled-components-to-stylex-codemod\";",
78
+ " const adapter = defineAdapter({ resolveValue() { return null; } });",
79
+ " await runTransform({ files: \"src/**/*.tsx\", adapter });"
80
+ ].join("\n"));
81
+ const filesValue = options.files;
82
+ if (typeof filesValue !== "string" && !Array.isArray(filesValue)) throw new Error([
83
+ "[styled-components-to-stylex] runTransform(options): `files` is required.",
84
+ "Expected: files: string | string[]",
85
+ `Received: files=${describeValue(filesValue)}`
86
+ ].join("\n"));
87
+ if (typeof filesValue === "string" && filesValue.trim() === "") throw new Error(["[styled-components-to-stylex] runTransform(options): `files` must be a non-empty string.", "Example: files: \"src/**/*.tsx\""].join("\n"));
88
+ if (Array.isArray(filesValue)) {
89
+ if (filesValue.length === 0) throw new Error(["[styled-components-to-stylex] runTransform(options): `files` must not be an empty array.", "Example: files: [\"src/**/*.ts\", \"src/**/*.tsx\"]"].join("\n"));
90
+ if (filesValue.find((p) => typeof p !== "string" || p.trim() === "") !== void 0) throw new Error(["[styled-components-to-stylex] runTransform(options): `files` array must contain non-empty strings.", `Received: files=${describeValue(filesValue)}`].join("\n"));
91
+ }
67
92
  const { files, dryRun = false, print = false, parser = "tsx", formatterCommand } = options;
68
93
  const adapter = options.adapter;
69
- if (!adapter || typeof adapter.resolveValue !== "function") throw new Error("Adapter must provide resolveValue(ctx) => { expr, imports } | null");
70
- const adapterWithLogging = { resolveValue(ctx) {
71
- try {
72
- return adapter.resolveValue(ctx);
73
- } catch (e) {
74
- const kind = ctx?.kind;
75
- const details = kind === "theme" ? `path=${String(ctx.path ?? "")}` : kind === "cssVariable" ? `name=${String(ctx.name ?? "")}` : kind === "call" ? `callee=${String(ctx.calleeImportedName ?? "")} source=${String(ctx.calleeSource?.value ?? "")} file=${String(ctx.callSiteFilePath ?? "")}` : "";
76
- logWarning(`[styled-components-to-stylex] adapter.resolveValue threw${kind ? ` (kind=${kind}${details ? ` ${details}` : ""})` : ""}: ${e?.stack ?? String(e)}\n`);
77
- throw e;
94
+ assertValidAdapter(adapter, "runTransform(options)");
95
+ const adapterWithLogging = {
96
+ styleMerger: adapter.styleMerger,
97
+ shouldSupportExternalStyling(ctx) {
98
+ return adapter.shouldSupportExternalStyling(ctx);
99
+ },
100
+ resolveValue(ctx) {
101
+ try {
102
+ return adapter.resolveValue(ctx);
103
+ } catch (e) {
104
+ const kind = ctx?.kind;
105
+ const details = kind === "theme" ? `path=${String(ctx.path ?? "")}` : kind === "cssVariable" ? `name=${String(ctx.name ?? "")}` : kind === "call" ? `callee=${String(ctx.calleeImportedName ?? "")} source=${String(ctx.calleeSource?.value ?? "")} file=${String(ctx.callSiteFilePath ?? "")}` : "";
106
+ logWarning(`[styled-components-to-stylex] adapter.resolveValue threw${kind ? ` (kind=${kind}${details ? ` ${details}` : ""})` : ""}: ${e?.stack ?? String(e)}\n`);
107
+ throw e;
108
+ }
78
109
  }
79
- } };
110
+ };
80
111
  const patterns = Array.isArray(files) ? files : [files];
81
112
  const filePaths = [];
82
113
  const cwd = process.cwd();
@@ -0,0 +1,116 @@
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 shouldSupportExternalStyling = obj?.shouldSupportExternalStyling;
27
+ if (!candidate || typeof candidate !== "object") throw new Error([
28
+ `[styled-components-to-stylex] ${where}: expected an adapter object.`,
29
+ `Received: ${describeValue(candidate)}`,
30
+ "",
31
+ "Adapter requirements:",
32
+ " - adapter.resolveValue(context) is required",
33
+ " - adapter.shouldSupportExternalStyling(context) is required",
34
+ "",
35
+ "resolveValue(context) is called with one of these shapes:",
36
+ " - { kind: \"theme\", path }",
37
+ " - { kind: \"cssVariable\", name, fallback?, definedValue? }",
38
+ " - { kind: \"call\", callSiteFilePath, calleeImportedName, calleeSource, args }",
39
+ "",
40
+ `Docs/examples: ${ADAPTER_DOCS_URL}`
41
+ ].join("\n"));
42
+ if (typeof resolveValue !== "function") throw new Error([
43
+ `[styled-components-to-stylex] ${where}: adapter.resolveValue must be a function.`,
44
+ `Received: resolveValue=${describeValue(resolveValue)}`,
45
+ "",
46
+ "Adapter shape:",
47
+ " {",
48
+ " resolveValue(context) { return { expr: string, imports: ImportSpec[] } | null }",
49
+ " }",
50
+ "",
51
+ `Docs/examples: ${ADAPTER_DOCS_URL}`
52
+ ].join("\n"));
53
+ if (typeof shouldSupportExternalStyling !== "function") throw new Error([`[styled-components-to-stylex] ${where}: adapter.shouldSupportExternalStyling must be a function.`, `Received: shouldSupportExternalStyling=${describeValue(shouldSupportExternalStyling)}`].join("\n"));
54
+ const styleMerger = obj?.styleMerger;
55
+ if (styleMerger !== null && styleMerger !== void 0) {
56
+ if (typeof styleMerger !== "object") throw new Error([
57
+ `[styled-components-to-stylex] ${where}: adapter.styleMerger must be null or an object.`,
58
+ `Received: styleMerger=${describeValue(styleMerger)}`,
59
+ "",
60
+ "Expected shape:",
61
+ " {",
62
+ " functionName: \"stylexProps\",",
63
+ " importSource: { kind: \"specifier\", value: \"@company/ui-utils\" }",
64
+ " }"
65
+ ].join("\n"));
66
+ const { functionName, importSource } = styleMerger;
67
+ if (typeof functionName !== "string" || !functionName.trim()) throw new Error([`[styled-components-to-stylex] ${where}: adapter.styleMerger.functionName must be a non-empty string.`, `Received: functionName=${describeValue(functionName)}`].join("\n"));
68
+ if (!importSource || typeof importSource !== "object") throw new Error([
69
+ `[styled-components-to-stylex] ${where}: adapter.styleMerger.importSource must be an object.`,
70
+ `Received: importSource=${describeValue(importSource)}`,
71
+ "",
72
+ "Expected shape:",
73
+ " { kind: \"specifier\", value: \"@company/ui-utils\" }",
74
+ " or",
75
+ " { kind: \"absolutePath\", value: \"/path/to/module.ts\" }"
76
+ ].join("\n"));
77
+ const { kind, value } = importSource;
78
+ if (kind !== "specifier" && kind !== "absolutePath") throw new Error([`[styled-components-to-stylex] ${where}: adapter.styleMerger.importSource.kind must be "specifier" or "absolutePath".`, `Received: kind=${describeValue(kind)}`].join("\n"));
79
+ if (typeof value !== "string" || !value.trim()) throw new Error([`[styled-components-to-stylex] ${where}: adapter.styleMerger.importSource.value must be a non-empty string.`, `Received: value=${describeValue(value)}`].join("\n"));
80
+ }
81
+ }
82
+ const ADAPTER_DOCS_URL = `https://github.com/skovhus/styled-components-to-stylex-codemod#adapter`;
83
+
84
+ //#endregion
85
+ //#region src/internal/logger.ts
86
+ /**
87
+ * Clear collected warnings and return them.
88
+ */
89
+ function flushWarnings() {
90
+ const result = collected;
91
+ collected = [];
92
+ return result;
93
+ }
94
+ /**
95
+ * Log a warning message to stderr.
96
+ * All codemod warnings go through this so tests can mock it.
97
+ */
98
+ function logWarning(message) {
99
+ process.stderr.write(message);
100
+ }
101
+ /**
102
+ * Log transform warnings to stderr and collect them.
103
+ */
104
+ function logWarnings(warnings, filePath) {
105
+ for (const warning of warnings) {
106
+ collected.push({
107
+ ...warning,
108
+ filePath
109
+ });
110
+ logWarning(`[styled-components-to-stylex] Warning${warning.loc ? ` (${filePath}:${warning.loc.line}:${warning.loc.column})` : ` (${filePath})`}: ${warning.message}\n`);
111
+ }
112
+ }
113
+ let collected = [];
114
+
115
+ //#endregion
116
+ export { describeValue as a, assertValidAdapter as i, logWarning as n, logWarnings as r, flushWarnings as t };
@@ -87,15 +87,51 @@ interface ExternalStylesContext {
87
87
  /** Whether it's a default export */
88
88
  isDefaultExport: boolean;
89
89
  }
90
+ /**
91
+ * Configuration for a custom style merger function that combines stylex.props()
92
+ * results with external className/style props.
93
+ *
94
+ * When configured, generates cleaner output:
95
+ * `{...stylexProps(styles.foo, className, style)}`
96
+ * instead of the verbose pattern:
97
+ * `{...sx} className={[sx.className, className].filter(Boolean).join(" ")} style={{...sx.style, ...style}}`
98
+ */
99
+ interface StyleMergerConfig {
100
+ /**
101
+ * Function name to use for merging (e.g., "stylexProps" or "mergeStylexProps").
102
+ */
103
+ functionName: string;
104
+ /**
105
+ * Import source for the merger function.
106
+ * Example: `{ kind: "specifier", value: "@company/ui-utils" }`
107
+ */
108
+ importSource: ImportSource;
109
+ }
90
110
  interface Adapter {
91
111
  /** Unified resolver for theme paths + CSS variables. Return null to leave unresolved. */
92
112
  resolveValue: (context: ResolveContext) => ResolveResult | null;
93
113
  /**
94
114
  * Called for exported styled components to determine if they should support
95
115
  * external className/style extension. Return true to generate wrapper with
96
- * className/style/rest merging. Default: false.
116
+ * className/style/rest merging.
117
+ */
118
+ shouldSupportExternalStyling: (context: ExternalStylesContext) => boolean;
119
+ /**
120
+ * Custom merger function for className/style combining.
121
+ * When provided, generates cleaner output using this function instead of
122
+ * the verbose className/style merging pattern.
123
+ * Set to `null` to use the verbose pattern (default).
124
+ *
125
+ * Expected merger function signature:
126
+ * ```typescript
127
+ * function merger(
128
+ * styles: StyleXStyles | StyleXStyles[],
129
+ * className?: string,
130
+ * style?: React.CSSProperties
131
+ * ): { className?: string; style?: React.CSSProperties }
132
+ * ```
97
133
  */
98
- shouldSupportExternalStyles?: (context: ExternalStylesContext) => boolean;
134
+ styleMerger: StyleMergerConfig | null;
99
135
  }
100
136
  /**
101
137
  * Helper for nicer user authoring + type inference.
@@ -114,8 +150,8 @@ interface Adapter {
114
150
  * return null;
115
151
  * },
116
152
  *
117
- * // Optional: Enable className/style/rest support for exported components
118
- * shouldSupportExternalStyles(ctx) {
153
+ * // Enable className/style/rest support for exported components
154
+ * shouldSupportExternalStyling(ctx) {
119
155
  * // Example: Enable for all exported components in a shared components folder
120
156
  * return ctx.filePath.includes("/shared/components/");
121
157
  * },
@@ -1,4 +1,4 @@
1
- import { n as TransformResult, r as TransformWarning, t as TransformOptions } from "./transform-types-CCX_WVPh.mjs";
1
+ import { n as TransformResult, r as TransformWarning, t as TransformOptions } from "./transform-types-t2Vk9Bka.mjs";
2
2
  import { API, FileInfo, Options } from "jscodeshift";
3
3
 
4
4
  //#region src/transform.d.ts