From 0bdeb3bb7578597f98f94ef19a03c36e328bce7a Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 22 Oct 2024 13:00:43 +0800 Subject: [PATCH] refactor: apply latest `cargo +nightly fmt` (#281) --- .rustfmt.toml | 7 + napi/src/lib.rs | 2 +- napi/src/options.rs | 3 +- napi/src/tracing.rs | 6 +- src/cache.rs | 4 +- src/error.rs | 1 + src/file_system.rs | 2 +- src/lib.rs | 5 +- src/options.rs | 11 +- src/resolution.rs | 3 +- src/specifier.rs | 3 +- src/tests/alias.rs | 6 +- src/tests/dependencies.rs | 4 +- src/tests/exports_field.rs | 6 +- src/tests/extensions.rs | 3 +- src/tests/fallback.rs | 3 +- src/tests/full_specified.rs | 3 +- src/tests/imports_field.rs | 3 +- src/tests/mod.rs | 3 +- src/tests/simple.rs | 4 +- src/tests/tsconfig_paths.rs | 326 ++++++++++++++++++------------------ 21 files changed, 215 insertions(+), 193 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index c15be506..1a21c134 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -4,3 +4,10 @@ use_small_heuristics = "Max" # Use field initialize shorthand if possible use_field_init_shorthand = true + +# For `cargo +nightly fmt` +# unstable_features = true +# style_edition = '2024' +# reorder_impl_items = true +# group_imports = "StdExternalCrate" +# imports_granularity = "Crate" diff --git a/napi/src/lib.rs b/napi/src/lib.rs index f59e705c..d41cb084 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -58,8 +58,8 @@ pub struct ResolveTask { #[napi] impl Task for ResolveTask { - type Output = ResolveResult; type JsValue = ResolveResult; + type Output = ResolveResult; fn compute(&mut self) -> napi::Result { Ok(resolve(&self.resolver, &self.directory, &self.request)) diff --git a/napi/src/options.rs b/napi/src/options.rs index ea35672f..c7ae4db7 100644 --- a/napi/src/options.rs +++ b/napi/src/options.rs @@ -1,8 +1,7 @@ -use std::path::PathBuf; +use std::{collections::HashMap, path::PathBuf}; use napi::Either; use napi_derive::napi; -use std::collections::HashMap; /// Module Resolution Options /// diff --git a/napi/src/tracing.rs b/napi/src/tracing.rs index 88e4cd7e..903b355e 100644 --- a/napi/src/tracing.rs +++ b/napi/src/tracing.rs @@ -1,8 +1,8 @@ use std::sync::OnceLock; -use tracing_subscriber::filter::Targets; -use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt; -use tracing_subscriber::util::SubscriberInitExt; +use tracing_subscriber::{ + filter::Targets, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, +}; /// To debug `oxc_resolver`: /// `OXC_LOG=DEBUG your program` diff --git a/src/cache.rs b/src/cache.rs index 0d9f18ce..6e5d1951 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,4 +1,3 @@ -use once_cell::sync::OnceCell as OnceLock; use std::{ borrow::{Borrow, Cow}, convert::AsRef, @@ -10,6 +9,7 @@ use std::{ }; use dashmap::{DashMap, DashSet}; +use once_cell::sync::OnceCell as OnceLock; use rustc_hash::FxHasher; use crate::{ @@ -345,9 +345,11 @@ impl Hasher for IdentityHasher { fn write(&mut self, _: &[u8]) { unreachable!("Invalid use of IdentityHasher") } + fn write_u64(&mut self, n: u64) { self.0 = n; } + fn finish(&self) -> u64 { self.0 } diff --git a/src/error.rs b/src/error.rs index 0cd18c84..456abf4c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,5 @@ use std::{io, path::PathBuf, sync::Arc}; + use thiserror::Error; /// All resolution errors diff --git a/src/file_system.rs b/src/file_system.rs index 82073bb3..4dc62c01 100644 --- a/src/file_system.rs +++ b/src/file_system.rs @@ -1,9 +1,9 @@ -use cfg_if::cfg_if; use std::{ fs, io, path::{Path, PathBuf}, }; +use cfg_if::cfg_if; #[cfg(feature = "yarn_pnp")] use pnp::fs::{LruZipCache, VPath, VPathInfo, ZipCache}; diff --git a/src/lib.rs b/src/lib.rs index 8f020485..f1ce6695 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,8 +92,7 @@ use crate::{ package_json::JSONMap, path::{PathUtil, SLASH_START}, specifier::Specifier, - tsconfig::ExtendsField, - tsconfig::{ProjectReference, TsConfig}, + tsconfig::{ExtendsField, ProjectReference, TsConfig}, }; type ResolveResult = Result, ResolveError>; @@ -624,7 +623,7 @@ impl ResolverGeneric { } } Restriction::RegExp(_) => { - return Err(ResolveError::Unimplemented("Restriction with regex")) + return Err(ResolveError::Unimplemented("Restriction with regex")); } } } diff --git a/src/options.rs b/src/options.rs index 9a1e44f3..00158e79 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,5 +1,7 @@ -use std::path::Path; -use std::{fmt, path::PathBuf}; +use std::{ + fmt, + path::{Path, PathBuf}, +}; /// Module Resolution Options /// @@ -275,6 +277,7 @@ impl ResolveOptions { self.fully_specified = fully_specified; self } + /// Sets the value for [ResolveOptions::prefer_relative] /// /// ## Examples @@ -291,6 +294,7 @@ impl ResolveOptions { self.prefer_relative = flag; self } + /// Sets the value for [ResolveOptions::prefer_absolute] /// /// ## Examples @@ -556,11 +560,12 @@ impl fmt::Display for ResolveOptions { #[cfg(test)] mod test { + use std::path::PathBuf; + use super::{ AliasValue, EnforceExtension, ResolveOptions, Restriction, TsconfigOptions, TsconfigReferences, }; - use std::path::PathBuf; #[test] fn enforce_extension() { diff --git a/src/resolution.rs b/src/resolution.rs index 586c231b..9df58a87 100644 --- a/src/resolution.rs +++ b/src/resolution.rs @@ -1,10 +1,11 @@ -use crate::package_json::PackageJson; use std::{ fmt, path::{Path, PathBuf}, sync::Arc, }; +use crate::package_json::PackageJson; + /// The final path resolution with optional `?query` and `#fragment` #[derive(Clone)] pub struct Resolution { diff --git a/src/specifier.rs b/src/specifier.rs index 7e24fc5c..16f255b2 100644 --- a/src/specifier.rs +++ b/src/specifier.rs @@ -1,6 +1,7 @@ -use crate::error::SpecifierError; use std::borrow::Cow; +use crate::error::SpecifierError; + #[derive(Debug)] pub struct Specifier<'a> { path: Cow<'a, str>, diff --git a/src/tests/alias.rs b/src/tests/alias.rs index e5892066..843ca53e 100644 --- a/src/tests/alias.rs +++ b/src/tests/alias.rs @@ -1,16 +1,18 @@ //! -use normalize_path::NormalizePath; use std::path::Path; +use normalize_path::NormalizePath; + use crate::{AliasValue, Resolution, ResolveContext, ResolveError, ResolveOptions, Resolver}; #[test] #[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows. fn alias() { + use std::path::{Path, PathBuf}; + use super::memory_fs::MemoryFS; use crate::ResolverGeneric; - use std::path::{Path, PathBuf}; let f = Path::new("/"); diff --git a/src/tests/dependencies.rs b/src/tests/dependencies.rs index bcc2355b..2e0420b1 100644 --- a/src/tests/dependencies.rs +++ b/src/tests/dependencies.rs @@ -2,12 +2,12 @@ #[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows. mod windows { - use rustc_hash::FxHashSet; use std::path::PathBuf; - use crate::{ResolveContext, ResolveOptions, ResolverGeneric}; + use rustc_hash::FxHashSet; use super::super::memory_fs::MemoryFS; + use crate::{ResolveContext, ResolveOptions, ResolverGeneric}; fn file_system() -> MemoryFS { MemoryFS::new(&[ diff --git a/src/tests/exports_field.rs b/src/tests/exports_field.rs index 5a55a78a..0e9e5cf7 100644 --- a/src/tests/exports_field.rs +++ b/src/tests/exports_field.rs @@ -2,10 +2,12 @@ //! //! The huge exports field test cases are at the bottom of this file. -use crate::{Ctx, PathUtil, ResolveError, ResolveOptions, Resolver}; -use serde_json::json; use std::path::Path; +use serde_json::json; + +use crate::{Ctx, PathUtil, ResolveError, ResolveOptions, Resolver}; + #[test] fn test_simple() { let f = super::fixture().join("exports-field"); diff --git a/src/tests/extensions.rs b/src/tests/extensions.rs index 1ebd86f4..2067e2c3 100644 --- a/src/tests/extensions.rs +++ b/src/tests/extensions.rs @@ -1,8 +1,9 @@ //! -use crate::{EnforceExtension, Resolution, ResolveContext, ResolveError, ResolveOptions, Resolver}; use rustc_hash::FxHashSet; +use crate::{EnforceExtension, Resolution, ResolveContext, ResolveError, ResolveOptions, Resolver}; + #[test] fn extensions() { let f = super::fixture().join("extensions"); diff --git a/src/tests/fallback.rs b/src/tests/fallback.rs index 3da2d767..5eb98a5f 100644 --- a/src/tests/fallback.rs +++ b/src/tests/fallback.rs @@ -3,9 +3,10 @@ #[test] #[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows. fn fallback() { + use std::path::{Path, PathBuf}; + use super::memory_fs::MemoryFS; use crate::{AliasValue, ResolveError, ResolveOptions, ResolverGeneric}; - use std::path::{Path, PathBuf}; let f = Path::new("/"); diff --git a/src/tests/full_specified.rs b/src/tests/full_specified.rs index a70c0191..e56cceb9 100644 --- a/src/tests/full_specified.rs +++ b/src/tests/full_specified.rs @@ -4,9 +4,8 @@ mod windows { use std::path::PathBuf; - use crate::{AliasValue, ResolveOptions, ResolverGeneric}; - use super::super::memory_fs::MemoryFS; + use crate::{AliasValue, ResolveOptions, ResolverGeneric}; fn file_system() -> MemoryFS { MemoryFS::new(&[ diff --git a/src/tests/imports_field.rs b/src/tests/imports_field.rs index c928627a..9392ab59 100644 --- a/src/tests/imports_field.rs +++ b/src/tests/imports_field.rs @@ -2,10 +2,11 @@ //! //! The huge imports field test cases are at the bottom of this file. +use std::path::Path; + use serde_json::json; use crate::{Ctx, JSONMap, PathUtil, ResolveError, ResolveOptions, Resolver}; -use std::path::Path; #[test] fn test_simple() { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index e347dd1f..b14e6e8d 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -23,9 +23,10 @@ mod symlink; mod tsconfig_paths; mod tsconfig_project_references; -use crate::Resolver; use std::{env, path::PathBuf, sync::Arc, thread}; +use crate::Resolver; + pub fn fixture_root() -> PathBuf { env::current_dir().unwrap().join("fixtures") } diff --git a/src/tests/simple.rs b/src/tests/simple.rs index 08e1238a..c5aee426 100644 --- a/src/tests/simple.rs +++ b/src/tests/simple.rs @@ -49,13 +49,13 @@ fn dashed_name() { #[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows. mod windows { use super::super::memory_fs::MemoryFS; - use crate::ResolveOptions; #[test] fn no_package() { - use crate::ResolverGeneric; use std::path::Path; + + use crate::ResolverGeneric; let f = Path::new("/"); let file_system = MemoryFS::new(&[]); let resolver = ResolverGeneric::::new_with_file_system( diff --git a/src/tests/tsconfig_paths.rs b/src/tests/tsconfig_paths.rs index b85bf6be..82b2b079 100644 --- a/src/tests/tsconfig_paths.rs +++ b/src/tests/tsconfig_paths.rs @@ -240,12 +240,11 @@ fn test_template_variable() { mod windows_test { use std::path::{Path, PathBuf}; + use super::super::memory_fs::MemoryFS; use crate::{ ResolveError, ResolveOptions, ResolverGeneric, TsconfigOptions, TsconfigReferences, }; - use super::super::memory_fs::MemoryFS; - struct OneTest { name: &'static str, tsconfig: String, @@ -319,171 +318,172 @@ mod windows_test { #[test] fn match_path() { let pass = [ - OneTest { - name: "should locate path that matches with star and exists", - existing_files: vec!["/root/location/mylib/index.ts"], - requested_module: "lib/mylib", - expected_path: "/root/location/mylib/index.ts", - ..OneTest::default() - }, - OneTest { - name: "should resolve to correct path when many are specified", - tsconfig: serde_json::json!({ - "compilerOptions": { - "paths": { - "lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"], + OneTest { + name: "should locate path that matches with star and exists", + existing_files: vec!["/root/location/mylib/index.ts"], + requested_module: "lib/mylib", + expected_path: "/root/location/mylib/index.ts", + ..OneTest::default() + }, + OneTest { + name: "should resolve to correct path when many are specified", + tsconfig: serde_json::json!({ + "compilerOptions": { + "paths": { + "lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"], + } } - } - }) - .to_string(), - existing_files: vec!["/root/location/mylib/index.ts"], - requested_module: "lib/mylib", - expected_path: "/root/location/mylib/index.ts", - ..OneTest::default() - }, - OneTest { - name: "should locate path that matches with star and prioritize pattern with longest prefix", - tsconfig: serde_json::json!({ - "compilerOptions": { - "paths": { - "*": ["location/*"], - "lib/*": ["location/*"], + }) + .to_string(), + existing_files: vec!["/root/location/mylib/index.ts"], + requested_module: "lib/mylib", + expected_path: "/root/location/mylib/index.ts", + ..OneTest::default() + }, + OneTest { + name: "should locate path that matches with star and prioritize pattern with longest prefix", + tsconfig: serde_json::json!({ + "compilerOptions": { + "paths": { + "*": ["location/*"], + "lib/*": ["location/*"], + } } - } - }) - .to_string(), - existing_files: vec![ - "/root/location/lib/mylib/index.ts", - "/root/location/mylib/index.ts", - ], - requested_module: "lib/mylib", - expected_path: "/root/location/mylib/index.ts", - ..OneTest::default() - }, - OneTest { - name: "should locate path that matches with star and exists with extension", - existing_files: vec![ - "/root/location/mylib.myext", - ], - requested_module: "lib/mylib", - extensions: vec![".js".into(), ".myext".into()], - expected_path: "/root/location/mylib.myext", - ..OneTest::default() - }, - OneTest { - name: "should resolve request with extension specified", - existing_files: vec![ - "/root/location/test.jpg", - ], - requested_module: "lib/test.jpg", - expected_path: "/root/location/test.jpg", - ..OneTest::default() - }, - OneTest { - name: "should locate path that matches without star and exists", - tsconfig: serde_json::json!({ - "compilerOptions": { - "paths": { - "lib/foo": ["location/foo"] + }) + .to_string(), + existing_files: vec![ + "/root/location/lib/mylib/index.ts", + "/root/location/mylib/index.ts", + ], + requested_module: "lib/mylib", + expected_path: "/root/location/mylib/index.ts", + ..OneTest::default() + }, + OneTest { + name: "should locate path that matches with star and exists with extension", + existing_files: vec!["/root/location/mylib.myext"], + requested_module: "lib/mylib", + extensions: vec![".js".into(), ".myext".into()], + expected_path: "/root/location/mylib.myext", + ..OneTest::default() + }, + OneTest { + name: "should resolve request with extension specified", + existing_files: vec!["/root/location/test.jpg"], + requested_module: "lib/test.jpg", + expected_path: "/root/location/test.jpg", + ..OneTest::default() + }, + OneTest { + name: "should locate path that matches without star and exists", + tsconfig: serde_json::json!({ + "compilerOptions": { + "paths": { + "lib/foo": ["location/foo"] + } } - } - }) - .to_string(), - existing_files: vec![ - "/root/location/foo.ts", - ], - requested_module: "lib/foo", - expected_path: "/root/location/foo.ts", - ..OneTest::default() - }, - OneTest { - name: "should resolve to parent folder when filename is in subfolder", - existing_files: vec![ - "/root/location/mylib/index.ts", - ], - requested_module: "lib/mylib", - expected_path: "/root/location/mylib/index.ts", - ..OneTest::default() - }, - OneTest { - name: "should resolve from main field in package.json", - package_json: Some((PathBuf::from("/root/location/mylib"), serde_json::json!({ - "main": "./kalle.ts" - }).to_string())), - existing_files: vec![ - "/root/location/mylib/kalle.ts", - ], - requested_module: "lib/mylib", - expected_path: "/root/location/mylib/kalle.ts", - ..OneTest::default() - }, - OneTest { - name: "should resolve from main field in package.json (js)", - package_json: Some((PathBuf::from("/root/location/mylib.js"), serde_json::json!({ - "main": "./kalle.js" - }).to_string())), - existing_files: vec![ - "/root/location/mylib.js/kalle.js", - ], - extensions: vec![".ts".into(), ".js".into()], - requested_module: "lib/mylib.js", - expected_path: "/root/location/mylib.js/kalle.js", - ..OneTest::default() - }, - OneTest { - name: "should resolve from list of fields by priority in package.json", - main_fields: Some(vec!["missing".into(), "browser".into(), "main".into()]), - package_json: Some((PathBuf::from("/root/location/mylibjs"), serde_json::json!({ - "main": "./main.js", - "browser": "./browser.js" - }).to_string())), - existing_files: vec![ - "/root/location/mylibjs/main.js", - "/root/location/mylibjs/browser.js", - ], - extensions: vec![".ts".into(), ".js".into()], - requested_module: "lib/mylibjs", - expected_path: "/root/location/mylibjs/browser.js", - ..OneTest::default() - }, -OneTest { - name: "should ignore field mappings to missing files in package.json", - main_fields: Some(vec!["browser".into(), "main".into()]), - package_json: Some((PathBuf::from("/root/location/mylibjs"), serde_json::json!({ - "main": "./kalle.js", - "browser": "./nope.js" - }).to_string())), - existing_files: vec![ - "/root/location/mylibjs/kalle.js", - ], - extensions: vec![".ts".into(), ".js".into()], - requested_module: "lib/mylibjs", - expected_path: "/root/location/mylibjs/kalle.js", - ..OneTest::default() - }, - // Tests that are not applicable: - // name: "should resolve nested main fields" - // name: "should ignore advanced field mappings in package.json" - // name: "should resolve to with the help of baseUrl when not explicitly set" - // name: "should not resolve with the help of baseUrl when asked not to" - // name: "should resolve main file with cjs file extension" - OneTest { - name: "should resolve .ts from .js alias", - tsconfig: serde_json::json!({ - "compilerOptions": { - "paths": { - "@/*": ["src/*"] + }) + .to_string(), + existing_files: vec!["/root/location/foo.ts"], + requested_module: "lib/foo", + expected_path: "/root/location/foo.ts", + ..OneTest::default() + }, + OneTest { + name: "should resolve to parent folder when filename is in subfolder", + existing_files: vec!["/root/location/mylib/index.ts"], + requested_module: "lib/mylib", + expected_path: "/root/location/mylib/index.ts", + ..OneTest::default() + }, + OneTest { + name: "should resolve from main field in package.json", + package_json: Some(( + PathBuf::from("/root/location/mylib"), + serde_json::json!({ + "main": "./kalle.ts" + }) + .to_string(), + )), + existing_files: vec!["/root/location/mylib/kalle.ts"], + requested_module: "lib/mylib", + expected_path: "/root/location/mylib/kalle.ts", + ..OneTest::default() + }, + OneTest { + name: "should resolve from main field in package.json (js)", + package_json: Some(( + PathBuf::from("/root/location/mylib.js"), + serde_json::json!({ + "main": "./kalle.js" + }) + .to_string(), + )), + existing_files: vec!["/root/location/mylib.js/kalle.js"], + extensions: vec![".ts".into(), ".js".into()], + requested_module: "lib/mylib.js", + expected_path: "/root/location/mylib.js/kalle.js", + ..OneTest::default() + }, + OneTest { + name: "should resolve from list of fields by priority in package.json", + main_fields: Some(vec!["missing".into(), "browser".into(), "main".into()]), + package_json: Some(( + PathBuf::from("/root/location/mylibjs"), + serde_json::json!({ + "main": "./main.js", + "browser": "./browser.js" + }) + .to_string(), + )), + existing_files: vec![ + "/root/location/mylibjs/main.js", + "/root/location/mylibjs/browser.js", + ], + extensions: vec![".ts".into(), ".js".into()], + requested_module: "lib/mylibjs", + expected_path: "/root/location/mylibjs/browser.js", + ..OneTest::default() + }, + OneTest { + name: "should ignore field mappings to missing files in package.json", + main_fields: Some(vec!["browser".into(), "main".into()]), + package_json: Some(( + PathBuf::from("/root/location/mylibjs"), + serde_json::json!({ + "main": "./kalle.js", + "browser": "./nope.js" + }) + .to_string(), + )), + existing_files: vec!["/root/location/mylibjs/kalle.js"], + extensions: vec![".ts".into(), ".js".into()], + requested_module: "lib/mylibjs", + expected_path: "/root/location/mylibjs/kalle.js", + ..OneTest::default() + }, + // Tests that are not applicable: + // name: "should resolve nested main fields" + // name: "should ignore advanced field mappings in package.json" + // name: "should resolve to with the help of baseUrl when not explicitly set" + // name: "should not resolve with the help of baseUrl when asked not to" + // name: "should resolve main file with cjs file extension" + OneTest { + name: "should resolve .ts from .js alias", + tsconfig: serde_json::json!({ + "compilerOptions": { + "paths": { + "@/*": ["src/*"] + } } - } - }).to_string(), - existing_files: vec![ - "/root/src/foo.ts", - ], - requested_module: "@/foo", // original data was "@/foo.ts" but I don't get why it is the case? - expected_path: "/root/src/foo.ts", // original data was "/root/src/foo" - ..OneTest::default() - }, - ]; + }) + .to_string(), + existing_files: vec!["/root/src/foo.ts"], + requested_module: "@/foo", // original data was "@/foo.ts" but I don't get why it is the case? + expected_path: "/root/src/foo.ts", // original data was "/root/src/foo" + ..OneTest::default() + }, + ]; let root = PathBuf::from("/root");