1- /**
2- * Snapshot Query and Search System
3- *
4- * Provides search functionality for snapshot text with glob pattern support
5- */
1+ import { matcher } from "micromatch" ;
62
73export const SKIP_ROLES = [
84 "generic" ,
@@ -21,59 +17,7 @@ export const SKIP_ROLES = [
2117] ;
2218
2319function hasGlobPattern ( str : string ) : boolean {
24- return / [ * ? [ \] { } ] / . test ( str ) ;
25- }
26-
27- function matchGlob (
28- pattern : string ,
29- text : string ,
30- caseSensitive : boolean = false ,
31- ) : boolean {
32- if ( ! caseSensitive ) {
33- pattern = pattern . toLowerCase ( ) ;
34- text = text . toLowerCase ( ) ;
35- }
36-
37- if ( pattern . includes ( "{" ) && pattern . includes ( "}" ) ) {
38- const braceStart = pattern . indexOf ( "{" ) ;
39- const braceEnd = pattern . indexOf ( "}" ) ;
40- if ( braceStart < braceEnd ) {
41- const prefix = pattern . substring ( 0 , braceStart ) ;
42- const suffix = pattern . substring ( braceEnd + 1 ) ;
43- const alternatives = pattern
44- . substring ( braceStart + 1 , braceEnd )
45- . split ( "," ) ;
46-
47- for ( const alt of alternatives ) {
48- const fullPattern = prefix + alt . trim ( ) + suffix ;
49- if ( matchGlob ( fullPattern , text , caseSensitive ) ) {
50- return true ;
51- }
52- }
53- return false ;
54- }
55- }
56-
57- let regexPattern = pattern
58- . replace ( / [ . * + ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" )
59- . replace ( / \\ \* / g, ".*" )
60- . replace ( / \\ \? / g, "." )
61- . replace ( / \\ \[ / g, "[" )
62- . replace ( / \\ \] / g, "]" ) ;
63-
64- regexPattern = regexPattern . replace ( / \[ ( [ ^ \] ] + ) \] / g, ( _ , chars ) => {
65- if ( chars . includes ( "-" ) && chars . length === 3 ) {
66- return `[${ chars } ]` ;
67- }
68- return `[${ chars . replace ( / [ . * + ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) } ]` ;
69- } ) ;
70-
71- try {
72- const regex = new RegExp ( `${ regexPattern } ` , "i" ) ;
73- return regex . test ( text ) ;
74- } catch {
75- return false ;
76- }
20+ return / [ * ? [ { \] } ] / . test ( str ) ;
7721}
7822
7923export interface SearchOptions {
@@ -108,6 +52,9 @@ export function searchSnapshotText(
10852 useGlob !== undefined
10953 ? useGlob
11054 : searchTerms . some ( ( term ) => hasGlobPattern ( term ) ) ;
55+ const matcherFns = shouldUseGlob
56+ ? searchTerms . map ( ( term ) => matcher ( term , { nocase : ! caseSensitive } ) )
57+ : [ ] ;
11158
11259 const lines = snapshotText . split ( "\n" ) ;
11360 const matchedLines : number [ ] = [ ] ;
@@ -117,7 +64,9 @@ export function searchSnapshotText(
11764 if ( line === undefined ) {
11865 continue ;
11966 }
120- if ( matchLine ( line , searchTerms , caseSensitive , shouldUseGlob ) ) {
67+ if (
68+ matchLine ( line , searchTerms , matcherFns , caseSensitive , shouldUseGlob )
69+ ) {
12170 matchedLines . push ( i ) ;
12271 }
12372 }
@@ -134,23 +83,19 @@ export function searchSnapshotText(
13483function matchLine (
13584 line : string ,
13685 searchTerms : string [ ] ,
86+ matchers : Array < ( value : string ) => boolean > ,
13787 caseSensitive : boolean ,
13888 useGlob : boolean ,
13989) : boolean {
140- for ( const term of searchTerms ) {
141- if ( useGlob ) {
142- if ( matchGlob ( term , line , caseSensitive ) ) {
143- return true ;
144- }
145- } else {
146- const lineValue = caseSensitive ? line : line . toLowerCase ( ) ;
147- const searchTerm = caseSensitive ? term : term . toLowerCase ( ) ;
148- if ( lineValue . includes ( searchTerm ) ) {
149- return true ;
150- }
151- }
90+ if ( useGlob ) {
91+ return matchers . some ( ( match ) => match ( line ) ) ;
15292 }
153- return false ;
93+
94+ const lineValue = caseSensitive ? line : line . toLowerCase ( ) ;
95+ return searchTerms . some ( ( term ) => {
96+ const searchTerm = caseSensitive ? term : term . toLowerCase ( ) ;
97+ return lineValue . includes ( searchTerm ) ;
98+ } ) ;
15499}
155100
156101function expandLineContext (
0 commit comments