diff --git a/src/main.ts b/src/main.ts index 8bfb7343..b5096b2a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -25,6 +25,7 @@ export { stringify, stringifyStep, parseSourceMap, + stripSourceMap, StringifyOptions, SourceMap, } from './stringify.js'; diff --git a/src/stringify.ts b/src/stringify.ts index 8cd0cbe5..bf9cdc4e 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -99,6 +99,10 @@ export async function stringifyStep( return out.toString(); } +function isSourceMapLine(line: string): boolean { + return line.trim().startsWith(SOURCE_MAP_PREFIX); +} + /** * Extracts a source map from a text. */ @@ -106,9 +110,14 @@ export function parseSourceMap(text: string): SourceMap | undefined { const lines = text.split('\n'); for (let i = lines.length - 1; i >= 0; i--) { const line = lines[i] as string; - if (line.trim().startsWith(SOURCE_MAP_PREFIX)) { + if (isSourceMapLine(line)) { return decode(line.trim().substring(SOURCE_MAP_PREFIX.length)); } } return; } + +export function stripSourceMap(text: string): string { + const lines = text.split('\n'); + return lines.filter((line) => !isSourceMapLine(line)).join('\n'); +} diff --git a/test/stringify.test.ts b/test/stringify.test.ts index 8b5d54ab..5768f806 100644 --- a/test/stringify.test.ts +++ b/test/stringify.test.ts @@ -14,7 +14,7 @@ limitations under the License. */ -import { parseSourceMap, stringify } from '../src/stringify.js'; +import { parseSourceMap, stringify, stripSourceMap } from '../src/stringify.js'; import { assert } from 'chai'; import { StringifyExtension } from '../src/StringifyExtension.js'; import { Step, StepType, UserFlow } from '../src/Schema.js'; @@ -285,4 +285,21 @@ describe('stringify', () => { `); assert.deepStrictEqual(sourceMap, [1, 1, 3]); }); + + it('should strip a source map', async () => { + const sourceMap = stripSourceMap(` + test + test + test + //# recorderSourceMap=BBD + `); + assert.deepStrictEqual( + sourceMap, + ` + test + test + test + ` + ); + }); });