From 3ae94b88019dec6beedf357e934d1daa34b604d0 Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 30 Aug 2024 12:19:59 +0800 Subject: [PATCH] refactor(semantic): change `build_module_record` to accept &Path instead of PathBuf --- crates/oxc/src/compiler.rs | 2 +- crates/oxc_linter/src/service.rs | 2 +- crates/oxc_semantic/examples/simple.rs | 2 +- crates/oxc_semantic/src/builder.rs | 7 ++++--- crates/oxc_semantic/src/module_record/mod.rs | 4 ++-- crates/oxc_wasm/src/lib.rs | 4 ++-- tasks/benchmark/benches/linter.rs | 8 ++------ tasks/benchmark/benches/semantic.rs | 4 ++-- 8 files changed, 15 insertions(+), 18 deletions(-) diff --git a/crates/oxc/src/compiler.rs b/crates/oxc/src/compiler.rs index a2d12ff44a457..37a8cdbb34fb4 100644 --- a/crates/oxc/src/compiler.rs +++ b/crates/oxc/src/compiler.rs @@ -186,7 +186,7 @@ pub trait CompilerInterface { ) -> SemanticBuilderReturn<'a> { SemanticBuilder::new(source_text, source_type) .with_check_syntax_error(self.check_semantic_error()) - .build_module_record(source_path.to_path_buf(), program) + .build_module_record(source_path, program) .build(program) } diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index b119e0194d54c..34314ce4f4846 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -277,7 +277,7 @@ impl Runtime { .with_build_jsdoc(true) .with_trivias(trivias) .with_check_syntax_error(check_syntax_errors) - .build_module_record(path.to_path_buf(), program); + .build_module_record(path, program); let module_record = semantic_builder.module_record(); if self.linter.options().plugins.import { diff --git a/crates/oxc_semantic/examples/simple.rs b/crates/oxc_semantic/examples/simple.rs index 74db9937446ca..8611bd98976c7 100644 --- a/crates/oxc_semantic/examples/simple.rs +++ b/crates/oxc_semantic/examples/simple.rs @@ -35,7 +35,7 @@ fn main() -> std::io::Result<()> { let program = allocator.alloc(parser_ret.program); let semantic = SemanticBuilder::new(&source_text, source_type) - .build_module_record(path.to_path_buf(), program) + .build_module_record(path, program) // Enable additional syntax checks not performed by the parser .with_check_syntax_error(true) // Inform Semantic about comments found while parsing diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index f5650a5cb38e8..1780daadba640 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -2,7 +2,7 @@ use std::{ cell::{Cell, RefCell}, - path::PathBuf, + path::Path, sync::Arc, }; @@ -195,10 +195,11 @@ impl<'a> SemanticBuilder<'a> { #[must_use] pub fn build_module_record( mut self, - resolved_absolute_path: PathBuf, + resolved_absolute_path: &Path, program: &Program<'a>, ) -> Self { - let mut module_record_builder = ModuleRecordBuilder::new(resolved_absolute_path); + let mut module_record_builder = + ModuleRecordBuilder::new(resolved_absolute_path.to_path_buf()); module_record_builder.visit(program); self.module_record = Arc::new(module_record_builder.build()); self diff --git a/crates/oxc_semantic/src/module_record/mod.rs b/crates/oxc_semantic/src/module_record/mod.rs index dda753698374e..3c7f3f57bfeda 100644 --- a/crates/oxc_semantic/src/module_record/mod.rs +++ b/crates/oxc_semantic/src/module_record/mod.rs @@ -4,7 +4,7 @@ pub use builder::ModuleRecordBuilder; #[cfg(test)] mod module_record_tests { - use std::{path::PathBuf, sync::Arc}; + use std::{path::Path, sync::Arc}; use oxc_allocator::Allocator; use oxc_parser::Parser; @@ -21,7 +21,7 @@ mod module_record_tests { let program = allocator.alloc(ret.program); let semantic_ret = SemanticBuilder::new(source_text, source_type) .with_trivias(ret.trivias) - .build_module_record(PathBuf::new(), program) + .build_module_record(Path::new(""), program) .build(program); Arc::clone(&semantic_ret.semantic.module_record) } diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 40af76812fab3..5a641ed021683 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -194,7 +194,7 @@ impl Oxc { let semantic_ret = SemanticBuilder::new(source_text, source_type) .with_trivias(trivias.clone()) .with_check_syntax_error(true) - .build_module_record(path.clone(), &program) + .build_module_record(&path, &program) .build(&program); if run_options.syntax.unwrap_or_default() { @@ -285,7 +285,7 @@ impl Oxc { let semantic_ret = SemanticBuilder::new(source_text, source_type) .with_cfg(true) .with_trivias(trivias.clone()) - .build_module_record(path.to_path_buf(), program) + .build_module_record(path, program) .build(program); let semantic = Rc::new(semantic_ret.semantic); let linter_ret = Linter::default().run(path, Rc::clone(&semantic)); diff --git a/tasks/benchmark/benches/linter.rs b/tasks/benchmark/benches/linter.rs index 3848dabeb686d..137b44cf43d14 100644 --- a/tasks/benchmark/benches/linter.rs +++ b/tasks/benchmark/benches/linter.rs @@ -1,8 +1,4 @@ -use std::{ - env, - path::{Path, PathBuf}, - rc::Rc, -}; +use std::{env, path::Path, rc::Rc}; use oxc_allocator::Allocator; use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion}; @@ -35,7 +31,7 @@ fn bench_linter(criterion: &mut Criterion) { let semantic_ret = SemanticBuilder::new(source_text, source_type) .with_trivias(ret.trivias) .with_cfg(true) - .build_module_record(PathBuf::new(), program) + .build_module_record(Path::new(""), program) .build(program); let filter = vec![ (AllowWarnDeny::Deny, "all".into()), diff --git a/tasks/benchmark/benches/semantic.rs b/tasks/benchmark/benches/semantic.rs index 1f10942fb65d4..10d45340ed612 100644 --- a/tasks/benchmark/benches/semantic.rs +++ b/tasks/benchmark/benches/semantic.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::Path; use oxc_allocator::Allocator; use oxc_benchmark::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; @@ -26,7 +26,7 @@ fn bench_semantic(criterion: &mut Criterion) { let ret = SemanticBuilder::new(source_text, source_type) .with_trivias(ret.trivias.clone()) .with_build_jsdoc(true) - .build_module_record(PathBuf::new(), program) + .build_module_record(Path::new(""), program) .build(program); let ret = black_box(ret); ret.errors