Skip to content

Commit

Permalink
removed redundant file field in source locations
Browse files Browse the repository at this point in the history
  • Loading branch information
azizghuloum committed Jan 1, 2025
1 parent 2feea2f commit e0d99ee
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 63 deletions.
9 changes: 1 addition & 8 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,13 +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), 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 Down
3 changes: 1 addition & 2 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 source_file = { package: { name: string; version: string }; path: string };

export type pos = number | { line: number; column: number; offset: number };

Expand All @@ -12,7 +12,6 @@ export type source = {
s: pos;
e: pos;
name: string | undefined;
f: source_file;
cuid: string;
};

Expand Down
17 changes: 9 additions & 8 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-019";
const cookie = "rewrite-ts-020";

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), 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 @@ -415,7 +411,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
15 changes: 7 additions & 8 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AST, source_file, src } from "./ast";
import { AST, src } from "./ast";
import { atom_tag, list_tag } from "./tags";
import { array_to_ll, LL, llappend, llforeach } from "./llhelpers";
import TS, { SyntaxKind } from "typescript";
Expand Down Expand Up @@ -82,15 +82,14 @@ function left_associate(op: string, [head, tail]: [AST, LL<AST>]): AST {
}
}

function absurdly(node: TS.Node, source: TS.SourceFile, f: source_file, cuid: string): AST {
function absurdly(node: TS.Node, source: TS.SourceFile, cuid: string): AST {
const children = node.getChildren(source);
if (children.length === 0 && node.kind !== SyntaxKind.SyntaxList) {
const content = node.getText(source);
const src: src = {
type: "origin",
s: node.end - content.length,
e: node.end,
f,
name: node.kind === TS.SyntaxKind.Identifier ? content : undefined,
cuid,
};
Expand Down Expand Up @@ -161,8 +160,8 @@ function absurdly(node: TS.Node, source: TS.SourceFile, f: source_file, cuid: st
}
}
} else {
const ls = children.filter((x) => x.kind !== null).map((x) => absurdly(x, source, f, cuid));
const src: src = { type: "origin", s: node.pos, e: node.end, f, name: undefined, cuid };
const ls = children.filter((x) => x.kind !== null).map((x) => absurdly(x, source, cuid));
const src: src = { type: "origin", s: node.pos, e: node.end, name: undefined, cuid };
const content = array_to_ll(ls);
{
const tag = remove_singleton_identifier[node.kind];
Expand Down Expand Up @@ -234,7 +233,7 @@ function absurdly(node: TS.Node, source: TS.SourceFile, f: source_file, cuid: st
assert(
lt.src &&
rt.src &&
lt.src.f === rt.src.f &&
lt.src.cuid === rt.src.cuid &&
typeof lt.src.s === "number" &&
typeof rt.src.e === "number" &&
lt.src.s < rt.src.e,
Expand Down Expand Up @@ -353,14 +352,14 @@ function remap_source(ast: AST, code: string) {
remap(ast);
}

export function parse(code: string, f: source_file, cuid: string): AST {
export function parse(code: string, cuid: string): AST {
try {
const options: TS.CreateSourceFileOptions = {
languageVersion: TS.ScriptTarget.ESNext,
jsDocParsingMode: TS.JSDocParsingMode.ParseNone,
};
const src = TS.createSourceFile("code.tsx", code, options);
const ast = absurdly(src, src, f, cuid);
const ast = absurdly(src, src, cuid);
remap_source(ast, code);
return ast;
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/preexpand-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type import_resolution = {
// };

export type imported_module = {
path: string;
imported_modules: imported_module[];
dependant_modules: imported_module[];
resolve_exported_identifier: (name: string, loc: Loc) => Promise<import_resolution[]>;
Expand Down
10 changes: 5 additions & 5 deletions src/stx-error.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { assert } from "./assert";
import { Loc, STX } from "./syntax-structures";
import { isolate, unisolate } from "./zipper";
import indexToPosition from "index-to-position";
import { codeFrameColumns } from "@babel/code-frame";
import fs from "node:fs/promises";
import { llmap, llreduce } from "./llhelpers";
import { AST, source } from "./ast";
import { assert } from "./assert";

export class StxError {
name: string;
Expand Down Expand Up @@ -36,17 +36,17 @@ export const in_isolation: <G extends { loc: Loc }>(
};

type LibraryManager = {
get_package: (name: string, version: string) => { dir: string } | undefined;
get_module_by_cuid: (cuid: string) => { path: string } | undefined;
};

export async function print_stx_error(error: StxError, library_manager: LibraryManager) {
console.error(`${error.name}: ${error.error}`);
if (error.info) console.error(error.info);
const ls = loc_src_origins(error.loc.t);
for (const x of ls) {
const pkg = library_manager.get_package(x.f.package.name, x.f.package.version);
assert(pkg !== undefined);
const full_path = pkg.dir + "/" + x.f.path;
const mod = library_manager.get_module_by_cuid(x.cuid);
assert(mod !== undefined, `cannot find module with cuid ${x.cuid}`);
const full_path = mod.path;
const code = await fs.readFile(full_path, { encoding: "utf8" });
const pos0 = typeof x.s === "number" ? indexToPosition(code, x.s + 1, { oneBased: true }) : x.s;
const pos1 = typeof x.e === "number" ? indexToPosition(code, x.e, { oneBased: true }) : x.e;
Expand Down
12 changes: 3 additions & 9 deletions src/syntax-core-patterns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from "./assert";
import { AST, source_file } from "./ast";
import { AST } from "./ast";
import { LL, llappend, llmap, llreduce, llreverse, ll_to_array } from "./llhelpers";
import { syntax_error } from "./stx-error";
import {
Expand Down Expand Up @@ -592,15 +592,9 @@ export const core_handlers: { [k: string]: walker } = {
define_rewrite_rules,
};

export const core_patterns = (
parse: (code: string, source_file: source_file, cuid: string) => AST,
) => {
export const core_patterns = (parse: (code: string, cuid: string) => AST) => {
const pattern = (code: string) => {
const src: source_file = {
package: { name: "rewrite-ts", version: "0.0.0" },
path: "internal-patterns",
};
const ast = parse(code, src, "internal-patterns rewrite-ts 0.0.0");
const ast = parse(code, "internal-patterns rewrite-ts 0.0.0");
assert(ast.type === "list" && ast.tag === "program");
const bodies = ast.content;
assert(bodies !== null);
Expand Down
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-019",
"cookie": "rewrite-ts-020",
"imports": [
{
"pkg": {"name": "rewrite-ts-visualized", "version": "0.0.0"},
Expand Down
16 changes: 1 addition & 15 deletions 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-019",
"cookie": "rewrite-ts-020",
"imports": [],
"exported_identifiers": {
"x": [
Expand Down Expand Up @@ -67,13 +67,6 @@
"type": "origin",
"s": {"line": 5, "column": 28, "offset": 89},
"e": {"line": 5, "column": 32, "offset": 93},
"f": {
"package": {
"name": "rewrite-ts-visualized",
"version": "0.0.0"
},
"path": "test-project/mod.rts"
},
"name": "fref",
"cuid": "test-project/mod.rts rewrite-ts-visualized 0.0.0"
}
Expand Down Expand Up @@ -107,13 +100,6 @@
"type": "origin",
"s": {"line": 5, "column": 40, "offset": 101},
"e": {"line": 5, "column": 41, "offset": 102},
"f": {
"package": {
"name": "rewrite-ts-visualized",
"version": "0.0.0"
},
"path": "test-project/mod.rts"
},
"name": "f",
"cuid": "test-project/mod.rts rewrite-ts-visualized 0.0.0"
}
Expand Down
2 changes: 1 addition & 1 deletion test-project/.rts/test1.test.rts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cid": "test-project/test1.test.rts rewrite-ts-visualized 0.0.0",
"cookie": "rewrite-ts-019",
"cookie": "rewrite-ts-020",
"imports": [
{
"pkg": {"name": "vitest", "version": "2.1.8"},
Expand Down
7 changes: 1 addition & 6 deletions ui/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { parse } from "../src/parse";
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";

type ExampleProps = {
Expand Down Expand Up @@ -92,12 +91,8 @@ function StepperView({ step, step_number }: { step: Step; step_number: number })
function Example({ code, onChange }: ExampleProps) {
type expand = (helpers: preexpand_helpers) => Promise<{ loc: Loc }>;
function init_state(code: string): [State, expand] {
const source_file: source_file = {
package: { name: "@rewrite-ts/example", version: "0.0.0" },
path: "example",
};
const cuid = "example";
const [loc0, expand] = initial_step(parse(code, source_file, cuid), cuid, ["es2024.full"]);
const [loc0, expand] = initial_step(parse(code, cuid), cuid, ["es2024.full"]);
return [initial_state(loc0), expand];
}
const globals = get_globals("es2024.full");
Expand Down

0 comments on commit e0d99ee

Please sign in to comment.