Skip to content

Commit

Permalink
Disallow "true", "false", and "null" as enum values (#3223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogu committed Aug 4, 2021
1 parent 5f86d69 commit af878f3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
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 }],
});

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 {
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.`,
);
}
return this.parseName();
}

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

0 comments on commit af878f3

Please sign in to comment.