@@ -74,6 +74,9 @@ function isValidSvelteImportDiagnostic(filename: string, diagnostic: any) {
74
74
const importTransformer : ts . TransformerFactory < ts . SourceFile > = ( context ) => {
75
75
const visit : ts . Visitor = ( node ) => {
76
76
if ( ts . isImportDeclaration ( node ) ) {
77
+ if ( node . importClause ?. isTypeOnly ) {
78
+ return ts . createEmptyStatement ( ) ;
79
+ }
77
80
return ts . createImportDeclaration (
78
81
node . decorators ,
79
82
node . modifiers ,
@@ -112,6 +115,72 @@ function isValidSvelteReactiveValueDiagnostic(
112
115
return ! ( usedVar && proposedVar && usedVar === proposedVar ) ;
113
116
}
114
117
118
+ function createImportTransformerFromProgram ( program : ts . Program ) {
119
+ const checker = program . getTypeChecker ( ) ;
120
+
121
+ const importedTypeRemoverTransformer : ts . TransformerFactory < ts . SourceFile > = context => {
122
+ const visit : ts . Visitor = node => {
123
+ if ( ts . isImportDeclaration ( node ) ) {
124
+
125
+ let newImportClause : ts . ImportClause = node . importClause ;
126
+
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
+ let newBindings = ts . getMutableClone ( newImportClause . namedBindings ) ;
139
+ let newElements = [ ] ;
140
+
141
+ for ( let spec of newBindings . elements ) {
142
+ let ident = spec . name ;
143
+ let symbol = checker . getSymbolAtLocation ( ident ) ;
144
+ let 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
+ }
152
+
153
+ if ( newElements . length > 0 ) {
154
+ newBindings . elements = ts . createNodeArray ( newElements , newBindings . elements . hasTrailingComma ) ;
155
+ newImportClause . namedBindings = newBindings ;
156
+ } else {
157
+ newImportClause . namedBindings = undefined ;
158
+ }
159
+ }
160
+
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 ( ) ;
164
+ }
165
+ }
166
+
167
+ return ts . createImportDeclaration (
168
+ node . decorators ,
169
+ node . modifiers ,
170
+ newImportClause ,
171
+ node . moduleSpecifier ,
172
+ ) ;
173
+ }
174
+ return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
175
+ } ;
176
+
177
+ return node => ts . visitNode ( node , visit ) ;
178
+ } ;
179
+
180
+ return importedTypeRemoverTransformer ;
181
+ }
182
+
183
+
115
184
function compileFileFromMemory (
116
185
compilerOptions : CompilerOptions ,
117
186
{ filename, content } : { filename : string ; content : string } ,
@@ -172,12 +241,15 @@ function compileFileFromMemory(
172
241
} ;
173
242
174
243
const program = ts . createProgram ( [ dummyFileName ] , compilerOptions , host ) ;
244
+
245
+ let transformers = { before : [ createImportTransformerFromProgram ( program ) ] }
246
+
175
247
const emitResult = program . emit (
248
+ program . getSourceFile ( dummyFileName ) ,
176
249
undefined ,
177
250
undefined ,
178
251
undefined ,
179
- undefined ,
180
- TS_TRANSFORMERS ,
252
+ transformers
181
253
) ;
182
254
183
255
// collect diagnostics without svelte import errors
0 commit comments