Skip to content

Commit

Permalink
feat(biome_graphql_parser): parse fragment definition (#2506)
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 authored Apr 22, 2024
1 parent 4847ab1 commit a094dac
Show file tree
Hide file tree
Showing 8 changed files with 722 additions and 17 deletions.
50 changes: 50 additions & 0 deletions crates/biome_graphql_parser/src/parser/definitions/fragment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::parser::{
directive::DirectiveList,
parse_error::{expected_name, expected_named_type},
parse_name,
r#type::parse_named_type,
GraphqlParser,
};
use biome_graphql_syntax::{GraphqlSyntaxKind::*, T};
use biome_parser::{
parse_lists::ParseNodeList, parsed_syntax::ParsedSyntax, prelude::ParsedSyntax::*,
CompletedMarker, Parser,
};

use super::operation::parse_selection_set;

#[inline]
pub(crate) fn parse_fragment_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_fragment_definition(p) {
return Absent;
}

let m = p.start();
p.bump(T![fragment]);

parse_name(p).or_add_diagnostic(p, expected_name);
parse_type_condition(p);

DirectiveList.parse_list(p);
parse_selection_set(p).ok();

Present(m.complete(p, GRAPHQL_FRAGMENT_DEFINITION))
}

#[inline]
pub(crate) fn parse_type_condition(p: &mut GraphqlParser) -> CompletedMarker {
let m = p.start();
p.expect(T![on]);
parse_named_type(p).or_add_diagnostic(p, expected_named_type);
m.complete(p, GRAPHQL_TYPE_CONDITION)
}

#[inline]
pub(crate) fn is_at_fragment_definition(p: &GraphqlParser<'_>) -> bool {
p.at(T![fragment])
}

#[inline]
pub(crate) fn is_at_type_condition(p: &GraphqlParser<'_>) -> bool {
p.at(T![on])
}
19 changes: 13 additions & 6 deletions crates/biome_graphql_parser/src/parser/definitions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod fragment;
mod operation;

use crate::parser::{parse_error::expected_any_definition, GraphqlParser};
Expand All @@ -7,7 +8,10 @@ use biome_parser::{
prelude::ParsedSyntax::*, Parser,
};

use self::operation::{is_at_operation, parse_operation_definition};
use self::{
fragment::{is_at_fragment_definition, parse_fragment_definition},
operation::{is_at_operation, parse_operation_definition},
};
pub(crate) use operation::is_at_selection_set_end;

struct DefinitionListParseRecovery;
Expand Down Expand Up @@ -50,15 +54,18 @@ impl ParseNodeList for DefinitionList {

#[inline]
fn parse_definition(p: &mut GraphqlParser) -> ParsedSyntax {
match p.cur() {
// TODO: parse any definition
_ if is_at_operation(p) => parse_operation_definition(p),
_ => Absent,
// TODO: parse any definition
if is_at_operation(p) {
parse_operation_definition(p)
} else if is_at_fragment_definition(p) {
parse_fragment_definition(p)
} else {
Absent
}
}

#[inline]
fn is_at_definition(p: &GraphqlParser<'_>) -> bool {
// TODO: recover at any definition
is_at_operation(p)
is_at_operation(p) || is_at_fragment_definition(p)
}
18 changes: 9 additions & 9 deletions crates/biome_graphql_parser/src/parser/definitions/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::parser::{
directive::{is_at_directive, DirectiveList},
is_at_name,
parse_error::{
expected_any_selection, expected_name, expected_named_type, expected_type, expected_value,
expected_any_selection, expected_name, expected_type, expected_value,
expected_variable_definition,
},
parse_name,
r#type::{parse_named_type, parse_type},
r#type::parse_type,
value::parse_value,
variable::{is_at_variable, parse_variable},
GraphqlParser,
Expand All @@ -21,7 +21,10 @@ use biome_parser::{
prelude::ParsedSyntax::*, token_set, Parser, TokenSet,
};

use super::is_at_definition;
use super::{
fragment::{is_at_type_condition, parse_type_condition},
is_at_definition,
};

const OPERATION_TYPE: TokenSet<GraphqlSyntaxKind> =
token_set![T![query], T![mutation], T![subscription]];
Expand Down Expand Up @@ -136,7 +139,7 @@ pub(crate) fn parse_operation_definition(p: &mut GraphqlParser) -> ParsedSyntax
}

#[inline]
fn parse_selection_set(p: &mut GraphqlParser) -> ParsedSyntax {
pub(crate) fn parse_selection_set(p: &mut GraphqlParser) -> ParsedSyntax {
let m = p.start();
p.expect(T!['{']);
SelectionList.parse_list(p);
Expand Down Expand Up @@ -204,11 +207,8 @@ fn parse_fragment(p: &mut GraphqlParser) -> ParsedSyntax {
DirectiveList.parse_list(p);
Present(m.complete(p, GRAPHQL_FRAGMENT_SPREAD))
} else {
if p.at(T![on]) {
let m = p.start();
p.bump(T![on]);
parse_named_type(p).or_add_diagnostic(p, expected_named_type);
m.complete(p, GRAPHQL_TYPE_CONDITION);
if is_at_type_condition(p) {
parse_type_condition(p);
}
DirectiveList.parse_list(p);
parse_selection_set(p).ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fragmen friendFields on User {
id
name
}

fragment friendFields User @deprecated {
id
name
}

fragment friendFields o User @deprecated {
id
name
}
Loading

0 comments on commit a094dac

Please sign in to comment.