Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use oxc_ecmascript::{
};
use oxc_semantic::{IsGlobalReference, Scoping, SymbolId};
use oxc_span::format_atom;
use oxc_syntax::reference::ReferenceId;
use oxc_syntax::{
identifier::{is_identifier_part, is_identifier_start},
reference::ReferenceId,
};

use crate::{options::CompressOptions, state::MinifierState, symbol_value::SymbolValue};

Expand Down Expand Up @@ -241,4 +244,14 @@ impl<'a> Ctx<'a, '_> {
}
Some(f64::from(int_value))
}

/// `is_identifier_name` patched with KATAKANA MIDDLE DOT and HALFWIDTH KATAKANA MIDDLE DOT
/// Otherwise `({ 'x・': 0 })` gets converted to `({ x・: 0 })`, which breaks in Unicode 4.1 to
/// 15.
/// <https://github.com/oxc-project/unicode-id-start/pull/3>
pub fn is_identifier_name_patched(s: &str) -> bool {
let mut chars = s.chars();
chars.next().is_some_and(is_identifier_start)
&& chars.all(|c| is_identifier_part(c) && c != '・' && c != '・')
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use oxc_allocator::TakeIn;
use oxc_ast::ast::*;
use oxc_syntax::identifier::is_identifier_name;

use crate::ctx::Ctx;

Expand All @@ -17,7 +16,7 @@ impl<'a> LatePeepholeOptimizations {
pub fn convert_to_dotted_properties(expr: &mut MemberExpression<'a>, ctx: &mut Ctx<'a, '_>) {
let MemberExpression::ComputedMemberExpression(e) = expr else { return };
let Expression::StringLiteral(s) = &e.expression else { return };
if is_identifier_name(&s.value) {
if Ctx::is_identifier_name_patched(&s.value) {
let property = ctx.ast.identifier_name(s.span, s.value);
*expr =
MemberExpression::StaticMemberExpression(ctx.ast.alloc_static_member_expression(
Expand Down
67 changes: 39 additions & 28 deletions crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use oxc_span::GetSpan;
use oxc_span::SPAN;
use oxc_syntax::{
es_target::ESTarget,
identifier::is_identifier_name,
number::NumberBase,
operator::{BinaryOperator, UnaryOperator},
};
Expand Down Expand Up @@ -877,35 +876,40 @@ impl<'a> PeepholeOptimizations {
computed: &mut bool,
ctx: &mut Ctx<'a, '_>,
) {
if let PropertyKey::NumericLiteral(_) = key {
if *computed {
*computed = false;
match key {
PropertyKey::NumericLiteral(_) => {
if *computed {
*computed = false;
}
}
return;
}
let PropertyKey::StringLiteral(s) = key else { return };
let value = s.value.as_str();
if is_identifier_name(value) {
*computed = false;
*key = PropertyKey::StaticIdentifier(ctx.ast.alloc_identifier_name(s.span, s.value));
ctx.state.changed = true;
return;
}
if let Some(value) = Ctx::string_to_equivalent_number_value(value) {
if value >= 0.0 {
*computed = false;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span,
value,
None,
NumberBase::Decimal,
));
ctx.state.changed = true;
return;
PropertyKey::StringLiteral(s) => {
let value = s.value.as_str();
if Ctx::is_identifier_name_patched(value) {
*computed = false;
*key = PropertyKey::StaticIdentifier(
ctx.ast.alloc_identifier_name(s.span, s.value),
);
ctx.state.changed = true;
return;
}
if let Some(value) = Ctx::string_to_equivalent_number_value(value) {
if value >= 0.0 {
*computed = false;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span,
value,
None,
NumberBase::Decimal,
));
ctx.state.changed = true;
return;
}
}
if *computed {
*computed = false;
}
}
}
if *computed {
*computed = false;
_ => {}
}
}

Expand Down Expand Up @@ -1744,6 +1748,13 @@ mod test {
"class C { static accessor __proto__ = 0 }",
);

// Patch KATAKANA MIDDLE DOT and HALFWIDTH KATAKANA MIDDLE DOT
// <https://github.com/oxc-project/unicode-id-start/pull/3>
test_same("x = { 'x・': 0 };");
test_same("x = { 'x・': 0 };");
test_same("x = y['x・'];");
test_same("x = y['x・'];");

// <https://tc39.es/ecma262/2024/multipage/ecmascript-language-functions-and-classes.html#sec-static-semantics-classelementkind>
// <https://tc39.es/ecma262/2024/multipage/ecmascript-language-functions-and-classes.html#sec-class-definitions-static-semantics-early-errors>
// <https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-class-definitions-static-semantics-early-errors>
Expand Down
Loading