Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extend-exclude = [
"crates/oxc_linter/src/rules/react/no_unknown_property.rs",
"crates/oxc_parser/src/lexer/byte_handlers.rs",
"crates/oxc_syntax/src/xml_entities.rs",
"crates/oxc_traverse/src/context/uid.rs",
"pnpm-lock.yaml",
"tasks/coverage/babel",
"tasks/coverage/test262",
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn main() {
TransformOptions::enable_all()
};

transform_options.debug = true;
transform_options.helper_loader.mode = HelperLoaderMode::External;

let ret = Transformer::new(&allocator, path, &transform_options)
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct TransformCtx<'a> {

pub assumptions: CompilerAssumptions,

pub debug: bool,

// Helpers
/// Manage helper loading
pub helper_loader: HelperLoaderStore<'a>,
Expand Down Expand Up @@ -60,6 +62,7 @@ impl TransformCtx<'_> {
source_text: "",
module: options.env.module,
assumptions: options.assumptions,
debug: options.debug,
helper_loader: HelperLoaderStore::new(&options.helper_loader),
module_imports: ModuleImportsStore::new(),
var_declarations: VarDeclarationsStore::new(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<'a> ClassProperties<'a, '_> {
let AssignmentTarget::AssignmentTargetIdentifier(ident) = &assign_expr.left else {
unreachable!();
};
assert!(ident.name.starts_with('_'));
assert!(ident.name.starts_with(if self.ctx.debug { '_' } else { '$' }));
assert!(ctx.scoping().get_reference(ident.reference_id()).symbol_id().is_some());
assert!(ident.span.is_empty());
assert!(prop.value.is_none());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ impl<'a> ClassProperties<'a, '_> {
..
} = self.classes_stack.find_get_set_private_prop(&field_expr.field);

let temp_var_name_base = get_var_name_from_node(field_expr);
let temp_var_name_base = get_var_name_from_node(field_expr, self.ctx.debug);

// TODO(improve-on-babel): Could avoid `move_expression` here and replace `update_expr.argument` instead.
// Only doing this first to match the order Babel creates temp vars.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ impl<'a> ClassPropertiesSuperConverter<'a, '_, '_> {
unreachable!()
};

let temp_var_name_base = get_var_name_from_node(member.as_ref());
let temp_var_name_base =
get_var_name_from_node(member.as_ref(), self.class_properties.ctx.debug);

let property =
ctx.ast.expression_string_literal(member.property.span, member.property.name, None);
Expand Down Expand Up @@ -436,7 +437,8 @@ impl<'a> ClassPropertiesSuperConverter<'a, '_, '_> {
unreachable!()
};

let temp_var_name_base = get_var_name_from_node(member.as_ref());
let temp_var_name_base =
get_var_name_from_node(member.as_ref(), self.class_properties.ctx.debug);

let property = member.expression.get_inner_expression_mut().take_in(ctx.ast.allocator);

Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oxc_ast::{AstBuilder, ast::*};
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::Scoping;
use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx, traverse_mut};
use oxc_traverse::{ReusableTraverseCtx, Traverse, TraverseCtx, traverse_mut_with_ctx};

// Core
mod common;
Expand Down Expand Up @@ -157,7 +157,11 @@ impl<'a> Transformer<'a> {
x4_regexp: RegExp::new(self.env.regexp, &self.ctx),
};

let scoping = traverse_mut(&mut transformer, allocator, program, scoping);
let mut traverse_ctx =
ReusableTraverseCtx::new_with_debug(scoping, allocator, self.ctx.debug);
traverse_mut_with_ctx(&mut transformer, program, &mut traverse_ctx);
let scoping = traverse_ctx.into_scoping();

