Skip to content

Commit cd2abe7

Browse files
committed
refactor: 💡 tiny refactor to remote some identation
1 parent b5eb427 commit cd2abe7

File tree

1 file changed

+69
-48
lines changed

1 file changed

+69
-48
lines changed

‎src/transformers/typescript.ts

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const importTransformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
7777
if (node.importClause?.isTypeOnly) {
7878
return ts.createEmptyStatement();
7979
}
80+
8081
return ts.createImportDeclaration(
8182
node.decorators,
8283
node.modifiers,
@@ -118,69 +119,87 @@ function isValidSvelteReactiveValueDiagnostic(
118119
function createImportTransformerFromProgram(program: ts.Program) {
119120
const checker = program.getTypeChecker();
120121

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+
}
124129

125-
let newImportClause: ts.ImportClause = node.importClause;
130+
let newImportClause: ts.ImportClause = node.importClause;
126131

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+
}
152137

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+
}
158167
}
168+
newElements.push(spec);
159169
}
160170

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;
164177
}
165178
}
166179

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+
}
173187
}
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+
);
175195
};
176196

177-
return node => ts.visitNode(node, visit);
197+
return (node) => ts.visitNode(node, visit);
178198
};
179199

180200
return importedTypeRemoverTransformer;
181201
}
182202

183-
184203
function compileFileFromMemory(
185204
compilerOptions: CompilerOptions,
186205
{ filename, content }: { filename: string; content: string },
@@ -242,14 +261,16 @@ function compileFileFromMemory(
242261

243262
const program = ts.createProgram([dummyFileName], compilerOptions, host);
244263

245-
const transformers = { before: [createImportTransformerFromProgram(program)] }
264+
const transformers = {
265+
before: [createImportTransformerFromProgram(program)],
266+
};
246267

247268
const emitResult = program.emit(
248269
program.getSourceFile(dummyFileName),
249270
undefined,
250271
undefined,
251272
undefined,
252-
transformers
273+
transformers,
253274
);
254275

255276
// collect diagnostics without svelte import errors

0 commit comments

Comments
 (0)