Use curly braces
This commit is contained in:
parent
766ec56f57
commit
4a655abb84
10 changed files with 126 additions and 100 deletions
|
@ -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<FileSpan>(
|
||||
fieldName.span, (out, d) => out.expand(d.span));
|
||||
if (arguments.isNotEmpty)
|
||||
}
|
||||
if (arguments.isNotEmpty) {
|
||||
return arguments.fold<FileSpan>(
|
||||
fieldName.span, (out, a) => out.expand(a.span));
|
||||
else
|
||||
} else {
|
||||
return fieldName.span;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,9 +10,9 @@ class NumberValueContext extends InputValueContext<num> {
|
|||
|
||||
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]);
|
||||
|
|
|
@ -63,8 +63,9 @@ class StringValueContext extends InputValueContext<String> {
|
|||
default:
|
||||
buf.writeCharCode(next);
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
throw SyntaxError('Unexpected "\\" in string literal.', span);
|
||||
}
|
||||
} else {
|
||||
buf.writeCharCode(ch);
|
||||
}
|
||||
|
|
|
@ -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,20 +79,20 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FragmentDefinitionContext parseFragmentDefinition() {
|
||||
if (nextName('fragment')) {
|
||||
|
@ -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,9 +133,10 @@ class Parser {
|
|||
FRAGMENT.span));
|
||||
return null;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
FragmentSpreadContext parseFragmentSpread() {
|
||||
if (next(TokenType.ELLIPSIS)) {
|
||||
|
@ -148,9 +149,10 @@ class Parser {
|
|||
_index--;
|
||||
return null;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
InlineFragmentContext parseInlineFragment() {
|
||||
if (next(TokenType.ELLIPSIS)) {
|
||||
|
@ -184,9 +186,10 @@ class Parser {
|
|||
'Missing "on" after "..." in inline fragment.', ELLIPSIS.span));
|
||||
return null;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
SelectionSetContext parseSelectionSet() {
|
||||
if (next(TokenType.LBRACE)) {
|
||||
|
@ -201,30 +204,31 @@ 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() {
|
||||
var fieldName = parseFieldName();
|
||||
|
@ -235,28 +239,30 @@ class Parser {
|
|||
return FieldContext(fieldName, selectionSet)
|
||||
..arguments.addAll(args ?? <ArgumentContext>[])
|
||||
..directives.addAll(directives);
|
||||
} else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
FieldNameContext parseFieldName() {
|
||||
if (next(TokenType.NAME)) {
|
||||
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() {
|
||||
if (next(TokenType.LPAREN)) {
|
||||
|
@ -270,17 +276,18 @@ 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() {
|
||||
var variable = parseVariable();
|
||||
|
@ -290,21 +297,21 @@ 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() {
|
||||
var name = parseTypeName();
|
||||
|
@ -314,10 +321,11 @@ class Parser {
|
|||
var listType = parseListType();
|
||||
if (listType != null) {
|
||||
return TypeContext(null, listType, maybe(TokenType.EXCLAMATION));
|
||||
} else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ListTypeContext parseListType() {
|
||||
if (next(TokenType.LBRACKET)) {
|
||||
|
@ -334,9 +342,10 @@ class Parser {
|
|||
errors.add(SyntaxError('Missing type after "[".', LBRACKET.span));
|
||||
return null;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
List<DirectiveContext> parseDirectives() {
|
||||
List<DirectiveContext> out = [];
|
||||
|
@ -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,16 +391,17 @@ 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<ArgumentContext> parseArguments() {
|
||||
if (next(TokenType.LPAREN)) {
|
||||
|
@ -406,16 +415,16 @@ 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() {
|
||||
if (next(TokenType.NAME)) {
|
||||
|
@ -423,21 +432,22 @@ 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.
|
||||
@deprecated
|
||||
|
@ -446,17 +456,18 @@ 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() {
|
||||
if (next(TokenType.EQUALS)) {
|
||||
|
@ -465,28 +476,30 @@ 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.
|
||||
@deprecated
|
||||
|
@ -544,9 +557,10 @@ class Parser {
|
|||
errors.add(SyntaxError('Unterminated list literal.', lastSpan));
|
||||
return null;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
ObjectValueContext parseObjectValue() {
|
||||
if (next(TokenType.LBRACE)) {
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Matcher> directives) =>
|
||||
_IsDirectiveList(directives);
|
||||
|
@ -57,9 +56,10 @@ 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
|
||||
bool matches(item, Map matchState) {
|
||||
|
@ -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,14 +79,16 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _IsDirectiveList extends Matcher {
|
||||
final List<Matcher> directives;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,8 +55,7 @@ main() {
|
|||
SelectionSetContext parseSelectionSet(String text) =>
|
||||
parse(text).parseSelectionSet();
|
||||
|
||||
Matcher isSelectionSet(List<Matcher> selections) =>
|
||||
_IsSelectionSet(selections);
|
||||
Matcher isSelectionSet(List<Matcher> selections) => _IsSelectionSet(selections);
|
||||
|
||||
class _IsSelectionSet extends Matcher {
|
||||
final List<Matcher> selections;
|
||||
|
@ -87,9 +86,10 @@ 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;
|
||||
}
|
||||
|
|
|
@ -84,11 +84,12 @@ 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
|
||||
bool matches(item, Map matchState) {
|
||||
|
|
Loading…
Reference in a new issue