Skip to content

Commit

Permalink
fix(traverse): invalid variable name generated by generate_uid_based_…
Browse files Browse the repository at this point in the history
…on_node
  • Loading branch information
Dunqing committed Sep 2, 2024
1 parent c7e61a1 commit b186666
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
58 changes: 58 additions & 0 deletions crates/oxc_traverse/src/context/identifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::borrow::Cow;

use oxc_syntax::identifier::{is_identifier_name, is_identifier_part, is_identifier_start};

/// Convert a str to a valid identifier name.
///
/// Based on Babel's [`toIdentifier`](https://github.com/babel/babel/blob/3bcfee232506a4cebe410f02042fb0f0adeeb0b1/packages/babel-types/src/converters/toIdentifier.ts#L4-L26) function.
pub fn to_identifier(input: &str) -> Cow<str> {
if is_identifier_name(input) {
return Cow::Borrowed(input);
}

let mut name = String::with_capacity(input.len());

let mut capitalize_next = false;

let mut chars = input.chars();
if let Some(first) = chars.next() {
if is_identifier_start(first) {
name.push(first);
} else {
capitalize_next = true;
}
}

for c in chars {
if c == '-' || c.is_whitespace() || !is_identifier_part(c) {
capitalize_next = true;
} else if capitalize_next {
name.push(c.to_ascii_uppercase());
capitalize_next = false;
} else {
name.push(c);
}
}

if name.is_empty() {
return Cow::Borrowed("_");
}

Cow::Owned(name)
}

#[test]
fn test() {
assert_eq!(to_identifier("foo"), "foo");
assert_eq!(to_identifier("fooBar"), "fooBar");
assert_eq!(to_identifier("fooBar1"), "fooBar1");

assert_eq!(to_identifier("foo-bar"), "fooBar");
assert_eq!(to_identifier("foo bar"), "fooBar");
assert_eq!(to_identifier("foo-bar-1"), "fooBar1");
assert_eq!(to_identifier("1foo-bar"), "FooBar");
assert_eq!(to_identifier("1-foo-bar"), "FooBar");
assert_eq!(to_identifier("-- --"), "_");

assert_eq!(to_identifier("_output$headers$x-amzn-requestid"), "_output$headers$xAmznRequestid");
}
1 change: 1 addition & 0 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod ancestry;
mod ast_operations;
use ancestry::PopToken;
pub use ancestry::TraverseAncestry;
mod identifier;
mod scoping;
pub use scoping::TraverseScoping;

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use oxc_syntax::{
symbol::{SymbolFlags, SymbolId},
};

use super::ast_operations::GatherNodeParts;
use super::{ast_operations::GatherNodeParts, identifier::to_identifier};
use crate::scopes_collector::ChildScopeCollector;

/// Traverse scope context.
Expand Down Expand Up @@ -242,7 +242,7 @@ impl TraverseScoping {
parts.push_str(part);
});
let name = if parts.is_empty() { "ref" } else { parts.trim_start_matches('_') };
self.generate_uid(name, scope_id, flags)
self.generate_uid(&to_identifier(name.get(..20).unwrap_or(name)), scope_id, flags)
}

/// Generate UID in current scope based on node.
Expand Down
11 changes: 9 additions & 2 deletions tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
commit: 3bcfee23

Passed: 10/38
Passed: 10/39

# All Passed:
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-arrow-functions


# babel-plugin-transform-nullish-coalescing-operator (0/1)
# babel-plugin-transform-nullish-coalescing-operator (0/2)
* invalid-variable-name/input.js
x Output mismatch
x Reference flags mismatch:
| after transform: ReferenceId(3): ReferenceFlags(Write)
| rebuilt : ReferenceId(0): ReferenceFlags(Read | Write)


* transform-in-arrow-function-expression/input.js
x Reference flags mismatch:
| after transform: ReferenceId(3): ReferenceFlags(Write)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output.headers["x-amzn-requestid"] ?? output.headers["x-amzn-request-id"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var _output$headers$xAmz;
(_output$headers$xAmz = output.headers["x-amzn-requestid"]) !== null && _output$headers$xAmz !== void 0 ? _output$headers$xAmz : output.headers["x-amzn-request-id"];

0 comments on commit b186666

Please sign in to comment.