-
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(traverse): function to get var name from node (#6317)
Pure refactor. Separate out the logic for creating a var name from an AST node into its own function, so it can be used standalone, outside of `generate_uid_based_on_node`. Apart from the new `get_var_name_from_node` function, and changing return type of `to_identifier` to `String`, the only other changes are moving files around.
- Loading branch information
1 parent
7d93b25
commit c0e2fef
Showing
7 changed files
with
95 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use oxc_syntax::identifier::{is_identifier_name, is_identifier_part, is_identifier_start}; | ||
|
||
/// Convert a String to a valid identifier name. | ||
/// | ||
/// Based on Babel's [`toIdentifier`] function. | ||
/// | ||
/// [`toIdentifier`]: https://github.com/babel/babel/blob/3bcfee232506a4cebe410f02042fb0f0adeeb0b1/packages/babel-types/src/converters/toIdentifier.ts#L4-L26 | ||
pub fn to_identifier(input: String) -> String { | ||
if is_identifier_name(&input) { | ||
return 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 !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 "_".to_string(); | ||
} | ||
|
||
name | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let cases = &[ | ||
("foo", "foo"), | ||
("fooBar", "fooBar"), | ||
("fooBar1", "fooBar1"), | ||
("foo-bar", "fooBar"), | ||
("foo bar", "fooBar"), | ||
("foo-bar-1", "fooBar1"), | ||
("1foo-bar", "FooBar"), | ||
("1-foo-bar", "FooBar"), | ||
("-- --", "_"), | ||
("_output$headers$x-amzn-requestid", "_output$headers$xAmznRequestid"), | ||
]; | ||
|
||
for &(input, expected) in cases { | ||
assert_eq!(to_identifier(input.to_string()), expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod gather_node_parts; | ||
pub use self::gather_node_parts::get_var_name_from_node; | ||
mod identifier; | ||
pub use identifier::to_identifier; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters