Skip to content

Commit 98bd8de

Browse files
fix(adr-0087-gate): a type alias after a semicolon-less statement keeps its body a type body (#38231)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DDypKu7B9nwbu5WuV7AEio8)_ Fixes #38207 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户无感:只改发版前检查破坏性改动的内部工具,前后差别在于它能正确读出写在无分号语句之后的类型定义。 - 为什么做:发版登记检查此前会把这类类型的成员误判为「没有标注」,本该拦下的收窄改动可能被放行。 - 出错会怎样:最坏是该检查对个别写法误判,只影响内部检查结果,可直接撤回本改动。 - 要你做的:无。 ## What changes `scripts/check-adr-0087-registration.mjs`, `braceOpensTypeBody`: the `type X =` alias test now reads the LAST statement inside the header `braceHeader` collects (new `headerStatement`), not the header's start. The statement start is the file's existing `NEXT_STATEMENT_RE` (keyword at a word boundary) behind the `RHS_CONTINUES_RE` boundary, the same ASI boundary `aliasRhsEnd` already reads. The boundary can only move the start later, so it never widens the reading toward the value side; only the alias test reads it (the `interface`/`class` test and the rest are byte-unchanged). The same mechanism had a mirror defect, fixed by the same line: after a ONE-LINE alias with no `;` (`type S = A`), the next value literal's header began with `type`, so `const o: S = { f: () => null }` read `f` as a function TYPE returning `null` (a value called concrete). TSO-U49 pins it; it is red at `origin/main` too. Pins (battery `TSO-U`, floor 45 to the measured 49): - TSO-U46: the card's minimal repro. - TSO-U47: the real `sidebar.tsx` `SidebarContextProps` shape (multi-line import, three `const` lines, no `;`): `setOpen`, `setOpenMobile`, `toggleSidebar` read `void`. - TSO-U48: lit control, the same alias after a `;`. - TSO-U49: value-direction control, a value literal after a `;`-less alias stays an unannotated VALUE (sidebar head and a one-line alias). TSO-U43 (the ThemeProvider shape) stays green. ## Tests <details><summary>Sidebar instance, origin/main f294277 vs this head</summary> `readTypeSurfaceRef(text, parseSymbolRef('ui/packages/components/src/ui/sidebar.tsx#SidebarContextProps.X'))`: ```text == origin/main f294277 ...#SidebarContextProps.setOpen -> {"shape":"`SidebarContextProps.setOpen`, which carries NO return annotation","type":null,"erased":true} ...#SidebarContextProps.setOpenMobile -> {"shape":"... which carries NO return annotation","type":null,"erased":true} ...#SidebarContextProps.toggleSidebar -> {"shape":"... which carries NO return annotation","type":null,"erased":true} == head ...#SidebarContextProps.setOpen -> {"shape":"the return annotation of `SidebarContextProps.setOpen`","type":"void","erased":false} ...#SidebarContextProps.setOpenMobile -> {"shape":"the return annotation of `SidebarContextProps.setOpenMobile`","type":"void","erased":false} ...#SidebarContextProps.toggleSidebar -> {"shape":"the return annotation of `SidebarContextProps.toggleSidebar`","type":"void","erased":false} ``` </details> <details><summary>Self-test at dd09524</summary> `node scripts/check-adr-0087-registration.mjs --self-test` exit 0: `✓ check-adr-0087-registration --self-test: 871 assertions over real temp git repos`. Every existing pin green (TSO-U31..U45, TSO-I, R8, R9, G2). Floor check: with the `TSO-U` floor set to 50 the self-test reds with `registered 49 case(s), below its pinned floor of 50`, so 49 is the measured count. </details> <details><summary>Ablation (fix reverted through scripts/ablation-replace.mjs)</summary> Anchor `.test(headerStatement(header)) && header.endsWith('=')` replaced by `.test(header) && header.endsWith('=')` (anchor 1 -> 0, blob 34381d4 -> 6dc33f1d322b). Self-test exit 1, failing exactly TSO-U46, TSO-U47 and TSO-U49; TSO-U48 (lit control) stays green. Restore: blob == HEAD (34381d4), `git diff HEAD` empty. </details> ## Acceptance notes - TSO-U49 is red at `origin/main` as well (the mirror direction above), so it is a fix pin as much as a control; it is the same mechanism in the same function, fixed by the same line, no separate card. ## Verification Read first: `headerStatement` and its one call in `braceOpensTypeBody` (the only behaviour change). Two-minute check: `node scripts/check-adr-0087-registration.mjs --self-test` prints the 871-assertion success line. <details><summary>Local gates at dd09524</summary> `node scripts/pm/dispatch-gates.mjs --commands`: 34 commands, all run, all exit 0 (including `pnpm check:pm-dispatch-gates`, 2369 cases). `--ran` reconciliation: `34 derived famil(ies) accounted for — 34 run, 0 NOT-MEASURED (a DERIVED zero)`. `overlap-check`: `VERDICT clean exit 0`. No changeset: the diff touches only the private root package's `scripts/`, which publishes nothing (`skip-changeset`). </details> --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4cxRDKprCtjpEdtiPFW)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent a262197 commit 98bd8de

1 file changed

Lines changed: 53 additions & 2 deletions

File tree

‎scripts/check-adr-0087-registration.mjs‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ const SELF_TEST_BATTERIES = Object.freeze({
492492
'The #13080 category: `type-surface-only`': 29,
493493
'TSO-6048: THE REGRESSION PIN -- the founding case must never admit': 8,
494494
'TSO-N: the predicate set is pinned BY NAME, never by count': 3,
495-
'TSO-U: unit pins on predicate 4\'s readers': 45,
495+
'TSO-U: unit pins on predicate 4\'s readers': 49,
496496
'TSO-D (#15627): a DOTTED member path resolves through the object-literal nesting': 27,
497497
'TSO-C (#17279): a DOTTED member path resolves through a CLASS body': 17,
498498
'TSO-I (#37445): a DOTTED member path resolves through an INTERFACE or type-literal alias body': 25,
@@ -3693,7 +3693,7 @@ function sitsInTypePosition(masked, pos) {
36933693
function braceOpensTypeBody(masked, open) {
36943694
const header = braceHeader(masked, open);
36953695
if (/(?:^|[^\w$.])(?:interface|class)\b/.test(header)) return true;
3696-
if (/^(?:export\s+)?(?:declare\s+)?type\s+[\w$]/.test(header) && header.endsWith('=')) return true;
3696+
if (/^(?:export\s+)?(?:declare\s+)?type\s+[\w$]/.test(headerStatement(header)) && header.endsWith('=')) return true;
36973697
if (/(?:^|[^|&])[|&]$/.test(header) || header.endsWith('<')) return true;
36983698
if (header.endsWith(':')) return sitsInTypePosition(masked, open);
36993699
return false;
@@ -3741,6 +3741,33 @@ function braceHeader(masked, open) {
37413741
return header.trim();
37423742
}
37433743

3744+
/**
3745+
* The LAST statement a `braceHeader` header holds: the header from its last line that
3746+
* opens a new top-level statement (`NEXT_STATEMENT_RE`, the keyword ending at a word
3747+
* boundary) after text that does not continue onto it (`RHS_CONTINUES_RE`) -- the
3748+
* same ASI boundary `aliasRhsEnd` reads. The whole header when no such line exists.
3749+
*
3750+
* ⚠️ A header runs back to the previous `;`, `{` or `}`, so after a statement with no
3751+
* `;` it starts at THAT statement: `} from "./tooltip"` + `const N = "b"` + `type P =`
3752+
* is one header, and a test anchored at its start never sees the `type` alias. Its
3753+
* body then read as a VALUE, and an annotated `f: (...) => void` in it as UNANNOTATED
3754+
* -- a concrete surface called erased, which predicate 4 admits (the permissive
3755+
* direction). Only the alias test reads this: the boundary can only move the start
3756+
* LATER, so a value literal after a `type X = …` with no `;` (`const o: S = {`) keeps
3757+
* `const` as its statement and stays a value.
3758+
*/
3759+
function headerStatement(header) {
3760+
const lines = header.split('\n');
3761+
for (let k = lines.length - 1; k > 0; k--) {
3762+
// The keyword must end at a word boundary: `typeof x` or `constructor` is no start.
3763+
if (!NEXT_STATEMENT_RE.test(lines[k]) || !/^[ \t]*(?:@|[A-Za-z]+(?![\w$]))/.test(lines[k])) continue;
3764+
const before = lines.slice(0, k).join('\n').trimEnd();
3765+
if (before === '' || RHS_CONTINUES_RE.test(before)) continue;
3766+
return lines.slice(k).join('\n').trim();
3767+
}
3768+
return header;
3769+
}
3770+
37443771
/**
37453772
* Whether the masked text in `[from, to)` closes a `<`, `(` or `[` it never opened --
37463773
* that is, whether a brace group ending just before `from` sits INSIDE an angle,
@@ -8043,6 +8070,30 @@ function selfTest() {
80438070
assert(memberReturnAnnotation('type C<T> = T extends { a: 1 } ? A : B\nconst o = {\n f: () => null,\n}\n', 'f')?.annotation === null &&
80448071
memberReturnAnnotation('type U = A | { a: 1 }\nconst o = {\n f: () => null,\n}\n', 'f')?.annotation === null, 'TSO-U45: the same statement boundary behind an `extends` (a conditional type) and a `|` (a union) with no `;` -- neither leaks its `type X =` header into the value literal after it');
80458072

8073+
// U46-U49: a `type X = {` alias AFTER a statement with no `;`. The header
8074+
// `braceHeader` returns runs back to the previous `;`, `{` or `}`, so it starts at
8075+
// that earlier statement (`from "b"`), and a `type` test anchored at the header's
8076+
// start read the alias body as a VALUE: its annotated `f: () => void` read as
8077+
// UNANNOTATED (erased: the permissive direction for predicate 4).
8078+
// `headerStatement` finds the last statement start inside the header first. U48 is
8079+
// the lit control (the same alias after a `;`). U49 is the value-direction control,
8080+
// and the same mechanism's mirror: a ONE-LINE alias with no `;` (`type S = A`) put
8081+
// `type` at the start of the NEXT literal's header, so the value `f: () => null`
8082+
// read as a function TYPE returning `null` (a value called concrete).
8083+
{
8084+
const SIDEBAR_HEAD = 'import * as React from "react"\nimport {\n Tooltip,\n TooltipContent,\n} from "./tooltip"\n\n' +
8085+
'const SIDEBAR_COOKIE_NAME = "sidebar_state"\nconst SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7\nconst SIDEBAR_KEYBOARD_SHORTCUT = "b"\n\n';
8086+
const SIDEBAR_ALIAS = 'type SidebarContextProps = {\n state: "expanded" | "collapsed"\n open: boolean\n setOpen: (open: boolean) => void\n' +
8087+
' openMobile: boolean\n setOpenMobile: (open: boolean) => void\n isMobile: boolean\n toggleSidebar: () => void\n}\n\n';
8088+
assert(memberReturnAnnotation('import { a } from "b"\n\ntype T = {\n f: () => void\n}\n', 'f')?.annotation === 'void', 'TSO-U46: THE MINIMAL REPRO -- a `type T = {` alias after an import with no `;` keeps its body a type body: the function-typed member reads its return type, as the same text with `export interface T {` does');
8089+
assert(['setOpen', 'setOpenMobile', 'toggleSidebar'].every((s) => memberReturnAnnotation(SIDEBAR_HEAD + SIDEBAR_ALIAS, s)?.annotation === 'void'), 'TSO-U47: THE sidebar.tsx SHAPE (`SidebarContextProps`) -- a multi-line import and three `const` lines, none ending in `;`, before the alias: `setOpen`, `setOpenMobile` and `toggleSidebar` all read `void`');
8090+
assert(memberReturnAnnotation('import { a } from "b";\n\ntype T = {\n f: () => void\n}\n', 'f')?.annotation === 'void' &&
8091+
memberReturnAnnotation('const k = 1;\nexport type T = {\n f: () => void\n}\n', 'f')?.annotation === 'void', 'TSO-U48: LIT CONTROL -- the same alias after a `;` reads its annotation (`void`), as it always did');
8092+
assert(memberReturnAnnotation(SIDEBAR_HEAD + SIDEBAR_ALIAS.replace('setOpen: (open: boolean) => void', 'other: string') + 'const fallback: SidebarContextProps = {\n setOpen: () => null,\n}\n', 'setOpen')?.annotation === null &&
8093+
memberReturnAnnotation('type S = A\nconst o: S = {\n f: () => null,\n}\n', 'f')?.annotation === null &&
8094+
memberReturnAnnotation('type S = typeof x\nconst o = {\n f: () => null,\n}\n', 'f')?.annotation === null, 'TSO-U49: VALUE-DIRECTION CONTROL -- a value literal after a `;`-less `type X = …` keeps `const` as its own statement: its arrow stays an unannotated VALUE, in the sidebar head as after a one-line alias');
8095+
}
8096+
80468097
// ---- TSO-D (#15627): a DOTTED member path, resolved through the nesting ---
80478098
//
80488099
// The grammar and the reader were widened in ONE commit, and this battery is

0 commit comments

Comments
 (0)