feat(wasm)!: keep API in-line with upstream and start aligning with node

This commit is contained in:
Amaan Qureshi 2024-03-09 01:28:35 -05:00
parent c070c92722
commit 728793a160
8 changed files with 743 additions and 107 deletions

View file

@ -1,11 +1,11 @@
const {assert} = require('chai');
let Parser; let JavaScript; let languageURL;
let Parser; let JavaScript; let HTML; let languageURL;
describe('Parser', () => {
let parser;
before(async () =>
({Parser, JavaScript, languageURL} = await require('./helper')),
({Parser, JavaScript, HTML, languageURL} = await require('./helper')),
);
beforeEach(() => {
@ -73,7 +73,7 @@ describe('Parser', () => {
it('rethrows errors thrown by the logging callback', () => {
const error = new Error('The error message');
parser.setLogger((msg, params) => {
parser.setLogger((_msg, _params) => {
throw error;
});
assert.throws(
@ -83,6 +83,99 @@ describe('Parser', () => {
});
});
describe('multiple included ranges', () => {
it('parses the text within multiple ranges', () => {
parser.setLanguage(JavaScript);
const sourceCode = 'html `<div>Hello, ${name.toUpperCase()}, it\'s <b>${now()}</b>.</div>`';
const jsTree = parser.parse(sourceCode);
const templateStringNode = jsTree.rootNode.descendantForIndex(sourceCode.indexOf('`<'), sourceCode.indexOf('>`'));
assert.equal(templateStringNode.type, 'template_string');
const openQuoteNode = templateStringNode.child(0);
const interpolationNode1 = templateStringNode.child(2);
const interpolationNode2 = templateStringNode.child(4);
const closeQuoteNode = templateStringNode.child(6);
parser.setLanguage(HTML);
const htmlRanges = [
{
startIndex: openQuoteNode.endIndex,
startPosition: openQuoteNode.endPosition,
endIndex: interpolationNode1.startIndex,
endPosition: interpolationNode1.startPosition,
},
{
startIndex: interpolationNode1.endIndex,
startPosition: interpolationNode1.endPosition,
endIndex: interpolationNode2.startIndex,
endPosition: interpolationNode2.startPosition,
},
{
startIndex: interpolationNode2.endIndex,
startPosition: interpolationNode2.endPosition,
endIndex: closeQuoteNode.startIndex,
endPosition: closeQuoteNode.startPosition,
},
];
const htmlTree = parser.parse(sourceCode, null, {includedRanges: htmlRanges});
assert.equal(
htmlTree.rootNode.toString(),
'(document (element' +
' (start_tag (tag_name))' +
' (text)' +
' (element (start_tag (tag_name)) (end_tag (tag_name)))' +
' (text)' +
' (end_tag (tag_name))))',
);
assert.deepEqual(htmlTree.getIncludedRanges(), htmlRanges);
const divElementNode = htmlTree.rootNode.child(0);
const helloTextNode = divElementNode.child(1);
const bElementNode = divElementNode.child(2);
const bStartTagNode = bElementNode.child(0);
const bEndTagNode = bElementNode.child(1);
assert.equal(helloTextNode.type, 'text');
assert.equal(helloTextNode.startIndex, sourceCode.indexOf('Hello'));
assert.equal(helloTextNode.endIndex, sourceCode.indexOf(' <b>'));
assert.equal(bStartTagNode.type, 'start_tag');
assert.equal(bStartTagNode.startIndex, sourceCode.indexOf('<b>'));
assert.equal(bStartTagNode.endIndex, sourceCode.indexOf('${now()}'));
assert.equal(bEndTagNode.type, 'end_tag');
assert.equal(bEndTagNode.startIndex, sourceCode.indexOf('</b>'));
assert.equal(bEndTagNode.endIndex, sourceCode.indexOf('.</div>'));
});
});
describe('an included range containing mismatched positions', () => {
it('parses the text within the range', () => {
const sourceCode = '<div>test</div>{_ignore_this_part_}';
parser.setLanguage(HTML);
const endIndex = sourceCode.indexOf('{_ignore_this_part_');
const rangeToParse = {
startIndex: 0,
startPosition: {row: 10, column: 12},
endIndex,
endPosition: {row: 10, column: 12 + endIndex},
};
const htmlTree = parser.parse(sourceCode, null, {includedRanges: [rangeToParse]});
assert.deepEqual(htmlTree.getIncludedRanges()[0], rangeToParse);
assert.equal(
htmlTree.rootNode.toString(),
'(document (element (start_tag (tag_name)) (text) (end_tag (tag_name))))',
);
});
});
describe('.parse', () => {
let tree;