Skip to content

Commit

Permalink
feat(transformer): Optional Chaining (#6990)
Browse files Browse the repository at this point in the history
close: #6958
  • Loading branch information
Boshen committed Nov 21, 2024
1 parent 0c1fb96 commit 885e37f
Show file tree
Hide file tree
Showing 63 changed files with 2,028 additions and 61 deletions.
9 changes: 9 additions & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,15 @@ impl<'a> MemberExpression<'a> {
}
}

#[allow(missing_docs)]
pub fn object_mut(&mut self) -> &mut Expression<'a> {
match self {
MemberExpression::ComputedMemberExpression(expr) => &mut expr.object,
MemberExpression::StaticMemberExpression(expr) => &mut expr.object,
MemberExpression::PrivateFieldExpression(expr) => &mut expr.object,
}
}

#[allow(missing_docs)]
pub fn static_property_name(&self) -> Option<&'a str> {
match self {
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_transformer/src/compiler_assumptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub struct CompilerAssumptions {
pub no_class_calls: bool,

#[serde(default)]
#[deprecated = "Not Implemented"]
pub no_document_all: bool,

#[serde(default)]
Expand Down Expand Up @@ -73,7 +72,6 @@ pub struct CompilerAssumptions {
pub private_fields_as_properties: bool,

#[serde(default)]
#[deprecated = "Not Implemented"]
pub pure_getters: bool,

#[serde(default)]
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub struct TransformCtx<'a> {

pub module: Module,

#[expect(dead_code)]
pub assumptions: CompilerAssumptions,

// Helpers
Expand Down
45 changes: 38 additions & 7 deletions crates/oxc_transformer/src/es2020/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
mod nullish_coalescing_operator;
mod optional_chaining;
mod options;

use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::TransformCtx;

mod nullish_coalescing_operator;
mod options;

pub use nullish_coalescing_operator::NullishCoalescingOperator;
pub use optional_chaining::OptionalChaining;
pub use options::ES2020Options;

use crate::TransformCtx;

pub struct ES2020<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,

options: ES2020Options,

// Plugins
nullish_coalescing_operator: NullishCoalescingOperator<'a, 'ctx>,
optional_chaining: OptionalChaining<'a, 'ctx>,
}

impl<'a, 'ctx> ES2020<'a, 'ctx> {
pub fn new(options: ES2020Options, ctx: &'ctx TransformCtx<'a>) -> Self {
Self { ctx, nullish_coalescing_operator: NullishCoalescingOperator::new(ctx), options }
Self {
ctx,
options,
nullish_coalescing_operator: NullishCoalescingOperator::new(ctx),
optional_chaining: OptionalChaining::new(ctx),
}
}
}

Expand All @@ -30,6 +37,30 @@ impl<'a, 'ctx> Traverse<'a> for ES2020<'a, 'ctx> {
if self.options.nullish_coalescing_operator {
self.nullish_coalescing_operator.enter_expression(expr, ctx);
}

if self.options.optional_chaining {
self.optional_chaining.enter_expression(expr, ctx);
}
}

fn enter_formal_parameters(
&mut self,
node: &mut FormalParameters<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.optional_chaining {
self.optional_chaining.enter_formal_parameters(node, ctx);
}
}

fn exit_formal_parameters(
&mut self,
node: &mut FormalParameters<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.optional_chaining {
self.optional_chaining.exit_formal_parameters(node, ctx);
}
}

fn enter_big_int_literal(&mut self, node: &mut BigIntLiteral<'a>, _ctx: &mut TraverseCtx<'a>) {
Expand Down
Loading

0 comments on commit 885e37f

Please sign in to comment.