Skip to content

Commit

Permalink
refactor(biome_graphql_parser): remove is_at_*_definition (#2841)
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 authored May 17, 2024
1 parent 2094856 commit b1e7be6
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 148 deletions.
12 changes: 1 addition & 11 deletions crates/biome_graphql_parser/src/parser/definitions/directive.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::parser::{
parse_description,
parse_error::{expected_directive_location, expected_name},
parse_name,
value::is_at_string,
GraphqlParser,
parse_name, GraphqlParser,
};
use biome_graphql_syntax::{
GraphqlSyntaxKind::{self, *},
Expand Down Expand Up @@ -41,9 +39,6 @@ const DIRECTIVE_LOCATION_SET: TokenSet<GraphqlSyntaxKind> = token_set!(

#[inline]
pub(crate) fn parse_directive_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_directive_definition(p) {
return Absent;
}
let m = p.start();

// description is optional
Expand Down Expand Up @@ -133,8 +128,3 @@ fn parse_directive_location(p: &mut GraphqlParser) -> ParsedSyntax {
p.bump_ts(DIRECTIVE_LOCATION_SET);
Present(m.complete(p, GRAPHQL_DIRECTIVE_LOCATION))
}

#[inline]
pub(crate) fn is_at_directive_definition(p: &mut GraphqlParser) -> bool {
p.at(T![directive]) || (is_at_string(p) && p.nth_at(1, T![directive]))
}
8 changes: 0 additions & 8 deletions crates/biome_graphql_parser/src/parser/definitions/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ use biome_parser::{

#[inline]
pub(crate) fn parse_enum_type_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_enum_type_definition(p) {
return Absent;
}
let m = p.start();

// description is optional
Expand Down Expand Up @@ -106,11 +103,6 @@ pub(crate) fn parse_enum_value_definition(p: &mut GraphqlParser) -> ParsedSyntax
Present(m.complete(p, GRAPHQL_ENUM_VALUE_DEFINITION))
}

#[inline]
pub(crate) fn is_at_enum_type_definition(p: &mut GraphqlParser) -> bool {
p.at(T![enum]) || (is_at_string(p) && p.nth_at(1, T![enum]))
}

/// Either a `{`, `|`, or a non kw name token must be present, else this is
/// likely the start of a new type definition
#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ 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]);

Expand All @@ -42,11 +38,6 @@ pub(crate) fn parse_type_condition(p: &mut GraphqlParser) -> CompletedMarker {
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])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::{
directive::DirectiveList, parse_description, parse_error::expected_name, parse_name,
value::is_at_string, GraphqlParser,
GraphqlParser,
};
use biome_graphql_syntax::{
GraphqlSyntaxKind::{self, *},
Expand All @@ -15,9 +15,6 @@ use super::field::{is_at_input_value_definition, parse_input_value_definition};

#[inline]
pub(crate) fn parse_input_object_type_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_input_object_type_definition(p) {
return Absent;
}
let m = p.start();

// description is optional
Expand Down Expand Up @@ -87,11 +84,6 @@ impl ParseRecovery for InputFieldListParseRecovery {
}
}

#[inline]
pub(crate) fn is_at_input_object_type_definition(p: &mut GraphqlParser) -> bool {
p.at(T![input]) || (is_at_string(p) && p.nth_at(1, T![input]))
}

#[inline]
fn is_at_input_fields_definition(p: &mut GraphqlParser) -> bool {
p.at(T!['{'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::parser::{
parse_error::{expected_name, expected_named_type},
parse_name,
r#type::parse_named_type,
value::is_at_string,
GraphqlParser,
};
use biome_graphql_syntax::{
Expand All @@ -23,9 +22,6 @@ use super::{

#[inline]
pub(super) fn parse_interface_type_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_interface_type_definition(p) {
return Absent;
}
let m = p.start();

// description is optional
Expand Down Expand Up @@ -118,11 +114,6 @@ impl ParseRecovery for ImplementsInterfaceListParseRecovery {
}
}

#[inline]
pub(super) fn is_at_interface_type_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![interface]) || (is_at_string(p) && p.nth_at(1, T![interface]))
}

#[inline]
fn is_at_implements_interface(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![implements])
Expand Down
90 changes: 47 additions & 43 deletions crates/biome_graphql_parser/src/parser/definitions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,30 @@ mod schema;
mod union;

use crate::parser::{parse_error::expected_any_definition, GraphqlParser};
use biome_graphql_syntax::GraphqlSyntaxKind::{self, *};
use biome_graphql_syntax::{
GraphqlSyntaxKind::{self, *},
T,
};
use biome_parser::{
parse_lists::ParseNodeList, parse_recovery::ParseRecovery, parsed_syntax::ParsedSyntax,
prelude::ParsedSyntax::*, Parser,
};

use self::{
directive::{is_at_directive_definition, parse_directive_definition},
fragment::{is_at_fragment_definition, parse_fragment_definition},
input_object::{is_at_input_object_type_definition, parse_input_object_type_definition},
interface::{is_at_interface_type_definition, parse_interface_type_definition},
object::{is_at_object_type_definition, parse_object_type_definition},
operation::{is_at_operation, parse_operation_definition},
r#enum::{is_at_enum_type_definition, parse_enum_type_definition},
scalar::{is_at_scalar_type_definition, parse_scalar_type_definition},
schema::{is_at_schema_definition, parse_schema_definition},
union::{is_at_union_type_definition, parse_union_type_definition},
directive::parse_directive_definition,
fragment::parse_fragment_definition,
input_object::parse_input_object_type_definition,
interface::parse_interface_type_definition,
object::parse_object_type_definition,
operation::{parse_operation_definition, parse_selection_set},
r#enum::parse_enum_type_definition,
scalar::parse_scalar_type_definition,
schema::parse_schema_definition,
union::parse_union_type_definition,
};

use super::value::is_at_string;

struct DefinitionListParseRecovery;

impl ParseRecovery for DefinitionListParseRecovery {
Expand Down Expand Up @@ -70,41 +75,40 @@ impl ParseNodeList for DefinitionList {

#[inline]
fn parse_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if is_at_operation(p) {
parse_operation_definition(p)
} else if is_at_fragment_definition(p) {
parse_fragment_definition(p)
} else if is_at_schema_definition(p) {
parse_schema_definition(p)
} else if is_at_scalar_type_definition(p) {
parse_scalar_type_definition(p)
} else if is_at_object_type_definition(p) {
parse_object_type_definition(p)
} else if is_at_interface_type_definition(p) {
parse_interface_type_definition(p)
} else if is_at_union_type_definition(p) {
parse_union_type_definition(p)
} else if is_at_enum_type_definition(p) {
parse_enum_type_definition(p)
} else if is_at_input_object_type_definition(p) {
parse_input_object_type_definition(p)
} else if is_at_directive_definition(p) {
parse_directive_definition(p)
} else {
Absent
let keyword = if is_at_string(p) { p.nth(1) } else { p.cur() };
match keyword {
T![query] | T![mutation] | T![subscription] => parse_operation_definition(p),
T!['{'] => parse_selection_set(p),
T![fragment] => parse_fragment_definition(p),
T![schema] => parse_schema_definition(p),
T![scalar] => parse_scalar_type_definition(p),
T![type] => parse_object_type_definition(p),
T![interface] => parse_interface_type_definition(p),
T![union] => parse_union_type_definition(p),
T![enum] => parse_enum_type_definition(p),
T![input] => parse_input_object_type_definition(p),
T![directive] => parse_directive_definition(p),
_ => Absent,
}
}

#[inline]
fn is_at_definition(p: &mut GraphqlParser<'_>) -> bool {
is_at_operation(p)
|| is_at_fragment_definition(p)
|| is_at_schema_definition(p)
|| is_at_scalar_type_definition(p)
|| is_at_object_type_definition(p)
|| is_at_interface_type_definition(p)
|| is_at_union_type_definition(p)
|| is_at_enum_type_definition(p)
|| is_at_input_object_type_definition(p)
|| is_at_directive_definition(p)
let keyword = if is_at_string(p) { p.nth(1) } else { p.cur() };
matches!(
keyword,
T![query]
| T![mutation]
| T![subscription]
| T!['{']
| T![fragment]
| T![schema]
| T![scalar]
| T![type]
| T![interface]
| T![union]
| T![enum]
| T![input]
| T![directive]
)
}
10 changes: 1 addition & 9 deletions crates/biome_graphql_parser/src/parser/definitions/object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::{
directive::DirectiveList, parse_description, parse_error::expected_name, parse_name,
value::is_at_string, GraphqlParser,
GraphqlParser,
};
use biome_graphql_syntax::{GraphqlSyntaxKind::*, T};
use biome_parser::{
Expand All @@ -11,9 +11,6 @@ use super::{field::parse_fields_definition, interface::parse_implements_interfac

#[inline]
pub(crate) fn parse_object_type_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_object_type_definition(p) {
return Absent;
}
let m = p.start();

// description is optional
Expand All @@ -32,8 +29,3 @@ pub(crate) fn parse_object_type_definition(p: &mut GraphqlParser) -> ParsedSynta

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

#[inline]
pub(crate) fn is_at_object_type_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![type]) || (is_at_string(p) && p.nth_at(1, T![type]))
}
28 changes: 5 additions & 23 deletions crates/biome_graphql_parser/src/parser/definitions/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,6 @@ impl ParseRecovery for VariableDefinitionListParseRecovery {
/// https://spec.graphql.org/October2021/#sec-Language.Operations.Query-shorthand
#[inline]
pub(crate) fn parse_operation_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_operation(p) {
return Absent;
}

if is_at_selection_set(p) {
return parse_selection_set(p);
}

let m = p.start();
{
let m = p.start();
Expand Down Expand Up @@ -187,7 +179,7 @@ fn parse_field(p: &mut GraphqlParser) -> ParsedSyntax {
parse_arguments(p).ok();
DirectiveList.parse_list(p);

if is_at_selection_set(p) {
if p.at(T!['{']) {
parse_selection_set(p).ok();
}
Present(m.complete(p, GRAPHQL_FIELD))
Expand Down Expand Up @@ -255,11 +247,6 @@ fn parse_variable_definition(p: &mut GraphqlParser) -> ParsedSyntax {
Present(m.complete(p, GRAPHQL_VARIABLE_DEFINITION))
}

#[inline]
pub(crate) fn is_at_operation(p: &GraphqlParser<'_>) -> bool {
p.at_ts(OPERATION_TYPE) || is_at_selection_set(p)
}

#[inline]
fn is_at_variable_definitions(p: &mut GraphqlParser) -> bool {
p.at(T!['('])
Expand All @@ -269,7 +256,10 @@ fn is_at_variable_definitions(p: &mut GraphqlParser) -> bool {

#[inline]
fn is_at_variable_definitions_end(p: &GraphqlParser) -> bool {
p.at(T![')']) || is_at_directive(p) || is_at_selection_set(p)
p.at(T![')'])
|| is_at_directive(p)
// At the start of a selection set
|| p.at(T!('{'))
}

#[inline]
Expand All @@ -283,14 +273,6 @@ fn is_at_variable_definition(p: &mut GraphqlParser) -> bool {
|| p.at(T![:])
}

// we must enforce that a selection set starts with a curly brace
// otherwise it would be too complex to determine if we are at a selection set,
// especially when we have nested selections
#[inline]
fn is_at_selection_set(p: &GraphqlParser) -> bool {
p.at(T!['{'])
}

// Since keywords are valid names, we could only be sure that we are at the end
// of a selection set if we are at a closing curly brace
#[inline]
Expand Down
10 changes: 1 addition & 9 deletions crates/biome_graphql_parser/src/parser/definitions/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::{
directive::DirectiveList, parse_description, parse_error::expected_name, parse_name,
value::is_at_string, GraphqlParser,
GraphqlParser,
};
use biome_graphql_syntax::{GraphqlSyntaxKind::*, T};
use biome_parser::{
Expand All @@ -9,9 +9,6 @@ use biome_parser::{

#[inline]
pub(crate) fn parse_scalar_type_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_scalar_type_definition(p) {
return Absent;
}
let m = p.start();
// description is optional
parse_description(p).ok();
Expand All @@ -23,8 +20,3 @@ pub(crate) fn parse_scalar_type_definition(p: &mut GraphqlParser) -> ParsedSynta

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

#[inline]
pub(crate) fn is_at_scalar_type_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![scalar]) || (is_at_string(p) && p.nth_at(1, T![scalar]))
}
9 changes: 0 additions & 9 deletions crates/biome_graphql_parser/src/parser/definitions/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::parser::{
expected_named_type, expected_operation_type, expected_root_operation_type_definition,
},
r#type::parse_named_type,
value::is_at_string,
GraphqlParser,
};
use biome_graphql_syntax::{
Expand All @@ -21,9 +20,6 @@ use super::{is_at_definition, operation::OPERATION_TYPE};

#[inline]
pub(crate) fn parse_schema_definition(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_schema_definition(p) {
return Absent;
}
let m = p.start();
// description is optional
parse_description(p).ok();
Expand Down Expand Up @@ -106,11 +102,6 @@ fn parse_root_operation_type_definition(p: &mut GraphqlParser) -> ParsedSyntax {
Present(m.complete(p, GRAPHQL_ROOT_OPERATION_TYPE_DEFINITION))
}

#[inline]
pub(crate) fn is_at_schema_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at(T![schema]) || (is_at_string(p) && p.nth_at(1, T![schema]))
}

#[inline]
fn is_at_root_operation_type_definition(p: &mut GraphqlParser<'_>) -> bool {
p.at_ts(OPERATION_TYPE)
Expand Down
Loading

0 comments on commit b1e7be6

Please sign in to comment.