diff --git a/packages/gamut-codemods/README.md b/packages/gamut-codemods/README.md index 15ef0eae56..9f3eb0a470 100644 --- a/packages/gamut-codemods/README.md +++ b/packages/gamut-codemods/README.md @@ -6,12 +6,35 @@ Codemods for moving a repo onto `@skillsoft/gamut` and between its releases. Each preset is one upgrade. Run them in order if you're more than one behind. -| Preset | What it's for | -| ------------ | ------------------------------------------------------- | -| `scope-swap` | Moving from `@codecademy/gamut*` to `@skillsoft/gamut*` | +| Preset | What it's for | +| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | +| `scope-swap` | Moving from `@codecademy/gamut*` to `@skillsoft/gamut*` | +| `size-and-alignment` | Size values move to `sm` / `md` / `lg`, Select's `sizeVariant` becomes `size`, and left / right alignment values become `start` / `end` | `npx @skillsoft/gamut-codemods list` shows every preset and the migrations inside it. +## Running size-and-alignment + +Run it on code that already imports `@skillsoft/gamut`. Consumers still on `@codecademy/gamut` run `scope-swap` first. + +```sh +# Preview without writing anything +npx @skillsoft/gamut-codemods size-and-alignment . --dry + +# Run it +npx @skillsoft/gamut-codemods size-and-alignment . +``` + +It rewrites JSX attributes on the components listed in `src/presets/size-and-alignment/manifest.ts`, and only when the component is imported from `@skillsoft/gamut`. A local component with the same name, or the same prop on a component that isn't listed (Modal's `size`, for example), is left alone. Aliased imports work. + +- **Sizes:** `size` on Button, Input, Toggle, SelectDropdown, ProgressBar, Badge, and DatePicker's `inputSize`, plus `labelSize` on FormGroup and ConnectedFormGroup, and `size` on FormGroupLabel. Old values map to the new scale. +- **Select:** `sizeVariant` is renamed to `size`, with `small` → `sm` and `base` → `md`. +- **Alignment:** `left` / `right` become `start` / `end` for `iconPosition`, `buttonPlacement`, `alignContentContainer`, `openFrom`, `menuAlignment`, Popover `align` and `beak`, Coachmark `beak`, and GridForm `layout`. Tip and PopoverContainer `alignment` values like `top-left` and `left-center` map to their `start` / `end` forms. + +Only string literals are converted, as `prop="old"` or `prop={'old'}`. Any other expression for a prop that has values to convert gets a warning, so check it by hand. + +Not covered, so check these by hand: values in object literals (such as DataList column `justify`), `textAlign` on `Text`, `position` on GridForm fields, and any prop read from a variable. + ## Running scope-swap Start from a clean working tree. The codemod won't write into uncommitted changes (pass `--force` to override), so `git checkout .` always undoes a run. diff --git a/packages/gamut-codemods/src/cli.test.ts b/packages/gamut-codemods/src/cli.test.ts index 2b36e5604d..a5afcccdc1 100644 --- a/packages/gamut-codemods/src/cli.test.ts +++ b/packages/gamut-codemods/src/cli.test.ts @@ -205,7 +205,7 @@ describe('main: full run', () => { expect(out).toMatch(/Video needs two peer dependencies/); expect(out).toMatch(/Some tsconfig files set "lib"/); expect(out).toMatch( - /\[moved-exports\] .*src\/index\.ts:1 .*no longer re-exports Video/ + /src\/index\.ts\n\s+:1 \[moved-exports\] .*no longer re-exports Video/ ); expect(out).toMatch( /\[moved-exports\] .*reexport\.mdx .*no longer re-exports Video/ diff --git a/packages/gamut-codemods/src/cli.ts b/packages/gamut-codemods/src/cli.ts index 7315dd62a3..979cb7f4c5 100644 --- a/packages/gamut-codemods/src/cli.ts +++ b/packages/gamut-codemods/src/cli.ts @@ -15,6 +15,7 @@ import { } from './lib/run-source'; import type { FileMigration, Preset } from './lib/types'; import { getPreset, presets } from './presets'; +import type { RecordedWarning } from './transform'; /* No published types for the Runner; it's jscodeshift's programmatic API. */ // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -125,6 +126,32 @@ const runFileMigrations = ( return changed; }; +/* Prints the warnings each worker recorded, grouped by file and sorted by line. */ +const printWarnings = (dir: string) => { + const files = fs.readdirSync(dir).map((key) => ({ + file: decodeURIComponent(key), + warnings: fs + .readFileSync(path.join(dir, key), 'utf8') + .split('\n') + .filter(Boolean) + .map((line) => JSON.parse(line) as RecordedWarning), + })); + const total = files.reduce((sum, f) => sum + f.warnings.length, 0); + heading(`warnings (${total})`); + files + .sort((a, b) => a.file.localeCompare(b.file)) + .forEach(({ file, warnings }) => { + console.log(file); + warnings + .sort((a, b) => (a.line ?? 0) - (b.line ?? 0)) + .forEach((w) => + console.log( + ` ${w.line ? `:${w.line} ` : ''}[${w.migration}] ${w.message}` + ) + ); + }); +}; + const printChecklist = (preset: Preset, notes: Set) => { heading('next steps'); const items = [ @@ -181,6 +208,9 @@ export const main = async (argv: string[]) => { opts.skip = skipped.join(',') || undefined; const notesDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gamut-codemods-')); + const warningsDir = fs.mkdtempSync( + path.join(os.tmpdir(), 'gamut-codemods-warnings-') + ); try { heading('source files'); const result = await Runner.run( @@ -191,6 +221,7 @@ export const main = async (argv: string[]) => { only: opts.only, skip: opts.skip, notesDir, + warningsDir, dry: opts.dry, extensions: 'ts,tsx,js,jsx,mjs,cjs,mts,cts', /* .d.ts stays in: Emotion theme augmentations import gamut-styles. */ @@ -205,6 +236,8 @@ export const main = async (argv: string[]) => { notes.add(decodeURIComponent(key)) ); + printWarnings(warningsDir); + heading('package.json and .eslintrc'); const changed = runFileMigrations(preset, opts, (key) => notes.add(key)); console.log(`${changed} file(s) ${opts.dry ? 'would change' : 'changed'}`); @@ -231,5 +264,6 @@ export const main = async (argv: string[]) => { return result.error > 0 ? 1 : 0; } finally { fs.rmSync(notesDir, { recursive: true, force: true }); + fs.rmSync(warningsDir, { recursive: true, force: true }); } }; diff --git a/packages/gamut-codemods/src/lib/manifest.ts b/packages/gamut-codemods/src/lib/manifest.ts index 1e667af00e..0ba3c78154 100644 --- a/packages/gamut-codemods/src/lib/manifest.ts +++ b/packages/gamut-codemods/src/lib/manifest.ts @@ -48,9 +48,28 @@ export interface DeepImport { note?: string; } +export interface PropChange { + /* The prop's new name, when the prop itself was renamed. */ + rename?: string; + /* Old string value -> new string value. Other values are left alone. */ + values?: Record; +} + +export interface ComponentPropChanges { + /* + The package whose components are matched. This is the NEW name, since + these rules run on code that already imports @skillsoft/gamut. + */ + package: string; + /* Component export name -> prop name -> change. */ + components: Record>; +} + export interface Manifest { /* Old package -> new package. */ packages: Record; + /* Prop renames and value changes on specific components, by name. */ + componentProps?: ComponentPropChanges; /* Packages with no replacement under the new name. */ removedPackages: Record; /* Version range written to package.json, keyed by NEW package name. */ diff --git a/packages/gamut-codemods/src/lib/run-source.ts b/packages/gamut-codemods/src/lib/run-source.ts index 42eb019392..67373fb5fb 100644 --- a/packages/gamut-codemods/src/lib/run-source.ts +++ b/packages/gamut-codemods/src/lib/run-source.ts @@ -95,6 +95,7 @@ export const mentionsOldNames = (manifest: Manifest, source: string) => ...Object.keys(manifest.packages), ...Object.keys(manifest.removedPackages), `${manifest.eslintPlugin.from}/`, + ...(manifest.componentProps ? [manifest.componentProps.package] : []), ].some((name) => source.includes(name)); /* `only` and `skip` are comma-separated migration names. */ diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/alignment.input.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/alignment.input.tsx new file mode 100644 index 0000000000..f36b86316f --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/alignment.input.tsx @@ -0,0 +1,10 @@ +import { Alert, InfoTip, Popover, PopoverContainer } from '@skillsoft/gamut'; + +export const Placement = () => ( + <> + Heads up + + + + +); diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/alignment.output.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/alignment.output.tsx new file mode 100644 index 0000000000..0ad719da28 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/alignment.output.tsx @@ -0,0 +1,10 @@ +import { Alert, InfoTip, Popover, PopoverContainer } from '@skillsoft/gamut'; + +export const Placement = () => ( + <> + Heads up + + + + +); diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.input.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.input.tsx new file mode 100644 index 0000000000..e2b3f35ce1 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.input.tsx @@ -0,0 +1,5 @@ +import { FillButton } from '@skillsoft/gamut'; + +const textSize = 'normal'; + +export const Declared = () => Go; diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.output.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.output.tsx new file mode 100644 index 0000000000..e2b3f35ce1 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.output.tsx @@ -0,0 +1,5 @@ +import { FillButton } from '@skillsoft/gamut'; + +const textSize = 'normal'; + +export const Declared = () => Go; diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.warnings.json b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.warnings.json new file mode 100644 index 0000000000..2f754dccec --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/declared.warnings.json @@ -0,0 +1 @@ +["FillButton size is not a string literal", "textSize is declared at line 3"] diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.input.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.input.tsx new file mode 100644 index 0000000000..a2252eb2f9 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.input.tsx @@ -0,0 +1,5 @@ +import { FillButton } from '@skillsoft/gamut'; + +export const Dynamic = ({ size }: { size: string }) => ( + Go +); diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.output.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.output.tsx new file mode 100644 index 0000000000..a2252eb2f9 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.output.tsx @@ -0,0 +1,5 @@ +import { FillButton } from '@skillsoft/gamut'; + +export const Dynamic = ({ size }: { size: string }) => ( + Go +); diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.warnings.json b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.warnings.json new file mode 100644 index 0000000000..4e03b4ba83 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/dynamic.warnings.json @@ -0,0 +1 @@ +["FillButton size is not a string literal"] diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/linked.input.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/linked.input.tsx new file mode 100644 index 0000000000..e59f9d4253 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/linked.input.tsx @@ -0,0 +1,7 @@ +import { FillButton } from '@skillsoft/gamut'; + +type Props = { size?: 'small' | 'normal' }; + +export const Thing = ({ size = 'normal' }: Props) => ( + Go +); diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/linked.output.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/linked.output.tsx new file mode 100644 index 0000000000..999b98688d --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/linked.output.tsx @@ -0,0 +1,7 @@ +import { FillButton } from '@skillsoft/gamut'; + +type Props = { size?: 'sm' | 'md' }; + +export const Thing = ({ size = 'md' }: Props) => ( + Go +); diff --git a/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/scoped.input.tsx b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/scoped.input.tsx new file mode 100644 index 0000000000..b9ed34f505 --- /dev/null +++ b/packages/gamut-codemods/src/migrations/prop-values/__testfixtures__/scoped.input.tsx @@ -0,0 +1,13 @@ +import { Badge as CodecademyBadge } from '@codecademy/gamut'; +import { FillButton as Btn, Modal } from '@skillsoft/gamut'; + +const Button = (props: { size: string }) => ; + +export const Scoped = () => ( + <> + Aliased +