Skip to content

Commit

Permalink
feat(codegen): align operator precedence with esbuild (#4509)
Browse files Browse the repository at this point in the history
closes #4339
  • Loading branch information
Boshen committed Jul 28, 2024
1 parent 6a94e3f commit 35654e6
Show file tree
Hide file tree
Showing 14 changed files with 448 additions and 446 deletions.
24 changes: 6 additions & 18 deletions crates/oxc_ast/src/precedence.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use oxc_syntax::precedence::{GetPrecedence, Precedence};

use crate::ast::{
match_member_expression, ArrowFunctionExpression, AssignmentExpression, AwaitExpression,
BinaryExpression, CallExpression, ComputedMemberExpression, ConditionalExpression, Expression,
ImportExpression, LogicalExpression, MemberExpression, NewExpression, PrivateFieldExpression,
SequenceExpression, StaticMemberExpression, TSTypeAssertion, UnaryExpression, UpdateExpression,
YieldExpression,
match_member_expression, AssignmentExpression, AwaitExpression, BinaryExpression,
CallExpression, ComputedMemberExpression, ConditionalExpression, Expression, ImportExpression,
LogicalExpression, MemberExpression, NewExpression, PrivateFieldExpression, SequenceExpression,
StaticMemberExpression, TSTypeAssertion, UnaryExpression, UpdateExpression, YieldExpression,
};

impl<'a> GetPrecedence for Expression<'a> {
Expand All @@ -14,7 +13,6 @@ impl<'a> GetPrecedence for Expression<'a> {
Self::SequenceExpression(expr) => expr.precedence(),
Self::AssignmentExpression(expr) => expr.precedence(),
Self::YieldExpression(expr) => expr.precedence(),
Self::ArrowFunctionExpression(expr) => expr.precedence(),
Self::ConditionalExpression(expr) => expr.precedence(),
Self::LogicalExpression(expr) => expr.precedence(),
Self::BinaryExpression(expr) => expr.precedence(),
Expand All @@ -41,12 +39,6 @@ impl<'a> GetPrecedence for YieldExpression<'a> {
}
}

impl<'a> GetPrecedence for ArrowFunctionExpression<'a> {
fn precedence(&self) -> Precedence {
Precedence::Arrow
}
}

impl<'a> GetPrecedence for ConditionalExpression<'a> {
fn precedence(&self) -> Precedence {
Precedence::Conditional
Expand Down Expand Up @@ -107,11 +99,7 @@ impl<'a> GetPrecedence for ImportExpression<'a> {

impl<'a> GetPrecedence for NewExpression<'a> {
fn precedence(&self) -> Precedence {
if self.arguments.is_empty() {
Precedence::NewWithoutArgs
} else {
Precedence::Call
}
Precedence::Call
}
}

Expand Down Expand Up @@ -141,6 +129,6 @@ impl<'a> GetPrecedence for PrivateFieldExpression<'a> {

impl<'a> GetPrecedence for TSTypeAssertion<'a> {
fn precedence(&self) -> Precedence {
Precedence::lowest()
Precedence::Lowest
}
}
5 changes: 5 additions & 0 deletions crates/oxc_codegen/src/annotation_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
pub(crate) fn get_leading_annotate_comment(&mut self, node_start: u32) -> Option<Comment> {
let maybe_leading_comment = self.try_get_leading_comment(node_start);
let comment = maybe_leading_comment?;
if self.latest_consumed_comment_end >= comment.span.end {
return None;
}
let real_end = match comment.kind {
CommentKind::SingleLine => comment.span.end,
CommentKind::MultiLine => comment.span.end + 2,
Expand Down Expand Up @@ -67,6 +70,8 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
self.print_soft_space();
}
}
// FIXME: esbuild function `restoreExprStartFlags`
self.start_of_default_export = self.code_len();
}

pub(crate) fn gen_comment(&mut self, node_start: u32) {
Expand Down
34 changes: 7 additions & 27 deletions crates/oxc_codegen/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
use bitflags::bitflags;

bitflags! {
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy)]
pub struct Context: u8 {
/// [In]
const In = 1 << 0;
const FORBID_IN = 1 << 0;
const FORBID_CALL = 1 << 1;
}
}

impl Default for Context {
fn default() -> Self {
Self::In
}
}

impl Context {
#[inline]
pub fn has_in(self) -> bool {
self.contains(Self::In)
pub fn forbid_in(self) -> bool {
self.contains(Self::FORBID_IN)
}

#[inline]
pub fn has_forbid_call(self) -> bool {
pub fn forbid_call(self) -> bool {
self.contains(Self::FORBID_CALL)
}

#[inline]
#[must_use]
pub fn and_in(self, include: bool) -> Self {
self.and(Self::In, include)
pub fn and_forbid_in(self, include: bool) -> Self {
self.and(Self::FORBID_IN, include)
}

#[inline]
Expand All @@ -46,18 +40,4 @@ impl Context {
self - flag
}
}

#[inline]
pub(crate) fn union_in_if(self, include: bool) -> Self {
self.union_if(Self::In, include)
}

#[inline]
fn union_if(self, other: Self, include: bool) -> Self {
if include {
self.union(other)
} else {
self
}
}
}
Loading

0 comments on commit 35654e6

Please sign in to comment.