fix(bindings/wasm): fix Parser.getIncludedRanges()

This commit is contained in:
Amaan Qureshi 2024-03-11 23:12:06 -04:00
parent ad07fa8a9e
commit 5c2f80ebb0
6 changed files with 71 additions and 10 deletions

View file

@ -83,6 +83,44 @@ describe('Parser', () => {
});
});
describe('one included range', () => {
it('parses the text within a range', () => {
parser.setLanguage(HTML);
const sourceCode = '<span>hi</span><script>console.log(\'sup\');</script>';
const htmlTree = parser.parse(sourceCode);
const scriptContentNode = htmlTree.rootNode.child(1).child(1);
assert.equal(scriptContentNode.type, 'raw_text');
parser.setLanguage(JavaScript);
assert.deepEqual(parser.getIncludedRanges(), [{
startIndex: 0,
endIndex: 2147483647,
startPosition: {row: 0, column: 0},
endPosition: {row: 4294967295, column: 2147483647},
}]);
const ranges = [{
startIndex: scriptContentNode.startIndex,
endIndex: scriptContentNode.endIndex,
startPosition: scriptContentNode.startPosition,
endPosition: scriptContentNode.endPosition,
}];
const jsTree = parser.parse(
sourceCode,
null,
{includedRanges: ranges},
);
assert.deepEqual(parser.getIncludedRanges(), ranges);
assert.equal(
jsTree.rootNode.toString(),
'(program (expression_statement (call_expression ' +
'function: (member_expression object: (identifier) property: (property_identifier)) ' +
'arguments: (arguments (string (string_fragment))))))',
);
assert.deepEqual(jsTree.rootNode.startPosition, {row: 0, column: sourceCode.indexOf('console')});
});
});
describe('multiple included ranges', () => {
it('parses the text within multiple ranges', () => {
parser.setLanguage(JavaScript);