Skip to content
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
21c09e0
Fix remaining cases with hardcoded output, rather than using AST.
MaartenStaa Oct 17, 2020
18ad028
Reduce code duplication for writing AST.
MaartenStaa Oct 17, 2020
c95f333
Fix some typescript tests using Flow typing instead.
MaartenStaa Oct 17, 2020
fac60f0
Allow opting into `import type` syntax for Typescript through config.
MaartenStaa Oct 17, 2020
42fd9e2
Bring format of fragment usage, imports & exports in line with curren…
MaartenStaa Oct 21, 2020
c6150b0
For typescript, use the field name " $data" rather than "$data".
MaartenStaa Oct 24, 2020
555ac15
Handle Windows-style newlines in GraphQL lexing (\r\n).
MaartenStaa Oct 24, 2020
213bf99
Fix grouped fragment refs.
MaartenStaa Oct 24, 2020
d6575ba
Type optional and nullable fields the same as relay-compiler-language…
MaartenStaa Oct 25, 2020
7eaef79
Rename AST::OtherEnumValue to AST::OtherTypename to reflect usage mor…
MaartenStaa Oct 30, 2020
16a642a
Merge remote-tracking branch 'upstream/master' into refine-typescript…
MaartenStaa Oct 30, 2020
6483808
Create AST for type definition when not using haste.
MaartenStaa Oct 30, 2020
f0f3ed8
Don't use relative import path in haste mode.
MaartenStaa Oct 31, 2020
b0c97bf
Allow opting out of future proof enums.
MaartenStaa Oct 31, 2020
0e04a86
Add better support for abstract types (unions and interfaces), using …
MaartenStaa Nov 1, 2020
7275cf7
Handle %other for unions, to be sure the accompanying comment is incl…
MaartenStaa Nov 1, 2020
75a9a26
Some suggestions from Clippy.
MaartenStaa Nov 1, 2020
cc85020
Merge remote-tracking branch 'upstream/master' into better-support-fo…
MaartenStaa Dec 1, 2020
20c443a
Update test fixtures including combinations of different values of fu…
MaartenStaa Dec 1, 2020
69e2811
s/babel/ast
MaartenStaa Dec 27, 2020
8d19116
Fix incorrect merge/rebase: don't use intermediate String for writing…
MaartenStaa Dec 27, 2020
455d3de
Wrap nullable intersections in parentheses as well as unions.
MaartenStaa Dec 27, 2020
c28af7b
Prevent intersections of exact objects, which would not make sense.
MaartenStaa Dec 27, 2020
573d12d
Either list possible type names or "%other", but not a combination of…
MaartenStaa Dec 27, 2020
a125a39
Prevent combinational test fixture output.
MaartenStaa Dec 27, 2020
d769f2a
Add an optional JSON header to typegen fixtures for customizable type…
MaartenStaa Dec 27, 2020
ba2d375
Remove outdated #[allow] attribute.
MaartenStaa Dec 27, 2020
7317056
Merge remote-tracking branch 'upstream/master' into better-support-fo…
MaartenStaa Dec 27, 2020
09ca96b
Revert "remove unused intersection variant"
MaartenStaa Dec 27, 2020
026ab4c
Merge remote-tracking branch 'upstream/master' into better-support-fo…
Oct 19, 2021
5ebfe4d
Update usage after merge of master.
Oct 19, 2021
2070f23
Merge remote-tracking branch 'upstream/main' into better-support-for-…
Oct 19, 2021
853ac17
Fix unused enum variant: FragmentReferenceType.
Oct 19, 2021
3380f23
Merge remote-tracking branch 'upstream/main' into better-support-for-…
Jan 13, 2022
191b4ab
Fix typegen config not being set correctly in typegen tests.
Feb 14, 2022
18ad43e
Fix properties being duplicated in typegen.
Feb 14, 2022
0f9360b
Allow excluding types from future proofness.
Feb 14, 2022
a3cb206
Add Flow version of added test.
Feb 14, 2022
9386f81
Merge remote-tracking branch 'upstream/main' into better-support-for-…
Feb 14, 2022
af6e762
Fix mistake in merge.
Feb 14, 2022
90e6d24
Remove commented out code.
Feb 16, 2022
5185095
Combination of four diffs
rbalicki2 Feb 25, 2022
e4e4c92
Sort enum members.
Feb 26, 2022
c671d16
Sort fragment refs.
Feb 26, 2022
6f4e49f
Only wrap unions & intersections if needed.
Feb 26, 2022
dbbfc3f
Impl Ord for AST in typegen, sort all unions and intersections.
Mar 2, 2022
ea9ee4e
Merge remote-tracking branch 'upstream/main' into typegen-sorting-and…
Mar 2, 2022
e278990
Fix impl Writer for TypeScript: supports_exact_objects is false.
Mar 8, 2022
b3f8072
Remove AST::Intersection from typegen, as it will not be needed.
Mar 9, 2022
bb96fa9
Merge branch 'typegen-sorting-and-wrapping' into better-support-for-a…
Mar 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion compiler/crates/relay-typegen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Default for TypegenLanguage {
}
}