let helpers_used = self.ctx.helper_loader.used_helpers.borrow_mut().drain().collect();
#[expect(deprecated)]
TransformerReturn { errors: self.ctx.take_errors(), scoping, helpers_used }
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_transformer/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub struct TransformOptions {
/// The working directory that all paths in the programmatic options will be resolved relative to.
pub cwd: PathBuf,

/// If `true`, produces code with inserted UIDs in a more easily debuggable form.
pub debug: bool,

// Core
/// Set assumptions in order to produce smaller output.
/// For more information, check the [assumptions](https://babel.dev/docs/assumptions) documentation page.
Expand Down Expand Up @@ -80,6 +83,7 @@ impl TransformOptions {
pub fn enable_all() -> Self {
Self {
cwd: PathBuf::new(),
debug: false,
assumptions: CompilerAssumptions::default(),
typescript: TypeScriptOptions::default(),
decorator: DecoratorOptions { legacy: true, emit_decorator_metadata: true },
Expand Down Expand Up @@ -260,6 +264,7 @@ impl TryFrom<&BabelOptions> for TransformOptions {

Ok(Self {
cwd: options.cwd.clone().unwrap_or_default(),
debug: false,
assumptions: options.assumptions,
typescript,
decorator,
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_transformer/tests/integrations/es_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ fn es_target() {
];

// Test no transformation for esnext.
let options = TransformOptions::from(ESTarget::from_str("esnext").unwrap());
let mut options = TransformOptions::from(ESTarget::from_str("esnext").unwrap());
options.debug = true;
for (_, case) in cases {
assert_eq!(test(case, &options), Ok(codegen(case, SourceType::mjs())));
}

#[cfg_attr(miri, expect(unused_variables))]
let snapshot =
cases.into_iter().enumerate().fold(String::new(), |mut w, (i, (target, case))| {
let options = TransformOptions::from_target(target).unwrap();
let mut options = TransformOptions::from_target(target).unwrap();
options.debug = true;
let result = match test(case, &options) {
Ok(code) => code,
Err(errors) => errors
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_traverse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ doctest = true
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_ast_visit = { workspace = true }
oxc_data_structures = { workspace = true, features = ["stack"] }
oxc_data_structures = { workspace = true, features = ["assert_unchecked", "stack"] }
oxc_ecmascript = { workspace = true }
oxc_semantic = { workspace = true }
oxc_span = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use oxc_ecmascript::BoundNames;

use super::to_identifier;

pub fn get_var_name_from_node<'a, N: GatherNodeParts<'a>>(node: &N) -> String {
pub fn get_var_name_from_node<'a, N: GatherNodeParts<'a>>(node: &N, debug: bool) -> String {
if !debug {
return String::new();
}

let mut name = String::new();
node.gather(&mut |mut part| {
if name.is_empty() {
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ impl<'a> TraverseCtx<'a> {
scope_id: ScopeId,
flags: SymbolFlags,
) -> BoundIdentifier<'a> {
let name = get_var_name_from_node(node);
let name = get_var_name_from_node(node, self.scoping.debug);
self.generate_uid(&name, scope_id, flags)
}

Expand Down Expand Up @@ -492,7 +492,7 @@ impl<'a> TraverseCtx<'a> {
&mut self,
node: &N,
) -> BoundIdentifier<'a> {
let name = get_var_name_from_node(node);
let name = get_var_name_from_node(node, self.scoping.debug);
self.generate_uid_in_current_hoist_scope(&name)
}

Expand Down Expand Up @@ -649,9 +649,9 @@ impl<'a> TraverseCtx<'a> {
///
/// # SAFETY
/// This function must not be public to maintain soundness of [`TraverseAncestry`].
pub(crate) fn new(scoping: Scoping, allocator: &'a Allocator) -> Self {
pub(crate) fn new(scoping: Scoping, allocator: &'a Allocator, debug: bool) -> Self {
let ancestry = TraverseAncestry::new();
let scoping = TraverseScoping::new(scoping);
let scoping = TraverseScoping::new(scoping, debug);
let ast = AstBuilder::new(allocator);
Self { ancestry, scoping, ast }
}
Expand Down
7 changes: 6 additions & 1 deletion crates/oxc_traverse/src/context/reusable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ pub struct ReusableTraverseCtx<'a>(TraverseCtx<'a>);
impl<'a> ReusableTraverseCtx<'a> {
/// Create new [`ReusableTraverseCtx`].
pub fn new(scoping: Scoping, allocator: &'a Allocator) -> Self {
Self(TraverseCtx::new(scoping, allocator))
Self(TraverseCtx::new(scoping, allocator, false))
}

/// Create new [`ReusableTraverseCtx`] with `debug` flag.
pub fn new_with_debug(scoping: Scoping, allocator: &'a Allocator, debug: bool) -> Self {
Self(TraverseCtx::new(scoping, allocator, debug))
}

/// Consume [`ReusableTraverseCtx`] and return [`Scoping`].
Expand Down
20 changes: 17 additions & 3 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::uid::UidGenerator;
pub struct TraverseScoping<'a> {
scoping: Scoping,
uid_generator: Option<UidGenerator<'a>>,
pub(super) debug: bool,
current_scope_id: ScopeId,
current_hoist_scope_id: ScopeId,
current_block_scope_id: ScopeId,
Expand Down Expand Up @@ -286,9 +287,21 @@ impl<'a> TraverseScoping<'a> {
///
/// See comments on `UidGenerator` for further details.
pub fn generate_uid_name(&mut self, name: &str, allocator: &'a Allocator) -> Atom<'a> {
if let Some(UidGenerator::Fast(uid_generator)) = &mut self.uid_generator {
uid_generator.create()
} else {
// `debug: true` is default and `FastUidGenerator` is only created once, so cold path
self.generate_uid_name_slow(name, allocator)
}
}

#[cold]
#[inline(never)]
fn generate_uid_name_slow(&mut self, name: &str, allocator: &'a Allocator) -> Atom<'a> {
// If `uid_generator` is not already populated, initialize it
let uid_generator =
self.uid_generator.get_or_insert_with(|| UidGenerator::new(&self.scoping, allocator));
let uid_generator = self
.uid_generator
.get_or_insert_with(|| UidGenerator::new(self.debug, &self.scoping, allocator));
// Generate unique name
uid_generator.create(name)
}
Expand Down Expand Up @@ -361,10 +374,11 @@ impl<'a> TraverseScoping<'a> {
// Methods used internally within crate
impl TraverseScoping<'_> {
/// Create new `TraverseScoping`
pub(super) fn new(scoping: Scoping) -> Self {
pub(super) fn new(scoping: Scoping, debug: bool) -> Self {
Self {
scoping,
uid_generator: None,
debug,
// Dummy values. Both immediately overwritten in `walk_program`.
current_scope_id: ScopeId::new(0),
current_hoist_scope_id: ScopeId::new(0),
Expand Down
Loading
Loading