@@ -77,6 +77,7 @@ const importTransformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
77
77
if ( node . importClause ?. isTypeOnly ) {
78
78
return ts . createEmptyStatement ( ) ;
79
79
}
80
+
80
81
return ts . createImportDeclaration (
81
82
node . decorators ,
82
83
node . modifiers ,
@@ -118,69 +119,86 @@ function isValidSvelteReactiveValueDiagnostic(
118
119
function createImportTransformerFromProgram ( program : ts . Program ) {
119
120
const checker = program . getTypeChecker ( ) ;
120
121
121
- const importedTypeRemoverTransformer : ts . TransformerFactory < ts . SourceFile > = context => {
122
- const visit : ts . Visitor = node => {
123
- if ( ts . isImportDeclaration ( node ) ) {
122
+ const importedTypeRemoverTransformer : ts . TransformerFactory < ts . SourceFile > = (
123
+ context ,
124
+ ) => {
125
+ const visit : ts . Visitor = ( node ) => {
126
+ if ( ! ts . isImportDeclaration ( node ) ) {
127
+ return ts . visitEachChild ( node , ( child ) => visit ( child ) , context ) ;
128
+ }
124
129
125
- let newImportClause : ts . ImportClause = node . importClause ;
130
+ let newImportClause : ts . ImportClause = node . importClause ;
126
131
127
- if ( node . importClause ) {
128
- // import type {...} from './blah'
129
- if ( node . importClause ?. isTypeOnly ) {
130
- return ts . createEmptyStatement ( ) ;
131
- }
132
-
133
- // import Blah, { blah } from './blah'
134
- newImportClause = ts . getMutableClone ( node . importClause ) ;
135
-
136
- // types can't be default exports, so we just worry about { blah } and { blah as name } exports
137
- if ( newImportClause . namedBindings && ts . isNamedImports ( newImportClause . namedBindings ) ) {
138
- const newBindings = ts . getMutableClone ( newImportClause . namedBindings ) ;
139
- const newElements = [ ] ;
140
-
141
- for ( const spec of newBindings . elements ) {
142
- const ident = spec . name ;
143
- const symbol = checker . getSymbolAtLocation ( ident ) ;
144
- const aliased = checker . getAliasedSymbol ( symbol ) ;
145
- if ( aliased ) {
146
- if ( ( aliased . flags & ( ts . SymbolFlags . TypeAlias | ts . SymbolFlags . Interface ) ) > 0 ) {
147
- continue ; //We found an imported type, don't add to our new import clause
148
- }
149
- }
150
- newElements . push ( spec )
151
- }
132
+ if ( node . importClause ) {
133
+ // import type {...} from './blah'
134
+ if ( node . importClause ?. isTypeOnly ) {
135
+ return ts . createEmptyStatement ( ) ;
136
+ }
152
137
153
- if ( newElements . length > 0 ) {
154
- newBindings . elements = ts . createNodeArray ( newElements , newBindings . elements . hasTrailingComma ) ;
155
- newImportClause . namedBindings = newBindings ;
156
- } else {
157
- newImportClause . namedBindings = undefined ;
138
+ // import Blah, { blah } from './blah'
139
+ newImportClause = ts . getMutableClone ( node . importClause ) ;
140
+
141
+ // types can't be default exports, so we just worry about { blah } and { blah as name } exports
142
+ if (
143
+ newImportClause . namedBindings &&
144
+ ts . isNamedImports ( newImportClause . namedBindings )
145
+ ) {
146
+ const newBindings = ts . getMutableClone ( newImportClause . namedBindings ) ;
147
+ const newElements = [ ] ;
148
+
149
+ newImportClause . namedBindings = undefined ;
150
+
151
+ for ( const spec of newBindings . elements ) {
152
+ const ident = spec . name ;
153
+
154
+ const symbol = checker . getSymbolAtLocation ( ident ) ;
155
+ const aliased = checker . getAliasedSymbol ( symbol ) ;
156
+
157
+ if ( aliased ) {
158
+ if (
159
+ ( aliased . flags &
160
+ ( ts . SymbolFlags . TypeAlias | ts . SymbolFlags . Interface ) ) >
161
+ 0
162
+ ) {
163
+ // We found an imported type, don't add to our new import clause
164
+ continue ;
165
+ }
158
166
}
167
+ newElements . push ( spec ) ;
159
168
}
160
169
161
- //we ended up removing all named bindings and we didn't have a name? nothing left to import.
162
- if ( ! newImportClause . namedBindings && ! newImportClause . name ) {
163
- return ts . createEmptyStatement ( ) ;
170
+ if ( newElements . length > 0 ) {
171
+ newBindings . elements = ts . createNodeArray (
172
+ newElements ,
173
+ newBindings . elements . hasTrailingComma ,
174
+ ) ;
175
+ newImportClause . namedBindings = newBindings ;
164
176
}
165
177
}
166
178
167
- return ts . createImportDeclaration (
168
- node . decorators ,
169
- node . modifiers ,
170
- newImportClause ,
171
- node . moduleSpecifier ,
172
- ) ;
179
+ // we ended up removing all named bindings and we didn't have a name? nothing left to import.
180
+ if (
181
+ newImportClause . namedBindings == null &&
182
+ newImportClause . name == null
183
+ ) {
184
+ return ts . createEmptyStatement ( ) ;
185
+ }
173
186
}
174
- return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
187
+
188
+ return ts . createImportDeclaration (
189
+ node . decorators ,
190
+ node . modifiers ,
191
+ newImportClause ,
192
+ node . moduleSpecifier ,
193
+ ) ;
175
194
} ;
176
195
177
- return node => ts . visitNode ( node , visit ) ;
196
+ return ( node ) => ts . visitNode ( node , visit ) ;
178
197
} ;
179
198
180
199
return importedTypeRemoverTransformer ;
181
200
}
182
201
183
-
184
202
function compileFileFromMemory (
185
203
compilerOptions : CompilerOptions ,
186
204
{ filename, content } : { filename : string ; content : string } ,
@@ -242,14 +260,16 @@ function compileFileFromMemory(
242
260
243
261
const program = ts . createProgram ( [ dummyFileName ] , compilerOptions , host ) ;
244
262
245
- const transformers = { before : [ createImportTransformerFromProgram ( program ) ] }
263
+ const transformers = {
264
+ before : [ createImportTransformerFromProgram ( program ) ] ,
265
+ } ;
246
266
247
267
const emitResult = program . emit (
248
268
program . getSourceFile ( dummyFileName ) ,
249
269
undefined ,
250
270
undefined ,
251
271
undefined ,
252
- transformers
272
+ transformers ,
253
273
) ;
254
274
255
275
// collect diagnostics without svelte import errors
0 commit comments