Skip to content

Commit

Permalink
Merge pull request #71 from azizghuloum/restore-prettier
Browse files Browse the repository at this point in the history
sourcemaps now map properly in prettified code
  • Loading branch information
azizghuloum authored Jan 2, 2025
2 parents 864e7c7 + 12bfcfa commit f0f11a3
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 16 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
"@babel/code-frame": "^7.26.2",
"@codemirror/lang-javascript": "^6.2.2",
"@types/babel__code-frame": "^7.0.6",
"@types/diff": "^6.0.0",
"@types/json-stable-stringify": "^1.1.0",
"@types/node": "^22.10.2",
"@uiw/codemirror-theme-abcdef": "^4.23.7",
"@uiw/react-codemirror": "^4.23.7",
"commander": "^12.1.0",
"diff": "^7.0.0",
"ignore": "^6.0.2",
"index-to-position": "^1.0.0",
"js-base64": "^3.7.7",
Expand Down
2 changes: 1 addition & 1 deletion src/library-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import stringify from "json-stringify-pretty-compact";
import { init_global_context } from "./global-module";
import { parse_dts } from "./parse-dts";

const cookie = "rewrite-ts-021";
const cookie = "rewrite-ts-022";

type module_state =
| { type: "initial" }
Expand Down
57 changes: 51 additions & 6 deletions src/pprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as prettier_estree from "prettier/plugins/estree";
import { SourceMapGenerator } from "source-map";
import { Base64 } from "js-base64";
import { assert } from "./assert";
import * as Diff from "diff";

type n = { val: string; src: source | false };
type ns = n | ns[];
Expand Down Expand Up @@ -202,7 +203,9 @@ async function add_src_map(code: string, ls: n[], options: map_options): Promise
});
const map_string = srcmap.toString();
const base64 = Base64.encode(map_string);
return `${code}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64}\n`;
return (
`${code}\n` + `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64}\n`
);
}

