Skip to content

Commit 387fa75

Browse files
fix: close the holes the audit found in its own guards
Reintroducing each defect and counting the reds surfaced four places where the branch guarded less than it claimed, and two sibling defects of the same class as the ones it fixes. Counts here are whole-suite, and exclude the three pre-existing Windows babel-plugin-tester failures. The isolation scan had a hole it could not see ---------------------------------------------- native-runtime-isolation restated its own plane census as ["native", "native-internal"], and the ways into the native plane are not directory names. src/index.ts re-exports runtime, and runtime is runtime.native on the native platform, so `import "react-native-css"` in a compiler source resolves to `index` — not under either directory, and pulling all of both. Measured against the census form: that import, `import "../runtime.native"` and `import "react-native-css/runtime"` each reddened zero cases while producing exactly the dist defect the VAR_SYMBOL fix exists to prevent. So the census is gone and the question is reachability: can the module a specifier resolves to reach native/reactivity, through any number of hops and on any platform. That is the fact the invariant actually rests on — the listeners — rather than a second name for it. All three specifiers now redden the scan, and so does a path through a third directory, which no scan of src/compiler/ alone could see: a value import of the runtime added to src/utilities reddens four cases, the scan among them. Two guards go with it. The detector walks the whole tree rather than the top-level statements, so a require() or a dynamic import() inside a function body is a module reference it can find; and a reachability table states what the check discriminates, so a renamed native/reactivity fails loudly instead of making every answer false. compiler/inheritance.test.ts stays in the scan. It sits in that directory rather than under __tests__, so bob compiles it into dist and the package ships it, which makes it a compiler source like any other. One comparison, not one and a half ---------------------------------- compare.ts claimed an operator has exactly one meaning at runtime, and `=` had two: the container evaluator short-circuited it before reaching the primitive, because a keyword feature like orientation answers a string. Measured: deciding `=` wrongly inside the primitive reddened 8 cases and none of them under @container, whose table holds (width = 400px) rows. The primitive takes StyleDescriptor now and answers `=` itself, since narrowing at the call site is how the second copy got written — an evaluator that must reject a keyword before it can call the primitive ends up deciding equality on its own. Both evaluators hand it whatever the feature answered and nothing else. The same mutation now reddens 14, six under @container, including the orientation rows. The numeric guard travels with it, so testMediaFeatureInterval holds none of its own: an unmeasured value or an unresolved bound fails whichever comparison it is an operand of. That guard was observed by nothing — dropping it reddened zero — because the cases that looked like they pinned it return false either way. "landscape" < 800 is NaN < 800; only a string that COERCES tells the two apart, and 400 < "500" is 400 < 500. Three rows cover it now, one per slot, each mutation-proven. Two sibling defects, same class ------------------------------- inline-size and block-size compile cleanly and answered undefined, so @container (min-inline-size: 400px) matched nothing at any size — on the axis container-type: inline-size names, which is the one most container queries are written against. React Native lays out in one writing mode, so inline is the width axis and block the height axis. Reverting reddens 5. A <ratio> with a zero denominator has no finite quotient, and (min-aspect-ratio: 1/0) emitted Infinity. The native injection path serialises through JSON.stringify, which writes Infinity and NaN as null, so the condition meant one thing under jest and another on a device. A degenerate ratio is refused instead, which routes it into the same "did not compile" path as every other unresolvable value. Reverting reddens 3. Assertions that could not fail ------------------------------ Five expect(cases).toHaveLength(census.length * ORDERINGS.length) compared a generated table against the generator that made it, so the product held for any census. Deleting "<" from the operator census removes 19 cases across four files and left all five green; only compare.test.ts's comparison against COMPARISON_MATCHES — a total Record over MediaFeatureComparison, whose keys are the union itself — noticed. Every table asserts coverage against that record now, and the same deletion reddens 5 tests in 5 suites. conditional-group-rules's control table asserted only that one rule came out, never which condition it carried, so a block emitted with the wrong condition — or with none — passed it. Each case names the condition now, including the two preludes that legitimately carry none. And the aspect-ratio and interval tables get the label they were missing: a matches: true row and a matches: false row fail under opposite defects, one for a block that stops being emitted and one for a block emitted with nothing to check. Neither half observes the other's direction, which is why both are there and why the size of a table is not its coverage. Corrections to what was claimed ------------------------------- - The audit table's "an interval's bounds swapped in the emit (@media)" row reads 0 -> 5. Measured whole-suite it is 9, of which 5 are the compiler-plane pins and 4 the runtime rows that already existed one commit earlier — so the head column is 4, not 0. Zero is the figure against the base, where an unevaluated interval cannot observe its own layout. Both readings of "swapped bounds" give the same numbers. - That table's counting rule is not constant across its rows: the 5 there counts the compiler plane only, while other rows count the whole suite. The counts above use one rule throughout. - "import VAR_SYMBOL for its type only" says the emitted declaration file is unchanged. It is not: compiler.types.d.ts carries `import type { VAR_SYMBOL }` where it carried `import { VAR_SYMBOL }`, and a declaration emit diffs in exactly that one line and nowhere else. Inert for consumers, but not nothing. - "evaluate interval (range pair) conditions" says it dissolves the last identical-bodied switch arms under src/native/conditions. That holds within each switch and not across the two files: `case "!!": return false` is the same body in both, and it is a gap rather than a decision. Known limits, now stated where the decision is made --------------------------------------------------- A comma-separated media query list is intersected, not unioned. rule.m is a flat array fed from two places with opposite meanings — one entry per comma branch, which CSS unions, and one per enclosing @media block or media-carrying selector, which CSS intersects — and testMediaQuery intersects the whole array. Measured: @media (min-width: 400px), (min-height: 300px) at 600x200 does not match while (min-width: 400px) alone does, and switching the evaluator to .some(...) fixes the list and breaks nesting — @media (min-width: 400px) { @media (min-height: 900px) { ... } } starts matching at 600x200. The existing suite catches neither direction, so the substitution passes it. Two entries of the same shape mean two different things; the emit has to say which, which is a change to what is produced rather than to how it is read. It is left standing and written down at the every(never) decision it sits beside, and at the evaluator a reader would otherwise reach for. The boolean context is unimplemented on both planes: (width) compiles to ["!!", name] and both evaluators answer false, so the query reads as valid and can never match. Answering it needs a truthiness rule per feature. Suite: 1361, up from 1325. Two runs with identical totals, zero suite-load failures. yarn typecheck and yarn lint exit 0.
1 parent 3bacba9 commit 387fa75

