@@ -22,20 +22,20 @@ const find_contextual_names = (compiler, node) => {
22
22
}
23
23
}
24
24
} ;
25
- // let-declaration when using TypeScript which is able to infer the type value of a autosubscribed store
26
- const tsLet = ( name ) =>
27
- name [ 0 ] === '$' ?
25
+ // let-declaration that ( when using TypeScript) is able to infer the type value of a autosubscribed store
26
+ const make_let = ( name , is_typescript ) =>
27
+ is_typescript && name [ 0 ] === '$' ?
28
28
// Disable eslint on that line because it may result in a "used before defined" error
29
29
`declare let ${ name } :Parameters<Parameters<typeof ${ name . slice ( 1 ) } .subscribe>[0]>[0]; // eslint-disable-line\n` :
30
30
`let ${ name } ;` ;
31
- // ignore_styles when a `lang=` or `type=` attribute is present on the <style> tag
32
- const ignoreStylesFallback = ( { type, lang } ) => ! ! type || ! ! lang ;
33
31
32
+ // ignore_styles when a `lang=` or `type=` attribute is present on the <style> tag
33
+ const ignore_styles_fallback = ( { type, lang } ) => ! ! type || ! ! lang ;
34
34
35
35
// extract scripts to lint from component definition
36
36
export const preprocess = text => {
37
37
const compiler = processor_options . custom_compiler || default_compiler || ( default_compiler = require ( 'svelte/compiler' ) ) ;
38
- const ignore_styles = processor_options . ignore_styles ? processor_options . ignore_styles : ignoreStylesFallback ;
38
+ const ignore_styles = processor_options . ignore_styles ? processor_options . ignore_styles : ignore_styles_fallback ;
39
39
if ( ignore_styles ) {
40
40
// wipe the appropriate <style> tags in the file
41
41
text = text . replace ( / < s t y l e ( \s [ ^ ] * ?) ? > [ ^ ] * ?< \/ s t y l e > / gi, ( match , attributes = '' ) => {
@@ -121,15 +121,9 @@ export const preprocess = text => {
121
121
const block = new_block ( ) ;
122
122
state . blocks . set ( with_file_ending ( 'instance' ) , block ) ;
123
123
124
+ block . transformed_code = vars . filter ( v => v . injected || ! processor_options . typescript && v . module ) . map ( v => make_let ( v . name , processor_options . typescript ) ) . join ( '' ) ;
124
125
if ( ast . module && processor_options . typescript ) {
125
- block . transformed_code = vars . filter ( v => v . injected ) . map ( v => tsLet ( v . name ) ) . join ( '' ) ;
126
126
block . transformed_code += text . slice ( ast . module . content . start , ast . module . content . end ) ;
127
- } else {
128
- if ( processor_options . typescript ) {
129
- block . transformed_code = vars . filter ( v => v . injected || v . module ) . map ( v => tsLet ( v . name ) ) . join ( '' ) ;
130
- } else {
131
- block . transformed_code = vars . filter ( v => v . injected || v . module ) . map ( v => `let ${ v . name } ;` ) . join ( '' ) ;
132
- }
133
127
}
134
128
135
129
get_translation ( text , block , ast . instance . content ) ;
@@ -150,7 +144,7 @@ export const preprocess = text => {
150
144
if ( ast . instance || vars . length ) {
151
145
block . transformed_code += '\n' ;
152
146
}
153
- block . transformed_code += vars . filter ( v => v . injected ) . map ( v => tsLet ( v . name ) ) . join ( '' ) ;
147
+ block . transformed_code += vars . filter ( v => v . injected ) . map ( v => make_let ( v . name , true ) ) . join ( '' ) ;
154
148
if ( ast . instance ) {
155
149
block . transformed_code += text . slice ( ast . instance . content . start , ast . instance . content . end ) ;
156
150
}
@@ -257,10 +251,10 @@ function compile_code(text, compiler, processor_options) {
257
251
transformers : {
258
252
before : [ ts_import_transformer ]
259
253
}
260
- }
254
+ } ;
261
255
262
256
// See if we can use `preserveValueImports` instead of the transformer (TS >= 4.5)
263
- const ts_version = ts . version . split ( "." ) . map ( str => parseInt ( str , 10 ) ) ;
257
+ const ts_version = ts . version . split ( '.' ) . map ( str => parseInt ( str , 10 ) ) ;
264
258
if ( ts_version [ 0 ] > 4 || ( ts_version [ 0 ] === 4 && ts_version [ 1 ] >= 5 ) ) {
265
259
ts_options . compilerOptions . preserveValueImports = true ;
266
260
ts_options . transformers = { } ;
@@ -307,7 +301,7 @@ function compile_code(text, compiler, processor_options) {
307
301
// if we do a full recompile Svelte can fail due to the blank script tag not declaring anything
308
302
// so instead we just parse for the AST (which is likely faster, anyways)
309
303
const ast = compiler . parse ( text , { ...processor_options . compiler_options } ) ;
310
- const { warnings, vars } = ts_result ;
304
+ const { warnings, vars } = ts_result ;
311
305
return { ast, warnings, vars, mapper } ;
312
306
}
313
307
}
0 commit comments