Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #313 by correctly using projects moduleResolution strategy if configured #333

Merged

Conversation

benjamind
Copy link
Contributor

  • Updates the repo to typescript 4.8.4 - this is the minimum tested TS version anyway, and supports the correct use of getNodeForUsageLocation to properly determine ModuleKind for resolving modules.
  • Updates use of resolveModuleNameFromCache to properly pass the right mode argument to resolve in more situations correctly.
  • Updates required and default compilerOptions to not overwrite the project under tests tsconfig options if provided. Will still default to the original behavior, but will now obey whats in the tsconfig.json if its specified.
  • Updates lots of package-locks which didn't seem to be up to date in main and were updated automatically.

@@ -158,8 +158,15 @@ function emitDirectModuleImportWithName(moduleSpecifier: string, node: Node, con
result = (context.program as any)["getResolvedModuleWithFailedLookupLocationsFromCache"](moduleSpecifier, fromSourceFile.fileName);
} else {
const cache = (context.program as MaybeModernProgram).getModuleResolutionCache?.();
let mode: tsModule.ModuleKind.CommonJS | tsModule.ModuleKind.ESNext | undefined = undefined; // tsModule.ModuleKind.ESNext;
if ((context.ts.isImportDeclaration(node) && !node.importClause?.isTypeOnly) || (context.ts.isExportDeclaration(node) && !node.isTypeOnly)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!node.importClause?.isTypeOnly

Should this instead be:

(node.importClause && !node.importClause.isTypeOnly)

Because this import doesn't have an import clause, but it shouldn't be treated like a type only import:

import 'foo';

Hm, actually, looking at the docs for getModeForUsageLocation, I think we should just pass the node itself and not have any of these checks, including the typeOnly check:

/**
 * Calculates the final resolution mode for a given module reference node. This is generally the explicitly provided resolution mode, if
 * one exists, or the mode of the containing source file. (Excepting import=require, which is always commonjs, and dynamic import, which is always esm).
 * Notably, this function always returns `undefined` if the containing file has an `undefined` `impliedNodeFormat` - this field is only set when
 * `moduleResolution` is `node16`+.
 * @param file The file the import or import-like reference is contained within
 * @param usage The module reference string
 * @returns The final resolution mode of the import
 */

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I tried to do that without the checks but it broke in TS 4.8.3 while passed in later TS versions. Not sure why exactly.

But yeah this conditional I just copied from elsewhere in this file so I'm not sure its the right one exactly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm thinking on it more, this conditional issue you raised might explain why i was still seeing issues in my larger repository.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it break in 4.8.3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm testing it now its not reproducing so maybe a red herring. I have simplified the conditional as you suggested above and it seems to be behaving as expected.

@rictic rictic merged commit 0b8c8ba into runem:master Dec 8, 2023
5 of 7 checks passed
@rictic
Copy link
Collaborator

rictic commented Dec 8, 2023

Thanks for this great change! I'll stamp a release for this on Monday

@benjamind
Copy link
Contributor Author

Nice! Thanks @rictic hopefully this solves a bunch of problems for folks.

@rictic
Copy link
Collaborator

rictic commented Dec 11, 2023

Published as v2.0.2 of lit-analyzer on npm and v1.4.1 of runem/lit-plugin in the vscode extension marketplace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants