styled-components-to-stylex-codemod 0.0.40 → 0.0.42

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.
@@ -1,6 +1,6 @@
1
1
  import { n as toRealPath } from "./path-utils-BIpoL4Ue.mjs";
2
2
  import { r as escapeRegex } from "./string-utils-DD9wdRHW.mjs";
3
- import { t as isSelectorContext } from "./selector-context-heuristic-6_jSRGkZ.mjs";
3
+ import { t as isSelectorContext } from "./selector-context-heuristic-CGggN74M.mjs";
4
4
  import { readFileSync } from "node:fs";
5
5
  //#region src/internal/bridge-consumer-patcher.ts
6
6
  /**
@@ -1,4 +1,4 @@
1
- import { o as resolveBarrelReExportBinding, r as findImportSource } from "./extract-external-interface-DbzIPO0Z.mjs";
1
+ import { o as resolveBarrelReExportBinding, r as findImportSource } from "./extract-external-interface-B61N1a6q.mjs";
2
2
  //#region src/internal/prepass/compute-leaf-set.ts
3
3
  /**
4
4
  * Computes which styled-component bindings are "leaves" for leaves-only mode:
@@ -348,7 +348,7 @@ function getFileExportsRe(name) {
348
348
  }
349
349
  return re;
350
350
  }
351
- function fileImportsFrom(usageSrc, usageFile, name, defFile, resolve) {
351
+ function fileImportsFrom(usageSrc, usageFile, name, defFile, resolve, read) {
352
352
  const [namedRe, defaultRe] = getFileImportsFromRes(name);
353
353
  namedRe.lastIndex = 0;
354
354
  defaultRe.lastIndex = 0;
@@ -358,11 +358,17 @@ function fileImportsFrom(usageSrc, usageFile, name, defFile, resolve) {
358
358
  const specifier = match[1];
359
359
  if (!specifier) continue;
360
360
  const resolved = resolve(specifier, usageFile);
361
- if (resolved && path.resolve(resolved) === path.resolve(defFile)) return true;
361
+ if (resolved && importCanReferenceDefinition(resolved, name, defFile, resolve, read)) return true;
362
362
  if (specifier.endsWith(stem) || specifier.endsWith(`${parent}/${stem}`) || specifier.endsWith(parent)) return true;
363
363
  }
364
364
  return false;
365
365
  }
366
+ function importCanReferenceDefinition(resolvedImport, name, defFile, resolve, read) {
367
+ if (path.resolve(resolvedImport) === path.resolve(defFile)) return true;
368
+ if (!read) return false;
369
+ const reExportedFile = resolveBarrelReExport(resolvedImport, name, resolve, read);
370
+ return reExportedFile !== null && path.resolve(reExportedFile) === path.resolve(defFile);
371
+ }
366
372
  const fileImportsFromReCache = /* @__PURE__ */ new Map();
367
373
  function getFileImportsFromRes(name) {
368
374
  let cached = fileImportsFromReCache.get(name);
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as CollectedWarning, c as MarkerFileContext, l as defineAdapter, n as TransformMode, o as AdapterInput, s as ImportSource } from "./transform-types-DUT5eSAm.mjs";
1
+ import { a as CollectedWarning, c as MarkerFileContext, l as defineAdapter, n as TransformMode, o as AdapterInput, s as ImportSource } from "./transform-types-CQjiY4Yp.mjs";
2
2
 
3
3
  //#region src/run.d.ts
