Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose tokenCount on the DocumentNode #4292

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading