@@ -22,27 +22,31 @@ export const InvalidComparisonSyntax: LiquidCheckDefinition = {
22
22
return ;
23
23
}
24
24
25
- // We need to examine the markup string directly
26
25
const markup = node . markup . toString ( ) ;
27
26
28
- // Find the offsets of any invalid tokens after comparisons
29
- const positions = findInvalidTokenPositionsAfterComparisons ( markup ) ;
27
+ const regex =
28
+ / ( > = | < = | > | < | = = | ! = ) \s + ( [ ^ \s ] + ) \s + ( [ ^ \s ] + ) (? ! \s + ( a n d | o r | % } | c o n t a i n s | s t a r t s w i t h | e n d s w i t h ) ) / g ;
30
29
31
- if ( positions . length > 0 ) {
32
- // Calculate markup position in the source
33
- const markupStart = node . position . start + ( node . name . length + 3 ) ; // {% + name + space
30
+ let match ;
31
+ while ( ( match = regex . exec ( markup ) ) !== null ) {
32
+ const invalidToken = match [ 3 ] ;
33
+
34
+ if ( ! isValidComparisonConnector ( invalidToken ) ) {
35
+ const invalidTokenOffset = match . index + match [ 0 ] . lastIndexOf ( invalidToken ) ;
36
+
37
+ const markupStart = node . position . start + node . name . length + 3 ;
38
+ const startIndex = markupStart + invalidTokenOffset + 1 ;
39
+ const endIndex = startIndex + invalidToken . length ;
34
40
35
- // Report each invalid token
36
- for ( const pos of positions ) {
37
41
context . report ( {
38
- message : `Invalid token '${ pos . token } ' after comparison` ,
39
- startIndex : markupStart + pos . start ,
40
- endIndex : markupStart + pos . end ,
42
+ message : `Invalid token '${ invalidToken } ' after comparison` ,
43
+ startIndex,
44
+ endIndex,
41
45
suggest : [
42
46
{
43
- message : `Remove '${ pos . token } '` ,
47
+ message : `Remove '${ invalidToken } '` ,
44
48
fix : ( corrector ) => {
45
- corrector . remove ( markupStart + pos . start , markupStart + pos . end ) ;
49
+ corrector . remove ( startIndex , endIndex + 1 ) ;
46
50
} ,
47
51
} ,
48
52
] ,
@@ -54,37 +58,7 @@ export const InvalidComparisonSyntax: LiquidCheckDefinition = {
54
58
} ,
55
59
} ;
56
60
57
- // Helper function to find all invalid tokens after comparisons in the markup
58
- function findInvalidTokenPositionsAfterComparisons ( markup ) {
59
- const positions = [ ] ;
60
-
61
- // Regex to find comparison operations with potentially invalid tokens after them
62
- const regex =
63
- / ( > = | < = | > | < | = = | ! = ) \s + ( [ ^ \s ] + ) \s + ( \w + ) (? ! \s + ( a n d | o r | % } | c o n t a i n s | s t a r t s w i t h | e n d s w i t h ) ) / g;
64
-
65
- let match ;
66
- while ( ( match = regex . exec ( markup ) ) !== null ) {
67
- const invalidToken = match [ 3 ] ;
68
-
69
- if ( ! isValidComparisonConnector ( invalidToken ) ) {
70
- // Find the position of the token within the match
71
- const matchStart = match . index ;
72
- const beforeInvalidToken = match [ 0 ] . substring ( 0 , match [ 0 ] . lastIndexOf ( invalidToken ) ) ;
73
- const tokenStart = matchStart + beforeInvalidToken . length ;
74
- const tokenEnd = tokenStart + invalidToken . length ;
75
-
76
- positions . push ( {
77
- token : invalidToken ,
78
- start : tokenStart ,
79
- end : tokenEnd ,
80
- } ) ;
81
- }
82
- }
83
-
84
- return positions ;
85
- }
86
-
87
- function isValidComparisonConnector ( token ) {
61
+ function isValidComparisonConnector ( token : string ) : boolean {
88
62
const validConnectors = [ 'and' , 'or' , '%}' , 'contains' , 'startswith' , 'endswith' ] ;
89
63
return validConnectors . some ( ( connector ) => token . includes ( connector ) ) ;
90
64
}
0 commit comments