@@ -130,7 +130,23 @@ const transform_message = (message, { unoffsets, dedent, offsets, range }) => {
130130 return true ;
131131} ;
132132
133- /// PRE- AND POSTPROCESSING FUNCTIONS FOR SVELTE COMPONENTS ///
133+ // find the contextual name or names described by a particular node in the AST
134+ const contextual_names = [ ] ;
135+ const find_contextual_names = node => {
136+ if ( node ) {
137+ if ( typeof node === 'string' ) {
138+ contextual_names . push ( node ) ;
139+ } else if ( typeof node === 'object' ) {
140+ walk ( node , {
141+ enter ( node , parent , prop ) {
142+ if ( node . name && prop !== 'key' ) {
143+ contextual_names . push ( node . name ) ;
144+ }
145+ } ,
146+ } ) ;
147+ }
148+ }
149+ } ;
134150
135151// extract scripts to lint from component definition
136152const preprocess = text => {
@@ -217,26 +233,23 @@ const preprocess = text => {
217233 }
218234
219235 // add expressions from template to the constructed string
236+ const nodes_with_contextual_scope = new WeakSet ( ) ;
220237 walk ( ast . html , {
221238 enter ( node , parent , prop ) {
222239 if ( prop === 'expression' ) {
223240 return this . skip ( ) ;
224241 }
225- if ( node . context && typeof node . context === 'object' ) {
226- // find all the variables declared in this context
227- const names = [ ] ;
228- walk ( node . context , {
229- enter ( node , parent , prop ) {
230- if ( node . name && prop !== 'key' ) {
231- names . push ( node . name ) ;
232- }
233- } ,
234- } ) ;
235- transformed_code += `{${ names . map ( name => `let ${ name } =0;` ) . join ( '' ) } \n` ;
242+ contextual_names . length = 0 ;
243+ find_contextual_names ( node . context ) ;
244+ if ( node . type === 'EachBlock' ) {
245+ find_contextual_names ( node . index ) ;
246+ } else if ( node . type === 'AwaitBlock' ) {
247+ find_contextual_names ( node . value ) ;
248+ find_contextual_names ( node . error ) ;
236249 }
237- if ( node . index && typeof node . index === 'string' ) {
238- // declare the index variable, if present
239- transformed_code += `{let ${ node . index } =0;\n` ;
250+ if ( contextual_names . length ) {
251+ nodes_with_contextual_scope . add ( node ) ;
252+ transformed_code += `{let ${ contextual_names . map ( name => ` ${ name } =0` ) . join ( ',' ) } ;\n` ;
240253 }
241254 if ( node . expression && typeof node . expression === 'object' ) {
242255 // add the expression in question to the constructed string
@@ -246,11 +259,8 @@ const preprocess = text => {
246259 }
247260 } ,
248261 leave ( node ) {
249- // close nested scopes created for context or index
250- if ( node . context && typeof node . context === 'object' ) {
251- transformed_code += '}' ;
252- }
253- if ( node . index && typeof node . index === 'string' ) {
262+ // close contextual scope
263+ if ( nodes_with_contextual_scope . has ( node ) ) {
254264 transformed_code += '}' ;
255265 }
256266 } ,
0 commit comments