|
| 1 | +import { readdir, readFile, writeFile } from "node:fs/promises"; |
| 2 | +import { join, dirname } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { applyTransform } from "jscodeshift/src/testUtils.js"; |
| 5 | +import { format } from "oxfmt"; |
| 6 | +import transform from "../dist/transform.mjs"; |
| 7 | +import { defineAdapter } from "../dist/index.mjs"; |
| 8 | + |
| 9 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 10 | +const repoRoot = join(__dirname, ".."); |
| 11 | +const testCasesDir = join(repoRoot, "test-cases"); |
| 12 | + |
| 13 | +const fixtureAdapter = defineAdapter({ |
| 14 | + shouldSupportExternalStyles(ctx) { |
| 15 | + return ( |
| 16 | + ctx.filePath.includes("external-styles-support") && ctx.componentName === "ExportedButton" |
| 17 | + ); |
| 18 | + }, |
| 19 | + resolveValue(ctx) { |
| 20 | + if (ctx.kind === "theme") { |
| 21 | + if (ctx.path === "color") { |
| 22 | + return { |
| 23 | + expr: "themeVars", |
| 24 | + imports: [ |
| 25 | + { |
| 26 | + from: { kind: "specifier", value: "./tokens.stylex" }, |
| 27 | + names: [{ imported: "themeVars" }], |
| 28 | + }, |
| 29 | + ], |
| 30 | + }; |
| 31 | + } |
| 32 | + const lastSegment = ctx.path.split(".").pop(); |
| 33 | + return { |
| 34 | + expr: `themeVars.${lastSegment}`, |
| 35 | + imports: [ |
| 36 | + { |
| 37 | + from: { kind: "specifier", value: "./tokens.stylex" }, |
| 38 | + names: [{ imported: "themeVars" }], |
| 39 | + }, |
| 40 | + ], |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + if (ctx.kind === "call") { |
| 45 | + if (ctx.calleeImportedName !== "transitionSpeed") { |
| 46 | + return null; |
| 47 | + } |
| 48 | + if (ctx.calleeSource.kind !== "absolutePath") { |
| 49 | + return null; |
| 50 | + } |
| 51 | + const src = ctx.calleeSource.value; |
| 52 | + if ( |
| 53 | + !src.endsWith("/test-cases/lib/helpers.ts") && |
| 54 | + !src.endsWith("\\test-cases\\lib\\helpers.ts") |
| 55 | + ) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + const arg0 = ctx.args[0]; |
| 59 | + const key = arg0?.kind === "literal" && typeof arg0.value === "string" ? arg0.value : null; |
| 60 | + if ( |
| 61 | + key !== "highlightFadeIn" && |
| 62 | + key !== "highlightFadeOut" && |
| 63 | + key !== "quickTransition" && |
| 64 | + key !== "regularTransition" && |
| 65 | + key !== "slowTransition" |
| 66 | + ) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + return { |
| 70 | + expr: `transitionSpeedVars.${key}`, |
| 71 | + imports: [ |
| 72 | + { |
| 73 | + from: { kind: "specifier", value: "./lib/helpers.stylex" }, |
| 74 | + names: [{ imported: "transitionSpeed", local: "transitionSpeedVars" }], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + if (ctx.kind === "cssVariable") { |
| 81 | + const { name, definedValue } = ctx; |
| 82 | + if (name === "--base-size") { |
| 83 | + return { |
| 84 | + expr: "calcVars.baseSize", |
| 85 | + imports: [ |
| 86 | + { |
| 87 | + from: { kind: "specifier", value: "./css-calc.stylex" }, |
| 88 | + names: [{ imported: "calcVars" }], |
| 89 | + }, |
| 90 | + ], |
| 91 | + ...(definedValue === "16px" ? { dropDefinition: true } : {}), |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + const combinedImport = { |
| 96 | + from: { kind: "specifier", value: "./css-variables.stylex" }, |
| 97 | + names: [{ imported: "vars" }, { imported: "textVars" }], |
| 98 | + }; |
| 99 | + const varsMap = { |
| 100 | + "--color-primary": "colorPrimary", |
| 101 | + "--color-secondary": "colorSecondary", |
| 102 | + "--spacing-sm": "spacingSm", |
| 103 | + "--spacing-md": "spacingMd", |
| 104 | + "--spacing-lg": "spacingLg", |
| 105 | + "--border-radius": "borderRadius", |
| 106 | + }; |
| 107 | + const textVarsMap = { |
| 108 | + "--text-color": "textColor", |
| 109 | + "--font-size": "fontSize", |
| 110 | + "--line-height": "lineHeight", |
| 111 | + }; |
| 112 | + |
| 113 | + const v = varsMap[name]; |
| 114 | + if (v) { |
| 115 | + return { expr: `vars.${v}`, imports: [combinedImport] }; |
| 116 | + } |
| 117 | + const t = textVarsMap[name]; |
| 118 | + if (t) { |
| 119 | + return { expr: `textVars.${t}`, imports: [combinedImport] }; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return null; |
| 124 | + }, |
| 125 | +}); |
| 126 | + |
| 127 | +async function normalizeCode(code) { |
| 128 | + const { code: formatted } = await format("test.tsx", code); |
| 129 | + // Remove extra blank line before return statements in tiny wrapper components: |
| 130 | + // const { ... } = props; |
| 131 | + // |
| 132 | + // return (...) |
| 133 | + const cleaned = formatted.replace( |
| 134 | + /\n(\s*(?:const|let|var)\s+[^\n]+;\n)\n(\s*return\b)/g, |
| 135 | + "\n$1$2", |
| 136 | + ); |
| 137 | + return cleaned.trimEnd() + "\n"; |
| 138 | +} |
| 139 | + |
| 140 | +async function listFixtureNames() { |
| 141 | + const files = await readdir(testCasesDir); |
| 142 | + const inputNames = files |
| 143 | + .filter( |
| 144 | + (f) => |
| 145 | + f.endsWith(".input.tsx") && !f.startsWith("_unsupported.") && !f.startsWith("unsupported-"), |
| 146 | + ) |
| 147 | + .map((f) => f.replace(".input.tsx", "")); |
| 148 | + return inputNames.sort(); |
| 149 | +} |
| 150 | + |
| 151 | +async function updateFixture(name) { |
| 152 | + const inputPath = join(testCasesDir, `${name}.input.tsx`); |
| 153 | + const outputPath = join(testCasesDir, `${name}.output.tsx`); |
| 154 | + const input = await readFile(inputPath, "utf-8"); |
| 155 | + |
| 156 | + const result = applyTransform( |
| 157 | + transform, |
| 158 | + { adapter: fixtureAdapter }, |
| 159 | + { source: input, path: inputPath }, |
| 160 | + { parser: "tsx" }, |
| 161 | + ); |
| 162 | + const out = result || input; |
| 163 | + await writeFile(outputPath, await normalizeCode(out), "utf-8"); |
| 164 | + return outputPath; |
| 165 | +} |
| 166 | + |
| 167 | +const args = new Set(process.argv.slice(2)); |
| 168 | +const only = args.has("--only") ? process.argv[process.argv.indexOf("--only") + 1] : null; |
| 169 | + |
| 170 | +const targetNames = (() => { |
| 171 | + if (only) { |
| 172 | + return only |
| 173 | + .split(",") |
| 174 | + .map((s) => s.trim()) |
| 175 | + .filter(Boolean); |
| 176 | + } |
| 177 | + // Default: update all fixtures that have outputs (excluding unsupported). |
| 178 | + return null; |
| 179 | +})(); |
| 180 | + |
| 181 | +const names = targetNames ?? (await listFixtureNames()); |
| 182 | +for (const name of names) { |
| 183 | + // Skip when output file doesn't exist (should only happen for unsupported fixtures). |
| 184 | + const outPath = join(testCasesDir, `${name}.output.tsx`); |
| 185 | + try { |
| 186 | + await readFile(outPath, "utf-8"); |
| 187 | + } catch { |
| 188 | + continue; |
| 189 | + } |
| 190 | + await updateFixture(name); |
| 191 | +} |
0 commit comments