Skip to content

Commit

Permalink
refactor(transformer): replace &’a Trivias with Rc<Trivias> (#3580)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Jun 11, 2024
1 parent 509871f commit 5793ff1
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 44 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_language_server/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_linter/examples/linter.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<OxcDiagnostic> = vec![];

Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/examples/cfg.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Trivias>) -> Self {
self.trivias = trivias;
self.jsdoc = JSDocBuilder::new(self.source_text, Rc::clone(&self.trivias));
self
}

Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_semantic/src/jsdoc/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/src/module_record/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/tests/integration/util/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 {
Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub type Ctx<'a> = Rc<TransformCtx<'a>>;
pub struct TransformCtx<'a> {
errors: RefCell<Vec<OxcDiagnostic>>,

pub trivias: &'a Trivias,
pub trivias: Rc<Trivias>,

pub ast: AstBuilder<'a>,

Expand All @@ -42,7 +42,7 @@ impl<'a> TransformCtx<'a> {
source_path: &Path,
source_type: SourceType,
source_text: &'a str,
trivias: &'a Trivias,
trivias: Rc<Trivias>,
options: &TransformOptions,
) -> Self {
let filename = source_path
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> Transformer<'a> {
source_path: &Path,
source_type: SourceType,
source_text: &'a str,
trivias: &'a Trivias,
trivias: Rc<Trivias>,
options: TransformOptions,
) -> Self {
let ctx = Rc::new(TransformCtx::new(
Expand Down
16 changes: 6 additions & 10 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,16 @@ 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::<Vec<_>>());

self.ir = format!("{:#?}", ret.program.body).into();

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);

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tasks/benchmark/benches/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down
4 changes: 2 additions & 2 deletions tasks/benchmark/benches/transformer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tasks/coverage/src/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
panic::UnwindSafe,
path::{Path, PathBuf},
process::{Command, Stdio},
rc::Rc,
};

use console::Style;
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions tasks/coverage/src/transformer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions tasks/transform_conformance/src/test_case.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs,
path::{Path, PathBuf},
rc::Rc,
};

use oxc_allocator::Allocator;
Expand Down Expand Up @@ -176,7 +177,7 @@ pub trait TestCase {
path,
source_type,
&source_text,
&ret.trivias,
Rc::new(ret.trivias),
transform_options.clone(),
)
.build(&mut program);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5793ff1

Please sign in to comment.