Repository navigation
feat(findings): copied-type-cast flags a cast onto a same-name copy of the value's type - #490
Merged
Merged
Conversation
…f the value's type Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gs, resolves enum members, checks one-file roots Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s project references to sources, dedupes across tsconfigs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…arentheses, forces source redirects, sees symlinked files as one Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
srcly now reports a new finding,
copied-type-cast: a cast such asfetchDoc(id) as Promise<DocDetail>onto a type that has the same name as the value's type but is a different declaration. The cast is what keeps tsc from comparing the two copies. This is the other half ofduplicate-type(#483): that rule finds the copied declaration, this one finds the cast that hides it, also when the original is a library type such as a Prisma enum.What you'll see
srcly findings --rule copied-type-cast,srcly report's checks line (srcly copied-type casts (<tsconfig>)), the findings queue and the code viewer.`t.status as RunStatus` casts the `RunStatus` from .prisma/client/index.d.ts:424 to a different `RunStatus`, declared in app/src/services/ai/ai-runs.queries.ts:4.How it decides
The rule needs the type checker, so a small Node script (
copied_type_casts.mjs) runs with the project's owntypescriptpackage once per tsconfig (the same configs and the same cap of 6 thattscgets). A cast (x as Tor<T>x, notas const) is reported when:Tare named types (a type alias, interface, class or enum) with the same name and no shared declaration; forPromise<A> as Promise<B>the payloads are compared;ReadableStreamstay out.The target name is the one written in the cast, so
type Other = Doc; x as Otheris not aDoccopy. An enum member stands for its enum. Anonymous object types, type parameters, test files and.d.tsfiles are left out. Project references are read from their sources, so an unbuilt reference does not read as a clean 0. A tsconfig the rule cannot check (nonode_modules/typescript, over the cap, no Node) makessrcly findings --rule copied-type-castread N/A with the reason. The cache key covers every TS and JSON file in the repository, because the checker reads files outside the scanned folder.Proof
Visual Notes
d59937fe(app/, 1,427 files, one tsconfig, 6 to 10 s for the rule), withsrcly findings:10 of 10 of the copies the TASK-0017 measurement found are reported:
DocDetailx4,ChatThreadStatus/ChatMessageRolex4 inai-chat.queries.ts,RunStatusx2 inai-runs.queries.ts. Every other hit, read by hand:TaskStatus(palette-search.queries.ts:64) andInteractionKind(memory-store.ts:59): real copies.TASK_STATUSESandINTERACTION_KINDSrestate Prisma'sTaskStatusandInteractionKindenums value for value, under the same names. The measurement missed them because the checker folds(typeof X)[number]to a nameless union. The rule reads the name written in the cast.regroup() as { members: … }and the like, and the dagreg.node(id)cast), and 2ReadableStreamcasts between DOM's and Node's declarations, which are both library types.So the 12 hits are 12 real copies. On srcly itself (6 tsconfigs) the rule reports 0.
Tests:
server/tests/test_copied_type_casts.pyruns the real checker on small projects: true cases (aPromisepayload copy, a union alias, a narrowed enum value), false cases (the same declaration under another name, an alias with another name, a parenthesized target, a wrappingPromisealias, an anonymous shape,as const, a lib type, a one-member enum, a type parameter), a test file scanned alone, one file as the scan root, a change outside the scanned folder that changes the cache signature, a brokenextendsthat reads N/A, two overlapping tsconfigs that give one finding, an unbuilt project reference, and skipped configs that make the count N/A.scripts/check.sh serverpasses (656 tests).Codex review (gpt-6.2-sol), 3 rounds:
extendsbase read as 0, enum members and type parameters read as copies, one-file roots checked nothing, configs over the cap left the count complete. All fixed.type Id = numberhalf of the alias finding (declined below).Promisealias that wraps its argument,disableSourceOfProjectReferenceRedirect, a parenthesized target, and one file loaded through a symlink and a relative path. Still open: changes undernode_modules(below), and inlineimport()targets.Round 3's architecture note: have the TypeScript runner return a project snapshot with canonical declaration ids, the files and resolution inputs it read, and a coverage status, and use that one snapshot to dedupe findings and to decide cache and job reuse.
Survey moves: server only, from this PR's corpus edits to
server/app/services/findings/(jobs.pynow callsrunner.tools_signature, which plans the tools and reads every tool'ssignature()). Boundaryfilesystem−6 and frontierexternal-code−54 net, over 12 traces that all staypartial.scan_codebaseandcache.fingerprintgain 1 boundary and 6 frontiers each (the newread_jsoncfile read and the planning calls behindtools_signature), andFindinggains 1 frontier. The traces of 6 findings classes and helpers (BuiltinTsTool,ReachabilityTool,RuffTool,MissingTool,EslintTool,find_up) lose 1 boundary and 11 frontiers each. They now end at the job list (self._jobs.append) and no longer list the cache-write, fix and listing stops: their downstream slice falls from about 600 to about 330 nodes, with the budget not exhausted.resolve_auto_baseandload_sidelose 1 boundary each, andbuild_compareloses 1 frontier. I reproduced the exact −6/−54 locally withsurvey-blockers.ts --every 5atf0fd3da8and at the head, and diffed per trace. No outcome count, error or trace time moves. Why the slice shrinks (instead of adding the job path to the cache path) is not explained yet; it is a follow-up for the engine.Limits
as Doc[]) or anonymous shapes is not checked.Promise<…>is the only wrapper it unwraps.node_modules, liketscdoes.Open findings
node_modules(prisma generaterenaming an enum) does not change the cache signature, so a cached result can stay stale until a source file changes. Open: the reuse key would need the files the checker resolved (round 3's architecture note).import('./view').Otherfalls back to the checker's folded type, so an alias with another name can read as a copy. Open: rare spelling; resolve theimport()qualifier.type Id = numberdeclarations are not reported: the operand's type is folded tonumberand has no name. Declined: catching it needs the producer's declaration, not the checker's type.🤖 Generated with Claude Code