fix: tests

This commit is contained in:
Felix Roos 2022-11-08 21:04:41 +01:00
parent a761aa9a37
commit c93fd900bc
2 changed files with 12 additions and 13 deletions

View File

@ -7,26 +7,26 @@ This program is free software: you can redistribute it and/or modify it under th
import { describe, it, expect } from 'vitest';
import { transpiler } from '../transpiler.mjs';
const simple = { wrapAsync: false, addReturn: false, simpleLocs: true };
describe('transpiler', () => {
it('wraps double quote string with mini and adds location', () => {
expect(transpiler('"c3"', { wrapAsync: false, addReturn: false })).toEqual("mini('c3').withMiniLocation(0, 4);");
expect(transpiler('stack("c3","bd sd")', { wrapAsync: false, addReturn: false })).toEqual(
expect(transpiler('"c3"', simple)).toEqual("mini('c3').withMiniLocation(0, 4);");
expect(transpiler('stack("c3","bd sd")', simple)).toEqual(
"stack(mini('c3').withMiniLocation(6, 10), mini('bd sd').withMiniLocation(11, 18));",
);
});
it('wraps backtick string with mini and adds location', () => {
expect(transpiler('`c3`', { wrapAsync: false, addReturn: false })).toEqual("mini('c3').withMiniLocation(0, 4);");
expect(transpiler('`c3`', simple)).toEqual("mini('c3').withMiniLocation(0, 4);");
});
it('replaces note variables with note strings', () => {
expect(transpiler('seq(c3, d3)', { wrapAsync: false, addReturn: false })).toEqual("seq('c3', 'd3');");
expect(transpiler('seq(c3, d3)', simple)).toEqual("seq('c3', 'd3');");
});
it('keeps tagged template literal as is', () => {
expect(transpiler('xxx`c3`', { wrapAsync: false, addReturn: false })).toEqual('xxx`c3`;');
expect(transpiler('xxx`c3`', simple)).toEqual('xxx`c3`;');
});
it('supports top level await', () => {
expect(transpiler("await samples('xxx');", { wrapAsync: false, addReturn: false })).toEqual(
"await samples('xxx');",
);
expect(transpiler("await samples('xxx');", simple)).toEqual("await samples('xxx');");
});
/* it('parses dynamic imports', () => {
expect(

View File

@ -4,7 +4,7 @@ import { walk } from 'estree-walker';
import { isNote } from '@strudel.cycles/core';
export function transpiler(input, options = {}) {
const { wrapAsync = false, addReturn = true } = options;
const { wrapAsync = false, addReturn = true, simpleLocs = false } = options;
let ast = parse(input, {
ecmaVersion: 2022,
@ -18,12 +18,12 @@ export function transpiler(input, options = {}) {
const { quasis, start, end } = node;
const { raw } = quasis[0].value;
this.skip();
return this.replace(miniWithLocation(raw, node));
return this.replace(miniWithLocation(raw, node, simpleLocs));
}
if (isStringWithDoubleQuotes(node)) {
const { value, start, end } = node;
this.skip();
return this.replace(miniWithLocation(value, node));
return this.replace(miniWithLocation(value, node, simpleLocs));
}
if (node.type === 'Identifier' && isNote(node.name)) {
this.skip();
@ -66,8 +66,7 @@ function isBackTickString(node, parent) {
return node.type === 'TemplateLiteral' && parent.type !== 'TaggedTemplateExpression';
}
function miniWithLocation(value, node) {
const simpleLocs = false; // TODO: use simple locs and refactor Pattern.withMiniLocation
function miniWithLocation(value, node, simpleLocs) {
let locs;
const { start: fromOffset, end: toOffset } = node;
if (simpleLocs) {