From 057f7473abb9e6525e36377999069195592eb0c3 Mon Sep 17 00:00:00 2001 From: Milan Hauth Date: Mon, 5 Oct 2020 13:01:56 +0200 Subject: [PATCH] add print option sourceMapEncodeMappings --- src/print/index.ts | 4 +++- test/test.ts | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/print/index.ts b/src/print/index.ts index eb8bd19..c8b5dad 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -16,6 +16,7 @@ type PrintOptions = { file?: string; sourceMapSource?: string; sourceMapContent?: string; + sourceMapEncodeMappings?: boolean; // default true getName?: (name: string) => string; }; @@ -93,7 +94,8 @@ export function print(node: Node, opts: PrintOptions = {}): { code: string, map: names: [] as string[], sources: [opts.sourceMapSource || null], sourcesContent: [opts.sourceMapContent || null], - mappings: encode(mappings) + mappings: opts.sourceMapEncodeMappings == undefined || opts.sourceMapEncodeMappings + ? encode(mappings) : mappings }; Object.defineProperties(map, { diff --git a/test/test.ts b/test/test.ts index 368c2fe..172471f 100644 --- a/test/test.ts +++ b/test/test.ts @@ -5,6 +5,7 @@ import { generateRandomJS } from 'eslump'; import * as codered from '../src/index'; import { ObjectExpression, Identifier, Node } from 'estree'; import { walk } from 'estree-walker'; +import { decode } from 'sourcemap-codec'; const d = (str: string) => str.replace(/^\t{5}/gm, '').trim(); @@ -459,6 +460,27 @@ describe('codered', () => { assert.throws(() => print(b`let foo = @bar;`), { message: 'Unhandled sigil @bar' }); }); + it('can return sourcemap with decoded mappings', () => { + const dir = 'sourcemap'; + const input = require(`./samples/${dir}/input.js`)({ b, x, p }); + + const expected = { + code: read(`test/samples/${dir}/expected.js`), + map: JSON.parse(read(`test/samples/${dir}/expected.js.map`) || '{}') + }; + if (expected.map && expected.map.mappings) { + expected.map.mappings = decode(expected.map.mappings); + } + + const actual = print(input, { + sourceMapSource: 'input.js', + getName: name => name.toUpperCase(), + sourceMapEncodeMappings: false + }); + + assert.deepEqual(actual.map, expected.map); + }); + it.skip('passes fuzz testing', () => { for (let i = 0; i < 100; i += 1) { const js = generateRandomJS({