1
1
import type { AST } from 'svelte-eslint-parser' ;
2
2
import type { RuleContext } from '../../types.js' ;
3
3
4
- const SVELTE_IGNORE_PATTERN = / ^ \s * s v e l t e - i g n o r e / m ;
4
+ const SVELTE_IGNORE_PATTERN = / ^ \s * s v e l t e - i g n o r e \s + / ;
5
5
6
6
/**
7
7
* Map of legacy code -> new code
@@ -37,29 +37,45 @@ export function getSvelteIgnoreItems(context: RuleContext): (IgnoreItem | Ignore
37
37
38
38
const ignoreComments : ( IgnoreItem | IgnoreItemWithoutCode ) [ ] = [ ] ;
39
39
for ( const comment of sourceCode . getAllComments ( ) ) {
40
- const ignores = extractSvelteIgnore ( comment . value , comment . range [ 0 ] + 2 , comment ) ;
41
- if ( ignores ) {
42
- ignoreComments . push ( ...ignores ) ;
43
- } else if ( hasMissingCodeIgnore ( comment . value ) ) {
40
+ const match = SVELTE_IGNORE_PATTERN . exec ( comment . value ) ;
41
+ if ( ! match ) {
42
+ continue ;
43
+ }
44
+ const codeListStart = match . index + match [ 0 ] . length ;
45
+ const codeList = comment . value . slice ( codeListStart ) ;
46
+ if ( hasMissingCodeIgnore ( codeList ) ) {
44
47
ignoreComments . push ( {
45
48
range : comment . range ,
46
49
code : null ,
47
50
token : comment
48
51
} ) ;
52
+ } else {
53
+ const ignores = extractSvelteIgnore ( comment . range [ 0 ] + 2 , comment , codeList , codeListStart ) ;
54
+ if ( ignores ) {
55
+ ignoreComments . push ( ...ignores ) ;
56
+ }
49
57
}
50
58
}
51
59
for ( const token of sourceCode . ast . tokens ) {
52
60
if ( token . type === 'HTMLComment' ) {
53
61
const text = token . value . slice ( 4 , - 3 ) ;
54
- const ignores = extractSvelteIgnore ( text , token . range [ 0 ] + 4 , token ) ;
55
- if ( ignores ) {
56
- ignoreComments . push ( ...ignores ) ;
57
- } else if ( hasMissingCodeIgnore ( text ) ) {
62
+ const match = SVELTE_IGNORE_PATTERN . exec ( text ) ;
63
+ if ( ! match ) {
64
+ continue ;
65
+ }
66
+ const codeListStart = match . index + match [ 0 ] . length ;
67
+ const codeList = text . slice ( codeListStart ) ;
68
+ if ( hasMissingCodeIgnore ( codeList ) ) {
58
69
ignoreComments . push ( {
59
70
range : token . range ,
60
71
code : null ,
61
72
token
62
73
} ) ;
74
+ } else {
75
+ const ignores = extractSvelteIgnore ( token . range [ 0 ] + 4 , token , codeList , codeListStart ) ;
76
+ if ( ignores ) {
77
+ ignoreComments . push ( ...ignores ) ;
78
+ }
63
79
}
64
80
}
65
81
}
@@ -69,46 +85,42 @@ export function getSvelteIgnoreItems(context: RuleContext): (IgnoreItem | Ignore
69
85
70
86
/** Extract svelte-ignore rule names */
71
87
function extractSvelteIgnore (
72
- text : string ,
73
88
startIndex : number ,
74
- token : AST . Token | AST . Comment
89
+ token : AST . Token | AST . Comment ,
90
+ codeList : string ,
91
+ ignoreStart : number
75
92
) : IgnoreItem [ ] | null {
76
- const m1 = SVELTE_IGNORE_PATTERN . exec ( text ) ;
77
- if ( ! m1 ) {
78
- return null ;
79
- }
80
- const ignoreStart = m1 . index + m1 [ 0 ] . length ;
81
- const beforeText = text . slice ( ignoreStart ) ;
82
- if ( ! / ^ \s / . test ( beforeText ) || ! beforeText . trim ( ) ) {
83
- return null ;
84
- }
85
- let start = startIndex + ignoreStart ;
86
-
93
+ const start = startIndex + ignoreStart ;
87
94
const results : IgnoreItem [ ] = [ ] ;
88
- for ( const code of beforeText . split ( / \s / ) ) {
89
- const end = start + code . length ;
90
- const trimmed = code . trim ( ) ;
91
- if ( trimmed ) {
95
+ const separatorPattern = / \s * [ \s , ] \s * / g;
96
+ const separators = codeList . matchAll ( separatorPattern ) ;
97
+ let lastSeparatorEnd = 0 ;
98
+ for ( const separator of separators ) {
99
+ const code = codeList . slice ( lastSeparatorEnd , separator . index ) ;
100
+ if ( code ) {
92
101
results . push ( {
93
- code : trimmed ,
94
- codeForV5 : V5_REPLACEMENTS [ trimmed ] || trimmed . replace ( / - / gu, '_' ) ,
95
- range : [ start , end ] ,
102
+ code,
103
+ codeForV5 : V5_REPLACEMENTS [ code ] || code . replace ( / - / gu, '_' ) ,
104
+ range : [ start + lastSeparatorEnd , start + separator . index ] ,
96
105
token
97
106
} ) ;
98
107
}
99
- start = end + 1 ; /* space */
108
+ lastSeparatorEnd = separator . index + separator [ 0 ] . length ;
109
+ }
110
+ if ( results . length === 0 ) {
111
+ const code = codeList ;
112
+ results . push ( {
113
+ code,
114
+ codeForV5 : V5_REPLACEMENTS [ code ] || code . replace ( / - / gu, '_' ) ,
115
+ range : [ start , start + code . length ] ,
116
+ token
117
+ } ) ;
100
118
}
101
119
102
120
return results ;
103
121
}
104
122
105
123
/** Checks whether given comment has missing code svelte-ignore */
106
- function hasMissingCodeIgnore ( text : string ) {
107
- const m1 = SVELTE_IGNORE_PATTERN . exec ( text ) ;
108
- if ( ! m1 ) {
109
- return false ;
110
- }
111
- const ignoreStart = m1 . index + m1 [ 0 ] . length ;
112
- const beforeText = text . slice ( ignoreStart ) ;
113
- return ! beforeText . trim ( ) ;
124
+ function hasMissingCodeIgnore ( codeList : string ) {
125
+ return ! codeList . trim ( ) ;
114
126
}
0 commit comments