Skip to content

Commit

Permalink
feat: parse source map (#386)
Browse files Browse the repository at this point in the history
This PR adds a helper for parsing a source map
from a recording.
  • Loading branch information
OrKoN committed Nov 18, 2022
1 parent ac253a4 commit 78c4b98
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export * from './Schema.js';
export * as Schema from './Schema.js';
export * from './SchemaUtils.js';
export { StringifyExtension } from './StringifyExtension.js';
export { stringify, stringifyStep, StringifyOptions } from './stringify.js';
export {
stringify,
stringifyStep,
StringifyOptions,
parseSourceMap,
} from './stringify.js';
export { LineWriter } from './LineWriter.js';
export { RunnerExtension } from './RunnerExtension.js';
export { createRunner, Runner } from './Runner.js';
Expand Down
24 changes: 22 additions & 2 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export interface StringifyOptions {
indentation?: string;
}

const SOURCE_MAP_PREFIX = '//# recorderSourceMap=';

/**
* The format is [version, [lineNo, length], [lineNo, length] ... [lineNo, length]].
*/
export type SourceMap = Array<number>;

/**
* Stringifes an entire recording. The following hooks are invoked with the `flow` parameter containing the entire flow:
* - `beforeAllSteps` (once)
Expand All @@ -47,7 +54,6 @@ export async function stringify(

await ext.beforeAllSteps?.(out, flow);

// version, [lineNo, length], [lineNo, length] ...
const sourceMap: Array<number> = [1];
for (const step of flow.steps) {
const firstLine = out.getSize();
Expand All @@ -59,7 +65,7 @@ export async function stringify(
}
await ext.afterAllSteps?.(out, flow);

out.appendLine('//# recorderSourceMap=' + vlq.encode(sourceMap));
out.appendLine(SOURCE_MAP_PREFIX + vlq.encode(sourceMap));

return out.toString();
}
Expand Down Expand Up @@ -92,3 +98,17 @@ export async function stringifyStep(

return out.toString();
}

/**
* Extracts a source map from a text.
*/
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)) {
return vlq.decode(line.trim().substring(SOURCE_MAP_PREFIX.length));
}
}
return;
}
12 changes: 11 additions & 1 deletion test/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

import { stringify } from '../src/stringify.js';
import { parseSourceMap, stringify } from '../src/stringify.js';
import { assert } from 'chai';
import { StringifyExtension } from '../src/StringifyExtension.js';
import { Step, StepType, UserFlow } from '../src/Schema.js';
Expand Down Expand Up @@ -275,4 +275,14 @@ describe('stringify', () => {
vlq.decode(sourceMapLine?.split('//# recorderSourceMap=').pop() as string)
);
});

it('should parse a source map', async () => {
const sourceMap = parseSourceMap(`
test
test
test
//# recorderSourceMap=CCG
`);
assert.deepStrictEqual(sourceMap, [1, 1, 3]);
});
});

0 comments on commit 78c4b98

Please sign in to comment.