feat(resolver): add resolver test fixtures (#542)

This commit is contained in:
Boshen 2023-07-13 13:37:29 +08:00 committed by GitHub
parent db3ef449fc
commit a9debc9fdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
88 changed files with 234 additions and 224 deletions

166
Cargo.lock generated
View file

@ -433,12 +433,6 @@ dependencies = [
"syn 2.0.18",
]
[[package]]
name = "daachorse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b7ef7a4be509357f4804d0a22e830daddb48f19fd604e4ad32ddce04a94c36"
[[package]]
name = "dashmap"
version = "5.4.0"
@ -482,12 +476,6 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "dunce"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b"
[[package]]
name = "either"
version = "1.8.1"
@ -850,15 +838,6 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "jsonc-parser"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b56a20e76235284255a09fcd1f45cf55d3c524ea657ebd3854735925c57743d"
dependencies = [
"serde_json",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -940,15 +919,6 @@ dependencies = [
"serde",
]
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata",
]
[[package]]
name = "memchr"
version = "2.5.0"
@ -1072,36 +1042,6 @@ dependencies = [
"libloading",
]
[[package]]
name = "nodejs-resolver"
version = "0.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7259edee7a18be2bdc9802f3357044a964d6e0624030201849ed734b8901a23b"
dependencies = [
"daachorse",
"dashmap",
"dunce",
"indexmap",
"jsonc-parser",
"once_cell",
"path-absolutize",
"rustc-hash",
"serde",
"serde_json",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]]
name = "num-bigint"
version = "0.4.3"
@ -1163,12 +1103,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "owo-colors"
version = "3.5.0"
@ -1265,7 +1199,6 @@ dependencies = [
"oxc_diagnostics",
"oxc_linter",
"oxc_parser",
"oxc_resolver",
"oxc_semantic",
"oxc_span",
"oxc_type_synthesis",
@ -1457,10 +1390,6 @@ dependencies = [
[[package]]
name = "oxc_resolver"
version = "0.0.0"
dependencies = [
"nodejs-resolver",
"oxc_span",
]
[[package]]
name = "oxc_semantic"
@ -1769,24 +1698,9 @@ checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f"
dependencies = [
"aho-corasick 1.0.2",
"memchr",
"regex-syntax 0.7.2",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax 0.6.29",
]
[[package]]
name = "regex-syntax"
version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "regex-syntax"
version = "0.7.2"
@ -1957,7 +1871,6 @@ version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
@ -1987,15 +1900,6 @@ dependencies = [
"digest",
]
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]]
name = "similar"
version = "2.2.1"
@ -2242,68 +2146,6 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "tracing"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8803eee176538f94ae9a14b55b2804eb7e1441f8210b1c31290b3bccdccff73b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.18",
]
[[package]]
name = "tracing-core"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922"
dependencies = [
"lazy_static",
"log",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77"
dependencies = [
"matchers",
"nu-ansi-term",
"once_cell",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]
[[package]]
name = "typenum"
version = "1.16.0"
@ -2409,12 +2251,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -28,7 +28,6 @@ oxc_semantic = { workspace = true }
oxc_linter = { workspace = true }
oxc_type_synthesis = { workspace = true }
oxc_span = { workspace = true }
oxc_resolver = { workspace = true }
# TODO temp, for type check output, replace with Miette
codespan-reporting = "0.11.1"

View file

@ -1,10 +1,9 @@
use std::{
ffi::OsStr,
fs,
io::{self, BufWriter, Write},
path::{Path, PathBuf},
rc::Rc,
sync::{Arc, OnceLock},
sync::Arc,
};
use crossbeam_channel::{unbounded, Receiver, Sender};
@ -17,9 +16,8 @@ use oxc_diagnostics::{
};
use oxc_linter::{FixResult, Fixer, LintContext, Linter, RuleCategory, RuleEnum, RULES};
use oxc_parser::{Parser, ParserReturn};
use oxc_resolver::{ResolveResult, Resolver};
use oxc_semantic::{SemanticBuilder, SemanticBuilderReturn};
use oxc_span::{SourceType, VALID_EXTENSIONS};
use oxc_span::SourceType;
use rayon::prelude::*;
use rustc_hash::FxHashSet;
@ -116,7 +114,7 @@ impl LintRunnerWithModuleTree {
// we can ignore the result because nothing bad happens if the resolver is already set
// TODO: make sure this is still true once we allow options to be set
// during runtime (config file, args, etc.)
let _ = RESOLVER.set(Resolver::default());
// let _ = RESOLVER.set(Resolver::default());
let visited = Arc::new(DashSet::new());
@ -247,7 +245,7 @@ fn run_for_dir(path: &Path, runtime_data: &LinterRuntimeData) -> Result<()> {
})
}
static RESOLVER: OnceLock<Resolver> = OnceLock::new();
// static RESOLVER: OnceLock<Resolver> = OnceLock::new();
fn run_for_file(path: &Path, runtime_data: &LinterRuntimeData) -> Result<()> {
let LinterRuntimeData { linter, visited, tx_error } = &runtime_data;
@ -295,35 +293,35 @@ fn run_for_file(path: &Path, runtime_data: &LinterRuntimeData) -> Result<()> {
};
// this is ok to unwrap because we know that the resolver is initialized, otherwise this function wouldn't be called
let resolver = RESOLVER.get().unwrap();
// let resolver = RESOLVER.get().unwrap();
let resolve_path = path.parent().expect("Absolute file path always has a parent");
// let resolve_path = path.parent().expect("Absolute file path always has a parent");
let imported_modules = semantic.module_record().module_requests.keys();
// let imported_modules = semantic.module_record().module_requests.keys();
imported_modules
.par_bridge()
.filter(|name| name.starts_with('.'))
.filter_map(|name| {
resolver.resolve(resolve_path, name).map_or_else(
|_| {
eprintln!("Couldn't resolve '{name}' in '{}'.", resolve_path.display());
None
},
Some,
)
})
.filter_map(|resolved| match resolved {
ResolveResult::Resource(r) => Some(r.path),
ResolveResult::Ignored => None,
})
.filter(|path| {
path.extension()
.and_then(OsStr::to_str)
.is_some_and(|ext| VALID_EXTENSIONS.contains(&ext))
})
.filter(|path| !visited.contains(path))
.try_for_each(|path| run_for_file(&path, runtime_data))?;
// imported_modules
// .par_bridge()
// .filter(|name| name.starts_with('.'))
// .filter_map(|name| {
// resolver.resolve(resolve_path, name).map_or_else(
// |_| {
// eprintln!("Couldn't resolve '{name}' in '{}'.", resolve_path.display());
// None
// },
// Some,
// )
// })
// .filter_map(|resolved| match resolved {
// ResolveResult::Resource(r) => Some(r.path),
// ResolveResult::Ignored => None,
// })
// .filter(|path| {
// path.extension()
// .and_then(OsStr::to_str)
// .is_some_and(|ext| VALID_EXTENSIONS.contains(&ext))
// })
// .filter(|path| !visited.contains(path))
// .try_for_each(|path| run_for_file(&path, runtime_data))?;
let lint_ctx = LintContext::new(&Rc::new(semantic));
let result = linter.run(lint_ctx);

View file

@ -9,8 +9,3 @@ homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
[dependencies]
oxc_span = { workspace = true }
nodejs-resolver = { workspace = true }

View file

@ -0,0 +1,3 @@
# Oxc Resolver
Tests ported from [enhanced-resolve](https://github.com/webpack/enhanced-resolve).

View file

@ -1,26 +1,29 @@
use std::ops::Deref;
//! # Oxc Resolver
//!
//! Tests ported from [enhanced-resolve](https://github.com/webpack/enhanced-resolve).
//
use std::path::{Path, PathBuf};
#[derive(Debug, Eq, PartialEq)]
pub struct ResolveError;
use nodejs_resolver::{EnforceExtension, Options, Resolver as NodeJSResolver};
pub use nodejs_resolver::{ResolveResult, Resource};
use oxc_span::VALID_EXTENSIONS;
pub type ResolveResult = Result<PathBuf, ResolveError>;
#[derive(Debug)]
pub struct Resolver(NodeJSResolver);
pub struct Resolver;
impl Default for Resolver {
fn default() -> Self {
Self(NodeJSResolver::new(Options {
enforce_extension: EnforceExtension::Enabled,
extensions: VALID_EXTENSIONS.into_iter().map(|ext| String::from(".") + ext).collect(),
..Default::default()
}))
}
}
impl Deref for Resolver {
type Target = NodeJSResolver;
fn deref(&self) -> &Self::Target {
&self.0
impl Resolver {
pub fn new() -> Self {
Self
}
/// # Errors
pub fn resolve<P: AsRef<Path>>(&self, path: P, request: &str) -> ResolveResult {
self.resolve_impl(path.as_ref(), request)
}
#[allow(clippy::unused_self)]
fn resolve_impl(&self, _path: &Path, _request: &str) -> ResolveResult {
unreachable!()
// let path = path.join(request).canonicalize().unwrap();
// Ok(path)
}
}

View file

@ -0,0 +1 @@
// mimic enhanced-resolved root directory structure

View file

@ -0,0 +1 @@
mod test;

View file

@ -0,0 +1,3 @@
module.exports = function a() {
return "This is a";
};

View file

@ -0,0 +1 @@
abc

View file

@ -0,0 +1,3 @@
module.exports = function b() {
return "This is b";
};

View file

@ -0,0 +1,22 @@
{
"browser": {
"./lib/ignore.js": false,
"./lib/replaced.js": "./lib/browser",
"module-a": "./browser/module-a.js",
"module-b": "module-c",
"./toString": "./lib/toString.js",
".": false
},
"innerBrowser1": {
"field": {
"browser": {
"./lib/main1.js": "./lib/main.js"
}
}
},
"innerBrowser2": {
"browser": {
"./lib/main2.js": "./lib/replaced.js"
}
}
}

View file

@ -0,0 +1,4 @@
module.exports = function b() {
require("./a");
return "This is c";
};

View file

@ -0,0 +1,13 @@
var complex1 = require("./lib/complex1");
require.ensure(["./lib/complex1", "complexm/step2"], function(require) {
require("./lib/complex1");
var a = function() {};
require.ensure(["complexm/step1"], function(require) {
require("./lib/complex1");
var s1 = require("complexm/step1");
var s2 = require("complexm/step2");
console.log(s1);
console.log(s2);
});
});
console.log(complex1);

View file

@ -0,0 +1 @@
module.exports = "file";

View file

@ -0,0 +1 @@
module.exports = "dir";

View file

@ -0,0 +1 @@
module.exports = 1;

View file

@ -0,0 +1,5 @@
{
"name": "@exports-field/core",
"version": "1.0.0",
"exports": "./a.js"
}

View file

@ -0,0 +1,3 @@
{
"main": "./index.js"
}

View file

@ -0,0 +1,4 @@
{
"name": "foo",
"version": "1.0.0"
}

View file

@ -0,0 +1 @@
module.exports = 1;

View file

@ -0,0 +1,17 @@
{
"name": "imports-field",
"version": "1.0.0",
"exports": "./a.js",
"imports": {
"#imports-field": "./b.js",
"#b": "../b.js",
"#ccc/": "c/",
"#c": "c",
"#a/": "a/"
},
"other": {
"imports": {
"#b": "./a.js"
}
}
}

View file

@ -0,0 +1,2 @@
{
"main": "./a.js",

View file

@ -0,0 +1,3 @@
{
"private": true
}

View file

@ -0,0 +1 @@
module.exports = "lib complex1";

View file

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

View file

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

View file

@ -0,0 +1,9 @@
var a = require("./a");
if (x) {
for (var i = 0; i < 100; i++) {
while (true) require("./b");
do {
i++;
} while (require("m1/a")());
}
}

View file

@ -0,0 +1,12 @@
var a = require("./a");
with (x) {
switch (a) {
case 1:
require("./b");
default:
require.ensure(["m1/a"], function() {
var a = require("m1/a"),
b = require("m1/b");
});
}
}

View file

@ -0,0 +1,4 @@
var a = require("./a");
require.ensure([], function(require) {
require("./c.js");
});

View file

@ -0,0 +1,3 @@
module.exports = function a() {
return "This is nested m1/a";
};

View file

@ -0,0 +1,7 @@
{
"main": "main.js",
"browser": {
"./package-alias/index.js": "./package-alias/browser.js",
"module": "pkg/dir/index"
}
}

View file

@ -0,0 +1 @@
module.exports = 1;

View file

@ -0,0 +1,8 @@
{
"name": "@exports-field/core",
"version": "1.0.0",
"exports": {
".": "./a.js",
"./x": "./a.js"
}
}

View file

@ -0,0 +1,7 @@
{
"name": "foo",
"version": "1.0.0",
"browser": {
"./a": false
}
}

View file

@ -0,0 +1 @@
mod simple;

View file

@ -0,0 +1,17 @@
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/simple.test.js>
// use std::env;
// use crate::resolve;
// #[test]
// fn test() {
// // mimic `enhanced-resolve/test/simple.test.js`
// let dirname = env::current_dir().unwrap().join("tests/enhanced_resolve/test/");
// let paths =
// vec![(dirname.clone(), "../lib/index", "direct"), (dirname.clone(), "..", "as directory")];
// for (path, request, comment) in paths {
// assert_eq!(resolve(path, request), Ok(dirname.join("../lib/index.js")), "{}", comment);
// }
// }

View file

@ -0,0 +1,14 @@
mod enhanced_resolve;
use std::path::Path;
use oxc_resolver::{ResolveResult, Resolver};
/// # Errors
/// # Panics
pub fn resolve<P: AsRef<Path>>(path: P, request: &str) -> ResolveResult {
let path = path.as_ref();
assert!(!request.is_empty());
let resolver = Resolver::new();
resolver.resolve(path, request)
}