Skip to content

Commit

Permalink
Path doesn't need cloning, just a borrowed mutable reference.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hammond authored and Nathan Hammond committed Jul 17, 2023
1 parent 8074fa7 commit 84a406f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions crates/turborepo-lib/src/rewrite_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ pub fn set_path(
let root = get_root(json_document_string)?;

// Find the token we'll be modifying and its path from the root.
let (closest_path, closest_node) = get_closest_node(&root, &path, vec![]);
let current_path = &mut vec![];
let (closest_path, closest_node) = get_closest_node(&root, &path, current_path);

// Pull the token metadata off of the token.
let (property_count, range): (usize, jsonc_parser::common::Range) = match closest_node {
Expand Down Expand Up @@ -127,8 +128,8 @@ fn get_root(json_document_string: &str) -> Result<jsonc_parser::ast::Value, Rewr
fn get_closest_node<'a>(
current_node: &'a jsonc_parser::ast::Value<'a>,
target_path: &[&'a str],
current_path: Vec<&'a str>,
) -> (Vec<&'a str>, &'a jsonc_parser::ast::Value<'a>) {
current_path: &'a mut Vec<&'a str>,
) -> (&'a mut Vec<&'a str>, &'a jsonc_parser::ast::Value<'a>) {
// No target_path? We've arrived.
if target_path.is_empty() {
return (current_path, current_node);
Expand All @@ -149,7 +150,7 @@ fn get_closest_node<'a>(
Some(property) => {
let next_current_node = &property.value;
let next_property_name = property.name.as_str();
let mut next_current_path = current_path.clone();
let next_current_path = &mut *current_path;
next_current_path.push(next_property_name);
let next_target_path = &target_path[1..];

Expand Down Expand Up @@ -215,7 +216,7 @@ pub fn unset_path(

// The key path can appear multiple times. This a vec that contains each time it
// occurs.
let path_ranges = find_all_paths(&root, &path, vec![]);
let path_ranges = find_all_paths(&root, &path, &mut vec![]);

if path_ranges.is_empty() {
return Ok((false, json_document_string.to_owned()));
Expand Down Expand Up @@ -262,7 +263,7 @@ pub fn unset_path(
fn find_all_paths<'a>(
current_node: &'a jsonc_parser::ast::Value<'a>,
target_path: &[&'a str],
current_path: Vec<&'a str>,
current_path: &mut Vec<&'a str>,
) -> Vec<Range> {
let mut ranges: Vec<Range> = vec![];

Expand Down Expand Up @@ -323,7 +324,7 @@ fn find_all_paths<'a>(
// We must recurse.
let next_current_node = &property.value;
let next_property_name = property.name.as_str();
let mut next_current_path = current_path.clone();
let next_current_path = &mut *current_path;
next_current_path.push(next_property_name);
let next_target_path = &target_path[1..];

Expand Down

0 comments on commit 84a406f

Please sign in to comment.