styled-components-to-stylex-codemod 0.0.13 → 0.0.15

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.
@@ -0,0 +1,39 @@
1
+ //#region src/internal/utilities/selector-context-heuristic.ts
2
+ /**
3
+ * Shared heuristic for determining whether a placeholder/expression is in a
4
+ * CSS selector context rather than a property value context.
5
+ *
6
+ * Used by both the prepass scanner (on reconstructed CSS with placeholders)
7
+ * and the consumer patcher (on raw TypeScript source).
8
+ */
9
+ /**
10
+ * Given the text before and after a template expression, determine if the
11
+ * expression is in a CSS selector context (e.g. `${Foo} { ... }`) rather
12
+ * than a property value context (e.g. `color: ${Foo};`).
13
+ *
14
+ * Selector clues:
15
+ * - Followed by `{` → definitely a selector
16
+ * - `{` appears before next `;` without a value-separator colon → likely selector
17
+ *
18
+ * Value clues:
19
+ * - Preceded by `:` with no intervening `{`, `}`, or `;` → value context
20
+ * (but `:hover`, `:focus` etc. are pseudo-selectors, not values)
21
+ */
22
+ function isSelectorContext(before, after) {
23
+ const lastSemiOrBrace = Math.max(before.lastIndexOf(";"), before.lastIndexOf("{"), before.lastIndexOf("}"));
24
+ const lastColon = before.lastIndexOf(":");
25
+ if (lastColon > lastSemiOrBrace) {
26
+ const colonContext = before.slice(lastColon).trim();
27
+ if (!/^:[a-z-]+/i.test(colonContext)) return false;
28
+ }
29
+ if (after.startsWith("{")) return true;
30
+ const afterUpToBrace = after.split("{")[0] ?? "";
31
+ const afterUpToSemi = after.split(";")[0] ?? "";
32
+ if (afterUpToBrace.length < afterUpToSemi.length) {
33
+ if (!/:\s|:$/.test(afterUpToBrace)) return true;
34
+ }
35
+ return false;
36
+ }
37
+
38
+ //#endregion
39
+ export { isSelectorContext as t };
@@ -0,0 +1,38 @@
1
+ import { compile } from "stylis";
2
+
3
+ //#region src/internal/styled-css.ts
4
+ /** Matches `__SC_EXPR_N__` and captures the slot index in group 1. */
5
+ const PLACEHOLDER_RE = /__SC_EXPR_(\d+)__/;
6
+ function parseStyledTemplateLiteral(template) {
7
+ const parts = [];
8
+ const slots = [];
9
+ for (let i = 0; i < template.quasis.length; i++) {
10
+ const quasi = template.quasis[i];
11
+ parts.push(quasi.value.raw);
12
+ const expr = template.expressions[i];
13
+ if (!expr) continue;
14
+ const placeholder = makeInterpolationPlaceholder(i);
15
+ const startOffset = parts.join("").length;
16
+ parts.push(placeholder);
17
+ const endOffset = parts.join("").length;
18
+ slots.push({
19
+ index: i,
20
+ placeholder,
21
+ expression: expr,
22
+ startOffset,
23
+ endOffset
24
+ });
25
+ }
26
+ const rawCss = parts.join("");
27
+ return {
28
+ rawCss,
29
+ slots,
30
+ stylisAst: compile(rawCss)
31
+ };
32
+ }
33
+ function makeInterpolationPlaceholder(index) {
34
+ return `__SC_EXPR_${index}__`;
35
+ }
36
+
37
+ //#endregion
38
+ export { parseStyledTemplateLiteral as n, PLACEHOLDER_RE as t };
@@ -1,4 +1,4 @@
1
- import { n as WarningLog, r as Adapter } from "./logger-kU4pnRpt.mjs";
1
+ import { n as WarningLog, r as Adapter } from "./logger-B7SOfCti.mjs";
2
2
  import "stylis";
3
3
  import { API, FileInfo, Options } from "jscodeshift";
4
4
 
@@ -9,6 +9,18 @@ import { API, FileInfo, Options } from "jscodeshift";
9
9
  interface TransformResult {
10
10
  code: string | null;
11
11
  warnings: WarningLog[];
12
+ /** Content for the sidecar .stylex.ts file (defineMarker declarations). Undefined when no markers needed. */
13
+ sidecarContent?: string;
14
+ /** Bridge components emitted for unconverted consumer selectors. */
15
+ bridgeResults?: BridgeComponentResult[];
16
+ }
17
+ /** Describes a bridge className emitted for a component targeted by unconverted consumer selectors. */
18
+ interface BridgeComponentResult {
19
+ componentName: string;
20
+ /** The export name (e.g. "default" for default exports, or the named export identifier). */
21
+ exportName?: string;
22
+ className: string;
23
+ globalSelectorVarName: string;
12
24
  }
13
25
  /**
14
26
  * Options for the transform
@@ -19,6 +31,35 @@ interface TransformOptions extends Options {
19
31
  * Controls value resolution and resolver-provided imports.
20
32
  */
21
33
  adapter: Adapter;
34
+ /**
35
+ * Cross-file selector information from the prepass.
36
+ * When present, enables cross-file component selector handling.
37
+ */
38
+ crossFileInfo?: CrossFileInfo;
39
+ }
40
+ /**
41
+ * Cross-file selector info passed from the prepass to the per-file transform.
42
+ * Kept minimal: only what the transform needs to know about this specific file.
43
+ */
44
+ interface CrossFileInfo {
45
+ /** Cross-file selector usages where this file is the consumer */
46
+ selectorUsages: CrossFileSelectorUsage[];
47
+ /** Component names in this file that need a global selector bridge className (consumer not transformed) */
48
+ bridgeComponentNames?: Set<string>;
49
+ }
50
+ interface CrossFileSelectorUsage {
51
+ /** Local name in the consumer file (e.g. "CollapseArrowIcon") */
52
+ localName: string;
53
+ /** Raw import specifier (e.g. "./lib/collapse-arrow-icon") */
54
+ importSource: string;
55
+ /** Imported binding name ("default" for default imports, otherwise named) */
56
+ importedName: string;
57
+ /** Absolute path of the target module */
58
+ resolvedPath: string;
59
+ /** Original component name for bridge GlobalSelector (e.g., "Foo" for "FooGlobalSelector") */
60
+ bridgeComponentName?: string;
61
+ /** Local name of the actual component in the consumer file (for JSX matching) */
62
+ bridgeComponentLocalName?: string;
22
63
  }
23
64
  //#endregion
24
65
  //#region src/transform.d.ts
@@ -34,4 +75,4 @@ declare function transform(file: FileInfo, api: API, options: Options): string |
34
75
  */
35
76
  declare function transformWithWarnings(file: FileInfo, api: API, options: TransformOptions): TransformResult;
36
77
  //#endregion
37
- export { type TransformOptions, type TransformResult, transform as default, transformWithWarnings };
78
+ export { type BridgeComponentResult, type TransformOptions, type TransformResult, transform as default, transformWithWarnings };