From c93fd900bcd771675e62057b1580f4c224bbfdfb Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Tue, 8 Nov 2022 21:04:41 +0100 Subject: [PATCH] fix: tests --- packages/transpiler/test/transpiler.test.mjs | 16 ++++++++-------- packages/transpiler/transpiler.mjs | 9 ++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/transpiler/test/transpiler.test.mjs b/packages/transpiler/test/transpiler.test.mjs index bfb77062..6fa79347 100644 --- a/packages/transpiler/test/transpiler.test.mjs +++ b/packages/transpiler/test/transpiler.test.mjs @@ -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( diff --git a/packages/transpiler/transpiler.mjs b/packages/transpiler/transpiler.mjs index c2858851..a0b24512 100644 --- a/packages/transpiler/transpiler.mjs +++ b/packages/transpiler/transpiler.mjs @@ -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) {