Skip to content

Commit

Permalink
fix(es/ast): Fix Ident::verify_symbol (#3108)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Dec 24, 2021
1 parent 5e6f6e5 commit e5971f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 12 additions & 1 deletion crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ impl Ident {
/// Returns [Ok] if it's a valid identifier and [Err] if it's not valid.
/// The returned [Err] contains the valid symbol.
pub fn verify_symbol(s: &str) -> Result<(), String> {
if s.is_reserved() || s.is_reserved_in_strict_mode(true) || s.is_reserved_in_strict_bind() {
fn is_reserved_symbol(s: &str) -> bool {
s.is_reserved() || s.is_reserved_in_strict_mode(true) || s.is_reserved_in_strict_bind()
}

if is_reserved_symbol(s) {
let mut buf = String::with_capacity(s.len() + 1);
buf.push('_');
buf.push_str(s);
Expand Down Expand Up @@ -153,6 +157,13 @@ impl Ident {
buf.push('_');
}

if is_reserved_symbol(&buf) {
let mut new_buf = String::with_capacity(buf.len() + 1);
new_buf.push('_');
new_buf.push_str(&buf);
buf = new_buf;
}

Err(buf)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ class ValidationStrategy {
static "string!"(value) {
return 2;
}

static "eval?"(value) {
return 3;
}

static "arguments!"(value) {
return 3;
}
}
console.log(ValidationStrategy.prototype['string!']);
console.log(ValidationStrategy.prototype["string!"]);
console.log(ValidationStrategy.prototype["eval?"]);
console.log(ValidationStrategy.prototype["arguments!"]);

1 comment on commit e5971f7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: e5971f7 Previous: 9f5a8f7 Ratio
full_es2015 219907200 ns/iter (± 20740134) 233003192 ns/iter (± 13800882) 0.94
full_es2016 180871103 ns/iter (± 15566801) 190382056 ns/iter (± 16708793) 0.95
full_es2017 182442947 ns/iter (± 16516063) 196360741 ns/iter (± 16636173) 0.93
full_es2018 184008866 ns/iter (± 16385807) 200769012 ns/iter (± 21774680) 0.92
full_es2019 181692076 ns/iter (± 16012290) 196262815 ns/iter (± 15047643) 0.93
full_es2020 181167097 ns/iter (± 10722812) 197488923 ns/iter (± 18505297) 0.92
full_es3 259954490 ns/iter (± 37953019) 271322888 ns/iter (± 22441872) 0.96
full_es5 237038430 ns/iter (± 18404753) 246804778 ns/iter (± 15170144) 0.96
parser 837369 ns/iter (± 122829) 894637 ns/iter (± 104894) 0.94

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.