2
2
3
3
const { compile } = require ( 'svelte/compiler' ) ;
4
4
5
- let compiler_options , messages , transformed_code , ignore_warnings , module_info , instance_info ;
5
+ let compiler_options , messages , transformed_code , ignore_warnings , translations ;
6
6
7
7
// get the total length, number of lines, and length of the last line of a string
8
8
const get_offsets = str => {
@@ -54,6 +54,9 @@ const dedent_code = str => {
54
54
55
55
// transform a linting message according to the module/instance script info we've gathered
56
56
const transform_message = ( message , { unoffsets, dedent, offsets, range } ) => {
57
+ if ( message . line < unoffsets . lines ) {
58
+ return false ;
59
+ }
57
60
// strip out the start and end of the fix if they are not actually changes
58
61
if ( message . fix ) {
59
62
while ( transformed_code [ message . fix . range [ 0 ] ] === message . fix . text [ 0 ] ) {
@@ -124,7 +127,7 @@ const transform_message = (message, { unoffsets, dedent, offsets, range }) => {
124
127
message . fix . range [ 1 ] = range [ 1 ] ;
125
128
}
126
129
}
127
- return message ;
130
+ return true ;
128
131
} ;
129
132
130
133
/// PRE- AND POSTPROCESSING FUNCTIONS FOR SVELTE COMPONENTS ///
@@ -166,29 +169,30 @@ const preprocess = text => {
166
169
endColumn : end && end . column + 1 ,
167
170
} ) ) ;
168
171
169
- if ( ! ast . module && ! ast . instance ) {
170
- return [ ] ;
171
- }
172
-
173
172
// build a string that we can send along to ESLint to get the remaining messages
174
173
175
174
// include declarations of all injected identifiers
176
175
transformed_code = injected_vars . length ? `/* eslint-disable */let ${ injected_vars . map ( v => v . name ) . join ( ',' ) } ;\n/* eslint-enable */` : '' ;
177
176
178
- // get module_info/instance_info and include the processed scripts in transformed_code
179
- const get_info = script => {
180
- const info = { unoffsets : get_offsets ( transformed_code ) } ;
181
- const { content } = script ;
182
- info . range = [ content . start , content . end ] ;
183
- const { dedented, offsets } = dedent_code ( text . slice ( content . start , content . end ) ) ;
177
+ // get translation info and include the processed scripts in transformed_code
178
+ const get_translation = node => {
179
+ const translation = { unoffsets : get_offsets ( transformed_code ) } ;
180
+ translation . range = [ node . start , node . end ] ;
181
+ const { dedented, offsets } = dedent_code ( text . slice ( node . start , node . end ) ) ;
184
182
transformed_code += dedented ;
185
- info . offsets = get_offsets ( text . slice ( 0 , content . start ) ) ;
186
- info . dedent = offsets ;
187
- return info ;
183
+ translation . offsets = get_offsets ( text . slice ( 0 , node . start ) ) ;
184
+ translation . dedent = offsets ;
185
+ translations . push ( translation ) ;
188
186
} ;
189
- module_info = ast . module && get_info ( ast . module ) ;
187
+
188
+ translations = [ ] ;
189
+ if ( ast . module ) {
190
+ get_translation ( ast . module . content ) ;
191
+ }
190
192
transformed_code += '/* eslint-disable */\n/* eslint-enable */' ;
191
- instance_info = ast . instance && get_info ( ast . instance ) ;
193
+ if ( ast . instance ) {
194
+ get_translation ( ast . instance . content ) ;
195
+ }
192
196
transformed_code += '/* eslint-disable */' ;
193
197
194
198
// no-unused-vars: create references to all identifiers referred to by the template
@@ -201,6 +205,9 @@ const preprocess = text => {
201
205
transformed_code += `\n{${ reassigned_vars . map ( v => v . name + '=0' ) . join ( ';' ) } }` ;
202
206
}
203
207
208
+ // reverse sort the translations
209
+ translations . sort ( ( a , b ) => b . unoffsets . length - a . unoffsets . length ) ;
210
+
204
211
// return processed string
205
212
return [ transformed_code ] ;
206
213
} ;
@@ -212,10 +219,11 @@ const postprocess = ([raw_messages]) => {
212
219
for ( let i = 0 ; i < raw_messages . length ; i ++ ) {
213
220
const message = raw_messages [ i ] ;
214
221
if ( message . ruleId !== 'no-self-assign' && ( message . ruleId !== 'no-unused-labels' || ! message . message . includes ( "'$:'" ) ) ) {
215
- if ( instance_info && message . line >= instance_info . unoffsets . lines ) {
216
- messages . push ( transform_message ( message , instance_info ) ) ;
217
- } else if ( module_info ) {
218
- messages . push ( transform_message ( message , module_info ) ) ;
222
+ for ( let k = 0 ; k < translations . length ; k ++ ) {
223
+ if ( transform_message ( message , translations [ k ] ) ) {
224
+ messages . push ( message ) ;
225
+ break ;
226
+ }
219
227
}
220
228
}
221
229
}
0 commit comments