lit-analyzer
lit-analyzer copied to clipboard
Using Typescript's `compilerOptions.paths` causes false-positives from no-missing-import
Initially I didn't use compilerOptions.paths, but I'm now adding that to the project.
Presence of that setting in tsconfig.json does not affect lit-analyser, but as I started gradually replacing imports in component modules I got some false positives from no-missing-import rule
I narrowed down the problem down a little bit, here's my investigation (collapsed)
(I'm fairly new to typescript as a user and I'm definitely not experienced in how it works, so the reasoning may have mistakes)
In a file I tested on I have 9 import statements:
- 3 are from node_modules
- one is styles import
- 2 are legitimate relative imports I want to preserve
- 3 are currently relative imports, but I'd like to make them aliased as those are imports of other components into my component
In DefaultLitAnalyzerContext#findDependenciesInFile helper parseDependencies shows 20 dependencies if I use relative imports in 3 relatively-imported deps which I mentioned, but only 2 if I switch to aliases. So - imports with aliased path is clearly omitted.
I wend into parseDependencies and inside, in parseAllIndirectImports call I get 163 and 53 elements respectively.
Next, down to visitDirectImports call in visitIndirectImportsFromSourceFile helper:
- without aliases: 8
- with aliases: 5
I also confirmed that all 9 import declarations in question regardless of whether they are aliased or not go through this line:
https://github.com/runem/lit-analyzer/blob/56489d04242b35d692b1db4d0f776f9ca0da19b9/packages/lit-analyzer/src/lib/analyze/parse/parse-dependencies/visit-dependencies.ts#L121
Import declarations with aliased module specifier make difference in those lines:
https://github.com/runem/lit-analyzer/blob/56489d04242b35d692b1db4d0f776f9ca0da19b9/packages/lit-analyzer/src/lib/analyze/parse/parse-dependencies/visit-dependencies.ts#L145-L153
For all 9 there's result but aliased 3 don't have resolvedModule.
All 9 have no context.project LanguageServiceHost and for all 9 result comes from context.program.
If I'm not mistaken the problem is that analyzeCommand creates context with access to program, but not project (I assume that it's "project" level what knows about paths):
https://github.com/runem/lit-analyzer/blob/56489d04242b35d692b1db4d0f776f9ca0da19b9/packages/lit-analyzer/src/lib/cli/analyze-command.ts#L24-L35
The only context that has access to that is this one:
https://github.com/runem/lit-analyzer/blob/8360bef72c4f9dd8170fc6be4f553c7c17958011/packages/ts-lit-plugin/src/index.ts#L52-L60
Additionally when I run lit-analyzer with just one file, instead of errors from no-missing-import I get errors from no-unknown-tag-name. Reason will be the same, but if analyser has access to other files it does know the tag name, but complains about it not being properly imported
got the same issue with imports using tsconfig paths.