Skip to content

Commit 910acb0

Browse files
committed
fix: Exclude imports with type-only bindings from list of dependencies to visit
1 parent e826628 commit 910acb0

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

.changeset/small-windows-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lit-analyzer-fork": patch
3+
---
4+
5+
Fix dependency visiting logic to exclude imports with type-only bindings

packages/lit-analyzer/src/lib/analyze/parse/parse-dependencies/visit-dependencies.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ function visitDirectImports(node: Node, context: IVisitDependenciesContext): voi
119119
return;
120120
}*/
121121

122+
const namedBindings = context.ts.isImportDeclaration(node) && node.importClause?.namedBindings;
123+
124+
// Exclude files that only have type imports like `import { type MyElement } from "./file1"`
125+
if (namedBindings && context.ts.isNamedImports(namedBindings) && namedBindings.elements && namedBindings.elements.every(e => e.isTypeOnly)) {
126+
return;
127+
}
128+
122129
emitDirectModuleImportWithName(node.moduleSpecifier.text, node, context);
123130
}
124131
}

packages/lit-analyzer/src/test/parser/dependencies/parse-dependencies.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,27 @@ tsTest("Ignores type-only imports in a file", t => {
301301
t.deepEqual(sortedFileNames, ["file2.ts"]);
302302
});
303303

304+
tsTest("Ignores imports with type-only bindings in a file", t => {
305+
const { sourceFile, context } = prepareAnalyzer([
306+
{ fileName: "file1.ts", text: `` },
307+
{
308+
fileName: "file2.ts",
309+
text: `
310+
import { type MyElement } from "./file1";
311+
`,
312+
entry: true
313+
}
314+
]);
315+
316+
const dependencies = parseAllIndirectImports(sourceFile, context);
317+
318+
const sortedFileNames = Array.from(dependencies)
319+
.map(file => file.fileName)
320+
.sort();
321+
322+
t.deepEqual(sortedFileNames, ["file2.ts"]);
323+
});
324+
304325
tsTest("Ignores type-only exports in a file", t => {
305326
const { sourceFile, context } = prepareAnalyzer([
306327
{ fileName: "file1.ts", text: `` },

0 commit comments

Comments
 (0)