@@ -4,6 +4,26 @@ import type { Diagnostic, ReactDoctorConfig } from "../types.js";
44import { compileIgnoredFilePatterns , isFileIgnoredByPatterns } from "./is-ignored-file.js" ;
55
66const OPENING_TAG_PATTERN = / < ( [ A - Z ] [ \w . ] * ) / ;
7+ const DISABLE_NEXT_LINE_PATTERN = / \/ \/ \s * r e a c t - d o c t o r - d i s a b l e - n e x t - l i n e \b (?: \s + ( .+ ) ) ? / ;
8+ const DISABLE_LINE_PATTERN = / \/ \/ \s * r e a c t - d o c t o r - d i s a b l e - l i n e \b (?: \s + ( .+ ) ) ? / ;
9+
10+ const createFileLinesCache = ( rootDirectory : string ) => {
11+ const cache = new Map < string , string [ ] | null > ( ) ;
12+
13+ return ( filePath : string ) : string [ ] | null => {
14+ const cached = cache . get ( filePath ) ;
15+ if ( cached !== undefined ) return cached ;
16+ const absolutePath = path . isAbsolute ( filePath ) ? filePath : path . join ( rootDirectory , filePath ) ;
17+ try {
18+ const lines = fs . readFileSync ( absolutePath , "utf-8" ) . split ( "\n" ) ;
19+ cache . set ( filePath , lines ) ;
20+ return lines ;
21+ } catch {
22+ cache . set ( filePath , null ) ;
23+ return null ;
24+ }
25+ } ;
26+ } ;
727
828const isInsideTextComponent = (
929 lines : string [ ] ,
@@ -13,13 +33,20 @@ const isInsideTextComponent = (
1333 for ( let lineIndex = diagnosticLine - 1 ; lineIndex >= 0 ; lineIndex -- ) {
1434 const match = lines [ lineIndex ] . match ( OPENING_TAG_PATTERN ) ;
1535 if ( ! match ) continue ;
16- const tagName = match [ 1 ] ;
17- const leafName = tagName . includes ( "." ) ? tagName . split ( "." ) . pop ( ) ! : tagName ;
18- return textComponentNames . has ( tagName ) || textComponentNames . has ( leafName ) ;
36+ const fullTagName = match [ 1 ] ;
37+ const leafTagName = fullTagName . includes ( "." )
38+ ? ( fullTagName . split ( "." ) . at ( - 1 ) ?? fullTagName )
39+ : fullTagName ;
40+ return textComponentNames . has ( fullTagName ) || textComponentNames . has ( leafTagName ) ;
1941 }
2042 return false ;
2143} ;
2244
45+ const isRuleSuppressed = ( commentRules : string | undefined , ruleId : string ) : boolean => {
46+ if ( ! commentRules ?. trim ( ) ) return true ;
47+ return commentRules . split ( / [ , \s ] + / ) . some ( ( rule ) => rule . trim ( ) === ruleId ) ;
48+ } ;
49+
2350export const filterIgnoredDiagnostics = (
2451 diagnostics : Diagnostic [ ] ,
2552 config : ReactDoctorConfig ,
@@ -31,21 +58,7 @@ export const filterIgnoredDiagnostics = (
3158 Array . isArray ( config . textComponents ) ? config . textComponents : [ ] ,
3259 ) ;
3360 const hasTextComponents = textComponentNames . size > 0 ;
34-
35- const fileLineCache = new Map < string , string [ ] | null > ( ) ;
36- const getFileLines = ( filePath : string ) : string [ ] | null => {
37- const cached = fileLineCache . get ( filePath ) ;
38- if ( cached !== undefined ) return cached ;
39- const absolutePath = path . isAbsolute ( filePath ) ? filePath : path . join ( rootDirectory , filePath ) ;
40- try {
41- const lines = fs . readFileSync ( absolutePath , "utf-8" ) . split ( "\n" ) ;
42- fileLineCache . set ( filePath , lines ) ;
43- return lines ;
44- } catch {
45- fileLineCache . set ( filePath , null ) ;
46- return null ;
47- }
48- } ;
61+ const getFileLines = createFileLinesCache ( rootDirectory ) ;
4962
5063 return diagnostics . filter ( ( diagnostic ) => {
5164 const ruleIdentifier = `${ diagnostic . plugin } /${ diagnostic . rule } ` ;
@@ -68,33 +81,11 @@ export const filterIgnoredDiagnostics = (
6881 } ) ;
6982} ;
7083
71- const DISABLE_NEXT_LINE_PATTERN = / \/ \/ \s * r e a c t - d o c t o r - d i s a b l e - n e x t - l i n e \b (?: \s + ( .+ ) ) ? / ;
72- const DISABLE_LINE_PATTERN = / \/ \/ \s * r e a c t - d o c t o r - d i s a b l e - l i n e \b (?: \s + ( .+ ) ) ? / ;
73-
74- const isRuleSuppressed = ( commentRules : string | undefined , ruleId : string ) : boolean => {
75- if ( ! commentRules ?. trim ( ) ) return true ;
76- return commentRules . split ( / [ , \s ] + / ) . some ( ( rule ) => rule . trim ( ) === ruleId ) ;
77- } ;
78-
7984export const filterInlineSuppressions = (
8085 diagnostics : Diagnostic [ ] ,
8186 rootDirectory : string ,
8287) : Diagnostic [ ] => {
83- const fileLineCache = new Map < string , string [ ] | null > ( ) ;
84-
85- const getFileLines = ( filePath : string ) : string [ ] | null => {
86- const cached = fileLineCache . get ( filePath ) ;
87- if ( cached !== undefined ) return cached ;
88- const absolutePath = path . isAbsolute ( filePath ) ? filePath : path . join ( rootDirectory , filePath ) ;
89- try {
90- const lines = fs . readFileSync ( absolutePath , "utf-8" ) . split ( "\n" ) ;
91- fileLineCache . set ( filePath , lines ) ;
92- return lines ;
93- } catch {
94- fileLineCache . set ( filePath , null ) ;
95- return null ;
96- }
97- } ;
88+ const getFileLines = createFileLinesCache ( rootDirectory ) ;
9889
9990 return diagnostics . filter ( ( diagnostic ) => {
10091 if ( diagnostic . line <= 0 ) return true ;
@@ -111,9 +102,9 @@ export const filterInlineSuppressions = (
111102 }
112103
113104 if ( diagnostic . line >= 2 ) {
114- const prevLine = lines [ diagnostic . line - 2 ] ;
115- if ( prevLine ) {
116- const nextLineMatch = prevLine . match ( DISABLE_NEXT_LINE_PATTERN ) ;
105+ const previousLine = lines [ diagnostic . line - 2 ] ;
106+ if ( previousLine ) {
107+ const nextLineMatch = previousLine . match ( DISABLE_NEXT_LINE_PATTERN ) ;
117108 if ( nextLineMatch && isRuleSuppressed ( nextLineMatch [ 1 ] , ruleId ) ) return false ;
118109 }
119110 }
0 commit comments