File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -137,6 +137,11 @@ required):
137137 one-based line and zero-based column numbers in ` {line, column} `
138138 form. Default is ` false ` .
139139
140+ - ** startLocation** : An optional ` {line, column} ` object to use for
141+ the start of the parse. This is mostly useful when using
142+ ` parseExpressionAt ` with ` locations: true ` , to prevent the parser
143+ from having to determine the line position at the start position.
144+
140145- ** onToken** : If a function is passed for this option, each found
141146 token will be passed in same format as tokens returned from
142147 ` tokenizer().getToken() ` .
Original file line number Diff line number Diff line change @@ -693,6 +693,14 @@ export interface Options {
693693 */
694694 locations ?: boolean
695695
696+ /**
697+ * Pass an optional `{line, column}` object to use for the start of
698+ * the parse. This is mostly useful when using `parseExpressionAt`
699+ * with `locations: true`, to prevent the parser from having to
700+ * determine the line position at the start position.
701+ */
702+ startLocation ?: { line : number , column : number }
703+
696704 /**
697705 * a callback that will cause Acorn to call that export function with object in the same
698706 * format as tokens returned from `tokenizer().getToken()`. Note
Original file line number Diff line number Diff line change @@ -60,6 +60,11 @@ export const defaultOptions = {
6060 // line being 1-based and column 0-based) will be attached to the
6161 // nodes.
6262 locations : false ,
63+ // Pass an optional `{line, column}` object to use for the start of
64+ // the parse. This is mostly useful when using `parseExpressionAt`
65+ // with `locations: true`, to prevent the parser from having to
66+ // determine the line position at the start position.
67+ startLocation : null ,
6368 // A function can be passed as `onToken` option, which will
6469 // cause Acorn to call that function with object in the same
6570 // format as tokens returned from `tokenizer().getToken()`. Note
Original file line number Diff line number Diff line change @@ -32,14 +32,17 @@ export class Parser {
3232 // Set up token state
3333
3434 // The current position of the tokenizer in the input.
35+ this . pos = startPos || 0
3536 this . curLine = 1
36- if ( startPos ) {
37- this . pos = startPos
37+ if ( options . startLocation ) {
38+ this . lineStart = this . pos - options . startLocation . column
39+ this . curLine = options . startLocation . line
40+ } else if ( startPos ) {
3841 this . lineStart = this . input . lastIndexOf ( "\n" , startPos - 1 ) + 1
3942 if ( this . options . locations )
4043 this . curLine = this . input . slice ( 0 , this . lineStart ) . split ( lineBreak ) . length
4144 } else {
42- this . pos = this . lineStart = 0
45+ this . lineStart = 0
4346 }
4447
4548 // Properties of the current token:
You can’t perform that action at this time.
0 commit comments