feat(linter): handle built-in modules in import/no_unresolved (#2479)

This commit is contained in:
Boshen 2024-02-23 22:10:27 +08:00 committed by GitHub
parent d741d72e17
commit 7f867221ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 9 deletions

4
Cargo.lock generated
View file

@ -1601,9 +1601,9 @@ dependencies = [
[[package]]
name = "oxc_resolver"
version = "1.5.1"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47777510a49fc554e7fb33101b67b6dc0bca28ea6d6fa852c113241e433a9e89"
checksum = "2033cc3b0e72446d3321866db0954804b9ca559ad692480205053f6aea4bfc15"
dependencies = [
"dashmap",
"dunce",

View file

@ -29,7 +29,7 @@ oxc_semantic = { workspace = true }
oxc_syntax = { workspace = true }
oxc_codegen = { workspace = true }
oxc_index = { workspace = true }
oxc_resolver = { version = "1.5.1" }
oxc_resolver = { version = "1.5.2" }
rayon = { workspace = true }
lazy_static = { workspace = true } # used in oxc_macros

View file

@ -1,8 +1,11 @@
use std::path::{Component, Path};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_resolver::NODEJS_BUILTINS;
use oxc_span::Span;
use crate::{context::LintContext, rule::Rule};
@ -29,10 +32,21 @@ impl Rule for NoUnresolved {
let module_record = ctx.semantic().module_record();
for (specifier, spans) in &module_record.requested_modules {
if !module_record.loaded_modules.contains_key(specifier) {
for span in spans {
ctx.diagnostic(NoUnresolvedDiagnostic(*span));
}
if module_record.loaded_modules.contains_key(specifier) {
continue;
}
// skip node.js builtin modules
if specifier.starts_with("node:")
|| (Path::new(specifier.as_str())
.components()
.next()
.is_some_and(|c| matches!(c, Component::Normal(_)))
&& NODEJS_BUILTINS.binary_search(&specifier.as_str()).is_ok())
{
continue;
}
for span in spans {
ctx.diagnostic(NoUnresolvedDiagnostic(*span));
}
}
}
@ -48,8 +62,8 @@ fn test() {
r#"import foo from "./bar";"#,
r"import bar from './bar.js';",
r"import {someThing} from './test-module';",
// TODO: exclude nodejs builtin modules
// r#"import fs from 'fs';"#,
r"import fs from 'fs';",
r"import fs from 'node:fs';",
r"import('fs');",
r"import('fs');",
r#"import * as foo from "a""#,