export async function pretty_print(code: string) {
Expand All @@ -223,12 +226,54 @@ type options = {
map?: map_options;
};

type d = { added: boolean; removed: boolean; value: string };

function remap(ls: n[], diffs: d[]): n[] {
const new_ls: n[] = [];
let i = 0,
j = 0;
while (i < diffs.length) {
const { added, removed, value } = diffs[i];
if (added) {
if (removed) {
throw new Error("invalid diff");
} else {
// new text added
new_ls.push({ src: false, val: value });
}
} else {
if (removed) {
/* nothing added but stuff removed */
let k = 0;
while (k < value.length) {
const item = ls[j];
k += item.val.length;
j += 1;
}
assert(k === value.length);
} else {
/* nothing added and nothing removed */
let k = 0;
while (k < value.length) {
const item = ls[j];
new_ls.push(item);
k += item.val.length;
j += 1;
}
assert(k === value.length);
}
}
i += 1;
}
assert(j === ls.length);
return new_ls;
}

export async function pprint(loc: Loc, options: options): Promise<string> {
const ls = ns_flatten(loc_to_ns(loc));
const code = ls.map((x) => x.val).join("");
return options.prettify
? await pretty_print(code)
: options.map
? add_src_map(code, ls, options.map)
: code;
const pretty = await pretty_print(code);
const diff = Diff.diffWordsWithSpace(code, pretty);
const pretty_ls = remap(ls, diff);
return options.map ? add_src_map(pretty, pretty_ls, options.map) : pretty;
}
2 changes: 1 addition & 1 deletion test-project/.rts/main.rts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cid": "test-project/main.rts rewrite-ts-visualized 0.0.0",
"cookie": "rewrite-ts-021",
"cookie": "rewrite-ts-022",
"imports": [
{
"pkg": {"name": "rewrite-ts-visualized", "version": "0.0.0"},
Expand Down
9 changes: 7 additions & 2 deletions test-project/.rts/main.rts.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import { type t_2 as t_2 , x_1 as x_3 , f_3 as f_4 } from "./mod.rts.ts" ; import { expect as expect_5 } from "vitest" ; export const y_1 : t_2 = ( x_3 + f_4 ) ; console . log ( expect_5 ) ; console . log ( y_1 ) ;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL21haW4ucnRzIiwiLi4vbW9kLnJ0cyJdLCJuYW1lcyI6WyJ5IiwidCIsIngiLCJmIiwiY29uc29sZSIsImxvZyIsImV4cGVjdCJdLCJtYXBwaW5ncyI6ImdJQUVBLE1BQU1BLElBQUMsRUFBRUMsSUFBRSxJQUFFQyxJQUFFLEVDR3lCQyxRREZ4Q0MsUUFBTyxFQUFDQyxJQUFHLEVBQUNDLFNBQU0sSUFDbEJGLFFBQU8sRUFBQ0MsSUFBRyxFQUFDTCxJQUFDIiwiZmlsZSI6Im1haW4ucnRzLnRzIn0=
import { type t_2 as t_2, x_1 as x_3, f_3 as f_4 } from "./mod.rts.ts";
import { expect as expect_5 } from "vitest";
export const y_1: t_2 = x_3 + f_4;
console.log(expect_5);
console.log(y_1);

//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL21haW4ucnRzIiwiLi4vbW9kLnJ0cyJdLCJuYW1lcyI6WyJ5IiwidCIsIngiLCJmIiwiY29uc29sZSIsImxvZyIsImV4cGVjdCJdLCJtYXBwaW5ncyI6Ijs7T0FFQSxNQUFNQSxHQUFDLEVBQUVDLElBQUUsRUFBRUMsSUFBRSxFQ0d5QkM7QURGeENDLE9BQU8sQ0FBQ0MsR0FBRyxDQUFDQyxRQUFNO0FBQ2xCRixPQUFPLENBQUNDLEdBQUcsQ0FBQ0wsR0FBQyIsImZpbGUiOiJtYWluLnJ0cy50cyJ9
2 changes: 1 addition & 1 deletion test-project/.rts/mod.rts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cid": "test-project/mod.rts rewrite-ts-visualized 0.0.0",
"cookie": "rewrite-ts-021",
"cookie": "rewrite-ts-022",
"imports": [],
"exported_identifiers": {
"x": [
Expand Down
7 changes: 5 additions & 2 deletions test-project/.rts/mod.rts.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export const x_1 = 12 ; export type t_2 = number ; export const f_3 = 13 ;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL21vZC5ydHMiXSwibmFtZXMiOlsieCIsInQiLCJmIl0sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLE1BQU1BLElBQUUsRUFBRSxHQUFFLEVBQ25CLE9BQU8sS0FBS0MsSUFBRSxFQUFFLE9BQU0sU0FFdEIsTUFBTUMsSUFBRSxFQUFFIiwiZmlsZSI6Im1vZC5ydHMudHMifQ==
export const x_1 = 12;
export type t_2 = number;
export const f_3 = 13;

//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL21vZC5ydHMiXSwibmFtZXMiOlsieCIsInQiLCJmIl0sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLE1BQU1BLElBQUUsRUFBRSxFQUFFO0FBQ25CLE9BQU8sS0FBS0MsSUFBRSxFQUFFLE1BQU07T0FFdEIsTUFBTUMsSUFBRSxFQUFFIiwiZmlsZSI6Im1vZC5ydHMudHMifQ==
2 changes: 1 addition & 1 deletion test-project/.rts/sourcemap1.test.rts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cid": "test-project/sourcemap1.test.rts rewrite-ts-visualized 0.0.0",
"cookie": "rewrite-ts-021",
"cookie": "rewrite-ts-022",
"imports": [
{
"pkg": {"name": "vitest", "version": "2.1.8"},
Expand Down
12 changes: 10 additions & 2 deletions test-project/.rts/sourcemap1.test.rts.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
import { suite as suite_1 , test as test_2 , expect as expect_5 } from "vitest" ; suite_1 ( "source mapping for errors" , ( ( ) => { test_2 ( "simple error" , ( ( ) => { const err_3 = new Error ( "HERE" ) ; const trace_4 = ( ( err_3 . stack || "" ) ) . split ( "\n" ) ; expect_5 ( trace_4 [ 1 ] ) . toMatch ( /\/sourcemap1\.test\.rts:5:17$/ ) ; } ) ) ; } ) ) ;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NvdXJjZW1hcDEudGVzdC5ydHMiXSwibmFtZXMiOlsic3VpdGUiLCJ0ZXN0IiwiZXJyIiwiRXJyb3IiLCJ0cmFjZSIsInN0YWNrIiwic3BsaXQiLCJleHBlY3QiLCJ0b01hdGNoIl0sIm1hcHBpbmdzIjoia0ZBRUFBLFFBQUssRUFBQyw0QkFBMkIsSUFBRSxFQUFDLEVBQUUsR0FBRyxFQUN2Q0MsT0FBSSxFQUFDLGVBQWMsSUFBRSxFQUFDLEVBQUUsR0FBRyxFQUN6QixNQUFNQyxNQUFJLEVBQUUsSUFBSUMsTUFBSyxFQUFDLE9BQU0sSUFHNUIsTUFBTUMsUUFBTSxFQUFFLElBQUNGLE1BQUcsRUFBQ0csTUFBTSxHQUFHLEtBQUUsRUFBQyxFQUFDQyxNQUFLLEVBQUMsS0FBSSxJQUMxQ0MsU0FBTSxFQUFDSCxRQUFLLEVBQUMsRUFBQyxFQUFDLEVBQUMsRUFBQ0ksUUFBTyxFQUFDLGdDQUErQixJQUMxRCxJQUFDLElBQ0gsSUFBQyIsImZpbGUiOiJzb3VyY2VtYXAxLnRlc3QucnRzLnRzIn0=
import { suite as suite_1, test as test_2, expect as expect_5 } from "vitest";
suite_1("source mapping for errors", () => {
test_2("simple error", () => {
const err_3 = new Error("HERE");
const trace_4 = (err_3.stack || "").split("\n");
expect_5(trace_4[1]).toMatch(/\/sourcemap1\.test\.rts:5:17$/);
});
});

//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NvdXJjZW1hcDEudGVzdC5ydHMiXSwibmFtZXMiOlsic3VpdGUiLCJ0ZXN0IiwiZXJyIiwiRXJyb3IiLCJ0cmFjZSIsInN0YWNrIiwic3BsaXQiLCJleHBlY3QiLCJ0b01hdGNoIl0sIm1hcHBpbmdzIjoiO0FBRUFBLE9BQUssQ0FBQywyQkFBMkIsR0FBRyxFQUFFLEdBQUc7RUFDdkNDLE1BQUksQ0FBQyxjQUFjLEdBQUcsRUFBRSxHQUFHO0lBQ3pCLE1BQU1DLE1BQUksRUFBRSxJQUFJQyxLQUFLLENBQUMsTUFBTTtJQUc1QixNQUFNQyxRQUFNLEVBQUUsQ0FBQ0YsS0FBRyxDQUFDRyxNQUFNLEdBQUcsR0FBRyxDQUFDQyxLQUFLLENBQUMsSUFBSTtJQUMxQ0MsUUFBTSxDQUFDSCxPQUFLLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQ0ksT0FBTyxDQUFDLCtCQUErQjtFQUMxRDtBQUNGIiwiZmlsZSI6InNvdXJjZW1hcDEudGVzdC5ydHMudHMifQ==

0 comments on commit f0f11a3

Please sign in to comment.