13 files changed

Lines changed: 600 additions & 114 deletions

‎src/__tests__/_media-features.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ import type { MediaFeatureComparison } from "react-native-css/compiler";
1212
*
1313
* This is not a test file — `testPathIgnorePatterns` skips a path segment
1414
* starting with an underscore.
15+
*
16+
* A note on how the tables built from this are read. A rendered case asserts a
17+
* verdict, and the two verdicts fail under opposite defects: a `matches: true`
18+
* row reddens when a condition stops being answered, because the block is then
19+
* dropped or refused; a `matches: false` row reddens when a condition stops
20+
* being asked, because the block is then emitted with nothing to check and
21+
* applies everywhere. Neither half observes the other's direction, so a table
22+
* of one verdict is half a table however many rows it has — which is why every
23+
* table here carries both, and why the counts of the two are worth keeping
24+
* near each other.
25+
*
26+
* A table's size is also not evidence that it covers anything. Every table is
27+
* generated from this census, so its length is the census's length by
28+
* construction and agrees with a census that lost an operator. Coverage is
29+
* asserted against {@link COMPARISON_MATCHES} instead, whose keys are the
30+
* `MediaFeatureComparison` union itself.
1531
*/
1632

1733
/**

‎src/__tests__/compiler/conditional-group-rules.test.ts‎

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ const uncompilable: [label: string, css: string][] = [
4141
"a negated media condition the compiler cannot resolve",
4242
"@media not (width > env(safe-area-inset-top)) { .child { color: red } }",
4343
],
44+
// A `<ratio>` stands for its quotient, and a zero denominator has none.
45+
// There is no number a comparison against it could be written as: the
46+
// emitted value would be `Infinity` or `NaN`, which the bundle serialises to
47+
// `null` and the runtime then reads as an unresolved bound anyway.
48+
[
49+
"a media ratio with no finite quotient",
50+
"@media (min-aspect-ratio: 1/0) { .child { color: red } }",
51+
],
52+
[
53+
"a media ratio that is not a number at all",
54+
"@media (min-aspect-ratio: 0/0) { .child { color: red } }",
55+
],
56+
[
57+
"a container ratio with no finite quotient",
58+
"@container (min-aspect-ratio: 1/0) { .child { color: red } }",
59+
],
4460
];
4561

4662
describe("a block whose condition does not compile is not emitted", () => {
@@ -56,23 +72,64 @@ describe("a block whose condition does compile is emitted", () => {
5672
/**
5773
* The control for the table above — without it, a compiler that emitted
5874
* nothing at all would pass every case there.
75+
*
76+
* Each case names the condition the rule must carry rather than counting the
77+
* rules, because the two failures being pinned are opposite and a count sees
78+
* only one of them: a block dropped when it should not be, and a block kept
79+
* but stripped of the condition that was the whole point of it. The second
80+
* is the more dangerous, since the declarations then apply everywhere.
81+
*
82+
* An absent condition is therefore stated, not omitted. `@media all` and
83+
* `@media not print and (…)` genuinely carry none — `not print` reads `not
84+
* (print and …)`, true on every non-print device whatever follows — and that
85+
* is exactly the state a condition which failed to compile must not be
86+
* confused with.
5987
*/
60-
const cases: [label: string, css: string][] = [
61-
["@container", "@container (width > 400px) { .child { color: red } }"],
62-
["@media", "@media (width > 400px) { .child { color: red } }"],
63-
["@media all", "@media all { .child { color: red } }"],
64-
["@media screen", "@media screen { .child { color: red } }"],
88+
const cases: [
89+
label: string,
90+
css: string,
91+
conditions: Pick<StyleRule, "m" | "cq">,
92+
][] = [
93+
[
94+
"@container",
95+
"@container (width > 400px) { .child { color: red } }",
96+
{ m: undefined, cq: [{ m: [">", "width", 400] }] },
97+
],
98+
[
99+
"@media",
100+
"@media (width > 400px) { .child { color: red } }",
101+
{ m: [[">", "width", 400]], cq: undefined },
102+
],
103+
[
104+
"@media all",
105+
"@media all { .child { color: red } }",
106+
{ m: undefined, cq: undefined },
107+
],
108+
[
109+
"@media screen",
110+
"@media screen { .child { color: red } }",
111+
{ m: undefined, cq: undefined },
112+
],
65113
[
66114
"@media not print",
67115
"@media not print and (width > 400px) { .child { color: red } }",
116+
{ m: undefined, cq: undefined },
68117
],
69118
[
70119
"a media query list with one uncompilable branch",
71120
"@media (width > env(safe-area-inset-top)), (width > 400px) { .child { color: red } }",
121+
{ m: [[">", "width", 400]], cq: undefined },
122+
],
123+
[
124+
"a ratio whose quotient is finite",
125+
"@media (min-aspect-ratio: 0/1) { .child { color: red } }",
126+
{ m: [[">=", "aspect-ratio", 0]], cq: undefined },
72127
],
73128
];
74129

75-
test.each(cases)("%s", (_label, css) => {
76-
expect(compileChildRules(css)).toHaveLength(1);
130+
test.each(cases)("%s", (_label, css, conditions) => {
131+
expect(
132+
compileChildRules(css).map((rule) => ({ m: rule.m, cq: rule.cq })),
133+
).toStrictEqual([conditions]);
77134
});
78135
});

‎src/__tests__/compiler/container-query.test.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { compile, type ContainerQuery } from "react-native-css/compiler";
22

3-
import { sizeComparisons } from "../_media-features";
3+
import { COMPARISON_MATCHES, sizeComparisons } from "../_media-features";
44

55
/**
66
* Returns the container queries the compiler attached to `.child`.
@@ -45,9 +45,14 @@ describe("size feature comparisons", () => {
4545
return [row.condition(400), query];
4646
});
4747

48-
test("the table covers the whole census", () => {
49-
expect(cases).toHaveLength(sizeComparisons().length);
48+
test("every operator in the census reaches this table", () => {
49+
// Against `COMPARISON_MATCHES`, whose keys are the operator union itself,
50+
// rather than against the length of the generator these cases came from —
51+
// that product holds for any census, an empty one included.
5052
expect(cases.length).toBeGreaterThan(0);
53+
expect(new Set(cases.map(([, query]) => query.m?.[0]))).toStrictEqual(
54+
new Set(Object.keys(COMPARISON_MATCHES)),
55+
);
5156
});
5257

5358
test.each(cases)("@container %s", (condition, query) => {
@@ -67,6 +72,12 @@ describe("other size features", () => {
6772
["(aspect-ratio >= 4/3)", { m: [">=", "aspect-ratio", 4 / 3] }],
6873
["(min-aspect-ratio: 16/9)", { m: [">=", "aspect-ratio", 16 / 9] }],
6974
["(max-aspect-ratio: 16/9)", { m: ["<=", "aspect-ratio", 16 / 9] }],
75+
// The logical axes. `inline-size` is the feature `container-type:
76+
// inline-size` names, so it is the one most container queries are written
77+
// against, and it compiles under its own name rather than being folded
78+
// into `width` here.
79+
["(min-inline-size: 400px)", { m: [">=", "inline-size", 400] }],
80+
["(max-block-size: 400px)", { m: ["<=", "block-size", 400] }],
7081
[
7182
"my-container (min-width: 400px)",
7283
{ m: [">=", "width", 400], n: "c:my-container" },

‎src/__tests__/compiler/media-query.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { compile, type MediaCondition } from "react-native-css/compiler";
22

3-
import { sizeComparisons } from "../_media-features";
3+
import { COMPARISON_MATCHES, sizeComparisons } from "../_media-features";
44

55
/**
66
* Returns the media conditions the compiler attached to `.my-class`.
@@ -127,9 +127,14 @@ describe("size feature comparisons", () => {
127127
return [row.condition(400), condition];
128128
});
129129

130-
test("the table covers the whole census", () => {
131-
expect(cases).toHaveLength(sizeComparisons().length);
130+
test("every operator in the census reaches this table", () => {
131+
// Against `COMPARISON_MATCHES`, whose keys are the operator union itself,
132+
// rather than against the length of the generator these cases came from —
133+
// that product holds for any census, an empty one included.
132134
expect(cases.length).toBeGreaterThan(0);
135+
expect(new Set(cases.map(([, condition]) => condition[0]))).toStrictEqual(
136+
new Set(Object.keys(COMPARISON_MATCHES)),
137+
);
133138
});
134139

135140
test.each(cases)("@media %s", (prelude, condition) => {

0 commit comments

Comments
 (0)