fix(node): handle empty 'main' entry in pkg json (#23155)

This commit is contained in:
David Sherret 2024-04-01 02:07:11 -04:00 committed by GitHub
parent b8af46e007
commit 8698492128
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 43 additions and 4 deletions

View File

@ -213,12 +213,13 @@ impl PackageJson {
Ok(package_json)
}
pub fn main(&self, referrer_kind: NodeModuleKind) -> Option<&String> {
if referrer_kind == NodeModuleKind::Esm && self.typ == "module" {
pub fn main(&self, referrer_kind: NodeModuleKind) -> Option<&str> {
let main = if referrer_kind == NodeModuleKind::Esm && self.typ == "module" {
self.module.as_ref().or(self.main.as_ref())
} else {
self.main.as_ref()
}
};
main.map(|m| m.trim()).filter(|m| !m.is_empty())
}
pub fn specifier(&self) -> ModuleSpecifier {

View File

@ -1232,7 +1232,7 @@ impl NodeResolver {
) -> Result<Option<ModuleSpecifier>, AnyError> {
let maybe_main = if mode.is_types() {
match package_json.types.as_ref() {
Some(types) => Some(types),
Some(types) => Some(types.as_str()),
None => {
// fallback to checking the main entrypoint for
// a corresponding declaration file

View File

@ -0,0 +1,10 @@
{
"steps": [{
"args": "check index.ts",
"output": "check.out",
"exitCode": 1
}, {
"args": "run index.ts",
"output": "run.out"
}]
}

View File

@ -0,0 +1,5 @@
Check file:///[WILDLINE]/index.ts
error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'.
const result: string = add(1, 2);
~~~~~~
at file:///[WILDLINE]/index.ts:4:7

View File

@ -0,0 +1,3 @@
{
"unstable": ["byonm"]
}

View File

@ -0,0 +1,5 @@
// this package has an empty "main" entry in its package.json for both the package and @types/package
import { add } from "package";
const result: string = add(1, 2);
console.log(result);

View File

@ -0,0 +1 @@
export function add(a: number, b: number): number;

View File

@ -0,0 +1,3 @@
{
"main": ""
}

View File

@ -0,0 +1 @@
module.exports.add = (a, b) => a + b;

View File

@ -0,0 +1,3 @@
{
"main": ""
}

View File

@ -0,0 +1,6 @@
{
"dependencies": {
"package": "*",
"@types/package": "*"
}
}

View File

@ -0,0 +1 @@
3