Skip to content

Commit e73aacd

Browse files
skovhusclaude
andcommitted
refactor: extract shared mergeMarkerDeclarations helper
DRY up the marker-merging logic that was duplicated in transform.ts, run.ts, and the regeneration script. All three now use the shared mergeMarkerDeclarations() from run.ts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d00a248 commit e73aacd

3 files changed

Lines changed: 38 additions & 62 deletions

File tree

scripts/regenerate-test-case-outputs.mts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ const [
2929
{ scanCrossFileSelectors },
3030
{ createModuleResolver },
3131
{ Logger },
32+
{ mergeMarkerDeclarations },
3233
] = await Promise.all([
3334
import("../src/transform.ts"),
3435
import("../src/__tests__/fixture-adapters.ts"),
3536
import("../src/internal/prepass/scan-cross-file-selectors.ts"),
3637
import("../src/internal/prepass/resolve-imports.ts"),
3738
import("../src/internal/logger.ts"),
39+
import("../src/run.ts"),
3840
]);
3941

4042
// Suppress diagnostic output during regeneration
@@ -81,18 +83,6 @@ async function listFixtureNames(): Promise<Array<{ name: string; ext: string }>>
8183
.sort((a, b) => a.name.localeCompare(b.name));
8284
}
8385

84-
/** Merge marker declarations from `incoming` into `base`, appending only new exports. */
85-
function mergeMarkerContent(base: string, incoming: string): string {
86-
const markerLineRe = /^export const \w+ = stylex\.defineMarker\(\);$/gm;
87-
const newMarkers = [...incoming.matchAll(markerLineRe)].map((m) => m[0]);
88-
const markersToAdd = newMarkers.filter((line) => !base.includes(line));
89-
if (markersToAdd.length === 0) {
90-
return base;
91-
}
92-
const trailingNewline = base.endsWith("\n") ? "" : "\n";
93-
return base + trailingNewline + markersToAdd.join("\n") + "\n";
94-
}
95-
9686
// Accumulate all sidecar files across fixtures for merging (shared marker files)
9787
const allSidecarFiles = new Map<string, string>();
9888

