-
-
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
new: Start on es2022 preset. Implement class-static-block
.
#3033
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
79 changes: 79 additions & 0 deletions
79
crates/oxc_transformer/src/es2022/class_static_block/mod.rs
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,79 @@ | ||
use std::rc::Rc; | ||
|
||
use oxc_ast::ast::{ClassBody, ClassElement, PropertyDefinitionType, Statement}; | ||
use oxc_span::SPAN; | ||
use rustc_hash::FxHashSet; | ||
|
||
use crate::context::Ctx; | ||
|
||
pub struct ClassStaticBlock<'a> { | ||
ctx: Ctx<'a>, | ||
} | ||
|
||
impl<'a> ClassStaticBlock<'a> { | ||
pub fn new(ctx: &Ctx<'a>) -> Self { | ||
Self { ctx: Rc::clone(ctx) } | ||
} | ||
|
||
pub fn transform_class_body(&mut self, body: &mut ClassBody<'a>) { | ||
let mut private_names = FxHashSet::default(); | ||
let ast = &self.ctx.ast; | ||
|
||
for stmt in &body.body { | ||
if stmt.is_private() { | ||
if let Some(name) = stmt.property_key().and_then(|k| k.private_name()) { | ||
private_names.insert(name.as_str().to_owned()); | ||
} | ||
} | ||
} | ||
|
||
let mut generate_id = || -> String { | ||
let mut id = "_".to_owned(); | ||
let mut i = 1; | ||
|
||
while private_names.contains(&id) { | ||
i += 1; | ||
id = format!("_{i}"); | ||
} | ||
|
||
private_names.insert(id.clone()); | ||
|
||
id | ||
}; | ||
|
||
let stmts = ast.move_vec(&mut body.body).into_iter().map(|stmt| { | ||
if let ClassElement::StaticBlock(mut block) = stmt { | ||
let private_id = generate_id(); | ||
|
||
// We special-case the single expression case to avoid the iife, | ||
// since it's common. | ||
let replacement = if block.body.len() == 1 | ||
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. Is there a better way to handle this branch?? |
||
&& matches!(&block.body[0], Statement::ExpressionStatement(_)) | ||
{ | ||
match block.body.remove(0) { | ||
Statement::ExpressionStatement(mut expr) => { | ||
ast.move_expression(&mut expr.expression) | ||
} | ||
_ => unreachable!(), | ||
} | ||
} else { | ||
ast.build_iife(SPAN, ast.move_vec(&mut block.body)) | ||
}; | ||
|
||
ast.class_property( | ||
PropertyDefinitionType::PropertyDefinition, | ||
SPAN, | ||
ast.property_key_private_identifier(ast.private_identifier(SPAN, &private_id)), | ||
Some(replacement), | ||
false, | ||
true, | ||
ast.new_vec(), | ||
) | ||
} else { | ||
stmt | ||
} | ||
}); | ||
|
||
body.body = ast.new_vec_from_iter(stmts); | ||
} | ||
} |
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,38 @@ | ||
mod class_static_block; | ||
mod options; | ||
|
||
use std::rc::Rc; | ||
|
||
use oxc_ast::ast::ClassBody; | ||
|
||
use crate::context::Ctx; | ||
|
||
use self::class_static_block::ClassStaticBlock; | ||
pub use self::options::Es2022Options; | ||
|
||
#[allow(dead_code)] | ||
pub struct Es2022<'a> { | ||
ctx: Ctx<'a>, | ||
options: Rc<Es2022Options>, | ||
|
||
// Plugins | ||
class_static_block: ClassStaticBlock<'a>, | ||
} | ||
|
||
impl<'a> Es2022<'a> { | ||
pub fn new(options: Es2022Options, ctx: &Ctx<'a>) -> Self { | ||
let options = Rc::new(options); | ||
|
||
Self { | ||
options: Rc::clone(&options), | ||
ctx: Rc::clone(ctx), | ||
class_static_block: ClassStaticBlock::new(ctx), | ||
} | ||
} | ||
|
||
pub fn transform_class_body(&mut self, body: &mut ClassBody<'a>) { | ||
if self.options.class_static_block_plugin { | ||
self.class_static_block.transform_class_body(body); | ||
} | ||
} | ||
} |
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,14 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
#[serde(default, rename_all = "camelCase")] | ||
pub struct Es2022Options { | ||
#[serde(skip)] | ||
pub class_static_block_plugin: bool, | ||
} | ||
|
||
impl Default for Es2022Options { | ||
fn default() -> Self { | ||
Self { class_static_block_plugin: true } | ||
} | ||
} |
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 es2022; | ||
mod react; | ||
mod typescript; | ||
mod utils; | ||
|
@@ -22,6 +23,7 @@ mod helpers { | |
|
||
use std::{path::Path, rc::Rc}; | ||
|
||
use es2022::Es2022; | ||
use oxc_allocator::{Allocator, Vec}; | ||
use oxc_ast::{ | ||
ast::*, | ||
|
@@ -31,8 +33,8 @@ use oxc_diagnostics::Error; | |
use oxc_semantic::Semantic; | ||
|
||
pub use crate::{ | ||
compiler_assumptions::CompilerAssumptions, options::TransformOptions, react::ReactOptions, | ||
typescript::TypeScriptOptions, | ||
compiler_assumptions::CompilerAssumptions, es2022::Es2022Options, options::TransformOptions, | ||
react::ReactOptions, typescript::TypeScriptOptions, | ||
}; | ||
|
||
use crate::{ | ||
|
@@ -43,9 +45,12 @@ use crate::{ | |
|
||
pub struct Transformer<'a> { | ||
ctx: Ctx<'a>, | ||
|
||
// NOTE: all callbacks must run in order. | ||
x0_typescript: TypeScript<'a>, | ||
x1_react: React<'a>, | ||
// x2_decorators, | ||
x3_es2022: Es2022<'a>, | ||
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. Ordering here is weird. When we add es2024, es2025, etc, all the numbers will shift. We probably need a different ordering system. |
||
} | ||
|
||
impl<'a> Transformer<'a> { | ||
|
@@ -60,6 +65,7 @@ impl<'a> Transformer<'a> { | |
ctx: Rc::clone(&ctx), | ||
x0_typescript: TypeScript::new(options.typescript, &ctx), | ||
x1_react: React::new(options.react, &ctx), | ||
x3_es2022: Es2022::new(options.es2022, &ctx), | ||
} | ||
} | ||
|
||
|
@@ -112,6 +118,7 @@ impl<'a> VisitMut<'a> for Transformer<'a> { | |
|
||
fn visit_class_body(&mut self, body: &mut ClassBody<'a>) { | ||
self.x0_typescript.transform_class_body(body); | ||
self.x3_es2022.transform_class_body(body); | ||
|
||
walk_mut::walk_class_body_mut(self, body); | ||
} | ||
|
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
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
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.
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.
I tried to avoid the clones but kept running into borrow issues.