runem / lit-analyzer

Monorepository for tools that analyze lit-html templates
MIT License
317 stars 36 forks source link

Using Typescript's `compilerOptions.paths` causes false-positives from no-missing-import #293

Open jrencz opened 1 year ago

jrencz commented 1 year ago

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`](https://github.com/runem/lit-analyzer/blob/56489d04242b35d692b1db4d0f776f9ca0da19b9/packages/lit-analyzer/src/lib/analyze/default-lit-analyzer-context.ts#L283-L289) helper [`parseDependencies`](https://github.com/runem/lit-analyzer/blob/92c084da4d33d6fbdd8b29603d2003387e65dbb3/packages/lit-analyzer/src/lib/analyze/parse/parse-dependencies/parse-dependencies.ts#L13-L56) 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](https://github.com/runem/lit-analyzer/blob/56489d04242b35d692b1db4d0f776f9ca0da19b9/packages/lit-analyzer/src/lib/analyze/parse/parse-dependencies/parse-dependencies.ts#L39-L41) I get 163 and 53 elements respectively. Next, down to [`visitDirectImports` call in `visitIndirectImportsFromSourceFile` helper](https://github.com/runem/lit-analyzer/blob/56489d04242b35d692b1db4d0f776f9ca0da19b9/packages/lit-analyzer/src/lib/analyze/parse/parse-dependencies/visit-dependencies.ts#L53-L54): - 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`](https://github.com/microsoft/TypeScript/blob/3716ffe748fe799b1c69090056397bda814c0d49/src/services/types.ts#L362) 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

jrencz commented 1 year ago

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

h4de5 commented 7 months ago

got the same issue with imports using tsconfig paths.