Skip to content

Commit 5d3114c

Browse files
committed
fix(mangler): allow using typescript keywords as variable names (#14315)
`as`, `any` should be allowed to use as a variable name.
1 parent e2ba8ad commit 5d3114c

File tree

4 files changed

+6
-12
lines changed

4 files changed

+6
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_mangler/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ oxc_data_structures = { workspace = true, features = ["inline_string"] }
2727
oxc_index = { workspace = true }
2828
oxc_semantic = { workspace = true }
2929
oxc_span = { workspace = true }
30+
oxc_syntax = { workspace = true }
3031

3132
itertools = { workspace = true }
3233
rustc-hash = { workspace = true }

crates/oxc_mangler/src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl<'t> Mangler<'t> {
421421
count += 1;
422422
// Do not mangle keywords and unresolved references
423423
let n = name.as_str();
424-
if !is_keyword(n)
424+
if !oxc_syntax::keyword::is_reserved_keyword(n)
425425
&& !is_special_name(n)
426426
&& !root_unresolved_references.contains_key(n)
427427
&& !(root_bindings.contains_key(n)
@@ -605,14 +605,6 @@ impl<'t> SlotFrequency<'t> {
605605
}
606606
}
607607

608-
#[rustfmt::skip]
609-
fn is_keyword(s: &str) -> bool {
610-
matches!(s, "as" | "do" | "if" | "in" | "is" | "of" | "any" | "for" | "get"
611-
| "let" | "new" | "out" | "set" | "try" | "var" | "case" | "else"
612-
| "enum" | "from" | "meta" | "null" | "this" | "true" | "type"
613-
| "void" | "with")
614-
}
615-
616608
// Maximum length of string is 15 (`slot_4294967295` for `u32::MAX`).
617609
fn debug_name(n: u32) -> InlineString<15, u8> {
618610
// Using `format!` here allocates a string unnecessarily.

tasks/minsize/minsize.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ Original | minified | minified | gzip | gzip | Iterations | Fi
1111

1212
544.10 kB | 71.04 kB | 72.48 kB | 25.78 kB | 26.20 kB | 2 | lodash.js
1313

14-
555.77 kB | 267.39 kB | 270.13 kB | 88.02 kB | 90.80 kB | 2 | d3.js
14+
555.77 kB | 267.39 kB | 270.13 kB | 88.01 kB | 90.80 kB | 2 | d3.js
1515

1616
1.01 MB | 439.40 kB | 458.89 kB | 122.06 kB | 126.71 kB | 2 | bundle.min.js
1717

1818
1.25 MB | 642.65 kB | 646.76 kB | 159.39 kB | 163.73 kB | 2 | three.js
1919

2020
2.14 MB | 711.15 kB | 724.14 kB | 160.43 kB | 181.07 kB | 2 | victory.js
2121

22-
3.20 MB | 1.00 MB | 1.01 MB | 322.53 kB | 331.56 kB | 3 | echarts.js
22+
3.20 MB | 1.00 MB | 1.01 MB | 322.59 kB | 331.56 kB | 3 | echarts.js
2323

2424
6.69 MB | 2.22 MB | 2.31 MB | 458.41 kB | 488.28 kB | 4 | antd.js
2525

26-
10.95 MB | 3.33 MB | 3.49 MB | 853.38 kB | 915.50 kB | 4 | typescript.js
26+
10.95 MB | 3.33 MB | 3.49 MB | 853.34 kB | 915.50 kB | 4 | typescript.js
2727

0 commit comments

Comments
 (0)