mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transformer): share TypeScriptOptions with ref not Rc (#6121)
Similar to #6118. Share `TypeScriptOptions` between transforms as a plain `&` reference, rather than an `Rc`, to reduce setup/teardown time. The code to parse pragmas from comments is moved into `Transformer::new`. This isn't the ideal place for it, but it means `TypeScriptOptions` can be shared with the existing `'ctx` lifetime, rather than having to have a 3rd lifetime `TypeScript<'a, 'ctx, 'options>`.
This commit is contained in:
parent
09e41c2c26
commit
43878451da
5 changed files with 15 additions and 20 deletions
|
|
@ -73,10 +73,13 @@ impl<'a> Transformer<'a> {
|
||||||
source_type: SourceType,
|
source_type: SourceType,
|
||||||
source_text: &'a str,
|
source_text: &'a str,
|
||||||
trivias: Trivias,
|
trivias: Trivias,
|
||||||
options: TransformOptions,
|
mut options: TransformOptions,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let ctx =
|
let ctx =
|
||||||
TransformCtx::new(allocator, source_path, source_type, source_text, trivias, &options);
|
TransformCtx::new(allocator, source_path, source_type, source_text, trivias, &options);
|
||||||
|
|
||||||
|
options.typescript.update_with_comments(&ctx);
|
||||||
|
|
||||||
Self { ctx, options }
|
Self { ctx, options }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +92,7 @@ impl<'a> Transformer<'a> {
|
||||||
let allocator = self.ctx.ast.allocator;
|
let allocator = self.ctx.ast.allocator;
|
||||||
|
|
||||||
let mut transformer = TransformerImpl {
|
let mut transformer = TransformerImpl {
|
||||||
x0_typescript: TypeScript::new(self.options.typescript, &self.ctx),
|
x0_typescript: TypeScript::new(&self.options.typescript, &self.ctx),
|
||||||
x1_react: React::new(self.options.react, &self.ctx),
|
x1_react: React::new(self.options.react, &self.ctx),
|
||||||
x2_es2021: ES2021::new(self.options.es2021),
|
x2_es2021: ES2021::new(self.options.es2021),
|
||||||
x2_es2020: ES2020::new(self.options.es2020),
|
x2_es2020: ES2020::new(self.options.es2020),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#![allow(clippy::unused_self)]
|
#![allow(clippy::unused_self)]
|
||||||
|
|
||||||
use std::{cell::Cell, rc::Rc};
|
use std::cell::Cell;
|
||||||
|
|
||||||
use oxc_allocator::Vec as ArenaVec;
|
use oxc_allocator::Vec as ArenaVec;
|
||||||
use oxc_ast::ast::*;
|
use oxc_ast::ast::*;
|
||||||
|
|
@ -20,7 +20,7 @@ use crate::{TransformCtx, TypeScriptOptions};
|
||||||
|
|
||||||
pub struct TypeScriptAnnotations<'a, 'ctx> {
|
pub struct TypeScriptAnnotations<'a, 'ctx> {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
options: Rc<TypeScriptOptions>,
|
options: &'ctx TypeScriptOptions,
|
||||||
ctx: &'ctx TransformCtx<'a>,
|
ctx: &'ctx TransformCtx<'a>,
|
||||||
/// Assignments to be added to the constructor body
|
/// Assignments to be added to the constructor body
|
||||||
assignments: Vec<Assignment<'a>>,
|
assignments: Vec<Assignment<'a>>,
|
||||||
|
|
@ -34,7 +34,7 @@ pub struct TypeScriptAnnotations<'a, 'ctx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'ctx> TypeScriptAnnotations<'a, 'ctx> {
|
impl<'a, 'ctx> TypeScriptAnnotations<'a, 'ctx> {
|
||||||
pub fn new(options: Rc<TypeScriptOptions>, ctx: &'ctx TransformCtx<'a>) -> Self {
|
pub fn new(options: &'ctx TypeScriptOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
|
||||||
let jsx_element_import_name = if options.jsx_pragma.contains('.') {
|
let jsx_element_import_name = if options.jsx_pragma.contains('.') {
|
||||||
options.jsx_pragma.split('.').next().map(String::from).unwrap()
|
options.jsx_pragma.split('.').next().map(String::from).unwrap()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ mod namespace;
|
||||||
mod options;
|
mod options;
|
||||||
mod rewrite_extensions;
|
mod rewrite_extensions;
|
||||||
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use module::TypeScriptModule;
|
use module::TypeScriptModule;
|
||||||
use namespace::TypeScriptNamespace;
|
use namespace::TypeScriptNamespace;
|
||||||
use oxc_allocator::Vec;
|
use oxc_allocator::Vec;
|
||||||
|
|
@ -41,7 +39,7 @@ use crate::TransformCtx;
|
||||||
/// In: `const x: number = 0;`
|
/// In: `const x: number = 0;`
|
||||||
/// Out: `const x = 0;`
|
/// Out: `const x = 0;`
|
||||||
pub struct TypeScript<'a, 'ctx> {
|
pub struct TypeScript<'a, 'ctx> {
|
||||||
options: Rc<TypeScriptOptions>,
|
options: &'ctx TypeScriptOptions,
|
||||||
ctx: &'ctx TransformCtx<'a>,
|
ctx: &'ctx TransformCtx<'a>,
|
||||||
|
|
||||||
annotations: TypeScriptAnnotations<'a, 'ctx>,
|
annotations: TypeScriptAnnotations<'a, 'ctx>,
|
||||||
|
|
@ -52,16 +50,14 @@ pub struct TypeScript<'a, 'ctx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'ctx> TypeScript<'a, 'ctx> {
|
impl<'a, 'ctx> TypeScript<'a, 'ctx> {
|
||||||
pub fn new(options: TypeScriptOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
|
pub fn new(options: &'ctx TypeScriptOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
|
||||||
let options = Rc::new(options.update_with_comments(ctx));
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
annotations: TypeScriptAnnotations::new(Rc::clone(&options), ctx),
|
annotations: TypeScriptAnnotations::new(options, ctx),
|
||||||
r#enum: TypeScriptEnum::new(ctx),
|
r#enum: TypeScriptEnum::new(ctx),
|
||||||
rewrite_extensions: TypeScriptRewriteExtensions::new(
|
rewrite_extensions: TypeScriptRewriteExtensions::new(
|
||||||
options.rewrite_import_extensions.clone().unwrap_or_default(),
|
options.rewrite_import_extensions.clone().unwrap_or_default(),
|
||||||
),
|
),
|
||||||
namespace: TypeScriptNamespace::new(Rc::clone(&options), ctx),
|
namespace: TypeScriptNamespace::new(options, ctx),
|
||||||
module: TypeScriptModule::new(ctx),
|
module: TypeScriptModule::new(ctx),
|
||||||
options,
|
options,
|
||||||
ctx,
|
ctx,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use oxc_allocator::{Box, Vec};
|
use oxc_allocator::{Box, Vec};
|
||||||
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames, NONE};
|
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames, NONE};
|
||||||
use oxc_span::{Atom, CompactStr, SPAN};
|
use oxc_span::{Atom, CompactStr, SPAN};
|
||||||
|
|
@ -19,11 +17,11 @@ use crate::TransformCtx;
|
||||||
|
|
||||||
pub struct TypeScriptNamespace<'a, 'ctx> {
|
pub struct TypeScriptNamespace<'a, 'ctx> {
|
||||||
ctx: &'ctx TransformCtx<'a>,
|
ctx: &'ctx TransformCtx<'a>,
|
||||||
options: Rc<TypeScriptOptions>,
|
options: &'ctx TypeScriptOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
|
impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
|
||||||
pub fn new(options: Rc<TypeScriptOptions>, ctx: &'ctx TransformCtx<'a>) -> Self {
|
pub fn new(options: &'ctx TypeScriptOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
|
||||||
Self { ctx, options }
|
Self { ctx, options }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ impl TypeScriptOptions {
|
||||||
/// otherwise `JSDoc` could be used instead.
|
/// otherwise `JSDoc` could be used instead.
|
||||||
///
|
///
|
||||||
/// This behavior is aligned with babel.
|
/// This behavior is aligned with babel.
|
||||||
pub(crate) fn update_with_comments(mut self, ctx: &TransformCtx) -> Self {
|
pub(crate) fn update_with_comments(&mut self, ctx: &TransformCtx) {
|
||||||
for comment in ctx.trivias.comments() {
|
for comment in ctx.trivias.comments() {
|
||||||
let mut comment = comment.span.source_text(ctx.source_text).trim_start();
|
let mut comment = comment.span.source_text(ctx.source_text).trim_start();
|
||||||
// strip leading jsdoc comment `*` and then whitespaces
|
// strip leading jsdoc comment `*` and then whitespaces
|
||||||
|
|
@ -92,8 +92,6 @@ impl TypeScriptOptions {
|
||||||
self.jsx_pragma = Cow::from(pragma.to_string());
|
self.jsx_pragma = Cow::from(pragma.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue