11const EXPRESSION_WRAPPER_RE =
22 / ^ (?: C h a i n E x p r e s s i o n | P a r e n t h e s i z e d E x p r e s s i o n | T S A s E x p r e s s i o n | T S N o n N u l l E x p r e s s i o n | T S T y p e A s s e r t i o n ) $ / ;
3- const TEST_FILE_SUFFIXES = [ ".test.ts" , ".test-utils.ts" , ".test-harness.ts" , ".e2e-harness.ts" ] ;
3+ const BOUNDARY_GUARD_FIXTURE_ROOT = "test/fixtures/oxlint-boundary-guards" ;
4+ // Shared test-path policy for guards that intentionally exclude fixture, mock, and harness code.
5+ const TEST_FILE_SUFFIXES = [
6+ ".test.ts" ,
7+ ".test.tsx" ,
8+ ".spec.ts" ,
9+ ".spec.tsx" ,
10+ ".test-utils.ts" ,
11+ ".test-utils.tsx" ,
12+ ".test-harness.ts" ,
13+ ".test-harness.tsx" ,
14+ ".e2e-harness.ts" ,
15+ ".e2e-harness.tsx" ,
16+ ] ;
17+ const TEST_PATH_MARKERS = [
18+ "/test/" ,
19+ "/tests/" ,
20+ "__tests__" ,
21+ "/e2e/" ,
22+ "test-helpers" ,
23+ "test-support" ,
24+ "test-fixtures" ,
25+ "test-mocks" ,
26+ "test-utils" ,
27+ "mock-http" ,
28+ "-harness." ,
29+ ".test-utils." ,
30+ "/mocks/" ,
31+ ] ;
32+
33+ function pathMatchesRoot ( repoPath , root ) {
34+ return repoPath === root || repoPath . startsWith ( `${ root } /` ) ;
35+ }
36+
37+ function isSkippedTestPath ( repoPath ) {
38+ if ( pathMatchesRoot ( repoPath , BOUNDARY_GUARD_FIXTURE_ROOT ) ) {
39+ return false ;
40+ }
41+ const slashPrefixedPath = `/${ repoPath } ` ;
42+ return (
43+ TEST_FILE_SUFFIXES . some ( ( suffix ) => repoPath . endsWith ( suffix ) ) ||
44+ TEST_PATH_MARKERS . some ( ( marker ) => slashPrefixedPath . includes ( marker ) )
45+ ) ;
46+ }
447
548function unwrapExpression ( node ) {
649 let current = node ;
@@ -18,7 +61,7 @@ function restrictedCallRule({ allowedFiles = [], message, objects, property, roo
1861 const repoPath = filename . startsWith ( `${ cwd } /` ) ? filename . slice ( cwd . length + 1 ) : filename ;
1962 if (
2063 ! filename . endsWith ( ".ts" ) ||
21- ! roots . some ( ( root ) => repoPath === root || repoPath . startsWith ( ` ${ root } /` ) ) ||
64+ ! roots . some ( ( root ) => pathMatchesRoot ( repoPath , root ) ) ||
2265 TEST_FILE_SUFFIXES . some ( ( suffix ) => filename . endsWith ( suffix ) ) ||
2366 allowedFiles . includes ( repoPath )
2467 ) {
@@ -46,6 +89,86 @@ function restrictedCallRule({ allowedFiles = [], message, objects, property, roo
4689 } ;
4790}
4891
92+ // Adapted from dmmulroy/anti-slop@446268e5d15baa968eaec669ff65358d36ae6259, MIT.
93+ function isTypeAssertionExpression ( node ) {
94+ return node . type === "TSAsExpression" || node . type === "TSTypeAssertion" ;
95+ }
96+
97+ function isConstAssertion ( node ) {
98+ const { typeAnnotation } = node ;
99+ return (
100+ typeAnnotation . type === "TSTypeReference" &&
101+ typeAnnotation . typeName . type === "Identifier" &&
102+ typeAnnotation . typeName . name === "const"
103+ ) ;
104+ }
105+
106+ function isOutermostAssertionInChain ( node ) {
107+ let current = node ;
108+ let parent = node . parent ;
109+
110+ while ( parent . type === "ParenthesizedExpression" && parent . expression === current ) {
111+ current = parent ;
112+ parent = parent . parent ;
113+ }
114+
115+ return ! isTypeAssertionExpression ( parent ) || parent . expression !== current ;
116+ }
117+
118+ function isForbiddenAssertionChain ( node ) {
119+ let assertionCount = 0 ;
120+ let hasNonConstAssertion = false ;
121+ let current = node ;
122+
123+ while ( isTypeAssertionExpression ( current ) ) {
124+ assertionCount += 1 ;
125+ hasNonConstAssertion ||= ! isConstAssertion ( current ) ;
126+ current = unwrapExpressionParentheses ( current . expression ) ;
127+ }
128+
129+ return assertionCount > 1 && hasNonConstAssertion ;
130+ }
131+
132+ function noChainedTypeAssertionsRule ( { excludedRoots = [ ] , roots } ) {
133+ return {
134+ meta : {
135+ type : "problem" ,
136+ docs : {
137+ description :
138+ "Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains." ,
139+ } ,
140+ messages : {
141+ chained :
142+ "This assertion chain discards type evidence. Keep the original precise type, or parse untrusted input at its boundary before narrowing it." ,
143+ } ,
144+ } ,
145+ create ( context ) {
146+ const filename = context . physicalFilename . replaceAll ( "\\" , "/" ) ;
147+ const cwd = context . cwd . replaceAll ( "\\" , "/" ) ;
148+ const repoPath = filename . startsWith ( `${ cwd } /` ) ? filename . slice ( cwd . length + 1 ) : filename ;
149+ if (
150+ ! roots . some ( ( root ) => pathMatchesRoot ( repoPath , root ) ) ||
151+ excludedRoots . some ( ( root ) => pathMatchesRoot ( repoPath , root ) ) ||
152+ isSkippedTestPath ( repoPath )
153+ ) {
154+ return { } ;
155+ }
156+
157+ const checkTypeAssertion = ( node ) => {
158+ if ( ! isOutermostAssertionInChain ( node ) || ! isForbiddenAssertionChain ( node ) ) {
159+ return ;
160+ }
161+ context . report ( { node, messageId : "chained" } ) ;
162+ } ;
163+
164+ return {
165+ TSAsExpression : checkTypeAssertion ,
166+ TSTypeAssertion : checkTypeAssertion ,
167+ } ;
168+ } ,
169+ } ;
170+ }
171+
49172// Adapted from dmmulroy/anti-slop, MIT.
50173const FUNCTION_BOUNDARY_TYPES = new Set ( [
51174 "ArrowFunctionExpression" ,
@@ -504,5 +627,61 @@ export default {
504627 "no-widen-then-assert" : noWidenThenAssertRule ( {
505628 roots : [ "src" , "extensions" , "packages" , "ui/src" , "test/fixtures/oxlint-boundary-guards" ] ,
506629 } ) ,
630+ "no-chained-type-assertions" : noChainedTypeAssertionsRule ( {
631+ roots : [ "src" , "extensions" , "packages" , "ui/src" , BOUNDARY_GUARD_FIXTURE_ROOT ] ,
632+ // Burn-down ledger — shrink only; see PR #124060/#124073/#124079/#124082.
633+ excludedRoots : [
634+ "extensions/amazon-bedrock-mantle" ,
635+ "extensions/anthropic-vertex" ,
636+ "extensions/browser" ,
637+ "extensions/codex" ,
638+ "extensions/copilot" ,
639+ "extensions/deepinfra" ,
640+ "extensions/diagnostics-otel" ,
641+ "extensions/diagnostics-prometheus" ,
642+ "extensions/discord" ,
643+ "extensions/github-copilot" ,
644+ "extensions/google" ,
645+ "extensions/googlechat" ,
646+ "extensions/imessage" ,
647+ "extensions/line" ,
648+ "extensions/llm-task" ,
649+ "extensions/longcat" ,
650+ "extensions/matrix" ,
651+ "extensions/microsoft-foundry" ,
652+ "extensions/msteams" ,
653+ "extensions/qa-lab" ,
654+ "extensions/reef" ,
655+ "extensions/signal" ,
656+ "extensions/slack" ,
657+ "extensions/sms" ,
658+ "extensions/synology-chat" ,
659+ "extensions/telegram" ,
660+ "extensions/tlon" ,
661+ "extensions/voice-call" ,
662+ "extensions/whatsapp" ,
663+ "extensions/workboard" ,
664+ "extensions/zalo" ,
665+ "extensions/zalouser" ,
666+ "packages/ai" ,
667+ "src/runtime.ts" ,
668+ "src/acp" ,
669+ "src/agents" ,
670+ "src/channels" ,
671+ "src/commands" ,
672+ "src/config" ,
673+ "src/gateway" ,
674+ "src/infra" ,
675+ "src/media" ,
676+ "src/meeting-bot" ,
677+ "src/plugin-sdk" ,
678+ "src/plugins" ,
679+ "src/process" ,
680+ "src/proxy-capture" ,
681+ "src/shared" ,
682+ "src/trajectory" ,
683+ "ui/src" ,
684+ ] ,
685+ } ) ,
507686 } ,
508687} ;
0 commit comments