-
-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(transformer/react): correct import binding names #3014
Closed
Dunqing
wants to merge
1
commit into
main
from
04-17-feat_transformer_react_correct_import_binding_names
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ mod compiler_assumptions; | |
mod context; | ||
mod options; | ||
// Presets: <https://babel.dev/docs/presets> | ||
mod naming; | ||
mod react; | ||
mod typescript; | ||
mod utils; | ||
|
@@ -20,12 +21,13 @@ mod helpers { | |
pub mod module_imports; | ||
} | ||
|
||
use std::{path::Path, rc::Rc}; | ||
use std::{cell::RefCell, path::Path, rc::Rc}; | ||
|
||
use oxc_allocator::{Allocator, Vec}; | ||
use oxc_ast::{ | ||
ast::*, | ||
visit::{walk_mut, VisitMut}, | ||
Visit, | ||
}; | ||
use oxc_diagnostics::Error; | ||
use oxc_semantic::Semantic; | ||
|
@@ -55,7 +57,8 @@ impl<'a> Transformer<'a> { | |
semantic: Semantic<'a>, | ||
options: TransformOptions, | ||
) -> Self { | ||
let ctx = Rc::new(TransformCtx::new(allocator, source_path, semantic, &options)); | ||
let ctx = | ||
Rc::new(RefCell::new(TransformCtx::new(allocator, source_path, semantic, &options))); | ||
Self { | ||
ctx: Rc::clone(&ctx), | ||
x0_typescript: TypeScript::new(options.typescript, &ctx), | ||
|
@@ -68,7 +71,7 @@ impl<'a> Transformer<'a> { | |
/// Returns `Vec<Error>` if any errors were collected during the transformation. | ||
pub fn build(mut self, program: &mut Program<'a>) -> Result<(), std::vec::Vec<Error>> { | ||
self.visit_program(program); | ||
let errors = self.ctx.take_errors(); | ||
let errors = self.ctx.borrow().take_errors(); | ||
if errors.is_empty() { | ||
Ok(()) | ||
} else { | ||
|
@@ -79,6 +82,7 @@ impl<'a> Transformer<'a> { | |
|
||
impl<'a> VisitMut<'a> for Transformer<'a> { | ||
fn visit_program(&mut self, program: &mut Program<'a>) { | ||
self.ctx.borrow_mut().naming.visit_program(program); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to collect all bindings first. |
||
walk_mut::walk_program_mut(self, program); | ||
self.x1_react.transform_program_on_exit(program); | ||
self.x0_typescript.transform_program_on_exit(program); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use oxc_allocator::Allocator; | ||
use oxc_ast::{ | ||
ast::{BindingIdentifier, LabeledStatement}, | ||
AstBuilder, Visit, | ||
}; | ||
use oxc_span::{Atom, CompactStr}; | ||
use rustc_hash::FxHashSet; | ||
|
||
pub struct TransformerNaming<'a> { | ||
ast: AstBuilder<'a>, | ||
bindings: FxHashSet<Atom<'a>>, | ||
labels: FxHashSet<Atom<'a>>, | ||
} | ||
|
||
impl<'a> TransformerNaming<'a> { | ||
pub fn new(allocator: &'a Allocator) -> Self { | ||
let ast = AstBuilder::new(allocator); | ||
Self { ast, bindings: FxHashSet::default(), labels: FxHashSet::default() } | ||
} | ||
|
||
pub fn has_binding(&self, name: &str) -> bool { | ||
self.bindings.contains(name) | ||
} | ||
|
||
pub fn has_label(&self, name: &str) -> bool { | ||
self.labels.contains(name) | ||
} | ||
|
||
// <https://github.com/babel/babel/blob/419644f27c5c59deb19e71aaabd417a3bc5483ca/packages/babel-traverse/src/scope/index.ts#L495> | ||
pub fn generate_uid(&mut self, name: &str) -> CompactStr { | ||
for i in 0.. { | ||
let name = Self::internal_generate_uid(name, i); | ||
if self.has_binding(&name) || self.has_label(&name) { | ||
continue; | ||
} | ||
|
||
// Add the generated name to the bindings set. | ||
self.bindings.insert(self.ast.new_atom(name.as_str())); | ||
|
||
return name; | ||
} | ||
unreachable!() | ||
} | ||
|
||
// <https://github.com/babel/babel/blob/419644f27c5c59deb19e71aaabd417a3bc5483ca/packages/babel-traverse/src/scope/index.ts#L523> | ||
fn internal_generate_uid(name: &str, i: i32) -> CompactStr { | ||
CompactStr::from(if i > 1 { format!("_{name}{i}") } else { format!("_{name}") }) | ||
} | ||
} | ||
|
||
impl<'a> Visit<'a> for TransformerNaming<'a> { | ||
fn visit_binding_identifier(&mut self, ident: &BindingIdentifier<'a>) { | ||
self.bindings.insert(ident.name.clone()); | ||
} | ||
|
||
fn visit_labeled_statement(&mut self, stmt: &LabeledStatement<'a>) { | ||
self.labels.insert(stmt.label.name.clone()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TransformerNaming
needs to mutation.In a previous implementation. All the
TransformerCtx
fields that cannot be copied are wrapped inRc
oxc/crates/oxc_transformer/src/context.rs
Lines 15 to 22 in 31ed532
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a big change.
It probably makes more sense to use interior mutability in
TransformerNaming
, instead of adding all these borrow calls everywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. When we finally consider this PR, I'll refactor it.