Set schema element description to strings retrieved from a continuous chunk of comment tokens immediately preceding the element

This commit is contained in:
jane-olszewska 2017-02-03 14:54:47 +00:00
parent dd31ab2b80
commit 060dc51595

View File

@ -910,10 +910,13 @@ class Parser
$name = $this->parseName(); $name = $this->parseName();
$directives = $this->parseDirectives(); $directives = $this->parseDirectives();
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new ScalarTypeDefinitionNode([ return new ScalarTypeDefinitionNode([
'name' => $name, 'name' => $name,
'directives' => $directives, 'directives' => $directives,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -935,12 +938,15 @@ class Parser
Token::BRACE_R Token::BRACE_R
); );
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new ObjectTypeDefinitionNode([ return new ObjectTypeDefinitionNode([
'name' => $name, 'name' => $name,
'interfaces' => $interfaces, 'interfaces' => $interfaces,
'directives' => $directives, 'directives' => $directives,
'fields' => $fields, 'fields' => $fields,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -972,12 +978,15 @@ class Parser
$type = $this->parseTypeReference(); $type = $this->parseTypeReference();
$directives = $this->parseDirectives(); $directives = $this->parseDirectives();
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new FieldDefinitionNode([ return new FieldDefinitionNode([
'name' => $name, 'name' => $name,
'arguments' => $args, 'arguments' => $args,
'type' => $type, 'type' => $type,
'directives' => $directives, 'directives' => $directives,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -1007,12 +1016,14 @@ class Parser
$defaultValue = $this->parseConstValue(); $defaultValue = $this->parseConstValue();
} }
$directives = $this->parseDirectives(); $directives = $this->parseDirectives();
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new InputValueDefinitionNode([ return new InputValueDefinitionNode([
'name' => $name, 'name' => $name,
'type' => $type, 'type' => $type,
'defaultValue' => $defaultValue, 'defaultValue' => $defaultValue,
'directives' => $directives, 'directives' => $directives,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -1032,11 +1043,14 @@ class Parser
Token::BRACE_R Token::BRACE_R
); );
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new InterfaceTypeDefinitionNode([ return new InterfaceTypeDefinitionNode([
'name' => $name, 'name' => $name,
'directives' => $directives, 'directives' => $directives,
'fields' => $fields, 'fields' => $fields,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -1053,11 +1067,14 @@ class Parser
$this->expect(Token::EQUALS); $this->expect(Token::EQUALS);
$types = $this->parseUnionMembers(); $types = $this->parseUnionMembers();
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new UnionTypeDefinitionNode([ return new UnionTypeDefinitionNode([
'name' => $name, 'name' => $name,
'directives' => $directives, 'directives' => $directives,
'types' => $types, 'types' => $types,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -1093,11 +1110,14 @@ class Parser
Token::BRACE_R Token::BRACE_R
); );
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new EnumTypeDefinitionNode([ return new EnumTypeDefinitionNode([
'name' => $name, 'name' => $name,
'directives' => $directives, 'directives' => $directives,
'values' => $values, 'values' => $values,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -1110,10 +1130,13 @@ class Parser
$name = $this->parseName(); $name = $this->parseName();
$directives = $this->parseDirectives(); $directives = $this->parseDirectives();
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new EnumValueDefinitionNode([ return new EnumValueDefinitionNode([
'name' => $name, 'name' => $name,
'directives' => $directives, 'directives' => $directives,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -1133,11 +1156,14 @@ class Parser
Token::BRACE_R Token::BRACE_R
); );
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new InputObjectTypeDefinitionNode([ return new InputObjectTypeDefinitionNode([
'name' => $name, 'name' => $name,
'directives' => $directives, 'directives' => $directives,
'fields' => $fields, 'fields' => $fields,
'loc' => $this->loc($start) 'loc' => $this->loc($start),
'description' => $description
]); ]);
} }
@ -1193,4 +1219,28 @@ class Parser
} while ($this->skip(Token::PIPE)); } while ($this->skip(Token::PIPE));
return $locations; return $locations;
} }
/**
* @param Token $nameToken
* @return null|string
*/
private function getDescriptionFromAdjacentCommentTokens(Token $nameToken)
{
$description = null;
$currentToken = $nameToken;
$previousToken = $currentToken->prev;
while ($previousToken->kind == Token::COMMENT
&& ($previousToken->line + 1) == $currentToken->line
) {
$description = $previousToken->value . $description;
// walk the tokens backwards until no longer adjacent comments
$currentToken = $previousToken;
$previousToken = $currentToken->prev;
}
return $description;
}
} }