Skip to content

Commit

Permalink
Fix AST::fromArray conversion of initial values (match initial to exp…
Browse files Browse the repository at this point in the history
…ected conversion)

- Parse `DirectiveDefinitionNode->locations` as `NodeList<NamedNode>`
- Parse `Parser::implementsInterfaces` as `NodeList<NamedTypeNode>`
- Update signature of `Parser::unionMemberTypes` to match actual `NodeList<NamedTypeNode>`

Fixes #723
  • Loading branch information
zimzat committed Aug 31, 2020
1 parent 4f34309 commit 3373240
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

Fix:
- Parse `DirectiveDefinitionNode->locations` as `NodeList<NamedNode>` (fixes AST::fromArray conversion) (#723)
- Parse `Parser::implementsInterfaces` as `NodeList<NamedTypeNode>` (fixes AST::fromArray conversion)
- Fix signature of `Parser::unionMemberTypes` to match actual `NodeList<NamedTypeNode>`

#### v14.3.0

Feat:
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ parameters:
path: src/Validator/Rules/KnownDirectives.php

-
message: "#^Only booleans are allowed in a negated boolean, array\\<string\\>\\|null given\\.$#"
message: "#^Only booleans are allowed in a negated boolean, array|null given\\.$#"
count: 1
path: src/Validator/Rules/KnownDirectives.php

Expand Down
18 changes: 7 additions & 11 deletions src/Language/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@
* @method static OperationTypeDefinitionNode operationTypeDefinition(Source|string $source, bool[] $options = [])
* @method static ScalarTypeDefinitionNode scalarTypeDefinition(Source|string $source, bool[] $options = [])
* @method static ObjectTypeDefinitionNode objectTypeDefinition(Source|string $source, bool[] $options = [])
* @method static NamedTypeNode[] implementsInterfaces(Source|string $source, bool[] $options = [])
* @method static NodeList<NamedTypeNode> implementsInterfaces(Source|string $source, bool[] $options = [])
* @method static NodeList<FieldDefinitionNode> fieldsDefinition(Source|string $source, bool[] $options = [])
* @method static FieldDefinitionNode fieldDefinition(Source|string $source, bool[] $options = [])
* @method static NodeList<InputValueDefinitionNode> argumentsDefinition(Source|string $source, bool[] $options = [])
* @method static InputValueDefinitionNode inputValueDefinition(Source|string $source, bool[] $options = [])
* @method static InterfaceTypeDefinitionNode interfaceTypeDefinition(Source|string $source, bool[] $options = [])
* @method static UnionTypeDefinitionNode unionTypeDefinition(Source|string $source, bool[] $options = [])
* @method static NamedTypeNode[] unionMemberTypes(Source|string $source, bool[] $options = [])
* @method static NodeList<NamedTypeNode> unionMemberTypes(Source|string $source, bool[] $options = [])
* @method static EnumTypeDefinitionNode enumTypeDefinition(Source|string $source, bool[] $options = [])
* @method static NodeList<EnumValueDefinitionNode> enumValuesDefinition(Source|string $source, bool[] $options = [])
* @method static EnumValueDefinitionNode enumValueDefinition(Source|string $source, bool[] $options = [])
Expand All @@ -130,7 +130,7 @@
* @method static EnumTypeExtensionNode enumTypeExtension(Source|string $source, bool[] $options = [])
* @method static InputObjectTypeExtensionNode inputObjectTypeExtension(Source|string $source, bool[] $options = [])
* @method static DirectiveDefinitionNode directiveDefinition(Source|string $source, bool[] $options = [])
* @method static DirectiveLocation[] directiveLocations(Source|string $source, bool[] $options = [])
* @method static NodeList<NamedNode> directiveLocations(Source|string $source, bool[] $options = [])
* @method static DirectiveLocation directiveLocation(Source|string $source, bool[] $options = [])
*/
class Parser
Expand Down Expand Up @@ -1220,10 +1220,8 @@ private function parseObjectTypeDefinition() : ObjectTypeDefinitionNode
* ImplementsInterfaces :
* - implements `&`? NamedType
* - ImplementsInterfaces & NamedType
*
* @return NamedTypeNode[]
*/
private function parseImplementsInterfaces() : array
private function parseImplementsInterfaces() : NodeList
{
$types = [];
if ($this->expectOptionalKeyword('implements')) {
Expand All @@ -1237,7 +1235,7 @@ private function parseImplementsInterfaces() : array
);
}

return $types;
return new NodeList($types);
}

/**
Expand Down Expand Up @@ -1746,11 +1744,9 @@ private function parseDirectiveDefinition() : DirectiveDefinitionNode
}

/**
* @return NameNode[]
*
* @throws SyntaxError
*/
private function parseDirectiveLocations() : array
private function parseDirectiveLocations() : NodeList
{
// Optional leading pipe
$this->skip(Token::PIPE);
Expand All @@ -1759,7 +1755,7 @@ private function parseDirectiveLocations() : array
$locations[] = $this->parseDirectiveLocation();
} while ($this->skip(Token::PIPE));

return $locations;
return new NodeList($locations);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Validator/Rules/KnownDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Type\Definition\Directive;
use GraphQL\Utils\Utils;
use GraphQL\Validator\ASTValidationContext;
use GraphQL\Validator\SDLValidationContext;
use GraphQL\Validator\ValidationContext;
Expand Down Expand Up @@ -76,11 +77,11 @@ public function getASTVisitor(ASTValidationContext $context)
continue;
}

$locationsMap[$def->name->value] = array_map(
$locationsMap[$def->name->value] = Utils::map(
$def->locations,
static function ($name) : string {
return $name->value;
},
$def->locations
}
);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Language/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,21 @@ public function testPartiallyParsesSource() : void
Parser::argumentsDefinition('(foo: Int!)')
);

self::assertInstanceOf(
NodeList::class,
Parser::directiveLocations('| INPUT_OBJECT | OBJECT')
);

self::assertInstanceOf(
NodeList::class,
Parser::implementsInterfaces('implements Foo & Bar')
);

self::assertInstanceOf(
NodeList::class,
Parser::unionMemberTypes('= | Foo | Bar')
);

$this->expectException(SyntaxError::class);
Parser::constValueLiteral('$foo');
}
Expand Down

0 comments on commit 3373240

Please sign in to comment.