1
1
import MagicString , { Bundle } from 'magic-string' ;
2
2
import { walk } from 'estree-walker' ;
3
- import isReference from '../utils/isReference.js' ;
4
- import flattenReference from '../utils/flattenReference.js' ;
5
- import globalWhitelist from '../utils/globalWhitelist.js' ;
6
- import reservedNames from '../utils/reservedNames.js' ;
7
- import namespaces from '../utils/namespaces.js' ;
8
- import { removeNode , removeObjectKey } from '../utils/removeNode.js' ;
9
- import getIntro from './shared/utils/getIntro.js' ;
10
- import getOutro from './shared/utils/getOutro.js' ;
11
- import processCss from './shared/processCss.js' ;
12
- import annotateWithScopes from '../utils/annotateWithScopes.js' ;
3
+ import isReference from '../utils/isReference' ;
4
+ import flattenReference from '../utils/flattenReference' ;
5
+ import globalWhitelist from '../utils/globalWhitelist' ;
6
+ import reservedNames from '../utils/reservedNames' ;
7
+ import namespaces from '../utils/namespaces' ;
8
+ import { removeNode , removeObjectKey } from '../utils/removeNode' ;
9
+ import getIntro from './shared/utils/getIntro' ;
10
+ import getOutro from './shared/utils/getOutro' ;
11
+ import processCss from './shared/processCss' ;
12
+ import annotateWithScopes from '../utils/annotateWithScopes' ;
13
+ import DomBlock from './dom/Block' ;
14
+ import SsrBlock from './server-side-rendering/Block' ;
15
+ import { Node , Parsed , CompileOptions } from '../interfaces' ;
13
16
14
17
const test = typeof global !== 'undefined' && global . __svelte_test ;
15
18
16
19
export default class Generator {
17
- constructor ( parsed , source , name , options ) {
20
+ parsed : Parsed ;
21
+ source : string ;
22
+ name : string ;
23
+ options : CompileOptions ;
24
+
25
+ imports : Node [ ] ;
26
+ helpers : Set < string > ;
27
+ components : Set < string > ;
28
+ events : Set < string > ;
29
+ transitions : Set < string > ;
30
+ importedComponents : Map < string , string > ;
31
+
32
+ bindingGroups : string [ ] ;
33
+ expectedProperties : Set < string > ;
34
+ css : string ;
35
+ cssId : string ;
36
+ usesRefs : boolean ;
37
+
38
+ importedNames : Set < string > ;
39
+ aliases : Map < string , string > ;
40
+ usedNames : Set < string > ;
41
+
42
+ constructor ( parsed : Parsed , source : string , name : string , options : CompileOptions ) {
18
43
this . parsed = parsed ;
19
44
this . source = source ;
20
45
this . name = name ;
@@ -39,33 +64,33 @@ export default class Generator {
39
64
this . usesRefs = false ;
40
65
41
66
// allow compiler to deconflict user's `import { get } from 'whatever'` and
42
- // Svelte's builtin `import { get, ... } from 'svelte/shared.js '`;
67
+ // Svelte's builtin `import { get, ... } from 'svelte/shared.ts '`;
43
68
this . importedNames = new Set ( ) ;
44
69
this . aliases = new Map ( ) ;
45
- this . _usedNames = new Set ( [ name ] ) ;
70
+ this . usedNames = new Set ( [ name ] ) ;
46
71
}
47
72
48
- addSourcemapLocations ( node ) {
73
+ addSourcemapLocations ( node : Node ) {
49
74
walk ( node , {
50
- enter : node => {
75
+ enter : ( node : Node ) => {
51
76
this . code . addSourcemapLocation ( node . start ) ;
52
77
this . code . addSourcemapLocation ( node . end ) ;
53
78
}
54
79
} ) ;
55
80
}
56
81
57
- alias ( name ) {
82
+ alias ( name : string ) {
58
83
if ( ! this . aliases . has ( name ) ) {
59
84
this . aliases . set ( name , this . getUniqueName ( name ) ) ;
60
85
}
61
86
62
87
return this . aliases . get ( name ) ;
63
88
}
64
89
65
- contextualise ( block , expression , context , isEventHandler ) {
90
+ contextualise ( block : DomBlock | SsrBlock , expression : Node , context : string , isEventHandler : boolean ) {
66
91
this . addSourcemapLocations ( expression ) ;
67
92
68
- const usedContexts = [ ] ;
93
+ const usedContexts : string [ ] = [ ] ;
69
94
70
95
const { code, helpers } = this ;
71
96
const { contexts, indexes } = block ;
@@ -76,7 +101,7 @@ export default class Generator {
76
101
const self = this ;
77
102
78
103
walk ( expression , {
79
- enter ( node , parent , key ) {
104
+ enter ( node : Node , parent : Node , key : string ) {
80
105
if ( / ^ F u n c t i o n / . test ( node . type ) ) lexicalDepth += 1 ;
81
106
82
107
if ( node . _scope ) {
@@ -138,7 +163,7 @@ export default class Generator {
138
163
}
139
164
} ,
140
165
141
- leave ( node ) {
166
+ leave ( node : Node ) {
142
167
if ( / ^ F u n c t i o n / . test ( node . type ) ) lexicalDepth -= 1 ;
143
168
if ( node . _scope ) scope = scope . parent ;
144
169
}
@@ -151,16 +176,16 @@ export default class Generator {
151
176
} ;
152
177
}
153
178
154
- findDependencies ( contextDependencies , indexes , expression ) {
179
+ findDependencies ( contextDependencies : Map < string , string [ ] > , indexes : Map < string , string > , expression : Node ) {
155
180
if ( expression . _dependencies ) return expression . _dependencies ;
156
181
157
182
let scope = annotateWithScopes ( expression ) ;
158
- const dependencies = [ ] ;
183
+ const dependencies : string [ ] = [ ] ;
159
184
160
185
const generator = this ; // can't use arrow functions, because of this.skip()
161
186
162
187
walk ( expression , {
163
- enter ( node , parent ) {
188
+ enter ( node : Node , parent : Node ) {
164
189
if ( node . _scope ) {
165
190
scope = node . _scope ;
166
191
return ;
@@ -180,7 +205,7 @@ export default class Generator {
180
205
}
181
206
} ,
182
207
183
- leave ( node ) {
208
+ leave ( node : Node ) {
184
209
if ( node . _scope ) scope = scope . parent ;
185
210
}
186
211
} ) ;
@@ -196,22 +221,22 @@ export default class Generator {
196
221
197
222
generate ( result , options , { name, format } ) {
198
223
if ( this . imports . length ) {
199
- const statements = [ ] ;
224
+ const statements : string [ ] = [ ] ;
200
225
201
226
this . imports . forEach ( ( declaration , i ) => {
202
227
if ( format === 'es' ) {
203
228
statements . push ( this . source . slice ( declaration . start , declaration . end ) ) ;
204
229
return ;
205
230
}
206
231
207
- const defaultImport = declaration . specifiers . find ( x => x . type === 'ImportDefaultSpecifier' || x . type === 'ImportSpecifier' && x . imported . name === 'default' ) ;
208
- const namespaceImport = declaration . specifiers . find ( x => x . type === 'ImportNamespaceSpecifier' ) ;
209
- const namedImports = declaration . specifiers . filter ( x => x . type === 'ImportSpecifier' && x . imported . name !== 'default' ) ;
232
+ const defaultImport = declaration . specifiers . find ( ( x : Node ) => x . type === 'ImportDefaultSpecifier' || x . type === 'ImportSpecifier' && x . imported . name === 'default' ) ;
233
+ const namespaceImport = declaration . specifiers . find ( ( x : Node ) => x . type === 'ImportNamespaceSpecifier' ) ;
234
+ const namedImports = declaration . specifiers . filter ( ( x : Node ) => x . type === 'ImportSpecifier' && x . imported . name !== 'default' ) ;
210
235
211
236
const name = ( defaultImport || namespaceImport ) ? ( defaultImport || namespaceImport ) . local . name : `__import${ i } ` ;
212
237
declaration . name = name ; // hacky but makes life a bit easier later
213
238
214
- namedImports . forEach ( specifier => {
239
+ namedImports . forEach ( ( specifier : Node ) => {
215
240
statements . push ( `var ${ specifier . local . name } = ${ name } .${ specifier . imported . name } ` ) ;
216
241
} ) ;
217
242
@@ -230,7 +255,7 @@ export default class Generator {
230
255
231
256
const compiled = new Bundle ( { separator : '' } ) ;
232
257
233
- function addString ( str ) {
258
+ function addString ( str : string ) {
234
259
compiled . addSource ( {
235
260
content : new MagicString ( str )
236
261
} ) ;
@@ -250,7 +275,7 @@ export default class Generator {
250
275
} ) ;
251
276
}
252
277
253
- parts . forEach ( str => {
278
+ parts . forEach ( ( str : string ) => {
254
279
const chunk = str . replace ( pattern , '' ) ;
255
280
if ( chunk ) addString ( chunk ) ;
256
281
@@ -274,11 +299,11 @@ export default class Generator {
274
299
} ;
275
300
}
276
301
277
- getUniqueName ( name ) {
302
+ getUniqueName ( name : string ) {
278
303
if ( test ) name = `${ name } $` ;
279
304
let alias = name ;
280
- for ( let i = 1 ; reservedNames . has ( alias ) || this . importedNames . has ( alias ) || this . _usedNames . has ( alias ) ; alias = `${ name } _${ i ++ } ` ) ;
281
- this . _usedNames . add ( alias ) ;
305
+ for ( let i = 1 ; reservedNames . has ( alias ) || this . importedNames . has ( alias ) || this . usedNames . has ( alias ) ; alias = `${ name } _${ i ++ } ` ) ;
306
+ this . usedNames . add ( alias ) ;
282
307
return alias ;
283
308
}
284
309
@@ -287,13 +312,13 @@ export default class Generator {
287
312
return name => {
288
313
if ( test ) name = `${ name } $` ;
289
314
let alias = name ;
290
- for ( let i = 1 ; reservedNames . has ( alias ) || this . importedNames . has ( alias ) || this . _usedNames . has ( alias ) || localUsedNames . has ( alias ) ; alias = `${ name } _${ i ++ } ` ) ;
315
+ for ( let i = 1 ; reservedNames . has ( alias ) || this . importedNames . has ( alias ) || this . usedNames . has ( alias ) || localUsedNames . has ( alias ) ; alias = `${ name } _${ i ++ } ` ) ;
291
316
localUsedNames . add ( alias ) ;
292
317
return alias ;
293
318
} ;
294
319
}
295
320
296
- parseJs ( ssr ) {
321
+ parseJs ( ssr : boolean = false ) {
297
322
const { source } = this ;
298
323
const { js } = this . parsed ;
299
324
@@ -315,23 +340,23 @@ export default class Generator {
315
340
removeNode ( this . code , js . content , node ) ;
316
341
imports . push ( node ) ;
317
342
318
- node . specifiers . forEach ( specifier => {
343
+ node . specifiers . forEach ( ( specifier : Node ) => {
319
344
this . importedNames . add ( specifier . local . name ) ;
320
345
} ) ;
321
346
}
322
347
}
323
348
324
- const defaultExport = body . find ( node => node . type === 'ExportDefaultDeclaration' ) ;
349
+ const defaultExport = body . find ( ( node : Node ) => node . type === 'ExportDefaultDeclaration' ) ;
325
350
326
351
if ( defaultExport ) {
327
- defaultExport . declaration . properties . forEach ( prop => {
352
+ defaultExport . declaration . properties . forEach ( ( prop : Node ) => {
328
353
templateProperties [ prop . key . name ] = prop ;
329
354
} ) ;
330
355
}
331
356
332
357
[ 'helpers' , 'events' , 'components' , 'transitions' ] . forEach ( key => {
333
358
if ( templateProperties [ key ] ) {
334
- templateProperties [ key ] . value . properties . forEach ( prop => {
359
+ templateProperties [ key ] . value . properties . forEach ( ( prop : node ) => {
335
360
this [ key ] . add ( prop . key . name ) ;
336
361
} ) ;
337
362
}
@@ -340,11 +365,11 @@ export default class Generator {
340
365
if ( templateProperties . computed ) {
341
366
const dependencies = new Map ( ) ;
342
367
343
- templateProperties . computed . value . properties . forEach ( prop => {
368
+ templateProperties . computed . value . properties . forEach ( ( prop : Node ) => {
344
369
const key = prop . key . name ;
345
370
const value = prop . value ;
346
371
347
- const deps = value . params . map ( param => param . type === 'AssignmentPattern' ? param . left . name : param . name ) ;
372
+ const deps = value . params . map ( ( param : Node ) => param . type === 'AssignmentPattern' ? param . left . name : param . name ) ;
348
373
dependencies . set ( key , deps ) ;
349
374
} ) ;
350
375
@@ -362,7 +387,7 @@ export default class Generator {
362
387
computations . push ( { key, deps } ) ;
363
388
}
364
389
365
- templateProperties . computed . value . properties . forEach ( prop => visit ( prop . key . name ) ) ;
390
+ templateProperties . computed . value . properties . forEach ( ( prop : Node ) => visit ( prop . key . name ) ) ;
366
391
}
367
392
368
393
if ( templateProperties . namespace ) {
@@ -374,7 +399,7 @@ export default class Generator {
374
399
375
400
if ( templateProperties . components ) {
376
401
let hasNonImportedComponent = false ;
377
- templateProperties . components . value . properties . forEach ( property => {
402
+ templateProperties . components . value . properties . forEach ( ( property : Node ) => {
378
403
const key = property . key . name ;
379
404
const value = source . slice ( property . value . start , property . value . end ) ;
380
405
if ( this . importedNames . has ( value ) ) {
0 commit comments