Skip to content

Commit

Permalink
Merge pull request #69 from azizghuloum/tackle-sourcemaps
Browse files Browse the repository at this point in the history
Added support for sourcemaps.  Runtime errors signal properly.
  • Loading branch information
azizghuloum authored Jan 2, 2025
2 parents b5b6e1b + a4ee5ad commit 864e7c7
Show file tree
Hide file tree
Showing 26 changed files with 338 additions and 132 deletions.
9 changes: 2 additions & 7 deletions files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { initial_step } from "./src/expander";
import { pprint } from "./src/pprint";
import { StxError, syntax_error } from "./src/stx-error";
import { preexpand_helpers } from "./src/preexpand-helpers";
import { source_file } from "./src/ast";
import { get_globals, init_global_context } from "./src/global-module";

const test_dir = __dirname + "/tests";
Expand Down Expand Up @@ -39,11 +38,7 @@ async function compile_script(filename: string, test_name: string) {
return k();
},
};
const source_file: source_file = {
package: { name: "@rewrite-ts/test", version: "0.0.0" },
path: filename,
};
const [_loc0, expand] = initial_step(parse(code, source_file), test_name, ["es2024.full"]);
const [_loc0, expand] = initial_step(parse(code, test_name), test_name, ["es2024.full"]);
const result = await (async () => {
try {
const { loc } = await expand(helpers);
Expand All @@ -65,7 +60,7 @@ async function compile_script(filename: string, test_name: string) {
function q(str: string): string {
return "`" + str + "`";
}
const prog = await pprint(result.loc);
const prog = await pprint(result.loc, { prettify: true });
const out =
`## ${q(test_name)}\n\n` +
`### Status: ${q(result.name)}\n\n` +
Expand Down
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.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"lint": "eslint .",
"preview": "vite preview"
},
"overrides": {
"vite-plugin-node-polyfills": {
"vite": "^6.0.0"
}
},
"dependencies": {
"@babel/code-frame": "^7.26.2",
"@codemirror/lang-javascript": "^6.2.2",
Expand All @@ -26,12 +31,14 @@
"commander": "^12.1.0",
"ignore": "^6.0.2",
"index-to-position": "^1.0.0",
"js-base64": "^3.7.7",
"json-stable-stringify": "^1.2.1",
"json-stringify-pretty-compact": "^4.0.0",
"prettier": "^3.4.2",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-router-dom": "^7.1.1",
"source-map": "^0.7.4",
"typescript": "^5.5.3",
"zipper": "github:azizghuloum/zipper"
},
Expand Down
9 changes: 5 additions & 4 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { atom_tag, list_tag } from "./tags";

export type no_source = false;

export type source_file = { package: { name: string; version: string }; path: string };
export type pos = number | { line: number; column: number; offset: number };

export type source = {
type: "origin";
p: number;
e: number;
f: source_file;
s: pos;
e: pos;
name: string | undefined;
cuid: string;
};

export type src = no_source | source;
Expand Down
3 changes: 3 additions & 0 deletions src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ const list_handlers_table: { [tag in list_tag]: "descend" | "stop" | "todo" } =
type_operator: "todo",
type_predicate: "todo",
type_reference: "todo",
new_expression: "descend",
throw_statement: "descend",
element_access_expression: "descend",
syntax_list: "descend",
};

Expand Down
32 changes: 23 additions & 9 deletions src/library-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
get_exported_identifiers_from_rib,
exported_identifiers,
} from "./preexpand-helpers";
import { AST, source_file } from "./ast";
import { AST } from "./ast";
import { normalize } from "node:path";
import { Binding, CompilationUnit, Context, Loc, Rib } from "./syntax-structures";
import stringify from "json-stringify-pretty-compact";
import { init_global_context } from "./global-module";
import { parse_dts } from "./parse-dts";

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

type module_state =
| { type: "initial" }
Expand Down Expand Up @@ -374,11 +374,7 @@ class RtsModule extends Module {
const code = await fs.readFile(this.path, { encoding: "utf-8" });
const my_pkg = this.pkg;
const my_path = this.pkg_relative_path;
const source_file: source_file = {
package: { name: my_pkg.name, version: my_pkg.version },
path: my_path,
};
const [_loc0, expand] = initial_step(parse(code, source_file), state.cid, this.libs);
const [_loc0, expand] = initial_step(parse(code, state.cid), state.cid, this.libs);
try {
const helpers = this.get_preexpand_helpers(my_pkg, my_path);
const { loc, unit, context, modular } = await expand(helpers);
Expand Down Expand Up @@ -406,7 +402,20 @@ class RtsModule extends Module {
};
const code_path = this.get_generated_code_absolute_path();
await fs.mkdir(dirname(code_path), { recursive: true });
await fs.writeFile(code_path, await pprint(loc));
await fs.writeFile(
code_path,
await pprint(loc, {
prettify: false,
map: {
filename: basename(code_path),
resolve: async (cuid: string) => {
const mod = this.find_module_by_cid(cuid);
assert(mod !== undefined);
return relative(dirname(code_path), mod.path);
},
},
}),
);
await fs.writeFile(this.get_proxy_path(), proxy_code);
const mtime = Date.now();
await fs.writeFile(this.get_json_path(), stringify(json_content));
Expand All @@ -415,7 +424,12 @@ class RtsModule extends Module {
} catch (error) {
this.state = { type: "error", reason: String(error) };
if (error instanceof StxError) {
await print_stx_error(error, this.library_manager);
await print_stx_error(error, {
get_module_by_cuid: (cuid) => {
return this.find_module_by_cid(cuid);
},
//this.library_manager
});
} else {
console.error(error);
}
Expand Down
7 changes: 7 additions & 0 deletions src/llhelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export function llmap<X, Y>(ls: LL<X>, f: (x: X) => Y): LL<Y> {
return ls === null ? null : [f(ls[0]), llmap(ls[1], f)];
}

export function llforeach<X, Y>(ls: LL<X>, f: (x: X) => Y): void {
if (ls !== null) {
f(ls[0]);
llforeach(ls[1], f);
}
}

export function array_to_ll<X>(a: X[]): LL<X> {
let ll: LL<X> = null;
for (let i = a.length - 1; i >= 0; i--) ll = [a[i], ll];
Expand Down
Loading

0 comments on commit 864e7c7

Please sign in to comment.