-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): support nullish-coalescing-operator plugin
- Loading branch information
Showing
8 changed files
with
229 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
mod nullish_coalescing_operator; | ||
mod options; | ||
|
||
pub use nullish_coalescing_operator::NullishCoalescingOperator; | ||
pub use options::ES2020Options; | ||
use oxc_allocator::Vec; | ||
use oxc_ast::ast::*; | ||
use oxc_traverse::TraverseCtx; | ||
use std::rc::Rc; | ||
|
||
use crate::context::Ctx; | ||
|
||
#[allow(dead_code)] | ||
pub struct ES2020<'a> { | ||
ctx: Ctx<'a>, | ||
options: ES2020Options, | ||
|
||
// Plugins | ||
nullish_coalescing_operator: NullishCoalescingOperator<'a>, | ||
} | ||
|
||
impl<'a> ES2020<'a> { | ||
pub fn new(options: ES2020Options, ctx: Ctx<'a>) -> Self { | ||
Self { | ||
nullish_coalescing_operator: NullishCoalescingOperator::new(Rc::clone(&ctx)), | ||
ctx, | ||
options, | ||
} | ||
} | ||
|
||
pub fn transform_statements( | ||
&mut self, | ||
statements: &mut Vec<'a, Statement<'a>>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
if self.options.nullish_coalescing_operator { | ||
self.nullish_coalescing_operator.transform_statements(statements, ctx); | ||
} | ||
} | ||
|
||
pub fn transform_statements_on_exit( | ||
&mut self, | ||
statements: &mut Vec<'a, Statement<'a>>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
if self.options.nullish_coalescing_operator { | ||
self.nullish_coalescing_operator.transform_statements_on_exit(statements, ctx); | ||
} | ||
} | ||
|
||
pub fn transform_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { | ||
if self.options.nullish_coalescing_operator { | ||
self.nullish_coalescing_operator.transform_expression(expr, ctx); | ||
} | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
crates/oxc_transformer/src/es2020/nullish_coalescing_operator.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,125 @@ | ||
use std::cell::Cell; | ||
|
||
use oxc_semantic::{ReferenceFlag, SymbolFlags}; | ||
use oxc_traverse::TraverseCtx; | ||
|
||
use oxc_allocator::Vec; | ||
use oxc_ast::ast::*; | ||
use oxc_span::SPAN; | ||
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, LogicalOperator}; | ||
|
||
use crate::context::Ctx; | ||
|
||
/// ES2020: Nullish Coalescing Operator | ||
/// | ||
/// References: | ||
/// * <https://babeljs.io/docs/babel-plugin-transform-nullish-coalescing-operator> | ||
/// * <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-nullish-coalescing-operator> | ||
pub struct NullishCoalescingOperator<'a> { | ||
_ctx: Ctx<'a>, | ||
var_declarations: std::vec::Vec<Vec<'a, VariableDeclarator<'a>>>, | ||
} | ||
|
||
impl<'a> NullishCoalescingOperator<'a> { | ||
pub fn new(ctx: Ctx<'a>) -> Self { | ||
Self { _ctx: ctx, var_declarations: vec![] } | ||
} | ||
|
||
pub fn transform_statements( | ||
&mut self, | ||
_statements: &mut Vec<'a, Statement<'a>>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
self.var_declarations.push(ctx.ast.vec()); | ||
} | ||
|
||
pub fn transform_statements_on_exit( | ||
&mut self, | ||
statements: &mut Vec<'a, Statement<'a>>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
if let Some(declarations) = self.var_declarations.pop() { | ||
if declarations.is_empty() { | ||
return; | ||
} | ||
let variable = ctx.ast.alloc_variable_declaration( | ||
SPAN, | ||
VariableDeclarationKind::Var, | ||
declarations, | ||
false, | ||
); | ||
statements.insert(0, Statement::VariableDeclaration(variable)); | ||
} | ||
} | ||
|
||
fn create_new_var_with_expression( | ||
&mut self, | ||
expr: &Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) -> IdentifierReference<'a> { | ||
// Add `var name` to scope | ||
let name = match expr { | ||
Expression::Identifier(ref ident) => ident.name.as_str(), | ||
_ => "nullish_coalescing_operator", | ||
}; | ||
let symbol_id = | ||
ctx.generate_uid_in_current_scope(name, SymbolFlags::FunctionScopedVariable); | ||
let symbol_name = ctx.ast.atom(ctx.symbols().get_name(symbol_id)); | ||
|
||
{ | ||
// var _name; | ||
let binding_identifier = BindingIdentifier { | ||
span: SPAN, | ||
name: symbol_name.clone(), | ||
symbol_id: Cell::new(Some(symbol_id)), | ||
}; | ||
let kind = VariableDeclarationKind::Var; | ||
let id = ctx.ast.binding_pattern_kind_from_binding_identifier(binding_identifier); | ||
let id = ctx.ast.binding_pattern(id, None::<TSTypeAnnotation<'_>>, false); | ||
self.var_declarations | ||
.last_mut() | ||
.unwrap() | ||
.push(ctx.ast.variable_declarator(SPAN, kind, id, None, false)); | ||
}; | ||
|
||
ctx.create_reference_id(SPAN, symbol_name, Some(symbol_id), ReferenceFlag::Read) | ||
} | ||
|
||
pub fn transform_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { | ||
// left ?? right | ||
let Expression::LogicalExpression(logical_expr) = expr else { return }; | ||
if logical_expr.operator != LogicalOperator::Coalesce { | ||
return; | ||
} | ||
|
||
let reference; | ||
let assignment; | ||
|
||
// skip creating extra reference when `left` is static | ||
if ctx.symbols().is_static(&logical_expr.left) { | ||
reference = ctx.ast.copy(&logical_expr.left); | ||
assignment = ctx.ast.copy(&logical_expr.left); | ||
} else { | ||
let ident = self.create_new_var_with_expression(&logical_expr.left, ctx); | ||
reference = ctx.ast.expression_from_identifier_reference(ident.clone()); | ||
let left = AssignmentTarget::from( | ||
ctx.ast.simple_assignment_target_from_identifier_reference(ident), | ||
); | ||
let right = ctx.ast.copy(&logical_expr.left); | ||
assignment = | ||
ctx.ast.expression_assignment(SPAN, AssignmentOperator::Assign, left, right); | ||
}; | ||
|
||
let op = BinaryOperator::StrictInequality; | ||
let null = ctx.ast.expression_null_literal(SPAN); | ||
let left = ctx.ast.expression_binary(SPAN, ctx.ast.copy(&assignment), op, null); | ||
|
||
let right = ctx.ast.expression_binary(SPAN, ctx.ast.copy(&reference), op, ctx.ast.void_0()); | ||
|
||
let test = ctx.ast.expression_logical(SPAN, left, LogicalOperator::And, right); | ||
|
||
let right = ctx.ast.move_expression(&mut logical_expr.right); | ||
|
||
*expr = ctx.ast.expression_conditional(SPAN, test, reference, right); | ||
} | ||
} |
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,16 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Default, Clone, Deserialize)] | ||
#[serde(default, rename_all = "camelCase", deny_unknown_fields)] | ||
pub struct ES2020Options { | ||
#[serde(skip)] | ||
pub nullish_coalescing_operator: bool, | ||
} | ||
|
||
impl ES2020Options { | ||
#[must_use] | ||
pub fn with_nullish_coalescing_operator(mut self, enable: bool) -> Self { | ||
self.nullish_coalescing_operator = enable; | ||
self | ||
} | ||
} |
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