-
Notifications
You must be signed in to change notification settings - Fork 5k
bun-types: declare Subprocess.writable and lint interface Subprocess against the classes.ts table #39279
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
Open
robobun
wants to merge
4
commits into
main
Choose a base branch
from
farm/c8823ce6/subprocess-writable-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
bun-types: declare Subprocess.writable and lint interface Subprocess against the classes.ts table #39279
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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,117 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { readFileSync } from "node:fs"; | ||
| import path from "node:path"; | ||
| import bunClasses from "../../../src/runtime/api/BunObject.classes.ts"; | ||
|
|
||
| // `interface Subprocess` in packages/bun-types/bun.d.ts is a hand-written mirror | ||
| // of the Subprocess `proto` table in src/runtime/api/BunObject.classes.ts, which | ||
| // is what the class codegen installs on Subprocess.prototype. Nothing else | ||
| // compares the two: `writable` was registered next to `readable` in 2022 and | ||
| // stayed undeclared until 2026, so `proc.writable` worked at runtime and failed | ||
| // to type-check. Same check as redis-client-types.test.ts, reading an interface | ||
| // body instead of a class body. | ||
|
|
||
| // Registered members that are not declared yet, each with the open PR that | ||
| // declares it. Delete the entry when that PR lands: the lint fails while a name | ||
| // listed here is declared (or is no longer registered). | ||
| const pendingDeclarations: Record<string, string> = { | ||
| connected: "#38677", | ||
| }; | ||
|
|
||
| // Registered members the interface declares through its `extends` clause rather | ||
| // than in its body, keyed to the interface that declares them. | ||
| const declaredByExtends: Record<string, string> = { | ||
| "@@asyncDispose": "AsyncDisposable", | ||
| }; | ||
|
|
||
| const classesFile = "src/runtime/api/BunObject.classes.ts"; | ||
| const dtsFile = "packages/bun-types/bun.d.ts"; | ||
| const root = path.resolve(import.meta.dir, "..", "..", ".."); | ||
|
|
||
| const definition = bunClasses.find(c => c.name === "Subprocess"); | ||
| if (definition === undefined) throw new Error(`${classesFile} no longer defines Subprocess`); | ||
| // An interface body can only mirror prototype members. A constructor or statics | ||
| // would need a value declaration this lint does not read. | ||
| if (!definition.noConstructor || Object.keys(definition.klass).length !== 0) { | ||
| throw new Error( | ||
| `Subprocess in ${classesFile} has a constructor or statics, which \`interface Subprocess\` cannot declare`, | ||
| ); | ||
| } | ||
|
|
||
| // Every member the codegen installs on the prototype. A well-known symbol stays | ||
| // in the table's `@@x` spelling; parseInterfaceBody maps `[Symbol.x]` onto it. | ||
| const registered = new Set<string>(); | ||
| for (const [name, field] of Object.entries(definition.proto)) { | ||
| // Installed under a private name or a Symbol.for() symbol, or (internal) not | ||
| // installed at all; none of these has a declaration to mirror. | ||
| if ("internal" in field || "privateSymbol" in field || "publicSymbol" in field) continue; | ||
| registered.add(name); | ||
| } | ||
|
|
||
| const { extended, declared, unrecognized } = parseInterfaceBody(readFileSync(path.join(root, dtsFile), "utf8")); | ||
| for (const [name, base] of Object.entries(declaredByExtends)) { | ||
| if (extended.includes(base)) declared.add(name); | ||
| } | ||
|
|
||
| function parseInterfaceBody(dts: string): { extended: string[]; declared: Set<string>; unrecognized: string[] } { | ||
| // The header spans several lines because of the type parameter list. | ||
| const open = /^ interface Subprocess<\n(?: {4}.*\n)* >(?: extends (.+))? \{$/m.exec(dts); | ||
| if (open === null) throw new Error(`${dtsFile} no longer declares \`interface Subprocess<...> {\``); | ||
| const extended = open[1] === undefined ? [] : open[1].split(",").map(base => base.trim()); | ||
| const bodyStart = open.index + open[0].length; | ||
| const bodyEnd = dts.indexOf("\n }\n", bodyStart); | ||
| if (bodyEnd === -1) throw new Error(`${dtsFile}: unterminated interface Subprocess body`); | ||
|
|
||
| const body = dts | ||
| .slice(bodyStart, bodyEnd) | ||
| .replace(/\/\*[\s\S]*?\*\//g, "") | ||
| .replace(/^[ \t]*\/\/.*$/gm, ""); | ||
|
|
||
| const declared = new Set<string>(); | ||
| const unrecognized: string[] = []; | ||
| // Members start at the body's four-space indent. The only other lines at that | ||
| // indent are the `): ...;` closers of multi-line signatures, which start with | ||
| // punctuation, so everything else at that indent is a member and has to | ||
| // parse: a declaration shape this does not know is reported rather than | ||
| // skipped. | ||
| const member = /^(?:readonly )?(?:\[Symbol\.(\w+)\]|([A-Za-z_$][\w$]*))\s*\??\s*[(:<]/; | ||
| for (const [line] of body.matchAll(/^ [^\s)\]}>].*$/gm)) { | ||
| const m = member.exec(line.slice(4)); | ||
| if (m === null) { | ||
| unrecognized.push(line.trim()); | ||
| continue; | ||
| } | ||
| declared.add(m[1] !== undefined ? `@@${m[1]}` : m[2]!); | ||
| } | ||
| return { extended, declared, unrecognized }; | ||
| } | ||
|
|
||
| test(`every member of interface Subprocess in ${dtsFile} has a shape this lint can read`, () => { | ||
| expect(unrecognized).toEqual([]); | ||
| }); | ||
|
|
||
| test(`${dtsFile} declares every Subprocess member ${classesFile} installs`, () => { | ||
| const undeclared = [...registered] | ||
| .filter(name => !declared.has(name) && !Object.hasOwn(pendingDeclarations, name)) | ||
| .sort(); | ||
| expect(undeclared).toEqual([]); | ||
| }); | ||
|
|
||
| test(`${dtsFile} declares no Subprocess member ${classesFile} does not install`, () => { | ||
| const phantom = [...declared].filter(name => !registered.has(name)).sort(); | ||
| expect(phantom).toEqual([]); | ||
| }); | ||
|
|
||
| test("pendingDeclarations and declaredByExtends describe the current files", () => { | ||
| const stale = [ | ||
| ...Object.entries(pendingDeclarations).flatMap(([name, pr]) => { | ||
| if (!registered.has(name)) return [`${name} (${pr}) is no longer registered in ${classesFile}`]; | ||
| if (declared.has(name)) return [`${name} (${pr}) is declared in ${dtsFile} now; delete its entry`]; | ||
| return []; | ||
| }), | ||
| ...Object.entries(declaredByExtends).flatMap(([name, base]) => | ||
| extended.includes(base) ? [] : [`interface Subprocess no longer extends ${base}, which declared ${name}`], | ||
| ), | ||
| ].sort(); | ||
| expect(stale).toEqual([]); | ||
| }); |
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.