Skip to content

Commit

Permalink
feat(minifier): minimize x["0"] -> x[0]
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 7, 2025
1 parent e3ff81e commit 8b42e36
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 18 deletions.
51 changes: 43 additions & 8 deletions crates/oxc_minifier/src/ast_passes/convert_to_dotted_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,26 @@ impl<'a> ConvertToDottedProperties {
) {
if let MemberExpression::ComputedMemberExpression(e) = expr {
let Expression::StringLiteral(s) = &e.expression else { return };
if !is_identifier_name(&s.value) {
if is_identifier_name(&s.value) {
let property = ctx.ast.identifier_name(s.span, s.value.clone());
let object = ctx.ast.move_expression(&mut e.object);
*expr = MemberExpression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(e.span, object, property, e.optional),
);
self.changed = true;
return;
}
let property = ctx.ast.identifier_name(s.span, s.value.clone());
let object = ctx.ast.move_expression(&mut e.object);
*expr = MemberExpression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(e.span, object, property, e.optional),
);
self.changed = true;
let v = s.value.as_str();
if !e.optional {
if let Some(n) = Ctx::string_to_equivalent_number_value(v) {
e.expression = ctx.ast.expression_numeric_literal(
s.span,
n as f64,
None,
NumberBase::Decimal,
);
}
}
}
}
}
Expand Down Expand Up @@ -110,7 +121,6 @@ mod test {
test_same("a[';']");
test_same("a[':']");
test_same("a['.']");
test_same("a['0']");
test_same("a['p ']");
test_same("a['p' + '']");
test_same("a[p]");
Expand Down Expand Up @@ -282,4 +292,29 @@ mod test {
",
);
}

#[test]
fn test_index() {
test("x['y']", "x.y;");
test_same("x['y z']");
test("x?.['y']", "x?.y;");
test_same("x?.['y z']");
test("x?.['y']()", "x?.y();");
test_same("x?.['y z']()");
test_same("x['y' + 'z']");
test_same("x?.['y' + 'z']");
test("x['0']", "x[0];");
test("x['123']", "x[123];");
test("x['-123']", "x[-123];");
test_same("x['-0']");
test_same("x['+0']");
test_same("x['01']");
test_same("x['-01']");
test_same("x['0x1']");
test_same("x['-0x1']");
test("x['2147483647']", "x[2147483647]");
test_same("x['2147483648']");
test_same("x['-2147483648']");
test_same("x['-2147483649']");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -843,16 +843,14 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
*key = PropertyKey::StaticIdentifier(
ctx.ast.alloc_identifier_name(s.span, s.value.clone()),
);
} else if (!s.value.starts_with('0') && !s.value.starts_with('+')) || s.value.len() <= 1 {
if let Ok(value) = s.value.parse::<u32>() {
self.changed = true;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span,
value as f64,
None,
NumberBase::Decimal,
));
}
} else if let Some(value) = Ctx::string_to_equivalent_number_value(s.value.as_str()) {
self.changed = true;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span,
value as f64,
None,
NumberBase::Decimal,
));
}
}

Expand Down
29 changes: 29 additions & 0 deletions crates/oxc_minifier/src/node_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,33 @@ impl<'a> Ctx<'a, '_> {
}
false
}

pub fn string_to_equivalent_number_value(s: &str) -> Option<f64> {
if s.is_empty() {
return None;
}
let mut is_negative = false;
let mut int_value = 0i32;
let mut start = 0;
let bytes = s.as_bytes();
if bytes[0] == b'-' && s.len() > 1 {
is_negative = true;
start += 1;
}
if bytes[start] == b'0' && s.len() > 1 {
return None;
}
for b in &bytes[start..] {
if b.is_ascii_digit() {
int_value =
int_value.checked_mul(10).and_then(|v| v.checked_add((b & 15) as i32))?;
} else {
return None;
}
}
if is_negative {
int_value = -int_value;
}
Some(int_value as f64)
}
}

0 comments on commit 8b42e36

Please sign in to comment.