Skip to content

Commit 5c4af50

Browse files
committed
Avoid scanning from start of document in loose parser's resetTo
Closes #1444
1 parent 5b9d16c commit 5c4af50

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

acorn-loose/src/tokenize.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,30 @@ lp.readToken = function() {
8888
}
8989

9090
lp.resetTo = function(pos) {
91+
if (this.options.locations) {
92+
if (pos >= this.toks.pos) {
93+
let skipped = this.input.slice(this.toks.pos, pos), match
94+
while (match = lineBreakG.exec(skipped)) {
95+
++this.toks.curLine
96+
this.toks.lineStart = match.index + match[0].length
97+
}
98+
} else {
99+
this.toks.curLine = 1
100+
this.toks.lineStart = lineBreakG.lastIndex = 0
101+
let match
102+
while ((match = lineBreakG.exec(this.input)) && match.index < pos) {
103+
++this.toks.curLine
104+
this.toks.lineStart = match.index + match[0].length
105+
}
106+
}
107+
}
108+
91109
this.toks.pos = pos
92110
this.toks.containsEsc = false
93111
let ch = this.input.charAt(pos - 1)
94112
this.toks.exprAllowed = !ch || /[[{(,;:?/*=+\-~!|&%^<>]/.test(ch) ||
95113
/[enwfd]/.test(ch) &&
96114
/\b(case|else|return|throw|new|in|(instance|type)?of|delete|void)$/.test(this.input.slice(pos - 10, pos))
97-
98-
if (this.options.locations) {
99-
this.toks.curLine = 1
100-
this.toks.lineStart = lineBreakG.lastIndex = 0
101-
let match
102-
while ((match = lineBreakG.exec(this.input)) && match.index < pos) {
103-
++this.toks.curLine
104-
this.toks.lineStart = match.index + match[0].length
105-
}
106-
}
107115
}
108116

109117
lp.lookAhead = function(n) {

0 commit comments

Comments
 (0)