Skip to content

Commit

Permalink
Fix Import Paths When Root Directory Prefix Is Empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Kim-Butler committed Nov 25, 2022
1 parent 15db012 commit 1bc2b1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
7 changes: 3 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ pub struct RuntimeConfig {
use_custom_scalars: bool,
custom_scalar_prefix: Option<String>,
number_threads: usize,
root_dir_import_prefix: String,
root_dir_import_prefix: Option<String>,
global_types_module_name: String,
generated_module_name: String,
}
Expand Down Expand Up @@ -490,8 +490,7 @@ impl RuntimeConfig {
let root_dir_import_prefix = arg_matches
.value_of("root_dir_import_prefix")
.map(|s| s.to_string())
.or(config_root_dir_import_prefix)
.unwrap_or_else(|| String::from(""));
.or(config_root_dir_import_prefix);
let global_types_module_name = arg_matches
.value_of("global_types_module_name")
.map(|s| s.to_string())
Expand Down Expand Up @@ -540,7 +539,7 @@ impl RuntimeConfig {
self.number_threads
}

pub fn root_dir_import_prefix(&self) -> String {
pub fn root_dir_import_prefix(&self) -> Option<String> {
self.root_dir_import_prefix.clone()
}

Expand Down
24 changes: 15 additions & 9 deletions src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct CompileConfig {
root_dir: PathBuf,
show_deprecation_warnings: bool,
pub bottom_type_config: BottomTypeConfig,
pub root_dir_import_prefix: String,
pub root_dir_import_prefix: Option<String>,
pub global_types_module_name: String,
pub generated_module_name: String,
}
Expand Down Expand Up @@ -124,14 +124,20 @@ fn get_file_path_of_fragment(
let last_quote = import_comment
.rfind('"')
.unwrap_or(import_comment.len() - 1);
let mut import_filename = &import_comment[IMPORT_START.len()..last_quote];
let root_dir_prefix = &config.root_dir_import_prefix;
if import_filename.starts_with(root_dir_prefix) {
import_filename = &import_filename[root_dir_prefix.len()..];
} else if import_filename.starts_with('.') {
return current_dir.join(import_filename);
}
config.root_dir.join(import_filename)
let import_path = &import_comment[IMPORT_START.len()..last_quote];

config
.root_dir_import_prefix
.as_deref()
.and_then(|root_dir_prefix| import_path.strip_prefix(root_dir_prefix))
.map(|import_path_without_prefix| config.root_dir.join(import_path_without_prefix))
.unwrap_or_else(|| {
if import_path.starts_with('.') {
current_dir.join(import_path)
} else {
config.root_dir.join(import_path)
}
})
}

fn add_imported_fragments(
Expand Down
13 changes: 8 additions & 5 deletions src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::collections::{HashMap, HashSet};

mod field;

const EMPTY: &str = "";
const HEADER: &str = "/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.
Expand Down Expand Up @@ -238,10 +239,12 @@ fn compile_documentation(documentation: &schema::Documentation, tab_width: usize
.as_ref()
.map(|docs| {
let tab = " ".repeat(tab_width);
let processed_desc = docs.replace('\n', &format!("\n {tab}* ")).replace("*/", "");
let processed_desc = docs
.replace('\n', &format!("\n {tab}* "))
.replace("*/", EMPTY);
format!("/**\n {tab}* {processed_desc}\n {tab}*/\n{tab}")
})
.unwrap_or_else(|| String::from(""))
.unwrap_or_else(|| String::from(EMPTY))
}

fn compile_custom_scalar_name(
Expand Down Expand Up @@ -431,7 +434,7 @@ fn compile_variables_type_definition(
op_ir: &ir::Operation<'_>,
) -> Result<Typescript> {
match op_ir.variables {
None => Ok("".to_string()),
None => Ok(EMPTY.to_string()),
Some(ref var_irs) => {
let prop_defs: Result<Vec<_>> = var_irs
.iter()
Expand Down Expand Up @@ -485,15 +488,15 @@ fn compile_variables_type_definition(

fn compile_imports(config: &CompileConfig, used_globals: &HashSet<String>) -> Typescript {
if used_globals.is_empty() {
return String::from("");
return String::from(EMPTY);
}
// For test and file signature stability
let mut sorted_names: Vec<&str> = used_globals.iter().map(|g| g.as_ref()).collect();
sorted_names.sort_unstable();
format!(
"import type {{ {} }} from \"{}{}/{}\";\n\n",
sorted_names.join(", "),
config.root_dir_import_prefix,
config.root_dir_import_prefix.as_deref().unwrap_or(EMPTY),
config.generated_module_name,
config.global_types_module_name,
)
Expand Down

0 comments on commit 1bc2b1b

Please sign in to comment.