Skip to content

Commit 142f3bb

Browse files
committed
refactor: 💡 tiny refactor to remote some identation
1 parent 3a15831 commit 142f3bb

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

‎src/transformers/typescript.ts

Lines changed: 68 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,86 @@ 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+
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+
}
158166
}
167+
newElements.push(spec);
159168
}
160169

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;
164176
}
165177
}
166178

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+
}
173186
}
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+
);
175194
};
176195

177-
return node => ts.visitNode(node, visit);
196+
return (node) => ts.visitNode(node, visit);
178197
};
179198

180199
return importedTypeRemoverTransformer;
181200
}
182201

183-
184202
function compileFileFromMemory(
185203
compilerOptions: CompilerOptions,
186204
{ filename, content }: { filename: string; content: string },
@@ -242,14 +260,16 @@ function compileFileFromMemory(
242260

243261
const program = ts.createProgram([dummyFileName], compilerOptions, host);
244262

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

247267
const emitResult = program.emit(
248268
program.getSourceFile(dummyFileName),
249269
undefined,
250270
undefined,
251271
undefined,
252-
transformers
272+
transformers,
253273
);
254274

255275
// collect diagnostics without svelte import errors

0 commit comments

Comments
 (0)