Skip to content

Commit 3e84962

Browse files
committed
Add 'isPunctuatorToken' to internal API
1 parent ce23444 commit 3e84962

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/language/__tests__/lexer-test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import dedent from '../../jsutils/dedent';
1515
import inspect from '../../jsutils/inspect';
1616
import { GraphQLError } from '../../error';
1717
import { Source } from '../source';
18-
import { createLexer, TokenKind } from '../lexer';
18+
import { createLexer, TokenKind, isPunctuatorToken } from '../lexer';
1919

2020
function lexOne(str) {
2121
const lexer = createLexer(new Source(str));
@@ -766,3 +766,31 @@ describe('Lexer', () => {
766766
]);
767767
});
768768
});
769+
770+
describe('isPunctuatorToken', () => {
771+
it('returns true for punctuator tokens', () => {
772+
expect(isPunctuatorToken(lexOne('!'))).to.equal(true);
773+
expect(isPunctuatorToken(lexOne('$'))).to.equal(true);
774+
expect(isPunctuatorToken(lexOne('&'))).to.equal(true);
775+
expect(isPunctuatorToken(lexOne('('))).to.equal(true);
776+
expect(isPunctuatorToken(lexOne(')'))).to.equal(true);
777+
expect(isPunctuatorToken(lexOne('...'))).to.equal(true);
778+
expect(isPunctuatorToken(lexOne(':'))).to.equal(true);
779+
expect(isPunctuatorToken(lexOne('='))).to.equal(true);
780+
expect(isPunctuatorToken(lexOne('@'))).to.equal(true);
781+
expect(isPunctuatorToken(lexOne('['))).to.equal(true);
782+
expect(isPunctuatorToken(lexOne(']'))).to.equal(true);
783+
expect(isPunctuatorToken(lexOne('{'))).to.equal(true);
784+
expect(isPunctuatorToken(lexOne('|'))).to.equal(true);
785+
expect(isPunctuatorToken(lexOne('}'))).to.equal(true);
786+
});
787+
788+
it('returns false for non-punctuator tokens', () => {
789+
expect(isPunctuatorToken(lexOne(''))).to.equal(false);
790+
expect(isPunctuatorToken(lexOne('name'))).to.equal(false);
791+
expect(isPunctuatorToken(lexOne('1'))).to.equal(false);
792+
expect(isPunctuatorToken(lexOne('3.14'))).to.equal(false);
793+
expect(isPunctuatorToken(lexOne('"str"'))).to.equal(false);
794+
expect(isPunctuatorToken(lexOne('"""str"""'))).to.equal(false);
795+
});
796+
});

src/language/lexer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,27 @@ export const TokenKind = Object.freeze({
129129
*/
130130
export type TokenKindEnum = $Values<typeof TokenKind>;
131131

132+
// @internal
133+
export function isPunctuatorToken(token: Token) {
134+
const kind = token.kind;
135+
return (
136+
kind === TokenKind.BANG ||
137+
kind === TokenKind.DOLLAR ||
138+
kind === TokenKind.AMP ||
139+
kind === TokenKind.PAREN_L ||
140+
kind === TokenKind.PAREN_R ||
141+
kind === TokenKind.SPREAD ||
142+
kind === TokenKind.COLON ||
143+
kind === TokenKind.EQUALS ||
144+
kind === TokenKind.AT ||
145+
kind === TokenKind.BRACKET_L ||
146+
kind === TokenKind.BRACKET_R ||
147+
kind === TokenKind.BRACE_L ||
148+
kind === TokenKind.PIPE ||
149+
kind === TokenKind.BRACE_R
150+
);
151+
}
152+
132153
/**
133154
* A helper function to describe a token as a string for debugging
134155
*/

0 commit comments

Comments
 (0)