From 34ea5fb3099af01c380d47b20fb7f9dc7f1079d2 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 7 Jun 2024 16:12:12 +0800 Subject: [PATCH] feat(ast): derive Clone trait for Trivias --- crates/oxc_language_server/src/linter.rs | 2 +- crates/oxc_linter/examples/linter.rs | 7 ++++--- crates/oxc_linter/src/service.rs | 4 +++- crates/oxc_semantic/examples/cfg.rs | 4 ++-- crates/oxc_semantic/examples/simple.rs | 4 ++-- crates/oxc_semantic/src/builder.rs | 7 +++---- crates/oxc_semantic/src/jsdoc/builder.rs | 4 +++- crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs | 4 +++- .../oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs | 4 +++- crates/oxc_semantic/src/module_record/mod.rs | 4 ++-- .../oxc_semantic/tests/integration/util/mod.rs | 4 ++-- crates/oxc_transformer/examples/transformer.rs | 5 +++-- crates/oxc_transformer/src/context.rs | 4 ++-- crates/oxc_transformer/src/lib.rs | 2 +- crates/oxc_wasm/src/lib.rs | 16 ++++++---------- tasks/benchmark/benches/linter.rs | 2 +- tasks/benchmark/benches/transformer.rs | 4 ++-- tasks/coverage/src/suite.rs | 3 ++- tasks/coverage/src/transformer.rs | 9 ++++++--- tasks/transform_conformance/src/test_case.rs | 5 +++-- 20 files changed, 54 insertions(+), 44 deletions(-) diff --git a/crates/oxc_language_server/src/linter.rs b/crates/oxc_language_server/src/linter.rs index 9dbfb3e8d34a8..1b2d4477f3b68 100644 --- a/crates/oxc_language_server/src/linter.rs +++ b/crates/oxc_language_server/src/linter.rs @@ -287,7 +287,7 @@ impl IsolatedLintHandler { let program = allocator.alloc(ret.program); let semantic_ret = SemanticBuilder::new(javascript_source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .with_check_syntax_error(true) .build(program); diff --git a/crates/oxc_linter/examples/linter.rs b/crates/oxc_linter/examples/linter.rs index 402a7f865f837..51428a96ba1ab 100644 --- a/crates/oxc_linter/examples/linter.rs +++ b/crates/oxc_linter/examples/linter.rs @@ -1,6 +1,6 @@ //! The simplest linter -use std::{env, path::Path}; +use std::{env, path::Path, rc::Rc}; use oxc_allocator::Allocator; use oxc_ast::AstKind; @@ -29,8 +29,9 @@ fn main() -> std::io::Result<()> { } let program = allocator.alloc(ret.program); - let semantic_ret = - SemanticBuilder::new(&source_text, source_type).with_trivias(ret.trivias).build(program); + let semantic_ret = SemanticBuilder::new(&source_text, source_type) + .with_trivias(Rc::new(ret.trivias)) + .build(program); let mut errors: Vec = vec![]; diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index c5e4262989b1e..0239f3e20f1c0 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -264,10 +264,12 @@ impl Runtime { let program = allocator.alloc(ret.program); + let trivias = Rc::new(ret.trivias); + // Build the module record to unblock other threads from waiting for too long. // The semantic model is not built at this stage. let semantic_builder = SemanticBuilder::new(source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(trivias) .with_check_syntax_error(check_syntax_errors) .build_module_record(path.to_path_buf(), program); let module_record = semantic_builder.module_record(); diff --git a/crates/oxc_semantic/examples/cfg.rs b/crates/oxc_semantic/examples/cfg.rs index 1817d925b84a7..6f63ada799f95 100644 --- a/crates/oxc_semantic/examples/cfg.rs +++ b/crates/oxc_semantic/examples/cfg.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, env, path::Path, sync::Arc}; +use std::{collections::HashMap, env, path::Path, rc::Rc, sync::Arc}; use itertools::Itertools; use oxc_allocator::Allocator; @@ -38,7 +38,7 @@ fn main() -> std::io::Result<()> { let semantic = SemanticBuilder::new(&source_text, source_type) .with_check_syntax_error(true) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .build(program); if !semantic.errors.is_empty() { diff --git a/crates/oxc_semantic/examples/simple.rs b/crates/oxc_semantic/examples/simple.rs index c40df5d01d207..32f66f231f1e9 100644 --- a/crates/oxc_semantic/examples/simple.rs +++ b/crates/oxc_semantic/examples/simple.rs @@ -1,4 +1,4 @@ -use std::{env, path::Path, sync::Arc}; +use std::{env, path::Path, rc::Rc, sync::Arc}; use itertools::Itertools; use oxc_allocator::Allocator; @@ -23,7 +23,7 @@ fn main() -> std::io::Result<()> { let semantic = SemanticBuilder::new(&source_text, source_type) .with_check_syntax_error(true) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .build(program); if !semantic.errors.is_empty() { diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index ad107019f2dad..8977f7753a2db 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -108,10 +108,9 @@ impl<'a> SemanticBuilder<'a> { } #[must_use] - pub fn with_trivias(mut self, trivias: Trivias) -> Self { - let trivias = Rc::new(trivias); - self.trivias = Rc::clone(&trivias); - self.jsdoc = JSDocBuilder::new(self.source_text, trivias); + pub fn with_trivias(mut self, trivias: Rc) -> Self { + self.trivias = trivias; + self.jsdoc = JSDocBuilder::new(self.source_text, Rc::clone(&self.trivias)); self } diff --git a/crates/oxc_semantic/src/jsdoc/builder.rs b/crates/oxc_semantic/src/jsdoc/builder.rs index 73a9fdd809d82..3f823ed23e039 100644 --- a/crates/oxc_semantic/src/jsdoc/builder.rs +++ b/crates/oxc_semantic/src/jsdoc/builder.rs @@ -230,6 +230,8 @@ fn should_attach_jsdoc(kind: &AstKind) -> bool { #[cfg(test)] mod test { + use std::rc::Rc; + use oxc_allocator::Allocator; use oxc_parser::Parser; use oxc_span::{SourceType, Span}; @@ -246,7 +248,7 @@ mod test { let ret = Parser::new(allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let semantic = SemanticBuilder::new(source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .build(program) .semantic; semantic diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs index 4daaae97c71cc..c83a2e53a0ab7 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs @@ -36,6 +36,8 @@ impl<'a> JSDoc<'a> { #[cfg(test)] mod test { + use std::rc::Rc; + use crate::{Semantic, SemanticBuilder}; use oxc_allocator::Allocator; use oxc_parser::Parser; @@ -46,7 +48,7 @@ mod test { let ret = Parser::new(allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let semantic = SemanticBuilder::new(source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .build(program) .semantic; semantic diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs index 84c5d1c42b735..3f42e6c1a12e1 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs @@ -182,6 +182,8 @@ impl<'a> JSDocTag<'a> { #[cfg(test)] mod test { + use std::rc::Rc; + use crate::{Semantic, SemanticBuilder}; use oxc_allocator::Allocator; use oxc_parser::Parser; @@ -192,7 +194,7 @@ mod test { let ret = Parser::new(allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let semantic = SemanticBuilder::new(source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .build(program) .semantic; semantic diff --git a/crates/oxc_semantic/src/module_record/mod.rs b/crates/oxc_semantic/src/module_record/mod.rs index f1b2abd7a02d4..fb3fe5c643dc6 100644 --- a/crates/oxc_semantic/src/module_record/mod.rs +++ b/crates/oxc_semantic/src/module_record/mod.rs @@ -9,7 +9,7 @@ mod module_record_tests { use oxc_span::{SourceType, Span}; #[allow(clippy::wildcard_imports)] use oxc_syntax::module_record::*; - use std::{path::PathBuf, sync::Arc}; + use std::{path::PathBuf, rc::Rc, sync::Arc}; use crate::SemanticBuilder; @@ -19,7 +19,7 @@ mod module_record_tests { let ret = Parser::new(&allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let semantic_ret = SemanticBuilder::new(source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .build_module_record(PathBuf::new(), program) .build(program); Arc::clone(&semantic_ret.semantic.module_record) diff --git a/crates/oxc_semantic/tests/integration/util/mod.rs b/crates/oxc_semantic/tests/integration/util/mod.rs index 0bf2203a56c5a..108e8c2b4e95a 100644 --- a/crates/oxc_semantic/tests/integration/util/mod.rs +++ b/crates/oxc_semantic/tests/integration/util/mod.rs @@ -1,7 +1,7 @@ mod class_tester; mod expect; mod symbol_tester; -use std::{path::PathBuf, sync::Arc}; +use std::{path::PathBuf, rc::Rc, sync::Arc}; use itertools::Itertools; use oxc_allocator::Allocator; @@ -80,7 +80,7 @@ impl<'a> SemanticTester<'a> { let program = self.allocator.alloc(parse.program); let semantic_ret = SemanticBuilder::new(self.source_text, self.source_type) .with_check_syntax_error(true) - .with_trivias(parse.trivias) + .with_trivias(Rc::new(parse.trivias)) .build_module_record(PathBuf::new(), program) .build(program); diff --git a/crates/oxc_transformer/examples/transformer.rs b/crates/oxc_transformer/examples/transformer.rs index 268baf51bdcf0..e6557c4779e8b 100644 --- a/crates/oxc_transformer/examples/transformer.rs +++ b/crates/oxc_transformer/examples/transformer.rs @@ -1,4 +1,4 @@ -use std::{env, path::Path}; +use std::{env, path::Path, rc::Rc}; use oxc_allocator::Allocator; use oxc_codegen::{Codegen, CodegenOptions}; @@ -22,6 +22,7 @@ fn main() { let source_type = SourceType::from_path(path).unwrap(); let ret = Parser::new(&allocator, &source_text, source_type).parse(); + let trivias = Rc::new(ret.trivias); if !ret.errors.is_empty() { for error in ret.errors { @@ -46,7 +47,7 @@ fn main() { }, ..Default::default() }; - Transformer::new(&allocator, path, source_type, &source_text, &ret.trivias, transform_options) + Transformer::new(&allocator, path, source_type, &source_text, trivias, transform_options) .build(&mut program) .unwrap(); diff --git a/crates/oxc_transformer/src/context.rs b/crates/oxc_transformer/src/context.rs index a2db69bd19efa..0828e7bbb6a3e 100644 --- a/crates/oxc_transformer/src/context.rs +++ b/crates/oxc_transformer/src/context.rs @@ -17,7 +17,7 @@ pub type Ctx<'a> = Rc>; pub struct TransformCtx<'a> { errors: RefCell>, - pub trivias: &'a Trivias, + pub trivias: Rc, pub ast: AstBuilder<'a>, @@ -42,7 +42,7 @@ impl<'a> TransformCtx<'a> { source_path: &Path, source_type: SourceType, source_text: &'a str, - trivias: &'a Trivias, + trivias: Rc, options: &TransformOptions, ) -> Self { let filename = source_path diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 19f5f8598c54a..0f4402f0b37ec 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -61,7 +61,7 @@ impl<'a> Transformer<'a> { source_path: &Path, source_type: SourceType, source_text: &'a str, - trivias: &'a Trivias, + trivias: Rc, options: TransformOptions, ) -> Self { let ctx = Rc::new(TransformCtx::new( diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 4ca3722973920..a172e59576f3a 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -172,6 +172,8 @@ impl Oxc { .parse(); self.comments = self.map_comments(&ret.trivias); + let trivias = Rc::new(ret.trivias); + self.save_diagnostics(ret.errors.into_iter().map(Error::from).collect::>()); self.ir = format!("{:#?}", ret.program.body).into(); @@ -179,7 +181,7 @@ impl Oxc { let program = allocator.alloc(ret.program); let semantic_ret = SemanticBuilder::new(source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(Rc::clone(&trivias)) .with_check_syntax_error(true) .build(program); @@ -229,15 +231,9 @@ impl Oxc { if run_options.transform() { let options = TransformOptions::default(); - let result = Transformer::new( - &allocator, - &path, - source_type, - source_text, - semantic.trivias(), - options, - ) - .build(program); + let result = + Transformer::new(&allocator, &path, source_type, source_text, trivias, options) + .build(program); if let Err(errs) = result { self.save_diagnostics(errs); } diff --git a/tasks/benchmark/benches/linter.rs b/tasks/benchmark/benches/linter.rs index 6cee90cd04c89..ad8034049b0d7 100644 --- a/tasks/benchmark/benches/linter.rs +++ b/tasks/benchmark/benches/linter.rs @@ -29,7 +29,7 @@ fn bench_linter(criterion: &mut Criterion) { let ret = Parser::new(&allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let semantic_ret = SemanticBuilder::new(source_text, source_type) - .with_trivias(ret.trivias) + .with_trivias(Rc::new(ret.trivias)) .build_module_record(PathBuf::new(), program) .build(program); let filter = vec![ diff --git a/tasks/benchmark/benches/transformer.rs b/tasks/benchmark/benches/transformer.rs index da1c08f368438..eebba65dd79a2 100644 --- a/tasks/benchmark/benches/transformer.rs +++ b/tasks/benchmark/benches/transformer.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::{path::Path, rc::Rc}; use oxc_allocator::Allocator; use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion}; @@ -28,7 +28,7 @@ fn bench_transformer(criterion: &mut Criterion) { Path::new(&file.file_name), source_type, source_text, - &trivias, + Rc::new(trivias), transform_options, ) .build(program) diff --git a/tasks/coverage/src/suite.rs b/tasks/coverage/src/suite.rs index 0acd209bbf1cc..602149a367fe7 100644 --- a/tasks/coverage/src/suite.rs +++ b/tasks/coverage/src/suite.rs @@ -5,6 +5,7 @@ use std::{ panic::UnwindSafe, path::{Path, PathBuf}, process::{Command, Stdio}, + rc::Rc, }; use console::Style; @@ -327,7 +328,7 @@ pub trait Case: Sized + Sync + Send + UnwindSafe { let program = allocator.alloc(parser_ret.program); let semantic_ret = SemanticBuilder::new(source_text, source_type) - .with_trivias(parser_ret.trivias) + .with_trivias(Rc::new(parser_ret.trivias)) .with_check_syntax_error(true) .build_module_record(PathBuf::new(), program) .build(program); diff --git a/tasks/coverage/src/transformer.rs b/tasks/coverage/src/transformer.rs index 5e18791c0a055..0f8ba81c2dfc1 100644 --- a/tasks/coverage/src/transformer.rs +++ b/tasks/coverage/src/transformer.rs @@ -1,4 +1,7 @@ -use std::path::{Path, PathBuf}; +use std::{ + path::{Path, PathBuf}, + rc::Rc, +}; use oxc_allocator::Allocator; use oxc_codegen::{Codegen, CodegenOptions}; @@ -48,7 +51,7 @@ fn get_result( source_path, source_type, source_text, - &parse_result1.trivias, + Rc::new(parse_result1.trivias), options.clone(), ) .build(&mut program); @@ -79,7 +82,7 @@ fn get_result( source_path, source_type, &source_text1, - &parse_result2.trivias, + Rc::new(parse_result2.trivias), options, ) .build(&mut program); diff --git a/tasks/transform_conformance/src/test_case.rs b/tasks/transform_conformance/src/test_case.rs index 9dfa35a61f949..206ac4b595038 100644 --- a/tasks/transform_conformance/src/test_case.rs +++ b/tasks/transform_conformance/src/test_case.rs @@ -1,6 +1,7 @@ use std::{ fs, path::{Path, PathBuf}, + rc::Rc, }; use oxc_allocator::Allocator; @@ -176,7 +177,7 @@ pub trait TestCase { path, source_type, &source_text, - &ret.trivias, + Rc::new(ret.trivias), transform_options.clone(), ) .build(&mut program); @@ -270,7 +271,7 @@ impl TestCase for ConformanceTestCase { &self.path, source_type, &input, - &ret.trivias, + Rc::new(ret.trivias), transform_options.clone(), ); let result = transformer.build(&mut program);