Add getSourceMap to Printer and move sourceText to BuilderOptions
MODIFY
packages/@romejs/js-compiler/api/compile.ts
packages/@romejs/js-compiler/api/compile.ts
@@ -48,7 +48,8 @@ export default async function compile(
sourceFileName: filename,
inputSourceMap: req.inputSourceMap,
sourceMaps: true,
- }, sourceText);
+ sourceText,
+ });
const res: CompileResult = {
compiledCode: generator.getCode(),

MODIFY
packages/@romejs/js-compiler/api/lint.ts
packages/@romejs/js-compiler/api/lint.ts
@@ -58,7 +58,8 @@ export default async function lint(req: FormatRequest): Promise<LintResult> {
const generator = formatJS(newAst, {
typeAnnotations: true,
format: 'pretty',
- }, sourceText);
+ sourceText,
+ });
formattedCode = generator.getCode();
}

MODIFY
packages/@romejs/js-formatter/Builder.ts
packages/@romejs/js-formatter/Builder.ts
@@ -40,6 +40,7 @@ export type BuilderOptions = {
sourceMapTarget?: string;
sourceRoot?: string;
sourceFileName?: string;
+ sourceText?: string;
};
type PrintJoinOptions = Omit<GroupToken, 'type' | 'groups'> & {

MODIFY
packages/@romejs/js-formatter/Printer.ts
packages/@romejs/js-formatter/Printer.ts
@@ -22,7 +22,12 @@ import {
PositionMarkerToken,
} from './tokens';
import {BuilderOptions} from './Builder';
-import {Mappings, SourceMapConsumer} from '@romejs/codec-source-map';
+import {
+ Mappings,
+ SourceMapConsumer,
+ SourceMapGenerator,
+ SourceMap,
+} from '@romejs/codec-source-map';
import {Number1, Number0, number0, number1, get0, inc, dec} from '@romejs/ob1';
import {SourceLocation} from '@romejs/parser-core';
@@ -657,4 +662,23 @@ export default class Printer {
getMappings(): Mappings {
return this.mappings.slice();
}
+
+ getSourceMap(): SourceMap {
+ const {options} = this;
+
+ const map = new SourceMapGenerator({
+ file: options.sourceMapTarget,
+ sourceRoot: options.sourceRoot,
+ });
+
+ if (options.sourceFileName !== undefined) {
+ map.setSourceContent(options.sourceFileName, options.sourceText);
+ }
+
+ for (const mapping of this.mappings) {
+ map.addMapping(mapping);
+ }
+
+ return map.toJSON();
+ }
}

MODIFY
packages/@romejs/js-formatter/index.ts
packages/@romejs/js-formatter/index.ts
@@ -11,11 +11,7 @@ import Printer from './Printer';
export {BuilderOptions, Builder};
-export function formatJS(
- ast: AnyNode,
- opts: BuilderOptions,
- code: string = '',
-): Printer {
+export function formatJS(ast: AnyNode, opts: BuilderOptions): Printer {
const builder = new Builder(opts);
const tokens = builder.tokenize(ast, MOCK_PARENT);
const printer = new Printer(opts);

Add getSourceMap to Printer and move sourceText to BuilderOptions