@@ -11,10 +11,13 @@ export class ContextignoreLinter {
1111 private criticalPatterns : Set < string > ;
1212 // Cache of ignore instances for each directory
1313 private ignoreCache : Map < string , ReturnType < typeof ignore > > ;
14+ // Store error messages
15+ private errorMessages : string [ ] ;
1416
1517 constructor ( ) {
1618 this . criticalPatterns = new Set ( [ '.context.md' , '.context.yaml' , '.context.json' , '.contextdocs.md' , '.contextignore' ] ) ;
1719 this . ignoreCache = new Map ( ) ;
20+ this . errorMessages = [ ] ;
1821 }
1922
2023 /**
@@ -25,6 +28,7 @@ export class ContextignoreLinter {
2528 */
2629 public async lintContextignoreFile ( content : string , filePath : string ) : Promise < boolean > {
2730 try {
31+ this . errorMessages = [ ] ; // Reset error messages
2832 const relativePath = path . relative ( process . cwd ( ) , filePath ) ;
2933 console . log ( `\nLinting file: ${ relativePath } ` ) ;
3034 console . log ( '- Validating .contextignore format' ) ;
@@ -39,7 +43,7 @@ export class ContextignoreLinter {
3943
4044 // Updated regex to catch more invalid patterns, including "invalid**pattern"
4145 if ( ! / ^ ! ? (?: [ \w \- . / ] + | \* (? ! \* ) | \? + | \[ .+ \] ) + $ / . test ( line ) ) {
42- console . error ( ` Error: Invalid pattern at line ${ i + 1 } : ${ line } `) ;
46+ this . errorMessages . push ( ` Error: Invalid pattern at line ${ i + 1 } : ${ line } `) ;
4347 isValid = false ;
4448 }
4549
@@ -61,7 +65,7 @@ export class ContextignoreLinter {
6165 console . log ( '' ) ; // Add a blank line for better readability
6266 return isValid ;
6367 } catch ( error ) {
64- console . error ( `Error linting .contextignore file ${ filePath } : ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
68+ this . errorMessages . push ( `Error linting .contextignore file ${ filePath } : ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
6569 return false ;
6670 }
6771 }
@@ -75,7 +79,7 @@ export class ContextignoreLinter {
7579 private validateCriticalPattern ( pattern : string , lineNumber : number ) : boolean {
7680 for ( const criticalPattern of this . criticalPatterns ) {
7781 if ( pattern . endsWith ( criticalPattern ) || pattern . includes ( `/${ criticalPattern } ` ) ) {
78- console . error ( ` Error: Pattern at line ${ lineNumber + 1 } ignores critical file: ${ criticalPattern } `) ;
82+ this . errorMessages . push ( ` Error: Pattern at line ${ lineNumber + 1 } ignores critical file: ${ criticalPattern } `) ;
7983 return false ;
8084 }
8185 }
@@ -92,7 +96,7 @@ export class ContextignoreLinter {
9296 for ( let j = i + 1 ; j < patterns . length ; j ++ ) {
9397 if ( patterns [ i ] . startsWith ( '!' ) && patterns [ j ] === patterns [ i ] . slice ( 1 ) ||
9498 patterns [ j ] . startsWith ( '!' ) && patterns [ i ] === patterns [ j ] . slice ( 1 ) ) {
95- console . error ( ` Error: Conflicting patterns found: ${ patterns [ i ] } and ${ patterns [ j ] } `) ;
99+ this . errorMessages . push ( ` Error: Conflicting patterns found: ${ patterns [ i ] } and ${ patterns [ j ] } `) ;
96100 return false ;
97101 }
98102 }
@@ -110,7 +114,7 @@ export class ContextignoreLinter {
110114 const ig = ignore ( ) . add ( patterns ) ;
111115 this . ignoreCache . set ( path . dirname ( filePath ) , ig ) ;
112116 } catch ( error ) {
113- console . error ( `Error updating ignore cache for ${ filePath } : ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
117+ this . errorMessages . push ( `Error updating ignore cache for ${ filePath } : ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
114118 }
115119 }
116120
@@ -137,7 +141,7 @@ export class ContextignoreLinter {
137141
138142 return false ;
139143 } catch ( error ) {
140- console . error ( `Error checking if file ${ filePath } is ignored: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
144+ this . errorMessages . push ( `Error checking if file ${ filePath } is ignored: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
141145 return false ;
142146 }
143147 }
@@ -170,7 +174,7 @@ export class ContextignoreLinter {
170174 return ig . ignores ( relativePath ) ;
171175 } ) ;
172176 } catch ( error ) {
173- console . error ( `Error getting ignored files for directory ${ directoryPath } : ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
177+ this . errorMessages . push ( `Error getting ignored files for directory ${ directoryPath } : ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
174178 return [ ] ;
175179 }
176180 }
@@ -198,4 +202,12 @@ export class ContextignoreLinter {
198202 walk ( directoryPath ) ;
199203 return files ;
200204 }
205+
206+ /**
207+ * Get the error messages
208+ * @returns An array of error messages
209+ */
210+ public getErrorMessages ( ) : string [ ] {
211+ return this . errorMessages ;
212+ }
201213}
0 commit comments