#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct TypegenConfig {
/// The desired output language, "flow" or "typescript".
Expand All @@ -49,6 +49,14 @@ pub struct TypegenConfig {
#[serde(default)]
pub use_import_type_syntax: bool,

/// Whether to future proof enums by including "%future added value" as a possible value.
#[serde(default = "default_future_proofness")]
pub future_proof_enums: bool,

/// Whether to future proof union and interface types by including "%other" as a possible type.
#[serde(default = "default_future_proofness")]
pub future_proof_abstract_types: bool,

/// A map from GraphQL scalar types to a custom JS type, example:
/// { "Url": "String" }
#[serde(default)]
Expand All @@ -59,3 +67,23 @@ pub struct TypegenConfig {
#[serde(default)]
pub haste: bool,
}

// Custom impl for Default to set future proofness to true when using Default::default().
impl Default for TypegenConfig {
fn default() -> Self {
Self {
language: TypegenLanguage::default(),
enum_module_suffix: None,
optional_input_fields: vec![],
use_import_type_syntax: false,
future_proof_abstract_types: default_future_proofness(),
future_proof_enums: default_future_proofness(),
custom_scalar_types: FnvHashMap::default(),
haste: false,
}
}
}

fn default_future_proofness() -> bool {
true
}
40 changes: 22 additions & 18 deletions compiler/crates/relay-typegen/src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ pub struct FlowPrinter {
}

impl Writer for FlowPrinter {
fn write_ast(&mut self, ast: &AST) -> String {
let mut writer = String::new();
self.write(&mut writer, ast)
.expect("Expected Ok result from writing Flow code");

writer
}
}

impl FlowPrinter {
pub fn new() -> Self {
Self { indentation: 0 }
}

fn write(&mut self, writer: &mut dyn Write, ast: &AST) -> Result {
match ast {
AST::Any => write!(writer, "any"),
Expand Down Expand Up @@ -61,12 +75,6 @@ impl Writer for FlowPrinter {
),
}
}
}

impl FlowPrinter {
pub fn new() -> Self {
Self { indentation: 0 }
}

fn write_indentation(&mut self, writer: &mut dyn Write) -> Result {
writer.write_str(&" ".repeat(self.indentation))
Expand Down Expand Up @@ -160,7 +168,7 @@ impl FlowPrinter {
writeln!(writer, ",")?;
continue;
}
if let AST::OtherTypename = prop.value {
if prop.value.contains_other_typename() {
writeln!(writer, "// This will never be '%other', but we need some")?;
self.write_indentation(writer)?;
writeln!(
Expand Down Expand Up @@ -214,10 +222,10 @@ impl FlowPrinter {
fn write_import_type(
&mut self,
writer: &mut dyn Write,
types: &Vec<StringKey>,
types: &[StringKey],
from: &StringKey,
) -> Result {
writeln!(
write!(
writer,
"import type {{ {} }} from \"{}\";",
types
Expand All @@ -235,7 +243,7 @@ impl FlowPrinter {
alias: &StringKey,
value: &StringKey,
) -> Result {
writeln!(writer, "declare export opaque type {}: {};", alias, value)
write!(writer, "declare export opaque type {}: {};", alias, value)
}

fn write_export_type_equals(
Expand All @@ -244,9 +252,7 @@ impl FlowPrinter {
name: &StringKey,
value: &AST,
) -> Result {
write!(writer, "export type {} = ", name)?;
self.write(writer, value)?;
writeln!(writer, ";")
write!(writer, "export type {} = {};", name, self.write_ast(value))
}

fn write_type_definition(
Expand All @@ -255,13 +261,11 @@ impl FlowPrinter {
name: &StringKey,
value: &AST,
) -> Result {
write!(writer, "type {} = ", name)?;
self.write(writer, value)?;
writeln!(writer, ";")
write!(writer, "type {} = {};", name, self.write_ast(value))
}

fn write_export_list(&mut self, writer: &mut dyn Write, names: &Vec<StringKey>) -> Result {
writeln!(
fn write_export_list(&mut self, writer: &mut dyn Write, names: &[StringKey]) -> Result {
write!(
writer,
"export type {{ {} }};",
names
Expand Down
Loading