diff --git a/graphql_parser/lib/src/language/ast/field.dart b/graphql_parser/lib/src/language/ast/field.dart index 3ac2c965..5aca6469 100644 --- a/graphql_parser/lib/src/language/ast/field.dart +++ b/graphql_parser/lib/src/language/ast/field.dart @@ -15,15 +15,17 @@ class FieldContext extends Node { @override FileSpan get span { - if (selectionSet != null) + if (selectionSet != null) { return fieldName.span.expand(selectionSet.span); - else if (directives.isNotEmpty) + } else if (directives.isNotEmpty) { return directives.fold( fieldName.span, (out, d) => out.expand(d.span)); - if (arguments.isNotEmpty) + } + if (arguments.isNotEmpty) { return arguments.fold( fieldName.span, (out, a) => out.expand(a.span)); - else + } else { return fieldName.span; + } } } diff --git a/graphql_parser/lib/src/language/ast/number_value.dart b/graphql_parser/lib/src/language/ast/number_value.dart index 8c18cfe1..d5fe010d 100644 --- a/graphql_parser/lib/src/language/ast/number_value.dart +++ b/graphql_parser/lib/src/language/ast/number_value.dart @@ -10,9 +10,9 @@ class NumberValueContext extends InputValueContext { num get numberValue { var text = NUMBER.text; - if (!text.contains('E') && !text.contains('e')) + if (!text.contains('E') && !text.contains('e')) { return num.parse(text); - else { + } else { var split = text.split(text.contains('E') ? 'E' : 'e'); var base = num.parse(split[0]); var exp = num.parse(split[1]); diff --git a/graphql_parser/lib/src/language/ast/string_value.dart b/graphql_parser/lib/src/language/ast/string_value.dart index 8789c886..da713b42 100644 --- a/graphql_parser/lib/src/language/ast/string_value.dart +++ b/graphql_parser/lib/src/language/ast/string_value.dart @@ -63,8 +63,9 @@ class StringValueContext extends InputValueContext { default: buf.writeCharCode(next); } - } else + } else { throw SyntaxError('Unexpected "\\" in string literal.', span); + } } else { buf.writeCharCode(ch); } diff --git a/graphql_parser/lib/src/language/parser.dart b/graphql_parser/lib/src/language/parser.dart index 64bd6476..9ea7d33c 100644 --- a/graphql_parser/lib/src/language/parser.dart +++ b/graphql_parser/lib/src/language/parser.dart @@ -68,9 +68,9 @@ class Parser { OperationDefinitionContext parseOperationDefinition() { var selectionSet = parseSelectionSet(); - if (selectionSet != null) + if (selectionSet != null) { return OperationDefinitionContext(null, null, null, selectionSet); - else { + } else { if (nextName('mutation') || nextName('query') || nextName('subscription')) { @@ -79,18 +79,18 @@ class Parser { var variables = parseVariableDefinitions(); var dirs = parseDirectives(); var selectionSet = parseSelectionSet(); - if (selectionSet != null) - return OperationDefinitionContext( - TYPE, NAME, variables, selectionSet) + if (selectionSet != null) { + return OperationDefinitionContext(TYPE, NAME, variables, selectionSet) ..directives.addAll(dirs); - else { + } else { errors.add(SyntaxError( 'Missing selection set in fragment definition.', NAME?.span ?? TYPE.span)); return null; } - } else + } else { return null; + } } } @@ -105,11 +105,11 @@ class Parser { if (typeCondition != null) { var dirs = parseDirectives(); var selectionSet = parseSelectionSet(); - if (selectionSet != null) + if (selectionSet != null) { return FragmentDefinitionContext( FRAGMENT, NAME, ON, typeCondition, selectionSet) ..directives.addAll(dirs); - else { + } else { errors.add(SyntaxError( 'Expected selection set in fragment definition.', typeCondition.span)); @@ -133,8 +133,9 @@ class Parser { FRAGMENT.span)); return null; } - } else + } else { return null; + } } FragmentSpreadContext parseFragmentSpread() { @@ -148,8 +149,9 @@ class Parser { _index--; return null; } - } else + } else { return null; + } } InlineFragmentContext parseInlineFragment() { @@ -184,8 +186,9 @@ class Parser { 'Missing "on" after "..." in inline fragment.', ELLIPSIS.span)); return null; } - } else + } else { return null; + } } SelectionSetContext parseSelectionSet() { @@ -201,29 +204,30 @@ class Parser { } eatCommas(); - if (next(TokenType.RBRACE)) + if (next(TokenType.RBRACE)) { return SelectionSetContext(LBRACE, current) ..selections.addAll(selections); - else { + } else { errors.add(SyntaxError('Missing "}" after selection set.', selections.isEmpty ? LBRACE.span : selections.last.span)); return null; } - } else + } else { return null; + } } SelectionContext parseSelection() { var field = parseField(); if (field != null) return SelectionContext(field); var fragmentSpread = parseFragmentSpread(); - if (fragmentSpread != null) - return SelectionContext(null, fragmentSpread); + if (fragmentSpread != null) return SelectionContext(null, fragmentSpread); var inlineFragment = parseInlineFragment(); - if (inlineFragment != null) + if (inlineFragment != null) { return SelectionContext(null, null, inlineFragment); - else + } else { return null; + } } FieldContext parseField() { @@ -235,8 +239,9 @@ class Parser { return FieldContext(fieldName, selectionSet) ..arguments.addAll(args ?? []) ..directives.addAll(directives); - } else + } else { return null; + } } FieldNameContext parseFieldName() { @@ -244,18 +249,19 @@ class Parser { var NAME1 = current; if (next(TokenType.COLON)) { var COLON = current; - if (next(TokenType.NAME)) - return FieldNameContext( - null, AliasContext(NAME1, COLON, current)); - else { - errors.add(SyntaxError( - 'Missing name after colon in alias.', COLON.span)); + if (next(TokenType.NAME)) { + return FieldNameContext(null, AliasContext(NAME1, COLON, current)); + } else { + errors.add( + SyntaxError('Missing name after colon in alias.', COLON.span)); return null; } - } else + } else { return FieldNameContext(NAME1); - } else + } + } else { return null; + } } VariableDefinitionsContext parseVariableDefinitions() { @@ -270,16 +276,17 @@ class Parser { def = parseVariableDefinition(); } - if (next(TokenType.RPAREN)) + if (next(TokenType.RPAREN)) { return VariableDefinitionsContext(LPAREN, current) ..variableDefinitions.addAll(defs); - else { + } else { errors.add(SyntaxError( 'Missing ")" after variable definitions.', LPAREN.span)); return null; } - } else + } else { return null; + } } VariableDefinitionContext parseVariableDefinition() { @@ -290,20 +297,20 @@ class Parser { var type = parseType(); if (type != null) { var defaultValue = parseDefaultValue(); - return VariableDefinitionContext( - variable, COLON, type, defaultValue); + return VariableDefinitionContext(variable, COLON, type, defaultValue); } else { - errors.add(SyntaxError( - 'Missing type in variable definition.', COLON.span)); + errors.add( + SyntaxError('Missing type in variable definition.', COLON.span)); return null; } } else { - errors.add(SyntaxError( - 'Missing ":" in variable definition.', variable.span)); + errors.add( + SyntaxError('Missing ":" in variable definition.', variable.span)); return null; } - } else + } else { return null; + } } TypeContext parseType() { @@ -314,8 +321,9 @@ class Parser { var listType = parseListType(); if (listType != null) { return TypeContext(null, listType, maybe(TokenType.EXCLAMATION)); - } else + } else { return null; + } } } @@ -334,8 +342,9 @@ class Parser { errors.add(SyntaxError('Missing type after "[".', LBRACKET.span)); return null; } - } else + } else { return null; + } } List parseDirectives() { @@ -358,10 +367,9 @@ class Parser { if (next(TokenType.COLON)) { var COLON = current; var val = parseInputValue(); - if (val != null) - return DirectiveContext( - ARROBA, NAME, COLON, null, null, null, val); - else { + if (val != null) { + return DirectiveContext(ARROBA, NAME, COLON, null, null, null, val); + } else { errors.add(SyntaxError( 'Missing value or variable in directive after colon.', COLON.span)); @@ -383,15 +391,16 @@ class Parser { SyntaxError('Missing argument in directive.', LPAREN.span)); return null; } - } else - return DirectiveContext( - ARROBA, NAME, null, null, null, null, null); + } else { + return DirectiveContext(ARROBA, NAME, null, null, null, null, null); + } } else { errors.add(SyntaxError('Missing name for directive.', ARROBA.span)); return null; } - } else + } else { return null; + } } List parseArguments() { @@ -406,15 +415,15 @@ class Parser { arg = parseArgument(); } - if (next(TokenType.RPAREN)) + if (next(TokenType.RPAREN)) { return out; - else { - errors - .add(SyntaxError('Missing ")" in argument list.', LPAREN.span)); + } else { + errors.add(SyntaxError('Missing ")" in argument list.', LPAREN.span)); return null; } - } else + } else { return []; + } } ArgumentContext parseArgument() { @@ -423,20 +432,21 @@ class Parser { if (next(TokenType.COLON)) { var COLON = current; var val = parseInputValue(); - if (val != null) + if (val != null) { return ArgumentContext(NAME, COLON, val); - else { + } else { errors.add(SyntaxError( 'Missing value or variable in argument.', COLON.span)); return null; } } else { - errors.add(SyntaxError( - 'Missing colon after name in argument.', NAME.span)); + errors.add( + SyntaxError('Missing colon after name in argument.', NAME.span)); return null; } - } else + } else { return null; + } } /// Use [parseInputValue] instead. @@ -446,16 +456,17 @@ class Parser { VariableContext parseVariable() { if (next(TokenType.DOLLAR)) { var DOLLAR = current; - if (next(TokenType.NAME)) + if (next(TokenType.NAME)) { return VariableContext(DOLLAR, current); - else { + } else { errors.add(SyntaxError( 'Missing name for variable; found a lone "\$" instead.', DOLLAR.span)); return null; } - } else + } else { return null; + } } DefaultValueContext parseDefaultValue() { @@ -465,27 +476,29 @@ class Parser { if (value != null) { return DefaultValueContext(EQUALS, value); } else { - errors - .add(SyntaxError('Missing value after "=" sign.', EQUALS.span)); + errors.add(SyntaxError('Missing value after "=" sign.', EQUALS.span)); return null; } - } else + } else { return null; + } } TypeConditionContext parseTypeCondition() { var name = parseTypeName(); - if (name != null) + if (name != null) { return TypeConditionContext(name); - else + } else { return null; + } } TypeNameContext parseTypeName() { if (next(TokenType.NAME)) { return TypeNameContext(current); - } else + } else { return null; + } } /// Use [parseInputValue] instead. @@ -544,8 +557,9 @@ class Parser { errors.add(SyntaxError('Unterminated list literal.', lastSpan)); return null; } - } else + } else { return null; + } } ObjectValueContext parseObjectValue() { diff --git a/graphql_parser/lib/src/language/token.dart b/graphql_parser/lib/src/language/token.dart index 5d12c90d..4a8498ae 100644 --- a/graphql_parser/lib/src/language/token.dart +++ b/graphql_parser/lib/src/language/token.dart @@ -10,9 +10,10 @@ class Token { @override String toString() { - if (span == null) + if (span == null) { return "'$text' -> $type"; - else + } else { return "(${span.start.line}:${span.start.column}) '$text' -> $type"; + } } } diff --git a/graphql_parser/test/directive_test.dart b/graphql_parser/test/directive_test.dart index 9dc30a4e..001c4ea8 100644 --- a/graphql_parser/test/directive_test.dart +++ b/graphql_parser/test/directive_test.dart @@ -37,8 +37,7 @@ main() { DirectiveContext parseDirective(String text) => parse(text).parseDirective(); Matcher isDirective(String name, {Matcher valueOrVariable, Matcher argument}) => - _IsDirective(name, - valueOrVariable: valueOrVariable, argument: argument); + _IsDirective(name, valueOrVariable: valueOrVariable, argument: argument); Matcher isDirectiveList(List directives) => _IsDirectiveList(directives); @@ -57,8 +56,9 @@ class _IsDirective extends Matcher { return valueOrVariable.describe(desc.add(' and ')); } else if (argument != null) { return argument.describe(desc.add(' and ')); - } else + } else { return desc; + } } @override @@ -67,9 +67,9 @@ class _IsDirective extends Matcher { item is DirectiveContext ? item : parseDirective(item.toString()); if (directive == null) return false; if (valueOrVariable != null) { - if (directive.value == null) + if (directive.value == null) { return false; - else { + } else { var v = directive.value; if (v is VariableContext) { return valueOrVariable.matches(v.name, matchState); @@ -79,12 +79,14 @@ class _IsDirective extends Matcher { } } } else if (argument != null) { - if (directive.argument == null) + if (directive.argument == null) { return false; - else + } else { return argument.matches(directive.argument, matchState); - } else + } + } else { return true; + } } } diff --git a/graphql_parser/test/field_test.dart b/graphql_parser/test/field_test.dart index 30acb837..e39f3f63 100644 --- a/graphql_parser/test/field_test.dart +++ b/graphql_parser/test/field_test.dart @@ -88,8 +88,7 @@ Matcher isField( Matcher selectionSet}) => _IsField(fieldName, arguments, directives, selectionSet); -Matcher isFieldName(String name, {String alias}) => - _IsFieldName(name, alias); +Matcher isFieldName(String name, {String alias}) => _IsFieldName(name, alias); class _IsField extends Matcher { final Matcher fieldName, arguments, directives, selectionSet; @@ -106,10 +105,12 @@ class _IsField extends Matcher { bool matches(item, Map matchState) { var field = item is FieldContext ? item : parseField(item.toString()); if (field == null) return false; - if (fieldName != null && !fieldName.matches(field.fieldName, matchState)) + if (fieldName != null && !fieldName.matches(field.fieldName, matchState)) { return false; - if (arguments != null && !arguments.matches(field.arguments, matchState)) + } + if (arguments != null && !arguments.matches(field.arguments, matchState)) { return false; + } return true; } } @@ -121,9 +122,10 @@ class _IsFieldName extends Matcher { @override Description describe(Description description) { - if (realName != null) + if (realName != null) { return description .add('is field with name "$name" and alias "$realName"'); + } return description.add('is field with name "$name"'); } @@ -131,10 +133,11 @@ class _IsFieldName extends Matcher { bool matches(item, Map matchState) { var fieldName = item is FieldNameContext ? item : parseFieldName(item.toString()); - if (realName != null) + if (realName != null) { return fieldName.alias?.alias == name && fieldName.alias?.name == realName; - else + } else { return fieldName.name == name; + } } } diff --git a/graphql_parser/test/fragment_spread_test.dart b/graphql_parser/test/fragment_spread_test.dart index 6fca400d..752e1122 100644 --- a/graphql_parser/test/fragment_spread_test.dart +++ b/graphql_parser/test/fragment_spread_test.dart @@ -35,9 +35,10 @@ class _IsFragmentSpread extends Matcher { @override Description describe(Description description) { - if (directives != null) + if (directives != null) { return directives.describe( description.add('is a fragment spread named "$name" that also ')); + } return description.add('is a fragment spread named "$name"'); } @@ -48,9 +49,10 @@ class _IsFragmentSpread extends Matcher { : parseFragmentSpread(item.toString()); if (spread == null) return false; if (spread.name != name) return false; - if (directives != null) + if (directives != null) { return directives.matches(spread.directives, matchState); - else + } else { return true; + } } } diff --git a/graphql_parser/test/selection_set_test.dart b/graphql_parser/test/selection_set_test.dart index 9cf89624..cd482481 100644 --- a/graphql_parser/test/selection_set_test.dart +++ b/graphql_parser/test/selection_set_test.dart @@ -55,8 +55,7 @@ main() { SelectionSetContext parseSelectionSet(String text) => parse(text).parseSelectionSet(); -Matcher isSelectionSet(List selections) => - _IsSelectionSet(selections); +Matcher isSelectionSet(List selections) => _IsSelectionSet(selections); class _IsSelectionSet extends Matcher { final List selections; @@ -87,8 +86,9 @@ class _IsSelectionSet extends Matcher { for (int i = 0; i < set.selections.length; i++) { var sel = set.selections[i]; if (!selections[i].matches( - sel.field ?? sel.fragmentSpread ?? sel.inlineFragment, matchState)) + sel.field ?? sel.fragmentSpread ?? sel.inlineFragment, matchState)) { return false; + } } return true; diff --git a/graphql_parser/test/type_test.dart b/graphql_parser/test/type_test.dart index 86ad0043..5ecb0030 100644 --- a/graphql_parser/test/type_test.dart +++ b/graphql_parser/test/type_test.dart @@ -84,10 +84,11 @@ class _IsType extends Matcher { @override Description describe(Description description) { - if (nonNull == true) + if (nonNull == true) { return description.add('is non-null type named "$name"'); - else + } else { return description.add('is nullable type named "$name"'); + } } @override