[types] Accept revert and revert-layer, and fix six properties that accept no CSS-wide keywords - #1879
Closed
henryqdineen wants to merge 4 commits into
Closed
[types] Accept revert and revert-layer, and fix six properties that accept no CSS-wide keywords#1879henryqdineen wants to merge 4 commits into
henryqdineen wants to merge 4 commits into
Conversation
`gen-types` translates the Flow types into TypeScript, which loses the most
useful thing these types can do, because a Flow union containing `string`
collapses to `string` and discards every literal member:
Flow: type Display = 'block' | 'flex' | string;
TypeScript: type Display = 'block' | 'flex' | (string & {});
`string & {}` is mutually assignable with `string`, so any string is still
accepted, but the literals survive and editors keep suggesting them. There is
no way to express this in Flow.
`gen-types` copies a `.d.ts` verbatim when one sits beside the Flow source, so
checking this file in is enough to take over that translation -- no build
changes. Verified byte-identical to what `gen-types` produced, once the 249
`string` -> `(string & {})` substitutions are reversed.
100 of the 522 properties were typed as bare `string` or `number | string` and
so offered no completions at all; another ~340 lost their literals to a
`string` member. Those now autocomplete.
`packages/typescript-tests/src/open-unions.ts` covers the behaviour: arbitrary
strings and `var()` are still accepted, `null` still unsets a property, the
CSS-wide keywords still resolve, literals survive, and closed unions stay
closed. Note `NonNullable` cannot be used to inspect these unions -- it is
`T & {}` in TypeScript >=4.9, and that intersection flattens them.
The two files must now be kept in sync by hand. Adding a css-tree-based test to
enforce that, and to check the literals against the CSS grammar, is the
intended follow-up.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Five things about `CSSProperties` are hand-maintained and can drift silently.
`StyleXCSSTypes.d.ts` duplicates `StyleXCSSTypes.js` so that TypeScript can use
`(string & {})` where Flow only has `string`. Nothing stops the two files
diverging, so the first test reads both, reduces each property to the keywords,
numbers and strings it accepts, and asserts the two agree. `string & {}` reads
as an arbitrary string, which is exactly what it is.
`@babel/parser` reads both dialects -- the Flow types with the `flow` plugin,
the definitions with `typescript` in declaration mode -- so one reader covers
both files. The two ASTs use disjoint node names, so a single switch handles
either.
The CSS-wide keywords are valid on every property and appear in no grammar, so
the second test is the only thing that can check them. Properties compose them
from the `all` alias.
The property values are written by hand, so `css-tree`'s value definition
grammar checks them in both directions. The third test asserts every keyword in
the types is one the grammar accepts, catching typos and invented values. The
fourth asserts the reverse for closed unions -- that every keyword the grammar
allows is typed -- catching values a user simply cannot write. Both follow
`<'other-property'>` references, which is how logical properties and shorthands
are defined.
The reverse check is restricted to closed unions on purpose. Where a property
accepts an arbitrary string, every keyword is already accepted and completeness
means nothing; asserting it everywhere demands over ten thousand additions,
because shorthands like `background` transitively pull in every named colour.
Restricted, it finds 125 real omissions across 33 properties.
The set of properties is also hand-maintained, so the fifth test asserts every
Baseline-available property is typed. `web-features` is the data behind MDN's
"Widely available" / "Newly available" banners; css-tree carries no support
data at all, and mdn-data's `status` describes the spec rather than browsers --
it calls `font-stretch` obsolete though every browser has supported it since
2020, while marking its replacement `font-width` experimental. Using Baseline
rather than "css-tree knows about it" keeps the exclusion list to real
decisions: 25 entries rather than 82.
All five pass as-is. Each list entry carries a reason, and each failure says
what to do about it:
- 6 properties accepting none of the CSS-wide keywords, written
`null | 'a' | 'b'` rather than composing `all`. They are also the only
properties in the file with no named type alias -- inlining the union at the
property is what hid the slip.
- 17 properties css-tree has no grammar for: abandoned drafts (`motion-*` became
`offset-*`, the CSS Display Level 3 longhands were dropped), `@font-face`
descriptors that were never properties (`src`, `unicode-range`), and names
that never shipped unprefixed.
- 18 properties keeping a keyword the grammar has dropped, where real
stylesheets still use it. `marginTrim` is the clearest: the spec was rewritten
to `none | in-flow | all`, but Safari ships the original `block`/`inline`
syntax.
- 33 properties whose closed union is missing keywords the grammar allows --
`display` cannot take `flow` or `table-caption`, `overflow` cannot take
`overlay`, `mixBlendMode` cannot take `plus-darker`. `marginTrim` appears in
both keyword lists, which is honest: it keeps values the spec dropped and is
missing values the spec added.
- 25 Baseline-available properties that are not typed: 7 that should be, 17 SVG
presentation attributes, and `all`.
`web-features` is pinned exactly rather than by range, because its whole
purpose is to change over time: a floating version would let a routine install
flip a property to Baseline and fail the build with no code change. Pinned, the
test only fires on a deliberate bump -- which is when the news is wanted.
Verified by injecting each failure: a typo'd keyword trips the grammar and
parity tests and is reported by name; editing one type file without the other
trips only parity; removing an allowlist entry reports what to add, and
satisfying one reports the entry as stale.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Worked example of the CSS-wide keyword loop: add the two missing keywords to
`GLOBAL_KEYWORDS`, and the test names every property that cannot take them.
display: does not accept 'revert', 'revert-layer'. Either compose the
`all` alias, or add it to NO_GLOBAL_KEYWORDS with a reason.
`revert` and `revert-layer` are CSS-wide keywords, valid on every property, but
the `all` alias that all 516 properties compose stopped at `unset`. `revert`
has been Baseline since 2021 and `revert-layer` since 2022; csstype has carried
both for years.
Fixed in one place, since `all` is the single definition. Pure widening -- no
value that used to typecheck stops doing so.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Worked example of emptying an allowlist: delete `NO_GLOBAL_KEYWORDS` and the
test names each property and what it is missing.
colorScheme: does not accept 'inherit', 'initial', 'unset', 'revert',
'revert-layer'. Every property should compose the `all` alias.
These six were written `null | 'a' | 'b'` rather than `all | 'a' | 'b'`, so
they accepted none of the CSS-wide keywords -- `colorScheme: 'inherit'` was a
type error, as were `marginTrim: 'unset'`, `paintOrder`, `textJustify`,
`WebkitBackgroundClip` and `WebkitBoxOrient`.
They are also the only six properties in the file with no named type alias:
everything else declares `type foo = ...` and writes `foo?: all | foo`, while
these inline their union at the property. Reaching for `null` -- what `all`
happens to start with -- instead of composing `all` is the slip that inlining
hid. Five date to the initial commit; `WebkitBackgroundClip` was added later by
"Chore: Allow additional -webkit- properties (facebook#28)" and copied the shape.
Pure widening. The allowlist is now gone rather than shortened, so nothing can
reintroduce the pattern.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
@henryqdineen is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
2 tasks
Collaborator
Author
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changed / motivation ?
Two bugs, both found by the CSS-wide keyword test added in #1878.
revertandrevert-layerwere missing from every property. Theallalias that 516 properties compose stopped atunset.reverthas been Baseline since 2021 andrevert-layersince 2022; csstype has carried both for years. Fixed in one place, sinceallis the single definition.Six properties accepted none of the CSS-wide keywords — not even
inherit.colorScheme: 'inherit'was a type error, as weremarginTrim: 'unset',paintOrder,textJustify,WebkitBackgroundClipandWebkitBoxOrient. They were writtennull | 'a' | 'b'rather thanall | 'a' | 'b'.Linked PR/Issues
Stacked on #1878.
Additional Context
The cause of the second one is worth recording, because it explains why nobody noticed. Those six are the only properties in the file with no named type alias. Everything else declares
type foo = …and writesfoo?: all | foo; these inline their union at the property instead — and reaching fornull, which is whatallhappens to start with, rather than composingall, is the slip that inlining hid. Five date to the initial commit;WebkitBackgroundClipwas added later by "Chore: Allow additional -webkit- properties (#28)" and copied the shape.Both fixes are pure widenings: no value that used to typecheck stops doing so. Confirmed after the change that
color: 'revert-layer',colorScheme: 'inherit',marginTrim: 'unset'andpaintOrder: 'revert'all compile.The
NO_GLOBAL_KEYWORDSallowlist that #1878 introduced for these six is deleted rather than shortened, so the pattern cannot come back.Pre-flight checklist
Contribution Guidelines
🤖 Generated with Claude Code