@@ -125,7 +115,10 @@ async function updateFixture(name: string, ext: string) {
125115
// Accumulate sidecar files for merging after all fixtures are processed
126116
for (const [sidecarPath, content] of sidecarFiles) {
127117
const existing = allSidecarFiles.get(sidecarPath);
128-
allSidecarFiles.set(sidecarPath, existing ? mergeMarkerContent(existing, content) : content);
118+
allSidecarFiles.set(
119+
sidecarPath,
120+
existing ? mergeMarkerDeclarations(existing, content) : content,
121+
);
129122
}
130123

131124
return outputPath;
@@ -164,7 +157,7 @@ for (const [sidecarPath, content] of allSidecarFiles) {
164157
if (only) {
165158
try {
166159
const existing = await readFile(sidecarPath, "utf-8");
167-
merged = mergeMarkerContent(content, existing);
160+
merged = mergeMarkerDeclarations(content, existing);
168161
} catch {
169162
// File doesn't exist yet, write fresh content
170163
}

src/run.ts

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -607,52 +607,42 @@ function createAutoPrepassFailureError(
607607
}
608608

609609
/**
610-
* Merge new sidecar marker content into an existing .stylex.ts file, preserving
611-
* user-owned exports (e.g. defineVars). If the file doesn't exist, returns content as-is.
612-
*
613-
* New marker declarations (`export const XMarker = stylex.defineMarker()`) are
614-
* appended only if they don't already exist in the file. The stylex import is
615-
* ensured at the top.
610+
* Merge marker declarations from `incoming` into `base`, appending only new
611+
* `export const XMarker = stylex.defineMarker()` lines. Returns `base` unchanged
612+
* if all markers already exist.
616613
*/
617-
export function mergeSidecarContent(sidecarPath: string, newContent: string): string {
618-
let existing: string;
619-
try {
620-
existing = readFileSync(sidecarPath, "utf-8");
621-
} catch {
622-
// File doesn't exist yet — use new content as-is
623-
return newContent;
624-
}
625-
626-
// Extract marker export lines from the new content
614+
export function mergeMarkerDeclarations(base: string, incoming: string): string {
627615
const markerLineRe = /^export const \w+ = stylex\.defineMarker\(\);$/gm;
628-
const newMarkers: string[] = [];
629-
for (const m of newContent.matchAll(markerLineRe)) {
630-
newMarkers.push(m[0]);
631-
}
632-
616+
const newMarkers = [...incoming.matchAll(markerLineRe)].map((m) => m[0]);
633617
if (newMarkers.length === 0) {
634-
return newContent;
618+
return base;
635619
}
636-
637-
// Filter out markers already present in the existing file
638-
const markersToAdd = newMarkers.filter((line) => !existing.includes(line));
639-
620+
const markersToAdd = newMarkers.filter((line) => !base.includes(line));
640621
if (markersToAdd.length === 0) {
641-
// All markers already exist — no changes needed
642-
return existing;
622+
return base;
643623
}
644-
645624
// Ensure the stylex import exists
646-
let merged = existing;
625+
let merged = base;
647626
if (!merged.includes("@stylexjs/stylex")) {
648627
merged = `import * as stylex from "@stylexjs/stylex";\n\n${merged}`;
649628
}
650-
651-
// Append new marker declarations at end
652629
const trailingNewline = merged.endsWith("\n") ? "" : "\n";
653-
merged = merged + trailingNewline + markersToAdd.join("\n") + "\n";
630+
return merged + trailingNewline + markersToAdd.join("\n") + "\n";
631+
}
654632

655-
return merged;
633+
/**
634+
* Merge new sidecar marker content into an existing .stylex.ts file, preserving
635+
* user-owned exports (e.g. defineVars). If the file doesn't exist, returns content as-is.
636+
*/
637+
export function mergeSidecarContent(sidecarPath: string, newContent: string): string {
638+
let existing: string;
639+
try {
640+
existing = readFileSync(sidecarPath, "utf-8");
641+
} catch {
642+
// File doesn't exist yet — use new content as-is
643+
return newContent;
644+
}
645+
return mergeMarkerDeclarations(existing, newContent);
656646
}
657647

658648
/** Run formatter commands on a list of files, logging warnings on failure. */

src/transform.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { API, FileInfo, Options } from "jscodeshift";
66
import { basename, dirname, join, resolve as pathResolve } from "node:path";
77
import { realpathSync } from "node:fs";
88

9+
import { mergeMarkerDeclarations } from "./run.js";
910
import { Logger } from "./internal/logger.js";
1011
import { TransformContext } from "./internal/transform-context.js";
1112
import type {
@@ -72,20 +73,12 @@ export default function transform(file: FileInfo, api: API, options: Options): s
7273
join(dirname(file.path), `${basename(file.path).replace(/\.\w+$/, "")}.stylex.ts`);
7374
// Merge with existing content when multiple files write to the same sidecar path
7475
const existing = sidecarFiles.get(sidecarPath);
75-
if (existing) {
76-
const markerLineRe = /^export const \w+ = stylex\.defineMarker\(\);$/gm;
77-
const newMarkers = [...result.sidecarContent.matchAll(markerLineRe)].map((m) => m[0]);
78-
const markersToAdd = newMarkers.filter((line) => !existing.includes(line));
79-
if (markersToAdd.length > 0) {
80-
const trailingNewline = existing.endsWith("\n") ? "" : "\n";
81-
sidecarFiles.set(
82-
sidecarPath,
83-
existing + trailingNewline + markersToAdd.join("\n") + "\n",
84-
);
85-
}
86-
} else {
87-
sidecarFiles.set(sidecarPath, result.sidecarContent);
88-
}
76+
sidecarFiles.set(
77+
sidecarPath,
78+
existing
79+
? mergeMarkerDeclarations(existing, result.sidecarContent)
80+
: result.sidecarContent,
81+
);
8982
}
9083
}
9184

0 commit comments

Comments
 (0)