Repository navigation
refactor(tools): read test code with the TypeScript parser in the async test-cleanup invariant - #637
Merged
Conversation
…nc test-cleanup invariant The hand-written scanner lost sync on regular expression literals holding a quote or backtick, on a string right after a keyword, on bracket access split across a line break, and took the exemption marker in JSX text for a comment. The check now parses each file by its extension and finds rmSync calls, aliases and the marker from the syntax tree. A test holds the classification of code to the parser across every file the check reads, and runs the check over the repository after install, since CI runs the invariants before it.
…nts the marker relies on The runner now prints SKIP for a check that could not run and counts it apart from the passes. The async test-cleanup check skips only when dependencies are not installed at all; any other failure to load the parser fails it. CI runs the invariants again after install. The parser-agreement test now covers the comments the exemption marker is read from, and the unused code-blanking export is gone. Calls through call and apply are found, and a file that does not parse is named in a note.
Contributor
Author
|
Review attestation: ready to merge at A push to this PR makes this attestation stale; the new head needs its own review. |
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.
Fixes #634. Refs #629, #632, #563.
What changed
tools/invariants/test-cleanup-retries-asynchronously.mjsno longer reads test code with a hand-written scanner. It parses each file with the TypeScript parser (thetypescriptdev dependency the repo already pins), choosing TypeScript, TSX or JavaScript with JSX by the file's extension.rmSyncor a name the file gives it,x.rmSync,x["rmSync"]/x[`rmSync`], optional calls, calls through parentheses or type assertions, andfs.rmSync.call(...)/.apply(...). The call asks for retries whenmaxRetriesis named among its arguments, including as a quoted key.const name = ...andname = .... A mock factory's{ rmSync: vi.fn() }object literal no longer counts as an alias.commentsOf(source, path?)exposes this classification.blankCommentsAndStringsis gone. Nothing used it since calls are found on the tree, and nothing outside the spec imported it.warning:note, because error recovery may read code after the error differently from how it was written.rmSyncandmaxRetriesare not parsed.Skipped is reported as skipped
check(name)results now carryskipped.formatReport()(intools/invariants/context.mjs, used bytools/check-invariants.mjs) printsSKIPfor a skipped check.14 invariant check(s) passed, 1 skippedinstead ofall 15 invariant checks passed.FAILeven if it also said it skipped.ERR_MODULE_NOT_FOUNDfortypescriptitself and there is nonode_modulesat the repository root.typescriptwithnode_modulespresent, any other missing module, or any other load error. The failure carries the error message..github/workflows/ci.ymlgets a second step,Repository invariants after install, right afterInstalland under the sameif:. The pre-install step stays for prose-only changes.A fresh worktree with no install was run end to end:
The four cases from the #632 review, each with a unit test
/say: `Đã /u,/it's "(/g), then a callreturn'(',case'(',typeof'(')fs[\n"rmSync"],fs?.[\n`rmSync`\n])<p>// invariant-allow: ...</p>), or inside a regex{/* ... */}inside JSX still isThere are also tests for:
.tsfile as TypeScript and a.jsxfile as JSX;(await import("node:fs")).rmSync(...);.call/.apply;Agreement with the parser
The test
agrees with the TypeScript parser on where the comments are, in every file it readscovers every file the invariant scans (708 today). Comments are what the marker depends on.commentsOfcharacter by character against an independent AST view. That view takesgetLeadingCommentRanges/getTrailingCommentRangesat every node and node-list edge, and excludes JSX text.}, is caught.The repository-wide test
finds no retrying synchronous removal in the repository's test codealso stays.For the record, from round 1: the earlier whole-code version of this comparison, run against main's scanner, found real code blanked in 32 test-scoped files. Among them were
apps/runtime/test/action-speech.spec.ts:94,packages/contracts/test/activity-timeline.spec.ts:198andtools/smoke-widget-tooling.mjs:55.Detection compared with main, as a one-off script over all 1528 source files under
apps packages packs tools examples: 0 files differ in the reported lines. There are no new false positives.Performance
node tools/check-invariants.mjs, whole tree, Windows 11, three runs each:Verification
tools/test/test-cleanup-retries.spec.ts+tools/test/invariants-context.spec.ts: 33/33 pass.node_modules, an emptynode_modules, and atypescriptpackage that throws on load. TheformatReporttests cover theSKIP,FAILand summary lines. Neither the exports nor theskippedfield exist on main.pnpm typecheck: exit 0.eslinton the 5 changed JS/TS files: exit 0.pnpm invariants: all 15 pass with dependencies installed. In a fresh worktree with no install: 14 passed, 1 skipped, exit 0..github/workflows/ci.ymlparses withyamlwith no errors. Theverifyjob's steps areRepository invariants,Install [if], thenRepository invariants after install [if].pnpm verify: exit 0, 7797 tests passed.Docs impact
None. This is an internal invariant and its runner. No doc in
docs/or on the official site describes them.