@@ -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,87 @@ 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
+ console . log ( { ident } ) ;
155
+ const symbol = checker . getSymbolAtLocation ( ident ) ;
156
+ const aliased = checker . getAliasedSymbol ( symbol ) ;
157
+
158
+ if ( aliased ) {
159
+ if (
160
+ ( aliased . flags &
161
+ ( ts . SymbolFlags . TypeAlias | ts . SymbolFlags . Interface ) ) >
162
+ 0
163
+ ) {
164
+ // We found an imported type, don't add to our new import clause
165
+ continue ;
166
+ }
158
167
}
168
+ newElements . push ( spec ) ;
159
169
}
160
170
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 ( ) ;
171
+ if ( newElements . length > 0 ) {
172
+ newBindings . elements = ts . createNodeArray (
173
+ newElements ,
174
+ newBindings . elements . hasTrailingComma ,
175
+ ) ;
176
+ newImportClause . namedBindings = newBindings ;
164
177
}
165
178
}
166
179
167
- return ts . createImportDeclaration (
168
- node . decorators ,
169
- node . modifiers ,
170
- newImportClause ,
171
- node . moduleSpecifier ,
172
- ) ;
180
+ // we ended up removing all named bindings and we didn't have a name? nothing left to import.
181
+ if (
182
+ newImportClause . namedBindings == null &&
183
+ newImportClause . name == null
184
+ ) {
185
+ return ts . createEmptyStatement ( ) ;
186
+ }
173
187
}
174
- return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
188
+
189
+ return ts . createImportDeclaration (
190
+ node . decorators ,
191
+ node . modifiers ,
192
+ newImportClause ,
193
+ node . moduleSpecifier ,
194
+ ) ;
175
195
} ;
176
196
177
- return node => ts . visitNode ( node , visit ) ;
197
+ return ( node ) => ts . visitNode ( node , visit ) ;
178
198
} ;
179
199
180
200
return importedTypeRemoverTransformer ;
181
201
}
182
202
183
-
184
203
function compileFileFromMemory (
185
204
compilerOptions : CompilerOptions ,
186
205
{ filename, content } : { filename : string ; content : string } ,
@@ -242,14 +261,16 @@ function compileFileFromMemory(
242
261
243
262
const program = ts . createProgram ( [ dummyFileName ] , compilerOptions , host ) ;
244
263
245
- const transformers = { before : [ createImportTransformerFromProgram ( program ) ] }
264
+ const transformers = {
265
+ before : [ createImportTransformerFromProgram ( program ) ] ,
266
+ } ;
246
267
247
268
const emitResult = program . emit (
248
269
program . getSourceFile ( dummyFileName ) ,
249
270
undefined ,
250
271
undefined ,
251
272
undefined ,
252
- transformers
273
+ transformers ,
253
274
) ;
254
275
255
276
// collect diagnostics without svelte import errors
0 commit comments