UNPKG

harmonyc

Version:

Harmony Code - model-driven BDD for Vitest

61 lines (60 loc) 1.75 kB
import { SourceNode } from 'source-map-js'; export class OutFile { constructor(name, sourceFile) { this.name = name; this.sourceFile = sourceFile; this.level = 0; this.indentSpaces = 2; this.sm = new SourceNode(0, 0, sourceFile); } indent(fn) { this.level++; try { fn(); } finally { this.level--; } } get currentIndent() { return ' '.repeat(this.level * this.indentSpaces); } clear() { this.sm = new SourceNode(0, 0, this.sourceFile); } printn(line, start, name, end) { this.write(this.currentIndent + line, start, name, end); } print(line, start, name, end) { this.write(this.currentIndent + line + '\n', start, name, end); } write(line, start, name, end) { const chunk = line; if (start) { this.sm.add(new SourceNode(start.line, start.column, this.sourceFile, chunk, name)); } else { this.sm.add(new SourceNode(null, null, null, chunk)); } if (end) { this.sm.add(new SourceNode(end.line, end.column, this.sourceFile)); } } nl() { this.write('\n'); } get sourceMap() { return this.sm.toStringWithSourceMap({ file: this.name }).map.toJSON(); } get valueWithoutSourceMap() { return this.sm.toString(); } get value() { const { code, map } = this.sm.toStringWithSourceMap({ file: this.name }); let res = code; res += `\n//# sour` + // not for this file ;) `ceMappingURL=data:application/json,${encodeURIComponent(map.toString())}`; return res; } }