Skip to content

Commit 81470c9

Browse files
marcoww6meta-codesync[bot]
authored andcommitted
Add feature flag and codegen for ReadonlyArray
Reviewed By: captbaritone Differential Revision: D86834414 fbshipit-source-id: 06eab97694a4fa140e951e03a722fd5ac4d19869
1 parent 111c07b commit 81470c9

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

compiler/crates/common/src/feature_flags.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ pub struct FeatureFlags {
186186
/// be null in normal operation).
187187
#[serde(default)]
188188
pub disallow_required_action_throw_on_semantically_nullable_fields: FeatureFlag,
189+
190+
/// Use ReadonlyArray<T> instead of $ReadOnlyArray<T> for Flow typegen.
191+
/// This enables gradual rollout of the new array type across files.
192+
#[serde(default)]
193+
pub readonly_array_for_flow: FeatureFlag,
189194
}
190195

191196
impl Default for FeatureFlags {
@@ -218,6 +223,7 @@ impl Default for FeatureFlags {
218223
disable_deduping_common_structures_in_artifacts: Default::default(),
219224
legacy_include_path_in_required_reader_nodes: Default::default(),
220225
disallow_required_action_throw_on_semantically_nullable_fields: Default::default(),
226+
readonly_array_for_flow: Default::default(),
221227

222228
// enabled-by-default
223229
enforce_fragment_alias_where_ambiguous: FeatureFlag::Enabled,

compiler/crates/relay-compiler/relay-compiler-config-schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,13 @@
694694
"type": "boolean",
695695
"default": false
696696
},
697+
"readonly_array_for_flow": {
698+
"description": "Use ReadonlyArray<T> instead of $ReadOnlyArray<T> for Flow typegen.\nThis enables gradual rollout of the new array type across files.",
699+
"$ref": "#/$defs/FeatureFlag",
700+
"default": {
701+
"kind": "disabled"
702+
}
703+
},
697704
"relay_resolver_enable_interface_output_type": {
698705
"description": "Enable returning interfaces from Relay Resolvers without @outputType",
699706
"$ref": "#/$defs/FeatureFlag",
@@ -930,6 +937,9 @@
930937
"kind": "disabled"
931938
},
932939
"prefer_fetchable_in_refetch_queries": false,
940+
"readonly_array_for_flow": {
941+
"kind": "disabled"
942+
},
933943
"relay_resolver_enable_interface_output_type": {
934944
"kind": "disabled"
935945
},

compiler/crates/relay-typegen/src/flow.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::writer::Writer;
2020
pub struct FlowPrinter {
2121
result: String,
2222
indentation: usize,
23+
use_readonly_array_rollout: bool,
2324
}
2425

2526
impl Write for FlowPrinter {
@@ -152,10 +153,11 @@ impl Writer for FlowPrinter {
152153
}
153154

154155
impl FlowPrinter {
155-
pub fn new() -> Self {
156+
pub fn new(use_readonly_array: impl Into<Option<bool>>) -> Self {
156157
Self {
157158
result: String::new(),
158159
indentation: 0,
160+
use_readonly_array_rollout: use_readonly_array.into().unwrap_or(false),
159161
}
160162
}
161163

@@ -198,7 +200,11 @@ impl FlowPrinter {
198200
}
199201

200202
fn write_read_only_array(&mut self, of_type: &AST) -> FmtResult {
201-
write!(&mut self.result, "$ReadOnlyArray<")?;
203+
if self.use_readonly_array_rollout {
204+
write!(&mut self.result, "ReadonlyArray<")?;
205+
} else {
206+
write!(&mut self.result, "$ReadOnlyArray<")?;
207+
}
202208
self.write(of_type)?;
203209
write!(&mut self.result, ">")
204210
}
@@ -392,7 +398,7 @@ mod tests {
392398
use crate::writer::SortedASTList;
393399

394400
fn print_type(ast: &AST) -> String {
395-
let mut printer = Box::new(FlowPrinter::new());
401+
let mut printer = Box::new(FlowPrinter::new(false));
396402
printer.write(ast).unwrap();
397403
printer.into_string()
398404
}
@@ -595,7 +601,7 @@ mod tests {
595601

596602
#[test]
597603
fn import_type() {
598-
let mut printer = Box::new(FlowPrinter::new());
604+
let mut printer = Box::new(FlowPrinter::new(false));
599605
printer.write_import_type(&["A", "B"], "module").unwrap();
600606
assert_eq!(
601607
printer.into_string(),
@@ -605,7 +611,7 @@ mod tests {
605611

606612
#[test]
607613
fn import_module() {
608-
let mut printer = Box::new(FlowPrinter::new());
614+
let mut printer = Box::new(FlowPrinter::new(false));
609615
printer.write_import_module_default("A", "module").unwrap();
610616
assert_eq!(printer.into_string(), "import A from \"module\";\n");
611617
}

compiler/crates/relay-typegen/src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ fn generate_fragment_type_exports_section_impl(
160160
is_extra_artifact_branch_module,
161161
},
162162
);
163-
let mut writer = new_writer_from_config(&project_config.typegen_config);
163+
let use_readonly_array = project_config
164+
.feature_flags
165+
.readonly_array_for_flow
166+
.is_enabled_for(fragment_definition.name.item.0);
167+
let mut writer = new_writer_from_config(&project_config.typegen_config, use_readonly_array);
164168
write_fragment_type_exports_section(&typegen_context, fragment_definition, &mut writer)
165169
.unwrap();
166170
writer.into_string()
@@ -186,7 +190,11 @@ pub fn generate_named_validator_export(
186190
is_extra_artifact_branch_module: false,
187191
},
188192
);
189-
let mut writer = new_writer_from_config(&project_config.typegen_config);
193+
let use_readonly_array = project_config
194+
.feature_flags
195+
.readonly_array_for_flow
196+
.is_enabled_for(fragment_definition.name.item.0);
197+
let mut writer = new_writer_from_config(&project_config.typegen_config, use_readonly_array);
190198
write_validator_function(&typegen_context, fragment_definition, &mut writer).unwrap();
191199
let validator_function_body = writer.into_string();
192200

@@ -222,7 +230,11 @@ pub fn generate_operation_type_exports_section(
222230
is_extra_artifact_branch_module: false,
223231
},
224232
);
225-
let mut writer = new_writer_from_config(&project_config.typegen_config);
233+
let use_readonly_array = project_config
234+
.feature_flags
235+
.readonly_array_for_flow
236+
.is_enabled_for(typegen_operation.name.item.0);
237+
let mut writer = new_writer_from_config(&project_config.typegen_config, use_readonly_array);
226238
write_operation_type_exports_section(
227239
&typegen_context,
228240
typegen_operation,
@@ -259,7 +271,11 @@ pub fn generate_split_operation_type_exports_section(
259271
is_extra_artifact_branch_module: false,
260272
},
261273
);
262-
let mut writer = new_writer_from_config(&project_config.typegen_config);
274+
let use_readonly_array = project_config
275+
.feature_flags
276+
.readonly_array_for_flow
277+
.is_enabled_for(typegen_operation.name.item.0);
278+
let mut writer = new_writer_from_config(&project_config.typegen_config, use_readonly_array);
263279

264280
write_split_operation_type_exports_section(
265281
&typegen_context,

compiler/crates/relay-typegen/src/writer.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,13 @@ pub trait Writer: Write {
387387
fn write_any_type_definition(&mut self, name: &str) -> FmtResult;
388388
}
389389

390-
pub(crate) fn new_writer_from_config(config: &TypegenConfig) -> Box<dyn Writer> {
390+
pub(crate) fn new_writer_from_config(
391+
config: &TypegenConfig,
392+
use_readonly_array_for_flow: bool,
393+
) -> Box<dyn Writer> {
391394
match config.language {
392395
TypegenLanguage::JavaScript => Box::<JavaScriptPrinter>::default(),
393-
TypegenLanguage::Flow => Box::new(FlowPrinter::new()),
396+
TypegenLanguage::Flow => Box::new(FlowPrinter::new(use_readonly_array_for_flow)),
394397
TypegenLanguage::TypeScript => Box::new(TypeScriptPrinter::new(config)),
395398
}
396399
}

0 commit comments

Comments
 (0)