Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow "true", "false", and "null" as enum values #3223

Merged
merged 1 commit into from Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/language/__tests__/parser-test.ts
Expand Up @@ -120,6 +120,26 @@ describe('Parser', () => {
});
});

it('does not allow "true", "false", or "null" as enum value', () => {
expectSyntaxError('enum Test { VALID, true }').to.deep.equal({
message:
'Syntax Error: Name "true" is reserved and cannot be used for an enum value.',
locations: [{ line: 1, column: 20 }],
});
Yogu marked this conversation as resolved.
Show resolved Hide resolved

expectSyntaxError('enum Test { VALID, false }').to.deep.equal({
message:
'Syntax Error: Name "false" is reserved and cannot be used for an enum value.',
locations: [{ line: 1, column: 20 }],
});

expectSyntaxError('enum Test { VALID, null }').to.deep.equal({
message:
'Syntax Error: Name "null" is reserved and cannot be used for an enum value.',
locations: [{ line: 1, column: 20 }],
});
});

it('parses multi-byte characters', () => {
// Note: \u0A0A could be naively interpreted as two line-feed chars.
const ast = parse(`
Expand Down
24 changes: 21 additions & 3 deletions src/language/parser.ts
Expand Up @@ -1036,13 +1036,11 @@ export class Parser {

/**
* EnumValueDefinition : Description? EnumValue Directives[Const]?
*
* EnumValue : Name
*/
parseEnumValueDefinition(): EnumValueDefinitionNode {
const start = this._lexer.token;
const description = this.parseDescription();
const name = this.parseName();
const name = this.parseEnumValueName();
const directives = this.parseConstDirectives();
return this.node<EnumValueDefinitionNode>(start, {
kind: Kind.ENUM_VALUE_DEFINITION,
Expand All @@ -1052,6 +1050,26 @@ export class Parser {
});
}

/**
* EnumValue : Name but not `true`, `false` or `null`
*/
parseEnumValueName(): NameNode {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed this to parseEnumValueName() because it's no longer parsing into an EnumValue. but still having it in its own function keeps things clean.

if (
this._lexer.token.value === 'true' ||
this._lexer.token.value === 'false' ||
this._lexer.token.value === 'null'
) {
throw syntaxError(
this._lexer.source,
this._lexer.token.start,
`${getTokenDesc(
this._lexer.token,
)} is reserved and cannot be used for an enum value.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW. Adding a similar message to fragment name parsing is welcomed if you want to work on such PR.

);
}
return this.parseName();
}

/**
* InputObjectTypeDefinition :
* - Description? input Name Directives[Const]? InputFieldsDefinition?
Expand Down