mirror of
https://github.com/eliasstepanik/strudel.git
synced 2026-01-11 13:48:40 +00:00
add emitMiniLocations flag to transpiler
This commit is contained in:
parent
d686b65dbe
commit
5f271ed127
@ -31,6 +31,7 @@
|
|||||||
"homepage": "https://github.com/tidalcycles/strudel#readme",
|
"homepage": "https://github.com/tidalcycles/strudel#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@strudel.cycles/core": "workspace:*",
|
"@strudel.cycles/core": "workspace:*",
|
||||||
|
"@strudel.cycles/mini": "workspace:*",
|
||||||
"acorn": "^8.8.1",
|
"acorn": "^8.8.1",
|
||||||
"escodegen": "^2.0.0",
|
"escodegen": "^2.0.0",
|
||||||
"estree-walker": "^3.0.1"
|
"estree-walker": "^3.0.1"
|
||||||
|
|||||||
@ -36,4 +36,12 @@ describe('transpiler', () => {
|
|||||||
}),
|
}),
|
||||||
).toEqual("const {default: foo} = await import('https://bar.com/foo.js');");
|
).toEqual("const {default: foo} = await import('https://bar.com/foo.js');");
|
||||||
}); */
|
}); */
|
||||||
|
it('collections locations', () => {
|
||||||
|
const { miniLocations } = transpiler(`s("bd", "hh oh")`, { ...simple, emitMiniLocations: true });
|
||||||
|
expect(miniLocations).toEqual([
|
||||||
|
[3, 5],
|
||||||
|
[9, 11],
|
||||||
|
[12, 14],
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -2,9 +2,10 @@ import escodegen from 'escodegen';
|
|||||||
import { parse } from 'acorn';
|
import { parse } from 'acorn';
|
||||||
import { walk } from 'estree-walker';
|
import { walk } from 'estree-walker';
|
||||||
import { isNoteWithOctave } from '@strudel.cycles/core';
|
import { isNoteWithOctave } from '@strudel.cycles/core';
|
||||||
|
import { getLeafLocations } from '@strudel.cycles/mini';
|
||||||
|
|
||||||
export function transpiler(input, options = {}) {
|
export function transpiler(input, options = {}) {
|
||||||
const { wrapAsync = false, addReturn = true, simpleLocs = false } = options;
|
const { wrapAsync = false, addReturn = true, simpleLocs = false, emitMiniLocations = false } = options;
|
||||||
|
|
||||||
let ast = parse(input, {
|
let ast = parse(input, {
|
||||||
ecmaVersion: 2022,
|
ecmaVersion: 2022,
|
||||||
@ -12,18 +13,27 @@ export function transpiler(input, options = {}) {
|
|||||||
locations: true,
|
locations: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let miniLocations = [];
|
||||||
|
const collectMiniLocations = (value, node) => {
|
||||||
|
const leafLocs = getLeafLocations(`"${value}"`);
|
||||||
|
const withOffset = leafLocs.map((offsets) => offsets.map((o) => o + node.start));
|
||||||
|
miniLocations = miniLocations.concat(withOffset);
|
||||||
|
};
|
||||||
|
|
||||||
walk(ast, {
|
walk(ast, {
|
||||||
enter(node, parent, prop, index) {
|
enter(node, parent /* , prop, index */) {
|
||||||
if (isBackTickString(node, parent)) {
|
if (isBackTickString(node, parent)) {
|
||||||
const { quasis, start, end } = node;
|
const { quasis } = node;
|
||||||
const { raw } = quasis[0].value;
|
const { raw } = quasis[0].value;
|
||||||
this.skip();
|
this.skip();
|
||||||
return this.replace(miniWithLocation(raw, node, simpleLocs));
|
emitMiniLocations && collectMiniLocations(raw, node);
|
||||||
|
return this.replace(miniWithLocation(raw, node, simpleLocs, miniLocations));
|
||||||
}
|
}
|
||||||
if (isStringWithDoubleQuotes(node)) {
|
if (isStringWithDoubleQuotes(node)) {
|
||||||
const { value, start, end } = node;
|
const { value } = node;
|
||||||
this.skip();
|
this.skip();
|
||||||
return this.replace(miniWithLocation(value, node, simpleLocs));
|
emitMiniLocations && collectMiniLocations(value, node);
|
||||||
|
return this.replace(miniWithLocation(value, node, simpleLocs, miniLocations));
|
||||||
}
|
}
|
||||||
// TODO: remove pseudo note variables?
|
// TODO: remove pseudo note variables?
|
||||||
if (node.type === 'Identifier' && isNoteWithOctave(node.name)) {
|
if (node.type === 'Identifier' && isNoteWithOctave(node.name)) {
|
||||||
@ -47,11 +57,14 @@ export function transpiler(input, options = {}) {
|
|||||||
argument: expression,
|
argument: expression,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const output = escodegen.generate(ast);
|
let output = escodegen.generate(ast);
|
||||||
if (wrapAsync) {
|
if (wrapAsync) {
|
||||||
return `(async ()=>{${output}})()`;
|
output = `(async ()=>{${output}})()`;
|
||||||
}
|
}
|
||||||
return output;
|
if (!emitMiniLocations) {
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
return { output, miniLocations };
|
||||||
}
|
}
|
||||||
|
|
||||||
function isStringWithDoubleQuotes(node, locations, code) {
|
function isStringWithDoubleQuotes(node, locations, code) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user