feat(linter): ignore unsupported extensions in import/no_unresolved (#2481)

This commit is contained in:
Boshen 2024-02-23 23:11:43 +08:00 committed by GitHub
parent 3d008abacb
commit 135e56a401
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View file

@ -6,6 +6,7 @@ use oxc_diagnostics::{
}; };
use oxc_macros::declare_oxc_lint; use oxc_macros::declare_oxc_lint;
use oxc_resolver::NODEJS_BUILTINS; use oxc_resolver::NODEJS_BUILTINS;
use oxc_span::SourceType;
use oxc_span::Span; use oxc_span::Span;
use crate::{context::LintContext, rule::Rule}; use crate::{context::LintContext, rule::Rule};
@ -35,9 +36,16 @@ impl Rule for NoUnresolved {
if module_record.loaded_modules.contains_key(specifier) { if module_record.loaded_modules.contains_key(specifier) {
continue; continue;
} }
let specifier_path = Path::new(specifier.as_str());
// skip if the extension is not supported
if specifier_path.extension().is_some()
&& SourceType::from_path(specifier_path).is_err()
{
continue;
}
// skip node.js builtin modules // skip node.js builtin modules
if specifier.starts_with("node:") if specifier.starts_with("node:")
|| (Path::new(specifier.as_str()) || (specifier_path
.components() .components()
.next() .next()
.is_some_and(|c| matches!(c, Component::Normal(_))) .is_some_and(|c| matches!(c, Component::Normal(_)))
@ -90,6 +98,8 @@ fn test() {
r"define([0, foo], function (bar) {})", r"define([0, foo], function (bar) {})",
r"require(0)", r"require(0)",
r"require(foo)", r"require(foo)",
// Unsupported extensions
r#"import "./test.png""#,
]; ];
let fail = vec![ let fail = vec![

View file

@ -137,6 +137,7 @@ impl Runtime {
fn resolver() -> Resolver { fn resolver() -> Resolver {
Resolver::new(ResolveOptions { Resolver::new(ResolveOptions {
extensions: VALID_EXTENSIONS.iter().map(|ext| format!(".{ext}")).collect(), extensions: VALID_EXTENSIONS.iter().map(|ext| format!(".{ext}")).collect(),
condition_names: vec!["require".into(), "module".into()],
..ResolveOptions::default() ..ResolveOptions::default()
}) })
} }