Mirror: The magical sticky regex-based parser generator 🧙

Add basic test case for interpolations

Changed files
+18 -3
src
+1 -2
src/codegen.js
···
const assignIndex = (depth) =>
js`var y${depth} = ${_state}.y` +
-
(_interpolations ? js`, var x${depth} = ${_state}.x;` : ';');
const restoreIndex = (depth) =>
js`${_state}.y = y${depth}` +
···
opts = copy(opts);
opts.capture = capture;
-
let group = '';
if (!opts.length && capture) {
opts.length = depth;
return js`
···
const assignIndex = (depth) =>
js`var y${depth} = ${_state}.y` +
+
(_interpolations ? js`, x${depth} = ${_state}.x;` : ';');
const restoreIndex = (depth) =>
js`${_state}.y = y${depth}` +
···
opts = copy(opts);
opts.capture = capture;
if (!opts.length && capture) {
opts.length = depth;
return js`
+17 -1
src/core.test.js
···
-
import { match } from './core';
const expectToParse = (node, input, result, lastIndex = 0) => {
const state = { quasis: [input], expressions: [], x: 0, y: 0 };
···
}
);
});
···
+
import { parse, match, interpolation } from './core';
const expectToParse = (node, input, result, lastIndex = 0) => {
const state = { quasis: [input], expressions: [], x: 0, y: 0 };
···
}
);
});
+
+
describe('interpolation parsing', () => {
+
const node = match('node')`
+
${/1/}
+
${interpolation}
+
${/3/}
+
`;
+
+
const expected = ['1', 2, '3'];
+
expected.tag = 'node';
+
+
expect(parse(node)`1${2}3`).toEqual(expected);
+
expect(parse(node)`13`).toBe(undefined);
+
expect(parse(node)`13${2}`).toBe(undefined);
+
expect(parse(node)`${2}13`).toBe(undefined);
+
});