Skip to content

Commit 05689b0

Browse files
voideanvaluefacebook-github-bot
authored andcommitted
js_module_import_path -> js_module_import_identifier
Reviewed By: alunyov Differential Revision: D51589285 fbshipit-source-id: 9811e86c262b9373ebb38a088f280bb4e87618e7
1 parent acd2573 commit 05689b0

File tree

7 files changed

+117
-122
lines changed

7 files changed

+117
-122
lines changed

compiler/crates/graphql-ir/src/ir.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ impl From<ExecutableDefinitionName> for StringKey {
202202
}
203203
}
204204

205+
impl From<OperationDefinitionName> for StringKey {
206+
fn from(operation_definition_name: OperationDefinitionName) -> Self {
207+
operation_definition_name.0
208+
}
209+
}
210+
211+
impl From<FragmentDefinitionName> for StringKey {
212+
fn from(fragment_definition_name: FragmentDefinitionName) -> Self {
213+
fragment_definition_name.0
214+
}
215+
}
216+
205217
impl Lookup for ExecutableDefinitionName {
206218
fn lookup(self) -> &'static str {
207219
match self {

compiler/crates/relay-codegen/src/build_ast.rs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
use std::path::PathBuf;
9+
810
use common::NamedItem;
911
use common::WithLocation;
1012
use graphql_ir::Argument;
@@ -720,15 +722,15 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
720722
let is_output_type = resolver_metadata
721723
.output_type_info
722724
.normalization_ast_should_have_is_output_type_true();
723-
let module = resolver_metadata.import_path;
724-
725-
let import_path = self
726-
.project_config
727-
.js_module_import_path(self.definition_source_location, module);
728725

729726
let variable_name = resolver_metadata.generate_local_resolver_name(self.schema);
730727
let resolver_js_module = JSModuleDependency {
731-
path: import_path,
728+
path: self.project_config.js_module_import_identifier(
729+
&self
730+
.project_config
731+
.artifact_path_for_definition(self.definition_source_location),
732+
&PathBuf::from(resolver_metadata.import_path.lookup()),
733+
),
732734
import_name: match resolver_metadata.import_name {
733735
Some(name) => ModuleImportName::Named {
734736
name,
@@ -1058,14 +1060,14 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
10581060
{
10591061
let fragment_source_location_key = no_inline_metadata.location;
10601062

1061-
let path_for_artifact = self.project_config.create_path_for_artifact(
1062-
fragment_source_location_key,
1063-
frag_spread.fragment.item.0.lookup().to_string(),
1064-
);
1065-
1066-
let normalization_import_path = self.project_config.js_module_import_path(
1067-
self.definition_source_location,
1068-
path_for_artifact.to_str().unwrap().intern(),
1063+
let normalization_import_path = self.project_config.js_module_import_identifier(
1064+
&self
1065+
.project_config
1066+
.artifact_path_for_definition(self.definition_source_location),
1067+
&self.project_config.create_path_for_artifact(
1068+
fragment_source_location_key,
1069+
frag_spread.fragment.item.0.lookup().to_string(),
1070+
),
10691071
);
10701072

10711073
return self
@@ -1128,7 +1130,6 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
11281130
relay_resolver_metadata: &RelayResolverMetadata,
11291131
fragment_primitive: Option<Primitive>,
11301132
) -> Primitive {
1131-
let module = relay_resolver_metadata.import_path;
11321133
let field = relay_resolver_metadata.field(self.schema);
11331134
let field_arguments = &relay_resolver_metadata.field_arguments;
11341135
let field_alias = relay_resolver_metadata.field_alias;
@@ -1141,9 +1142,12 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
11411142
CODEGEN_CONSTANTS.relay_resolver
11421143
};
11431144

1144-
let import_path = self
1145-
.project_config
1146-
.js_module_import_path(self.definition_source_location, module);
1145+
let import_path = self.project_config.js_module_import_identifier(
1146+
&self
1147+
.project_config
1148+
.artifact_path_for_definition(self.definition_source_location),
1149+
&PathBuf::from(relay_resolver_metadata.import_path.lookup()),
1150+
);
11471151

11481152
let args = if field.arguments.is_empty() {
11491153
Primitive::SkippableNull
@@ -1175,14 +1179,14 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
11751179
let resolver_module = if let Some((fragment_name, injection_mode)) =
11761180
relay_resolver_metadata.fragment_data_injection_mode
11771181
{
1178-
let path_for_artifact = self.project_config.create_path_for_artifact(
1179-
fragment_name.location.source_location(),
1180-
fragment_name.item.to_string(),
1181-
);
1182-
1183-
let fragment_import_path = self.project_config.js_module_import_path(
1184-
self.definition_source_location,
1185-
path_for_artifact.to_str().unwrap().intern(),
1182+
let fragment_import_path = self.project_config.js_module_import_identifier(
1183+
&self
1184+
.project_config
1185+
.artifact_path_for_definition(self.definition_source_location),
1186+
&self.project_config.create_path_for_artifact(
1187+
fragment_name.location.source_location(),
1188+
fragment_name.item.to_string(),
1189+
),
11861190
);
11871191

11881192
Primitive::RelayResolverModel {
@@ -1244,14 +1248,14 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
12441248
.location
12451249
.source_location();
12461250

1247-
let path_for_artifact = self.project_config.create_path_for_artifact(
1248-
normalization_artifact_source_location,
1249-
normalization_info.normalization_operation.item.to_string(),
1250-
);
1251-
1252-
let normalization_import_path = self.project_config.js_module_import_path(
1253-
self.definition_source_location,
1254-
path_for_artifact.to_str().unwrap().intern(),
1251+
let normalization_import_path = self.project_config.js_module_import_identifier(
1252+
&self
1253+
.project_config
1254+
.artifact_path_for_definition(self.definition_source_location),
1255+
&self.project_config.create_path_for_artifact(
1256+
normalization_artifact_source_location,
1257+
normalization_info.normalization_operation.item.to_string(),
1258+
),
12551259
);
12561260
let concrete_type = if normalization_info.inner_type.is_abstract_type() {
12571261
Primitive::Null
@@ -2011,9 +2015,11 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
20112015
provider.module_name
20122016
} else {
20132017
// This will build a path from the operation artifact to the provider module
2014-
self.project_config.js_module_import_path(
2015-
operation.name.map(|name| name.0),
2016-
provider.module_path().to_str().unwrap().intern(),
2018+
self.project_config.js_module_import_identifier(
2019+
&self
2020+
.project_config
2021+
.artifact_path_for_definition(operation.name),
2022+
&provider.module_path(),
20172023
)
20182024
};
20192025

compiler/crates/relay-compiler/src/build_project/generate_artifacts.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn generate_artifacts(
8383
return Artifact {
8484
artifact_source_keys,
8585
path: project_config
86-
.path_for_artifact(source_file, normalization.name.item.0),
86+
.artifact_path_for_definition(normalization.name),
8787
content: ArtifactContent::SplitOperation {
8888
normalization_operation: Arc::clone(normalization),
8989
typegen_operation,
@@ -96,10 +96,6 @@ pub fn generate_artifacts(
9696
RefetchableDerivedFromMetadata::find(&normalization.directives)
9797
{
9898
let source_name = derived_from_metadata.0;
99-
let source_fragment = programs
100-
.source
101-
.fragment(source_name)
102-
.expect("Expected the source document for the SplitOperation to exist.");
10399
let source_hash = source_hashes.get(&source_name.into()).cloned().unwrap();
104100

105101
return generate_normalization_artifact(
@@ -108,24 +104,18 @@ pub fn generate_artifacts(
108104
project_config,
109105
&operations,
110106
source_hash,
111-
source_fragment.name.location.source_location(),
112107
)
113108
} else if let Some(client_edges_directive) =
114109
ClientEdgeGeneratedQueryMetadataDirective::find(&normalization.directives)
115110
{
116111
let source_name = client_edges_directive.source_name.item;
117-
let source_file = client_edges_directive
118-
.source_name
119-
.location
120-
.source_location();
121112
let source_hash = source_hashes.get(&source_name).cloned().unwrap();
122113
return generate_normalization_artifact(
123114
&mut operation_printer,
124115
ArtifactSourceKey::ExecutableDefinition(source_name),
125116
project_config,
126117
&operations,
127118
source_hash,
128-
source_file,
129119
)
130120
} else {
131121
let source_hash = source_hashes
@@ -138,7 +128,6 @@ pub fn generate_artifacts(
138128
project_config,
139129
&operations,
140130
source_hash,
141-
normalization.name.location.source_location(),
142131
)
143132
}
144133
} else if let Some(reader) = operations.reader {
@@ -158,7 +147,6 @@ pub fn generate_artifacts(
158147
project_config,
159148
&operations,
160149
source_hash,
161-
reader.name.location.source_location(),
162150
)
163151
}
164152
}
@@ -208,7 +196,6 @@ fn generate_normalization_artifact(
208196
project_config: &ProjectConfig,
209197
operations: &OperationGroup<'_>,
210198
source_hash: String,
211-
source_file: SourceLocationKey,
212199
) -> Artifact {
213200
let text = operations
214201
.operation_text
@@ -220,7 +207,7 @@ fn generate_normalization_artifact(
220207

221208
Artifact {
222209
artifact_source_keys: vec![artifact_source],
223-
path: project_config.path_for_artifact(source_file, normalization.name.item.0),
210+
path: project_config.artifact_path_for_definition(normalization.name),
224211
content: ArtifactContent::Operation {
225212
normalization_operation: Arc::clone(normalization),
226213
reader_operation: operations.expect_reader(),
@@ -238,15 +225,14 @@ fn generate_updatable_query_artifact(
238225
project_config: &ProjectConfig,
239226
operations: &OperationGroup<'_>,
240227
source_hash: String,
241-
source_file: SourceLocationKey,
242228
) -> Artifact {
243229
let reader = operations
244230
.reader
245231
.expect("Updatable operations must have a reader entry.");
246232

247233
Artifact {
248234
artifact_source_keys: vec![artifact_source],
249-
path: project_config.path_for_artifact(source_file, reader.name.item.0),
235+
path: project_config.artifact_path_for_definition(reader.name),
250236
content: ArtifactContent::UpdatableQuery {
251237
reader_operation: operations.expect_reader(),
252238
typegen_operation: operations.expect_typegen(),
@@ -270,8 +256,7 @@ fn generate_reader_artifact(
270256
.expect("a type fragment should be generated for this fragment");
271257
Artifact {
272258
artifact_source_keys,
273-
path: project_config
274-
.path_for_artifact(reader_fragment.name.location.source_location(), name.0),
259+
path: project_config.artifact_path_for_definition(reader_fragment.name),
275260
content: ArtifactContent::Fragment {
276261
reader_fragment: Arc::clone(reader_fragment),
277262
typegen_fragment: Arc::clone(typegen_fragment),

compiler/crates/relay-config/src/project_config.rs

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::fmt;
99
use std::path::Path;
1010
use std::path::PathBuf;
1111
use std::path::MAIN_SEPARATOR;
12-
use std::str::FromStr;
1312
use std::sync::Arc;
1413
use std::usize;
1514

@@ -23,7 +22,6 @@ use fnv::FnvBuildHasher;
2322
use indexmap::IndexMap;
2423
use intern::string_key::Intern;
2524
use intern::string_key::StringKey;
26-
use intern::Lookup;
2725
use regex::Regex;
2826
use serde::de::Error;
2927
use serde::Deserialize;
@@ -365,68 +363,59 @@ impl ProjectConfig {
365363
}
366364
}
367365

368-
pub fn path_for_artifact(
366+
pub fn artifact_path_for_definition(
369367
&self,
370-
source_file: SourceLocationKey,
371-
definition_name: StringKey,
368+
definition_name: WithLocation<impl Into<StringKey>>,
372369
) -> PathBuf {
370+
let source_location = definition_name.location.source_location();
371+
let artifact_name = definition_name.item.into();
373372
let filename = if let Some(filename_for_artifact) = &self.filename_for_artifact {
374-
filename_for_artifact(source_file, definition_name)
373+
filename_for_artifact(source_location, artifact_name)
375374
} else {
376375
match &self.typegen_config.language {
377376
TypegenLanguage::Flow | TypegenLanguage::JavaScript => {
378-
format!("{}.graphql.js", definition_name)
377+
format!("{}.graphql.js", artifact_name)
379378
}
380-
TypegenLanguage::TypeScript => format!("{}.graphql.ts", definition_name),
379+
TypegenLanguage::TypeScript => format!("{}.graphql.ts", artifact_name),
381380
}
382381
};
383-
self.create_path_for_artifact(source_file, filename)
382+
self.create_path_for_artifact(source_location, filename)
384383
}
385384

386-
/// Generates a relative import path in Common JS projects, and a module name in Haste projects.
387-
pub fn js_module_import_path(
385+
/// Generates identifier for importing module at `target_module_path` from module at `importing_artifact_path`.
386+
/// Import Identifier is a relative path in CommonJS projects and a module name in Haste projects.
387+
pub fn js_module_import_identifier(
388388
&self,
389-
definition_source_location: WithLocation<StringKey>,
390-
target_module: StringKey,
389+
importing_artifact_path: &PathBuf,
390+
target_module_path: &PathBuf,
391391
) -> StringKey {
392392
match self.js_module_format {
393393
JsModuleFormat::CommonJS => {
394-
let definition_artifact_location = self.path_for_artifact(
395-
definition_source_location.location.source_location(),
396-
definition_source_location.item,
397-
);
398-
399-
let module_location =
400-
PathBuf::from_str(target_module.lookup()).unwrap_or_else(|_| {
401-
panic!(
402-
"expected to be able to build a path from target_module : {}",
403-
target_module.lookup()
404-
);
405-
});
406-
407-
let module_path = module_location.parent().unwrap_or_else(||{
394+
let importing_artifact_directory = importing_artifact_path.parent().unwrap_or_else(||{
408395
panic!(
409-
"expected module_location: {:?} to have a parent path, maybe it's not a file?",
410-
module_location
396+
"expected importing_artifact_path: {:?} to have a parent path, maybe it's not a file?",
397+
importing_artifact_path
411398
);
412399
});
413-
414-
let definition_artifact_location_path = definition_artifact_location.parent().unwrap_or_else(||{panic!("expected definition_artifact_location: {:?} to have a parent path, maybe it's not a file?", definition_artifact_location);
415-
});
416-
417-
let resolver_module_location =
418-
pathdiff::diff_paths(module_path, definition_artifact_location_path).unwrap();
419-
420-
let module_file_name = module_location.file_name().unwrap_or_else(|| {
400+
let target_module_directory = target_module_path.parent().unwrap_or_else(||{
401+
panic!(
402+
"expected target_module_path: {:?} to have a parent path, maybe it's not a file?",
403+
target_module_path
404+
);
405+
});
406+
let target_module_file_name = target_module_path.file_name().unwrap_or_else(|| {
421407
panic!(
422-
"expected module_location: {:?} to have a file name",
423-
module_location
408+
"expected target_module_path: {:?} to have a file name",
409+
target_module_path
424410
)
425411
});
412+
let relative_path =
413+
pathdiff::diff_paths(target_module_directory, importing_artifact_directory)
414+
.unwrap();
426415

427-
format_normalized_path(&resolver_module_location.join(module_file_name)).intern()
416+
format_normalized_path(&relative_path.join(target_module_file_name)).intern()
428417
}
429-
JsModuleFormat::Haste => Path::new(&target_module.to_string())
418+
JsModuleFormat::Haste => target_module_path
430419
.file_stem()
431420
.unwrap()
432421
.to_string_lossy()

compiler/crates/relay-transforms/src/match_/split_module_import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl Transformer for SplitModuleImportTransform<'_, '_> {
142142
},
143143
OperationDefinition {
144144
name: WithLocation::new(
145-
module_metadata.location,
145+
module_metadata.fragment_source_location,
146146
OperationDefinitionName(normalization_name),
147147
),
148148
type_: parent_type,

0 commit comments

Comments
 (0)