Skip to content

Commit

Permalink
Expose tokenCount on the DocumentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Nov 8, 2024
1 parent 423e72d commit 780846d
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 @@ -84,6 +84,11 @@ describe('Parser', () => {
`);
});

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

it('limit maximum number of tokens', () => {
expect(() => parse('{ foo }', { maxTokens: 3 })).to.not.throw();
expect(() => parse('{ foo }', { maxTokens: 2 })).to.throw(
Expand Down
1 change: 1 addition & 0 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export interface DocumentNode {
readonly kind: Kind.DOCUMENT;
readonly loc?: Location;
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 @@ -114,7 +114,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 @@ -198,6 +203,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 @@ -1564,9 +1573,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 780846d

Please sign in to comment.