@@ -67,6 +67,9 @@ function isValidSvelteImportDiagnostic(filename: string, diagnostic: any) {
67
67
const importTransformer : ts . TransformerFactory < ts . SourceFile > = context => {
68
68
const visit : ts . Visitor = node => {
69
69
if ( ts . isImportDeclaration ( node ) ) {
70
+ if ( node . importClause ?. isTypeOnly ) {
71
+ return ts . createEmptyStatement ( ) ;
72
+ }
70
73
return ts . createImportDeclaration (
71
74
node . decorators ,
72
75
node . modifiers ,
@@ -103,6 +106,72 @@ function isValidSvelteReactiveValueDiagnostic(
103
106
return ! ( usedVar && proposedVar && usedVar === proposedVar ) ;
104
107
}
105
108
109
+ function createImportTransformerFromProgram ( program : ts . Program ) {
110
+ const checker = program . getTypeChecker ( ) ;
111
+
112
+ const importedTypeRemoverTransformer : ts . TransformerFactory < ts . SourceFile > = context => {
113
+ const visit : ts . Visitor = node => {
114
+ if ( ts . isImportDeclaration ( node ) ) {
115
+
116
+ let newImportClause : ts . ImportClause = node . importClause ;
117
+
118
+ if ( node . importClause ) {
119
+ // import type {...} from './blah'
120
+ if ( node . importClause ?. isTypeOnly ) {
121
+ return ts . createEmptyStatement ( ) ;
122
+ }
123
+
124
+ // import Blah, { blah } from './blah'
125
+ newImportClause = ts . getMutableClone ( node . importClause ) ;
126
+
127
+ // types can't be default exports, so we just worry about { blah } and { blah as name } exports
128
+ if ( newImportClause . namedBindings && ts . isNamedImports ( newImportClause . namedBindings ) ) {
129
+ let newBindings = ts . getMutableClone ( newImportClause . namedBindings ) ;
130
+ let newElements = [ ] ;
131
+
132
+ for ( let spec of newBindings . elements ) {
133
+ let ident = spec . name ;
134
+ let symbol = checker . getSymbolAtLocation ( ident ) ;
135
+ let aliased = checker . getAliasedSymbol ( symbol ) ;
136
+ if ( aliased ) {
137
+ if ( ( aliased . flags & ( ts . SymbolFlags . TypeAlias | ts . SymbolFlags . Interface ) ) > 0 ) {
138
+ continue ; //We found an imported type, don't add to our new import clause
139
+ }
140
+ }
141
+ newElements . push ( spec )
142
+ }
143
+
144
+ if ( newElements . length > 0 ) {
145
+ newBindings . elements = ts . createNodeArray ( newElements , newBindings . elements . hasTrailingComma ) ;
146
+ newImportClause . namedBindings = newBindings ;
147
+ } else {
148
+ newImportClause . namedBindings = undefined ;
149
+ }
150
+ }
151
+
152
+ //we ended up removing all named bindings and we didn't have a name? nothing left to import.
153
+ if ( ! newImportClause . namedBindings && ! newImportClause . name ) {
154
+ return ts . createEmptyStatement ( ) ;
155
+ }
156
+ }
157
+
158
+ return ts . createImportDeclaration (
159
+ node . decorators ,
160
+ node . modifiers ,
161
+ newImportClause ,
162
+ node . moduleSpecifier ,
163
+ ) ;
164
+ }
165
+ return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
166
+ } ;
167
+
168
+ return node => ts . visitNode ( node , visit ) ;
169
+ } ;
170
+
171
+ return importedTypeRemoverTransformer ;
172
+ }
173
+
174
+
106
175
function compileFileFromMemory (
107
176
compilerOptions : CompilerOptions ,
108
177
{ filename, content } : { filename : string ; content : string } ,
@@ -162,12 +231,15 @@ function compileFileFromMemory(
162
231
} ;
163
232
164
233
const program = ts . createProgram ( [ dummyFileName ] , compilerOptions , host ) ;
234
+
235
+ let transformers = { before : [ createImportTransformerFromProgram ( program ) ] }
236
+
165
237
const emitResult = program . emit (
238
+ program . getSourceFile ( dummyFileName ) ,
166
239
undefined ,
167
240
undefined ,
168
241
undefined ,
169
- undefined ,
170
- TS_TRANSFORMERS ,
242
+ transformers
171
243
) ;
172
244
173
245
// collect diagnostics without svelte import errors
0 commit comments