feat(unstable): repurpose --unstable-detect-cjs to attempt loading more modules as cjs (#27094)

This resurrects the `--unstable-detect-cjs` flag (which became stable),
and repurposes it to attempt loading .js/.jsx/.ts/.tsx files as CJS in
the following additional scenarios:

1. There is no package.json
1. There is a package.json without a "type" field

Also cleans up the implementation of this in the LSP a lot by hanging
`resolution_mode()` off `Document` (didn't think about doing that until
now).
This commit is contained in:
David Sherret 2024-11-27 09:50:38 -05:00 committed by Bartek Iwańczuk
parent 8171b761c2
commit bb3e918f4a
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
37 changed files with 217 additions and 226 deletions

View file

@ -1281,9 +1281,7 @@ impl CodeActionCollection {
import_start_from_specifier(document, i)
})?;
let referrer = document.specifier();
let referrer_kind = language_server
.is_cjs_resolver
.get_doc_resolution_mode(document);
let resolution_mode = document.resolution_mode();
let file_referrer = document.file_referrer();
let config_data = language_server
.config
@ -1309,7 +1307,7 @@ impl CodeActionCollection {
if !language_server.resolver.is_bare_package_json_dep(
&dep_key,
referrer,
referrer_kind,
resolution_mode,
) {
return None;
}
@ -1329,7 +1327,7 @@ impl CodeActionCollection {
}
if language_server
.resolver
.npm_to_file_url(&npm_ref, referrer, referrer_kind, file_referrer)
.npm_to_file_url(&npm_ref, referrer, resolution_mode, file_referrer)
.is_some()
{
// The package import has types.