feat: add error information in the progress callback
This allows users to bail parsing if an error was *definitely* detected using the progress callback, as all possible stack versions have a non-zero error cost. Co-authored-by: Amaan Qureshi <amaanq12@gmail.com>
This commit is contained in:
parent
ca087d2c07
commit
cda634a1c4
9 changed files with 87 additions and 4 deletions
|
|
@ -413,5 +413,34 @@ describe('Parser', () => {
|
|||
{ progressCallback },
|
||||
)).toBeNull();
|
||||
});
|
||||
|
||||
it('times out when an error is detected', { timeout: 5000 }, () => {
|
||||
parser.setLanguage(JSON);
|
||||
|
||||
let offset = 0;
|
||||
const erroneousCode = '!,';
|
||||
const progressCallback = (state: ParseState) => {
|
||||
offset = state.currentOffset;
|
||||
return state.hasError;
|
||||
};
|
||||
|
||||
const tree = parser.parse(
|
||||
(offset) => {
|
||||
if (offset === 0) return '[';
|
||||
if (offset >= 1 && offset < 1000) return '0,';
|
||||
return erroneousCode;
|
||||
},
|
||||
null,
|
||||
{ progressCallback },
|
||||
);
|
||||
|
||||
// The callback is called at the end of parsing, however, what we're asserting here is that
|
||||
// parsing ends immediately as the error is detected. This is verified by checking the offset
|
||||
// of the last byte processed is the length of the erroneous code we inserted, aka, 1002, or
|
||||
// 1000 + the length of the erroneous code. Note that in this WASM test, we multiply the offset
|
||||
// by 2 because JavaScript strings are UTF-16 encoded.
|
||||
expect(offset).toBe((1000 + erroneousCode.length) * 2);
|
||||
expect(tree).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue