harmonyc
Version:
Harmony Code - model-driven BDD for Vitest
52 lines (51 loc) • 1.49 kB
JavaScript
import { SourceMapGenerator } from 'source-map-js';
export class OutFile {
constructor(name) {
this.name = name;
this.lines = [];
this.level = 0;
this.sm = new SourceMapGenerator();
this.indentSpaces = 2;
}
indent(fn) {
this.level++;
try {
fn();
}
finally {
this.level--;
}
}
print(...lines) {
const l = this.lines.length;
this.lines.push(...lines.map((line) => ' '.repeat(this.level * this.indentSpaces) + line));
if (this.currentLoc)
for (const i of lines.keys()) {
this.sm.addMapping({
source: this.currentLoc.fileName,
original: {
line: this.currentLoc.line,
column: this.currentLoc.column,
},
generated: {
line: l + i,
column: this.level * this.indentSpaces,
},
});
}
return this;
}
loc({ location }) {
this.currentLoc = location;
return this;
}
get value() {
let res = this.lines.join('\n');
if (this.currentLoc) {
res +=
`\n\n//# sour` + // not for this file ;)
`ceMappingURL=data:application/json,${encodeURIComponent(this.sm.toString())}`;
}
return res;
}
}