Skip to content

Commit

Permalink
Merge pull request #2 from anmonteiro/anmonteiro/reson-support
Browse files Browse the repository at this point in the history
Add support for Reason / OCaml
  • Loading branch information
zth authored May 30, 2022
2 parents 416be1b + 8afe900 commit 0fbb3e5
Show file tree
Hide file tree
Showing 12 changed files with 1,274 additions and 7 deletions.
39 changes: 34 additions & 5 deletions compiler/crates/extract-graphql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ impl<'a> Iterator for CharReader<'a> {
// This should work for Flow or TypeScript alike.
pub fn extract(input: &str) -> Vec<JavaScriptSourceFeature> {
let mut res = Vec::new();
if !input.contains("%relay(") && !input.contains("@RelayResolver") {
if !input.contains("%relay") && !input.contains("@RelayResolver") {
return res;
}
let mut it = CharReader::new(input);
'code: while let Some((i, c)) = it.next() {
match c {
'%' => {
for expected in ['r', 'e', 'l', 'a', 'y', '('] {
for expected in ['r', 'e', 'l', 'a', 'y'] {
if let Some((_, c)) = it.next() {
if c != expected {
consume_identifier(&mut it);
Expand All @@ -106,15 +106,44 @@ pub fn extract(input: &str) -> Vec<JavaScriptSourceFeature> {

let mut whitespace_num: usize = 0;

// ReScript uses %relay(` ... `)
// Reason / OCaml use [%relay {| ... |}]
let expected_close_char: char;

loop {
if let Some((_, c)) = it.next() {
match c {
'`' => {
break;
'(' => {
// ReScript
if let Some((_, c)) = it.next() {
match c {
'`' => {
expected_close_char = '`';
break;
}
_ => {
consume_identifier(&mut it);
continue 'code;
}
}
}
}
' ' | '\n' | '\r' | '\t' => {
whitespace_num += 1;
}
'{' => {
if let Some((_, c)) = it.next() {
match c {
'|' => {
expected_close_char = '|';
break
}
_ => {
continue 'code;
}
}
}
}
_ => {
consume_identifier(&mut it);
continue 'code;
Expand All @@ -128,7 +157,7 @@ pub fn extract(input: &str) -> Vec<JavaScriptSourceFeature> {
let mut has_visited_first_char = false;
for (i, c) in &mut it {
match c {
'`' => {
c if c == expected_close_char => {
let end = i;
let text = &input[start + (8 + whitespace_num)..end];
res.push(JavaScriptSourceFeature::GraphQL(GraphQLSource::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ fn is_source_code_extension(extension: &OsStr) -> bool {
|| extension == "ts"
|| extension == "tsx"
|| extension == "res"
|| extension == "re"
|| extension == "ml"
}

fn is_schema_extension(extension: &OsStr) -> bool {
Expand All @@ -352,7 +354,7 @@ fn is_extra_extensions(extension: &OsStr) -> bool {

fn is_valid_source_code_extension(typegen_language: &TypegenLanguage, extension: &OsStr) -> bool {
match typegen_language {
TypegenLanguage::ReScript => extension == "res",
TypegenLanguage::ReScript => extension == "res" || extension == "re" || extension == "ml",
TypegenLanguage::TypeScript => is_source_code_extension(extension),
TypegenLanguage::Flow | TypegenLanguage::JavaScript => {
extension == "js" || extension == "jsx"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ fn get_expected_file_extensions(config: &Config) -> HashSet<&str> {
match project.typegen_config.language {
TypegenLanguage::ReScript => {
file_extensions.insert("res");
file_extensions.insert("re");
file_extensions.insert("ml");
}
TypegenLanguage::Flow | TypegenLanguage::JavaScript => {
file_extensions.insert("js");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub fn get_watchman_expr(config: &Config) -> Expr {
Expr::All(vec![
// Ending in *.js(x) or *.ts(x) depending on the project language.
Expr::Suffix(match &project.typegen_config.language {
TypegenLanguage::ReScript => vec![PathBuf::from("res")],
TypegenLanguage::ReScript => {
vec![PathBuf::from("res"), PathBuf::from("re"), PathBuf::from("ml")]
}
TypegenLanguage::Flow | TypegenLanguage::JavaScript => {
vec![PathBuf::from("js"), PathBuf::from("jsx")]
}
Expand Down
29 changes: 29 additions & 0 deletions compiler/test-project-res/src/Test_ml_query.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Query = [%relay {|
query TestMlQuery($status: OnlineStatus) {
users(status: $status) {
edges {
node {
id
firstName
onlineStatus
}
}
}
}
|}]

module QueryWithRequired = [%relay {|
query TestMlQueryWithRequiredQuery {
loggedInUser {
avatarUrl @required(action: NONE)
}
}
|}]

module QueryWithRequired_BubbleToTop = [%relay {|
query TestMlQueryWithRequired_BubbleToTop_Query {
loggedInUser @required(action: NONE) {
avatarUrl @required(action: NONE)
}
}
|}]
29 changes: 29 additions & 0 deletions compiler/test-project-res/src/Test_re_query.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Query = [%relay {|
query TestReQuery($status: OnlineStatus) {
users(status: $status) {
edges {
node {
id
firstName
onlineStatus
}
}
}
}
|}];

module QueryWithRequired = [%relay {|
query TestReQueryWithRequiredQuery {
loggedInUser {
avatarUrl @required(action: NONE)
}
}
|}];

module QueryWithRequired_BubbleToTop = [%relay {|
query TestReQueryWithRequired_BubbleToTop_Query {
loggedInUser @required(action: NONE) {
avatarUrl @required(action: NONE)
}
}
|}];

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0fbb3e5

Please sign in to comment.