Skip to content

Commit

Permalink
Expose tokenCount on the DocumentNode (#4251)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JoviDeCroock authored Oct 25, 2024
1 parent 84b122c commit cf1c080
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/language/__tests__/parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ describe('Parser', () => {
);
});

it('exposes the tokenCount', () => {
expect(parse('{ foo }').tokenCount).to.equal(3);
expect(parse('{ foo(bar: "baz") }').tokenCount).to.equal(8);
});

it('parses variable inline values', () => {
expect(() =>
parse('{ field(complex: { a: { b: [ $var ] } }) }'),
Expand Down
1 change: 1 addition & 0 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ export interface DocumentNode {
readonly kind: Kind.DOCUMENT;
readonly loc?: Location | undefined;
readonly definitions: ReadonlyArray<DefinitionNode>;
readonly tokenCount?: number | undefined;
}

export type DefinitionNode =
Expand Down
15 changes: 12 additions & 3 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ export function parse(
options?: ParseOptions | undefined,
): DocumentNode {
const parser = new Parser(source, options);
return parser.parseDocument();
const document = parser.parseDocument();
Object.defineProperty(document, 'tokenCount', {
enumerable: false,
value: parser.tokenCount,
});
return document;
}

/**
Expand Down Expand Up @@ -201,6 +206,10 @@ export class Parser {
this._tokenCounter = 0;
}

get tokenCount(): number {
return this._tokenCounter;
}

/**
* Converts a name lex token into a name parse node.
*/
Expand Down Expand Up @@ -1601,9 +1610,9 @@ export class Parser {
const { maxTokens } = this._options;
const token = this._lexer.advance();

if (maxTokens !== undefined && token.kind !== TokenKind.EOF) {
if (token.kind !== TokenKind.EOF) {
++this._tokenCounter;
if (this._tokenCounter > maxTokens) {
if (maxTokens !== undefined && this._tokenCounter > maxTokens) {
throw syntaxError(
this._lexer.source,
token.start,
Expand Down

0 comments on commit cf1c080

Please sign in to comment.