refactor(transformer): replace &’a Trivias with Rc<Trivias> (#3580)

`Transformer` needs to borrow `Trivias`.
8be1cc8052 (r1630711060)
This commit is contained in:
Dunqing 2024-06-11 13:23:41 +08:00 committed by GitHub
parent 509871f478
commit 5793ff1986
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 54 additions and 44 deletions

View file

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

View file

@ -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<OxcDiagnostic> = vec![];

View file

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

View file

@ -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() {

View file

@ -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() {

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

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

View file

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

View file

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

View file

@ -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>,
@ -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

View file

@ -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(

View file

@ -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::<Vec<_>>());
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);
}

View file

@ -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![

View file

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

View file

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

View file

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

View file

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