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,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 ?? <ArgumentContext>[])
|
||||
..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<DirectiveContext> 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<ArgumentContext> 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() {
|
||||
|
|
|
@ -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,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue