Skip to content

Commit

Permalink
Expose tokenCount on the DocumentNode
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.
  • Loading branch information
JoviDeCroock committed Oct 23, 2024
1 parent 84b122c commit 54bca6a
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 54bca6a

Please sign in to comment.