4
4
  interface RunTransformOptions {
@@ -44,7 +44,11 @@ interface RunTransformOptions {
44
44
  */
45
45
  print?: boolean;
46
46
  /**
47
- * jscodeshift parser to use
47
+ * jscodeshift parser to use.
48
+ *
49
+ * When set to `"ts"` or `"tsx"` (including the default), runTransform also
50
+ * builds TypeScript compiler metadata for more accurate wrapper interfaces.
51
+ *
48
52
  * @default "tsx"
49
53
  */
50
54
  parser?: "babel" | "babylon" | "flow" | "ts" | "tsx";
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { o as assertValidAdapterInput, r as defineAdapter, s as describeValue, t as mergeMarkerDeclarations } from "./merge-markers-BC5YNB7D.mjs";
2
- import { a as resolveBarrelReExport, s as Logger } from "./extract-external-interface-DbzIPO0Z.mjs";
3
- import { r as extractStyledDefBasesFromSource } from "./compute-leaf-set-C-xis90s.mjs";
1
+ import { c as describeValue, i as defineAdapter, n as mergeMarkerDeclarations, s as assertValidAdapterInput, t as transformedComponentAcceptsSx } from "./sx-surface-DBy6tOUv.mjs";
2
+ import { a as resolveBarrelReExport, s as Logger } from "./extract-external-interface-B61N1a6q.mjs";
3
+ import { r as extractStyledDefBasesFromSource } from "./compute-leaf-set-Be5Cgxnr.mjs";
4
4
  import { n as toRealPath } from "./path-utils-BIpoL4Ue.mjs";
5
5
  import jscodeshift from "jscodeshift";
6
6
  import { fileURLToPath, pathToFileURL } from "node:url";
@@ -149,10 +149,12 @@ async function runTransform(options) {
149
149
  const { createModuleResolver } = await import("./resolve-imports-DgSAddIF.mjs").then((n) => n.n);
150
150
  const sharedResolver = createModuleResolver();
151
151
  filePaths = orderFilesByLocalImportDependencies(filePaths, sharedResolver, toRealPath);
152
- const { runPrepass } = await import("./run-prepass-jnJbdfxU.mjs");
152
+ const { runPrepass } = await import("./run-prepass-COJ2IOPl.mjs");
153
153
  const absoluteFiles = filePaths.map((f) => resolve(f));
154
154
  const absoluteConsumers = consumerFilePaths.map((f) => resolve(f));
155
155
  let prepassResult;
156
+ const prepassStartedAt = performance.now();
157
+ Logger.info(`Prepass: starting (${absoluteFiles.length} file${absoluteFiles.length === 1 ? "" : "s"}, ${absoluteConsumers.length} consumer${absoluteConsumers.length === 1 ? "" : "s"}, parser=${parser})\n`);
156
158
  try {
157
159
  prepassResult = await runPrepass({
158
160
  filesToTransform: absoluteFiles,
@@ -164,9 +166,10 @@ async function runTransform(options) {
164
166
  leavesOnly,
165
167
  resolveBaseComponent: adapterInput.resolveBaseComponent
166
168
  });
169
+ Logger.info(`Prepass: completed in ${formatElapsedSeconds(prepassStartedAt)}s\n`);
167
170
  } catch (err) {
168
171
  if (adapterInput.externalInterface === "auto") throw createAutoPrepassFailureError(err, consumerPatterns, parser);
169
- Logger.warn(`Prepass failed, continuing without cross-file analysis: ${err instanceof Error ? err.message : String(err)}`);
172
+ Logger.warn(`Prepass failed after ${formatElapsedSeconds(prepassStartedAt)}s, continuing without cross-file analysis: ${err instanceof Error ? err.message : String(err)}`);
170
173
  prepassResult = {
171
174
  crossFileInfo: {
172
175
  selectorUsages: /* @__PURE__ */ new Map(),
@@ -176,13 +179,16 @@ async function runTransform(options) {
176
179
  globalLeafKeys: leavesOnly ? /* @__PURE__ */ new Set() : void 0
177
180
  },
178
181
  consumerAnalysis: void 0,
179
- forwardedAsConsumers: /* @__PURE__ */ new Map()
182
+ forwardedAsConsumers: /* @__PURE__ */ new Map(),
183
+ typeScriptMetadata: void 0
180
184
  };
181
185
  }
182
186
  const transformedFiles = /* @__PURE__ */ new Set();
187
+ const transformedFileSources = /* @__PURE__ */ new Map();
183
188
  const crossFilePrepassResult = {
184
189
  ...prepassResult.crossFileInfo,
185
- transformedFiles
190
+ transformedFiles,
191
+ typeScriptMetadata: prepassResult.typeScriptMetadata
186
192
  };
187
193
  const resolvedAdapter = (() => {
188
194
  if (adapterInput.externalInterface === "auto" && prepassResult.consumerAnalysis) {
@@ -243,12 +249,17 @@ async function runTransform(options) {
243
249
  if (!resolvedImport) return;
244
250
  const resolvedPath = toRealPath(resolvedImport);
245
251
  const definitionSourcePath = resolveExistingSourcePath(resolveBarrelReExport(resolvedPath, ctx.importedName, prepassResolve, cachedRead) ?? resolvedPath);
246
- if (!transformedFiles.has(toRealPath(definitionSourcePath))) return;
247
252
  const autoInterfaceNames = ctx.importedName === "default" ? [ctx.localName, ctx.importedName] : [ctx.importedName];
248
253
  const styledDefinitionNames = getStyledDefinitionNames(definitionSourcePath);
249
254
  const sourceComponentNames = ctx.importedName === "default" ? [ctx.localName, getDefaultExportedName(definitionSourcePath)].filter((name) => typeof name === "string") : [ctx.importedName];
250
- const definitionSource = cachedRead(definitionSourcePath);
251
- if (!(definitionSource.includes("sx?: stylex.StyleXStyles") && sourceComponentNames.some((name) => definitionSource.includes(name))) && !sourceComponentNames.some((name) => styledDefinitionNames.has(name))) return;
255
+ if (findTypedComponentMetadata(prepassResult.typeScriptMetadata, definitionSourcePath, sourceComponentNames)?.supportsSxProp === true) return { acceptsSx: true };
256
+ if (transformedComponentAcceptsSx({
257
+ absolutePath: definitionSourcePath,
258
+ componentNames: sourceComponentNames,
259
+ sourceOverrides: transformedFileSources
260
+ })) return { acceptsSx: true };
261
+ if (!transformedFiles.has(toRealPath(definitionSourcePath))) return;
262
+ if (!sourceComponentNames.some((name) => styledDefinitionNames.has(name))) return;
252
263
  return autoInterfaceNames.map((name) => lookupAutoExternalInterface(definitionSourcePath, name)).find((result) => result !== void 0)?.styles ? { acceptsSx: true } : void 0;
253
264
  }
254
265
  };
@@ -297,7 +308,7 @@ async function runTransform(options) {
297
308
  sidecarFiles,
298
309
  bridgeResults,
299
310
  transformedFiles,
300
- transformedFileSources: /* @__PURE__ */ new Map(),
311
+ transformedFileSources,
301
312
  transientPropRenames,
302
313
  allowPartialMigration: options.allowPartialMigration ?? (leavesOnly ? true : false),
303
314
  transformMode: leavesOnly ? "leavesOnly" : options.transformMode ?? "all",
@@ -309,7 +320,7 @@ async function runTransform(options) {
309
320
  const result = await runTransformSequentially(transformModule, filePaths, runnerOptions);
310
321
  if (sidecarFiles.size > 0 && !dryRun) for (const [sidecarPath, content] of sidecarFiles) await writeFile(sidecarPath, mergeSidecarContent(sidecarPath, content), "utf-8");
311
322
  if (bridgeResults.size > 0 && !dryRun) {
312
- const { buildConsumerReplacements, patchConsumerFile } = await import("./bridge-consumer-patcher-31jI1854.mjs");
323
+ const { buildConsumerReplacements, patchConsumerFile } = await import("./bridge-consumer-patcher-DNsxze1l.mjs");
313
324
  const consumerReplacements = buildConsumerReplacements(crossFilePrepassResult.selectorUsages, bridgeResults, transformedFiles);
314
325
  const patchedFiles = [];
315
326
  for (const [consumerPath, replacements] of consumerReplacements) {
@@ -322,7 +333,7 @@ async function runTransform(options) {
322
333
  if (formatterCommands && patchedFiles.length > 0) await runFormatters(formatterCommands, patchedFiles);
323
334
  }
324
335
  if (prepassResult.forwardedAsConsumers.size > 0 && !dryRun) {
325
- const { buildForwardedAsReplacements, patchConsumerForwardedAs } = await import("./forwarded-as-consumer-patcher-BYCrqzRm.mjs");
336
+ const { buildForwardedAsReplacements, patchConsumerForwardedAs } = await import("./forwarded-as-consumer-patcher-DoVqLOgw.mjs");
326
337
  const forwardedAsReplacements = buildForwardedAsReplacements(prepassResult.forwardedAsConsumers, transformedFiles);
327
338
  const patchedFiles = [];
328
339
  for (const [consumerPath, entries] of forwardedAsReplacements) {
@@ -335,7 +346,7 @@ async function runTransform(options) {
335
346
  if (formatterCommands && patchedFiles.length > 0) await runFormatters(formatterCommands, patchedFiles);
336
347
  }
337
348
  if (transientPropRenames.size > 0 && !dryRun) {
338
- const { collectTransientPropPatches } = await import("./transient-prop-consumer-patcher-DdIYPSFk.mjs");
349
+ const { collectTransientPropPatches } = await import("./transient-prop-consumer-patcher-BX-LeC2B.mjs");
339
350
  const patches = collectTransientPropPatches({
340
351
  transientPropRenames,
341
352
  consumerFilePaths: consumerFilePaths.map((p) => resolve(p)),
@@ -376,6 +387,15 @@ function createAutoPrepassFailureError(err, consumerPatterns, parser) {
376
387
  `consumerPaths: ${consumerPatterns.length > 0 ? consumerPatterns.join(", ") : "(none)"}`
377
388
  ].join("\n"));
378
389
  }
390
+ function formatElapsedSeconds(startedAt) {
391
+ return ((performance.now() - startedAt) / 1e3).toFixed(1);
392
+ }
393
+ function findTypedComponentMetadata(metadata, filePath, componentNames) {
394
+ if (!metadata) return;
395
+ const names = new Set(componentNames);
396
+ const resolvedFilePath = toRealPath(filePath);
397
+ return metadata.files.find((file) => file.filePath === resolvedFilePath)?.components.find((component) => names.has(component.name));
398
+ }
379
399
  /**
380
400
  * Merge new sidecar marker content into an existing .stylex.ts file, preserving
381
401
  * user-owned exports (e.g. defineVars). If the file doesn't exist, returns content as-is.