Repository navigation
fix(bestax-migrate): keep href on the anchor, and as inside its union #669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
d507a5c
fix(bestax-migrate): keep href on the anchor, and as inside its union
allxsmith 9f938fa
docs: name the href and as rules the codemod now applies
allxsmith 65c1864
fix(bestax-migrate): drop an href on the targets that declare none
allxsmith a1de6c1
test(bestax-migrate): hold RBC's TODO rules to its skill reference too
allxsmith dfc68e8
docs: correct two href claims the references make
allxsmith 10209e5
fix(bestax-migrate): decide the href from the element, not the leftovers
allxsmith d6e923f
fix(bestax-migrate): read the element a target renders, not the type …
allxsmith 78a2017
fix(bestax-migrate): take the anchor's other attributes with it, and …
allxsmith 6ece337
fix(bestax-migrate): give the TODO the remedy that works on the targe…
allxsmith 1d0fba3
fix(bestax-migrate): judge every link attribute against the element, …
allxsmith f0f2563
fix(bestax-migrate): keep a TODO on one line, and correct the link-at…
allxsmith 0fb251e
fix(bestax-migrate): bound the link attributes by the component too, …
allxsmith c793554
test(bestax-migrate): fail a case that never migrated, instead of che…
allxsmith 68e705f
fix(bestax-migrate): run the link cleanup on react-bulma-components' …
allxsmith 766eed7
test(bestax-migrate): assert the three sources agree on the same shape
allxsmith d335daf
fix(bestax-migrate): the component and the element must both allow th…
allxsmith 4e4a198
fix(bestax-migrate): plain markup keeps the href its own tag accepts
allxsmith b94d4c9
test(bestax-migrate): pin the last table, which was wrong the moment …
allxsmith 5cf4e86
docs: make every rule the link pass can emit greppable
allxsmith 98bf245
test(bestax-migrate): assert the premise behind the one-row exception
allxsmith c5465f0
fix(bestax-migrate): judge props against the component they will end …
allxsmith 3f844b6
fix(bestax-migrate): widen the element universe until it disagreed, a…
allxsmith b184236
test(bestax-migrate): check the rows against all of HTML, not a list …
allxsmith 04af8cc
fix(bestax-migrate): announce the one removal that was still silent
allxsmith 321143b
fix(bestax-migrate): trust an `as` a spread cannot overwrite, and sto…
allxsmith 0324455
fix(bestax-migrate): a spread hides the element, not the component
allxsmith b807790
test(bestax-migrate): assert HREF_OK names the right targets, not jus…
allxsmith 8b14c92
fix(bestax-migrate): only rbx's spread can carry an `as`
allxsmith 9e73145
docs(bestax-migrate): settle the bare-spread reading
allxsmith 77ae350
fix(bestax-migrate): keep an href the plain tag takes, and quieten th…
allxsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /** | ||
| * `AS_UNIONS` decides which `as` values survive a migration, and it is a hand | ||
| * written copy of unions that live in bulma-ui. A copy that drifts is worse | ||
| * than no table at all: too narrow, and the codemod TODOs an `as` that would | ||
| * have worked; too wide, and it emits output the user's project cannot | ||
| * compile — which is the defect that #662 was. | ||
| * | ||
| * bulma-ui exports these as types, not as runtime constants, so no runtime | ||
| * diff can check them. This generates one type assertion per row and hands it | ||
| * to `tsc` against the built library, which makes drift in either direction a | ||
| * failure here rather than in someone's project. | ||
| */ | ||
|
|
||
| import { | ||
| AS_ANY_TARGETS, | ||
| AS_UNIONS, | ||
| HREF_TABLE, | ||
| declaresAs, | ||
| } from '../src/sources/_shared/polymorphic.js'; | ||
| import { typecheckTsx } from './support/typecheck-tsx.js'; | ||
|
|
||
| const TARGETS = Object.keys(AS_UNIONS).sort(); | ||
|
|
||
| /** `Level.Item` is reached through the `Level` its mapping target names. */ | ||
| const importRoots = [...new Set(TARGETS.map(t => t.split('.')[0]))].sort(); | ||
|
|
||
| function buildSource(): string { | ||
| const rows = TARGETS.map((target, i) => { | ||
| const union = AS_UNIONS[target].map(v => `'${v}'`).join(' | '); | ||
| return `type _${i} = Assert<Exact<${union}, AsOf<typeof ${target}>>>; // ${target}`; | ||
| }); | ||
| return [ | ||
| "import type { JSXElementConstructor } from 'react';", | ||
| `import { ${importRoots.join(', ')} } from '@allxsmith/bestax-bulma';`, | ||
| '', | ||
| // Reach `as` through the props rather than `ComponentProps<C>['as']`: | ||
| // several of these components type their props as a UNION of shapes, and | ||
| // the `infer` distributes over it where an index access cannot. | ||
| 'type AsOf<C> = C extends JSXElementConstructor<infer P>', | ||
| ' ? P extends { as?: infer A } ? NonNullable<A> : never', | ||
| ' : never;', | ||
| 'type Exact<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;', | ||
| 'type Assert<T extends true> = T;', | ||
| '', | ||
| ...rows, | ||
| '', | ||
| 'export {};', | ||
| ].join('\n'); | ||
| } | ||
|
|
||
| /** | ||
| * tsc reports a row by line number, and a bare "Type 'false' does not satisfy | ||
| * the constraint 'true'" says nothing about which component drifted. Quote | ||
| * the generated line beside each diagnostic so the failure names the target. | ||
| */ | ||
| function annotate(diagnostics: string, source: string): string { | ||
| const lines = source.split('\n'); | ||
| return diagnostics | ||
| .split('\n') | ||
| .map(line => { | ||
| const at = /check\.tsx\((\d+),/.exec(line); | ||
| const row = at && lines[Number(at[1]) - 1]; | ||
| return row ? `${line}\n ↳ ${row.trim()}` : line; | ||
| }) | ||
| .join('\n'); | ||
| } | ||
|
|
||
| describe('AS_UNIONS matches the `as` each bestax component declares', () => { | ||
| it('asserts every row against the library, in both directions', () => { | ||
| const source = buildSource(); | ||
| const { status, diagnostics } = typecheckTsx(source, 'as-unions'); | ||
| expect({ status, diagnostics: annotate(diagnostics, source) }).toEqual({ | ||
| status: 0, | ||
| diagnostics: '', | ||
| }); | ||
| }); | ||
|
|
||
| it('covers every component the mappings can hand a narrowed `as`', () => { | ||
| // A row removed by accident would make the assertion above pass while | ||
| // the transform stopped guarding that component, so pin the set itself. | ||
| expect(TARGETS).toEqual([ | ||
| 'Control', | ||
| 'Dropdown.Item', | ||
| 'Footer', | ||
| 'Image', | ||
| 'Level.Item', | ||
| 'Media', | ||
| 'Media.Left', | ||
| 'SubTitle', | ||
| 'Title', | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * `HREF_TABLE` and `AS_ANY_TARGETS` are the other two hand-written mirrors of | ||
| * bulma-ui's types, and the more dangerous ones: they say what the codemod may | ||
| * KEEP, so a row that is wrong in either direction ships output that does not | ||
| * compile, or deletes an attribute that did. | ||
| * | ||
| * A type assertion cannot express "this element accepts this attribute", so | ||
| * these assert by USE -- each row becomes the JSX it licenses, and tsc accepts | ||
| * or rejects it exactly as a migrated project would. | ||
| */ | ||
| function buildHrefSource(): string { | ||
| const rows: string[] = []; | ||
| const roots = new Set<string>(); | ||
| for (const [target, defaultEl] of Object.entries(HREF_TABLE)) { | ||
| roots.add(target.split('.')[0]); | ||
| const i = rows.length; | ||
| // Only where the component declares an `as` at all -- `Pagination.Link` | ||
| // and `Panel.Block` are anchors outright and take no such prop. | ||
| if (declaresAs(target)) { | ||
| rows.push( | ||
| `export const h${i} = <${target} as="a" href="#">{'x'}</${target}>; // ${target}: as="a"` | ||
| ); | ||
| } | ||
| if (defaultEl === 'a') { | ||
| rows.push( | ||
| `export const b${i} = <${target} href="#">{'x'}</${target}>; // ${target}: bare` | ||
| ); | ||
| } | ||
| } | ||
| for (const target of AS_ANY_TARGETS) { | ||
| roots.add(target.split('.')[0]); | ||
| const i = rows.length; | ||
| rows.push( | ||
| `export const a${i} = <${target} as="span">{'x'}</${target}>; // ${target}: any` | ||
| ); | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
| } | ||
| return [ | ||
| `import { ${[...roots].sort().join(', ')} } from '@allxsmith/bestax-bulma';`, | ||
| '', | ||
| ...rows, | ||
| '', | ||
| ].join('\n'); | ||
| } | ||
|
|
||
| describe('the href and `as` tables match what the library accepts', () => { | ||
| it('licenses exactly the shapes the tables claim', () => { | ||
| const source = buildHrefSource(); | ||
| const { status, diagnostics } = typecheckTsx(source, 'href-table'); | ||
| expect({ status, diagnostics: annotate(diagnostics, source) }).toEqual({ | ||
| status: 0, | ||
| diagnostics: '', | ||
| }); | ||
| }); | ||
|
|
||
| it('pins the set of href-capable targets, and what each renders', () => { | ||
| // The table says where an `href` may stay; everything absent from it loses | ||
| // the attribute. A row added by accident is how #662 shipped, and a row | ||
| // missed is how `Pagination.Next` did — it is reached through a `special` | ||
| // rather than a `target:` line, so reading the mappings does not find it. | ||
| // The typecheck above cannot pin the default element: `Level.Item` | ||
| // declares `href` at every `as`, so the bare form compiles even though it | ||
| // renders a <div> and drops the attribute. That row is held here and by | ||
| // the comment on the table, which cites the line in bulma-ui. | ||
| expect(HREF_TABLE).toEqual({ | ||
| Button: 'button', | ||
| 'Level.Item': 'div', | ||
| 'Menu.Item': 'a', | ||
| 'Navbar.Item': 'a', | ||
| 'Navbar.Link': 'a', | ||
| 'Pagination.Link': 'a', | ||
| 'Pagination.Next': 'a', | ||
| 'Pagination.Previous': 'a', | ||
| 'Panel.Block': 'a', | ||
| }); | ||
| }); | ||
|
|
||
| it('pins the targets whose `as` takes any element', () => { | ||
| expect(AS_ANY_TARGETS).toEqual([ | ||
| 'Button', | ||
| 'Menu.Item', | ||
| 'Navbar.Item', | ||
| 'Navbar.Link', | ||
| ]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.