Skip to content

Commit

Permalink
feat(minifier): partially re-enable minifier (#963)
Browse files Browse the repository at this point in the history
closes #949
closes #950
closes #951

All minifier tests are disable from this PR.

We are going to fix the compilation errors first, then the behavioral
errors.
  • Loading branch information
Boshen authored Oct 8, 2023
1 parent 9ad2634 commit 55b2f03
Show file tree
Hide file tree
Showing 17 changed files with 971 additions and 245 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"
members = ["crates/*", "tasks/*", "editor/vscode/server"]
exclude = ["crates/oxc_minifier", "tasks/minsize"]
exclude = ["tasks/minsize"]

[workspace.package]
authors = ["Boshen <[email protected]>", "Oxc contributors"]
Expand Down
28 changes: 26 additions & 2 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_syntax::{
operator::{
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
},
reference::ReferenceId,
reference::{ReferenceFlag, ReferenceId},
symbol::SymbolId,
};
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -163,6 +163,11 @@ impl<'a> Expression<'a> {
self.is_null() || self.evaluate_to_undefined()
}

/// Determines whether the given expr is a `NaN` literal
pub fn is_nan(&self) -> bool {
matches!(self, Self::Identifier(ident) if ident.name == "NaN")
}

/// Remove nested parentheses from this expression.
pub fn without_parenthesized(&self) -> &Self {
match self {
Expand Down Expand Up @@ -246,6 +251,23 @@ impl<'a> Expression<'a> {
_ => None,
}
}

pub fn is_immutable_value(&self) -> bool {
match self {
Self::BooleanLiteral(_)
| Self::NullLiteral(_)
| Self::NumberLiteral(_)
| Self::BigintLiteral(_)
| Self::RegExpLiteral(_)
| Self::StringLiteral(_) => true,
Self::TemplateLiteral(lit) if lit.is_no_substitution_template() => true,
Self::UnaryExpression(unary_expr) => unary_expr.argument.is_immutable_value(),
Self::Identifier(ident) => {
matches!(ident.name.as_str(), "undefined" | "Infinity" | "NaN")
}
_ => false,
}
}
}

/// Identifier Name
Expand All @@ -272,6 +294,8 @@ pub struct IdentifierReference {
pub name: Atom,
#[cfg_attr(feature = "serde", serde(skip))]
pub reference_id: Cell<Option<ReferenceId>>,
#[cfg_attr(feature = "serde", serde(skip))]
pub reference_flag: ReferenceFlag,
}

impl Hash for IdentifierReference {
Expand All @@ -283,7 +307,7 @@ impl Hash for IdentifierReference {

impl IdentifierReference {
pub fn new(span: Span, name: Atom) -> Self {
Self { span, name, reference_id: Cell::default() }
Self { span, name, reference_id: Cell::default(), reference_flag: ReferenceFlag::default() }
}
}

Expand Down
26 changes: 26 additions & 0 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ impl<'a> NumberLiteral<'a> {
pub fn new(span: Span, value: f64, raw: &'a str, base: NumberBase) -> Self {
Self { span, value, raw, base }
}

/// port from [closure compiler](https://github.com/google/closure-compiler/blob/a4c880032fba961f7a6c06ef99daa3641810bfdd/src/com/google/javascript/jscomp/base/JSCompDoubles.java#L113)
/// <https://262.ecma-international.org/5.1/#sec-9.5>
#[allow(clippy::cast_possible_truncation)] // for `as i32`
pub fn ecmascript_to_int32(num: f64) -> i32 {
// Fast path for most common case. Also covers -0.0
let int32_value = num as i32;
if (f64::from(int32_value) - num).abs() < f64::EPSILON {
return int32_value;
}

// NaN, Infinity if not included in our NumberLiteral, so we just skip step 2.

// step 3
let pos_int = num.signum() * num.abs().floor();

// step 4
let int32bit = pos_int % 2f64.powi(32);

// step5
if int32bit >= 2f64.powi(31) {
(int32bit - 2f64.powi(32)) as i32
} else {
int32bit as i32
}
}
}

impl<'a> Hash for NumberLiteral<'a> {
Expand Down
59 changes: 57 additions & 2 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
clippy::unused_self,
)]

use num_bigint::BigInt;
use std::mem;

use oxc_allocator::{Allocator, Box, String, Vec};
use oxc_span::{Atom, GetSpan, SourceType, Span};
use oxc_syntax::{
Expand All @@ -13,7 +16,6 @@ use oxc_syntax::{
},
NumberBase,
};
use std::mem;

#[allow(clippy::wildcard_imports)]
use crate::ast::*;
Expand Down Expand Up @@ -88,8 +90,12 @@ impl<'a> AstBuilder<'a> {

/* ---------- Literals ---------- */

pub fn string_literal(&self, span: Span, value: Atom) -> StringLiteral {
StringLiteral { span, value }
}

pub fn number_literal(
&mut self,
&self,
span: Span,
value: f64,
raw: &'a str,
Expand All @@ -98,6 +104,44 @@ impl<'a> AstBuilder<'a> {
NumberLiteral { span, value, raw, base }
}

pub fn boolean_literal(&self, span: Span, value: bool) -> BooleanLiteral {
BooleanLiteral { span, value }
}

pub fn null_literal(&self, span: Span) -> NullLiteral {
NullLiteral { span }
}

pub fn bigint_literal(&self, span: Span, value: BigInt) -> BigintLiteral {
BigintLiteral { span, value }
}

pub fn template_literal(
&self,
span: Span,
quasis: Vec<'a, TemplateElement>,
expressions: Vec<'a, Expression<'a>>,
) -> TemplateLiteral<'a> {
TemplateLiteral { span, quasis, expressions }
}

pub fn template_element(
&self,
span: Span,
tail: bool,
value: TemplateElementValue,
) -> TemplateElement {
TemplateElement { span, tail, value }
}

pub fn template_element_value(&self, raw: Atom, cooked: Option<Atom>) -> TemplateElementValue {
TemplateElementValue { raw, cooked }
}

pub fn reg_exp_literal(&self, span: Span, pattern: Atom, flags: RegExpFlags) -> RegExpLiteral {
RegExpLiteral { span, value: EmptyObject, regex: RegExp { pattern, flags } }
}

pub fn literal_string_expression(&self, literal: StringLiteral) -> Expression<'a> {
Expression::StringLiteral(self.alloc(literal))
}
Expand All @@ -122,6 +166,17 @@ impl<'a> AstBuilder<'a> {
Expression::BigintLiteral(self.alloc(literal))
}

pub fn literal_template_expression(&mut self, literal: TemplateLiteral<'a>) -> Expression<'a> {
Expression::TemplateLiteral(self.alloc(literal))
}

pub fn identifier_reference_expression(
&mut self,
ident: IdentifierReference,
) -> Expression<'a> {
Expression::Identifier(self.alloc(ident))
}

/* ---------- Identifiers ---------- */

pub fn identifier_expression(&self, identifier: IdentifierReference) -> Expression<'a> {
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ oxc_allocator = { workspace = true }
oxc_span = { workspace = true }
oxc_parser = { workspace = true }
oxc_ast = { workspace = true }
oxc_ast_lower = { workspace = true }
oxc_hir = { workspace = true }
oxc_semantic = { workspace = true }
oxc_syntax = { workspace = true }
oxc_index = { workspace = true }
bitflags = { workspace = true }
num-bigint = { workspace = true }
itertools = { workspace = true }
num-traits = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
Expand Down
Loading

0 comments on commit 55b2f03

Please sign in to comment.