wasm: Fix infinite loop in Node.text when tree and text are out-of-sync

This commit is contained in:
Max Brunsfeld 2019-07-31 13:55:15 -07:00
parent 9e1649d9f8
commit 9848ceb3e2
2 changed files with 22 additions and 11 deletions

View file

@ -67,7 +67,7 @@ class Parser {
parse(callback, oldTree, options) {
if (typeof callback === 'string') {
currentParseCallback = index => callback.slice(index);
currentParseCallback = (index, _, endIndex) => callback.slice(index, endIndex);
} else if (typeof callback === 'function') {
currentParseCallback = callback;
} else {
@ -243,13 +243,23 @@ class Node {
}
get text() {
const startIndex = this.startIndex;
const length = this.endIndex - startIndex;
let result = this.tree.textCallback(startIndex);
while (result.length < length) {
result += this.tree.textCallback(startIndex + result.length);
let {startIndex, endIndex} = this;
const length = endIndex - startIndex;
let result = this.tree.textCallback(startIndex, null, endIndex);
startIndex += result.length;
while (startIndex < endIndex) {
const string = this.tree.textCallback(startIndex, null, endIndex);
if (string && string.length > 0) {
startIndex += string.length;
result += string;
} else {
break;
}
}
return result.slice(0, length);
if (startIndex > endIndex) {
result = result.slice(0, length);
}
return result;
}
isNamed() {

View file

@ -35,10 +35,11 @@ declare module 'web-tree-sitter' {
type: "parse" | "lex"
) => void;
export interface Input {
seek(index: number): void;
read(): any;
}
export type Input = (
startIndex: number,
startPoint?: Point,
endIndex?: number,
) => string | null;
export interface SyntaxNode {
tree: Tree;