Skip to content

Commit

Permalink
Merge pull request #72 from azizghuloum/change-underscores-to-dollar-…
Browse files Browse the repository at this point in the history
…signs-in-generated-code

changed variable names to be foo$n instead of foo_n
added warnings to generated files
  • Loading branch information
azizghuloum authored Jan 2, 2025
2 parents f0f11a3 + e79c31d commit 4f3f983
Show file tree
Hide file tree
Showing 36 changed files with 100 additions and 93 deletions.
6 changes: 3 additions & 3 deletions examples/arrow-function-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const h = (x) => x((x) => f(x));
### Output Program

```typescript
export const f_1 = (x_4) => x_4;
export const g_2 = (x_5) => f_1(x_5);
export const h_3 = (x_6) => x_6((x_7) => f_1(x_7));
export const f$1 = (x$4) => x$4;
export const g$2 = (x$5) => f$1(x$5);
export const h$3 = (x$6) => x$6((x$7) => f$1(x$7));
```

4 changes: 2 additions & 2 deletions examples/arrow-function-3.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const foo = (x) => {
### Output Program

```typescript
export const foo_1 = (x_2) => {
const bar_3 = x_2(13);
export const foo$1 = (x$2) => {
const bar$3 = x$2(13);
};
```

4 changes: 2 additions & 2 deletions examples/arrow-function-4.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const foo = (x) => {
### Output Program

```typescript
export const foo_1 = (x_2) => {
x_2;
export const foo$1 = (x$2) => {
x$2;
};
```

4 changes: 2 additions & 2 deletions examples/arrow-function-5.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const foo = (x) => {
### Output Program

```typescript
export const foo_1 = (x_2) => {
x_2;
export const foo$1 = (x$2) => {
x$2;
};
```

6 changes: 3 additions & 3 deletions examples/confused-parens-with-calls.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const foo = (x) => {
### Output Program

```typescript
export const foo_1 = (x_2) => {
x_2;
x_2;
export const foo$1 = (x$2) => {
x$2;
x$2;
};
```

2 changes: 1 addition & 1 deletion examples/curry-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ using_rewrite_rules(
### Output Program

```typescript
(a_1) => (b_2) => (c_3) => (d_4) => a_1 + b_2 + c_3 + d_4;
(a$1) => (b$2) => (c$3) => (d$4) => a$1 + b$2 + c$3 + d$4;
```

2 changes: 1 addition & 1 deletion examples/curry-2.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ const curried =
### Output Program

```typescript
export const curried_1 = (a_2) => (b_3) => (c_4) => (d_5) => a_2 + b_3 + c_4 + d_5;
export const curried$1 = (a$2) => (b$3) => (c$4) => (d$5) => a$2 + b$3 + c$4 + d$5;
```

4 changes: 2 additions & 2 deletions examples/export-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ x;
### Output Program

```typescript
export const x_1 = 12;
x_1;
export const x$1 = 12;
x$1;
```

2 changes: 1 addition & 1 deletion examples/expr-dot-where.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ console.log(x + y).where(x = 1, y = x + 2);
### Output Program

```typescript
((x_1) => ((y_2) => console.log(x_1 + y_2))(x_1 + 2))(1);
((x$1) => ((y$2) => console.log(x$1 + y$2))(x$1 + 2))(1);
```

4 changes: 2 additions & 2 deletions examples/implicit-and-explicit-type-exports.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type explicitly_exported_type = number; // this should stay exported in t
### Output Program

```typescript
export type not_explicitly_exported_type_1 = string;
export type explicitly_exported_type_2 = number;
export type not_explicitly_exported_type$1 = string;
export type explicitly_exported_type$2 = number;
```

14 changes: 7 additions & 7 deletions examples/lexical-declarations-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export const x = (z) => {
### Output Program

```typescript
export type t_1 = 12;
export const y_2 = 13,
z_3: t_1 = y_2,
q_4: t_1,
r_5;
export const x_6 = (z_7) => {
const t_8 = z_7;
export type t$1 = 12;
export const y$2 = 13,
z$3: t$1 = y$2,
q$4: t$1,
r$5;
export const x$6 = (z$7) => {
const t$8 = z$7;
};
```

2 changes: 1 addition & 1 deletion examples/macro-generating-macro-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ using_rewrite_rules(
### Output Program

```typescript
(x_1) => (x_2) => x_2 + x_1;
(x$1) => (x$2) => x$2 + x$1;
```

6 changes: 3 additions & 3 deletions examples/macro-generating-macro-2.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ using_rewrite_rules(
### Output Program

```typescript
(x_1) => {
const using_rewrite_rules_2 = 10;
(x_3) => x_3 + x_1;
(x$1) => {
const using_rewrite_rules$2 = 10;
(x$3) => x$3 + x$1;
};
```

8 changes: 4 additions & 4 deletions examples/macro-generating-tmp-binding-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const tmp = 17; // which has nothing to do with this tmp
### Output Program

```typescript
export const x_1 = 1;
export const tmp_2 = x_1;
console.log(tmp_2);
export const tmp_3 = 17;
export const x$1 = 1;
export const tmp$2 = x$1;
console.log(tmp$2);
export const tmp$3 = 17;
```

4 changes: 2 additions & 2 deletions examples/macro-generating-type-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type b = a;
### Output Program

```typescript
export type a_1 = string;
export type b_2 = a_1;
export type a$1 = string;
export type b$2 = a$1;
```

2 changes: 1 addition & 1 deletion examples/not-test-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ using_rewrite_rules(
### Output Program

```typescript
1 ? !3 : (x_1) => !x_1;
1 ? !3 : (x$1) => !x$1;
```

8 changes: 4 additions & 4 deletions examples/splice-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const q = t + x;
### Output Program

```typescript
export const t_1 = 13;
export const x_2 = 17;
export const y_3 = x_2 + t_1;
export const q_4 = t_1 + x_2;
export const t$1 = 13;
export const x$2 = 17;
export const y$3 = x$2 + t$1;
export const q$4 = t$1 + x$2;
```

6 changes: 3 additions & 3 deletions examples/splice-2.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const foo = (x) => {
### Output Program

```typescript
export const foo_1 = (x_2) => {
x_2;
x_2;
export const foo$1 = (x$2) => {
x$2;
x$2;
};
```

4 changes: 2 additions & 2 deletions examples/test-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ x;
### Output Program

```typescript
export const x_1 = 12;
x_1;
export const x$1 = 12;
x$1;
```

4 changes: 2 additions & 2 deletions examples/test-2.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ x + x;
### Output Program

```typescript
export const x_1 = 12;
x_1 + x_1;
export const x$1 = 12;
x$1 + x$1;
```

6 changes: 3 additions & 3 deletions examples/type-alias-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type q = number;
### Output Program

```typescript
export type t_1 = string;
export const t_2 = 12;
export type q_3 = number;
export type t$1 = string;
export const t$2 = 12;
export type q$3 = number;
```

4 changes: 2 additions & 2 deletions examples/type-associativity-1.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const q = 12 | 13 & 14 | 15
### Output Program

```typescript
export type q_1 = (number | (string & 17)) | 13;
export const q_2 = 12 | (13 & 14) | 15;
export type q$1 = (number | (string & 17)) | 13;
export const q$2 = 12 | (13 & 14) | 15;
```

4 changes: 2 additions & 2 deletions examples/using-rewrite-rules-2.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const x = 12;
### Output Program

```typescript
x_1 + x_1;
export const x_1 = 12;
x$1 + x$1;
export const x$1 = 12;
```

4 changes: 2 additions & 2 deletions examples/using-rewrite-rules-3.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const x = 12;
### Output Program

```typescript
(foo_2) => x_1 + x_1;
export const x_1 = 12;
(foo$2) => x$1 + x$1;
export const x$1 = 12;
```

4 changes: 2 additions & 2 deletions examples/using-rewrite-rules-4.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const x = 12;
### Output Program

```typescript
x_1 + ((foo_2, foo_3) => foo_3 + foo_2);
export const x_1 = 12;
x$1 + ((foo$2, foo$3) => foo$3 + foo$2);
export const x$1 = 12;
```

4 changes: 2 additions & 2 deletions src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ const sort_env = { type: "types_env" as const, value: "normal_env" as const };
const postexpand_forms = (sort: "type" | "value") => non_modular(postexpand_body(sort));

function cleanup_name(name: string): string {
return name.replace(/(_\d+)+$/, "");
return name.replace(/(\$\d+)+$/, "");
}

const postexpand_body = (sort: "type" | "value") => (data: data) => {
Expand Down Expand Up @@ -1164,7 +1164,7 @@ const postexpand_body = (sort: "type" | "value") => (data: data) => {
return cont({ ...data, loc: rename(loc, existing.new_name) });
} else {
const { name } = binding;
const new_name = `${cleanup_name(name)}_${counters.vars}`;
const new_name = `${cleanup_name(name)}$${counters.vars}`;
const new_counters = { ...counters, vars: counters.vars + 1 };
const new_imp: import_req = {
...imp,
Expand Down
3 changes: 2 additions & 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-022";
const cookie = "rewrite-ts-025";

type module_state =
| { type: "initial" }
Expand Down Expand Up @@ -406,6 +406,7 @@ class RtsModule extends Module {
code_path,
await pprint(loc, {
prettify: false,
add_warning: true,
map: {
filename: basename(code_path),
resolve: async (cuid: string) => {
Expand Down
5 changes: 4 additions & 1 deletion src/pprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export async function pretty_print(code: string) {

type options = {
prettify: boolean;
add_warning?: boolean;
map?: map_options;
};

Expand Down Expand Up @@ -272,7 +273,9 @@ function remap(ls: n[], diffs: d[]): n[] {
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("");
const pretty = await pretty_print(code);
const pretty =
(options.add_warning ? "/* This file is automatically generated. Do not edit. */\n" : "") +
(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;
Expand Down
2 changes: 1 addition & 1 deletion src/stx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function extend_context_lexical<S>(
original_name: string,
k: (args: { context: Context; name: string; counters: counters }) => S,
): S {
const new_name = `${original_name}_${counters.vars}`;
const new_name = `${original_name}$${counters.vars}`;
const new_counters = { ...counters, vars: counters.vars + 1 };
const new_context: Context = {
...context,
Expand Down
4 changes: 2 additions & 2 deletions 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-022",
"cookie": "rewrite-ts-025",
"imports": [
{
"pkg": {"name": "rewrite-ts-visualized", "version": "0.0.0"},
Expand All @@ -12,7 +12,7 @@
}
],
"exported_identifiers": {},
"context": {"l1": {"type": "lexical", "name": "y_1"}},
"context": {"l1": {"type": "lexical", "name": "y$1"}},
"unit": {
"cu_id": "test-project/main.rts rewrite-ts-visualized 0.0.0",
"store": {
Expand Down
13 changes: 7 additions & 6 deletions test-project/.rts/main.rts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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);
/* This file is automatically generated. Do not edit. */
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
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL21haW4ucnRzIiwiLi4vbW9kLnJ0cyJdLCJuYW1lcyI6WyJ5IiwidCIsIngiLCJmIiwiY29uc29sZSIsImxvZyIsImV4cGVjdCJdLCJtYXBwaW5ncyI6Ijs7O09BRUEsTUFBTUEsR0FBQyxFQUFFQyxJQUFFLEVBQUVDLElBQUUsRUNHeUJDO0FERnhDQyxPQUFPLENBQUNDLEdBQUcsQ0FBQ0MsUUFBTTtBQUNsQkYsT0FBTyxDQUFDQyxHQUFHLENBQUNMLEdBQUMiLCJmaWxlIjoibWFpbi5ydHMudHMifQ==
8 changes: 4 additions & 4 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-022",
"cookie": "rewrite-ts-025",
"imports": [],
"exported_identifiers": {
"x": [
Expand Down Expand Up @@ -32,9 +32,9 @@
]
},
"context": {
"l1": {"type": "lexical", "name": "x_1"},
"l2": {"type": "type", "name": "t_2"},
"l3": {"type": "lexical", "name": "f_3"},
"l1": {"type": "lexical", "name": "x$1"},
"l2": {"type": "type", "name": "t$2"},
"l3": {"type": "lexical", "name": "f$3"},
"l4": {
"type": "syntax_rules_transformer",
"clauses": [
Expand Down
Loading

0 comments on commit 4f3f983

Please sign in to comment.