File tree Expand file tree Collapse file tree 3 files changed +18
-3
lines changed Expand file tree Collapse file tree 3 files changed +18
-3
lines changed Original file line number Diff line number Diff 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 ] } }) }' ) ,
Original file line number Diff line number Diff 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
317318export type DefinitionNode =
Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments