Skip to content

Commit 4322ba8

Browse files
committed
fix: Exclude imports with type-only bindings from list of dependencies to visit
1 parent 58ca1a2 commit 4322ba8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ 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 (
126+
namedBindings &&
127+
context.ts.isNamedImports(namedBindings) &&
128+
namedBindings.elements.length > 0 &&
129+
namedBindings.elements.every(e => e.isTypeOnly)
130+
) {
131+
return;
132+
}
133+
122134
emitDirectModuleImportWithName(node.moduleSpecifier.text, node, context);
123135
}
124136
}

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)