Skip to content

Commit

Permalink
Style and naming adjustments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hammond authored and Nathan Hammond committed Jul 14, 2023
1 parent ff28084 commit 1923e46
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions crates/turborepo-lib/src/rewrite_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum RewriteError {
*/
enum GenerateType {
Object,
Path,
Member,
}

/**
Expand Down Expand Up @@ -61,11 +61,11 @@ pub fn set_path(

// Figure out what we should be generating:
// - An object to be assigned to an existing member. ("object")
// - A member to add to an existing object. ("path")
// - A member to add to an existing object. ("member")
let generate_type: GenerateType = if closest_path.len() > 0 {
GenerateType::Object
} else {
GenerateType::Path
GenerateType::Member
};

// Identify the token replacement metadata: start, end, and possible trailing
Expand All @@ -80,7 +80,7 @@ pub fn set_path(
start = range.start;
end = range.end;
}
GenerateType::Path => {
GenerateType::Member => {
if property_count > 0 {
separator = ",";
} else {
Expand All @@ -97,13 +97,14 @@ pub fn set_path(
let missing_path_segments = path[closest_path.len()..].to_vec();
let computed_object = match generate_type {
GenerateType::Object => generate_object(missing_path_segments, json_value),
GenerateType::Path => generate_path(missing_path_segments, json_value, separator),
GenerateType::Member => generate_member(missing_path_segments, json_value, separator),
};

// Generate a new document!
let mut output: String = json_document_string.to_owned();
output.replace_range(start..end, &computed_object);
return Ok(output);

Ok(output)
}

/**
Expand Down Expand Up @@ -136,16 +137,18 @@ fn get_closest_node<'a>(
current_path: Vec<&'a str>,
) -> (Vec<&'a str>, &'a jsonc_parser::ast::Value<'a>) {
// No target_path? We've arrived.
if target_path.len() == 0 {
if target_path.is_empty() {
return (current_path, current_node);
}

match current_node {
// Only objects can have key paths.
jsonc_parser::ast::Value::Object(obj) => {
// Grab the last property (member) which matches the current target_path
// element.
let object_property = obj.properties.iter().rev().find(|property| {
let current_property_name = property.name.as_str();
return target_path[0] == current_property_name;
target_path[0] == current_property_name
});

// See if we found a matching key. If so, recurse.
Expand Down Expand Up @@ -194,7 +197,7 @@ fn generate_object(path_segments: Vec<&str>, value: &str) -> String {
* Given path segments, generate a JSON object member with an optional
* trailing separator.
*/
fn generate_path(path_segments: Vec<&str>, value: &str, separator: &str) -> String {
fn generate_member(path_segments: Vec<&str>, value: &str, separator: &str) -> String {
let mut output = String::new();
output.push_str("\"");
output.push_str(path_segments[0]);
Expand Down Expand Up @@ -264,7 +267,7 @@ fn find_all_paths<'a>(
let mut ranges: Vec<Range> = vec![];

// Early exit when it's impossible to have matching ranges.
if target_path.len() == 0 {
if target_path.is_empty() {
return ranges;
}

Expand Down

0 comments on commit 1923e46

Please sign in to comment.