Skip to content

Commit cf1c080

Browse files
authored
Expose tokenCount on the DocumentNode (#4251)
This way people can add this to metrics and inform themselves about sane values to use with the `maxTokens` option on `parse`. This was added as a non-enumerable property to increase backwards compatability. Currently there is now way to know the tokens and by extension set sane defaults.
1 parent 84b122c commit cf1c080

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/language/__tests__/parser-test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ describe('Parser', () => {
100100
);
101101
});
102102

103+
it('exposes the tokenCount', () => {
104+
expect(parse('{ foo }').tokenCount).to.equal(3);
105+
expect(parse('{ foo(bar: "baz") }').tokenCount).to.equal(8);
106+
});
107+
103108
it('parses variable inline values', () => {
104109
expect(() =>
105110
parse('{ field(complex: { a: { b: [ $var ] } }) }'),

src/language/ast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ export interface DocumentNode {
312312
readonly kind: Kind.DOCUMENT;
313313
readonly loc?: Location | undefined;
314314
readonly definitions: ReadonlyArray<DefinitionNode>;
315+
readonly tokenCount?: number | undefined;
315316
}
316317

317318
export type DefinitionNode =

src/language/parser.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ export function parse(
119119
options?: ParseOptions | undefined,
120120
): DocumentNode {
121121
const parser = new Parser(source, options);
122-
return parser.parseDocument();
122+
const document = parser.parseDocument();
123+
Object.defineProperty(document, 'tokenCount', {
124+
enumerable: false,
125+
value: parser.tokenCount,
126+
});
127+
return document;
123128
}
124129

125130
/**
@@ -201,6 +206,10 @@ export class Parser {
201206
this._tokenCounter = 0;
202207
}
203208

209+
get tokenCount(): number {
210+
return this._tokenCounter;
211+
}
212+
204213
/**
205214
* Converts a name lex token into a name parse node.
206215
*/
@@ -1601,9 +1610,9 @@ export class Parser {
16011610
const { maxTokens } = this._options;
16021611
const token = this._lexer.advance();
16031612

1604-
if (maxTokens !== undefined && token.kind !== TokenKind.EOF) {
1613+
if (token.kind !== TokenKind.EOF) {
16051614
++this._tokenCounter;
1606-
if (this._tokenCounter > maxTokens) {
1615+
if (maxTokens !== undefined && this._tokenCounter > maxTokens) {
16071616
throw syntaxError(
16081617
this._lexer.source,
16091618
token.start,

0 commit comments

Comments
 (0)