fix(es/ast): Fix Ident::verify_symbol (#3108)

This commit is contained in:
magic-akari 2021-12-24 12:29:49 +08:00 committed by GitHub
parent 5e6f6e5122
commit e5971f77d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -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);
@ -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)
}
}

View File

@ -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!"]);