Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding type aliases #11

Merged
merged 9 commits into from
Dec 2, 2024
108 changes: 108 additions & 0 deletions deno.lock

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

23 changes: 23 additions & 0 deletions examples/macro-generating-type-1.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## `macro-generating-type-1.ts`

### Status: `DONE`

### Input Program

```typescript
using_syntax_rules(
[deftype, deftype(x as y), splice(() => {
type x = y;
})]
).rewrite(deftype(a as string));

type b = a;
```

### Output Program

```typescript
type a_6 = string;
type b_8 = a_6;
```

20 changes: 20 additions & 0 deletions examples/type-alias-1.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## `type-alias-1.ts`

### Status: `DONE`

### Input Program

```typescript
type t = string;
const t = 12;
type q = number;
```

### Output Program

```typescript
type t_3 = string;
const t_5 = 12;
type q_7 = number;
```

18 changes: 18 additions & 0 deletions examples/type-associativity-1.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## `type-associativity-1.ts`

### Status: `DONE`

### Input Program

```typescript
type q = number | string & 17 | 13;
const q = 12 | 13 & 14 | 15
```

### Output Program

```typescript
type q_3 = (number | (string & 17)) | 13;
const q_5 = 12 | (13 & 14) | 15;
```

74 changes: 74 additions & 0 deletions scribblings/rtsc2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env deno --allow-all

import TS from "typescript";

function make_example1() {
return TS.createSourceFile("test.ts", "export type foo = number;\n", {
languageVersion: TS.ScriptTarget.ES2020,
});
}

function make_example2() {
const t1 = TS.factory.createTypeAliasDeclaration(
[TS.factory.createToken(TS.SyntaxKind.ExportKeyword)],
"foo",
undefined,
TS.factory.createLiteralTypeNode(TS.factory.createNull()),
);

const lit1 = TS.factory.createNumericLiteral("1");
const t2 = TS.factory.createExpressionStatement(
TS.factory.createAsExpression(lit1, TS.factory.createLiteralTypeNode(TS.factory.createNull())),
);

const lit2 = TS.factory.createNumericLiteral("2");
const add = TS.factory.createAdd(lit1, lit2);
const exprstmt = TS.factory.createExpressionStatement(add);
const finalsrc = TS.factory.createSourceFile(
[t1, t2, exprstmt],
TS.factory.createToken(TS.SyntaxKind.EndOfFileToken),
TS.NodeFlags.None,
);

const src1 = TS.createSourceMapSource("foo.txt", "some context");
const src2 = TS.createSourceMapSource("bar.txt", "some other context");
TS.setSourceMapRange(lit1, { pos: 5, end: 20, source: src1 });
TS.setSourceMapRange(lit2, { pos: 0, end: 4, source: src2 });
TS.setSourceMapRange(finalsrc, { pos: 5, end: 20, source: src1 });
finalsrc.fileName = "mysrc.ts";

return finalsrc;
}

const example = make_example1();
const printer = TS.createPrinter();
console.log(printer.printFile(example));

const prog = TS.createProgram({
options: {
sourceMap: true,
declaration: true,
declarationMap: true,
target: TS.ScriptTarget.ES2020,
},
rootNames: [],
});

const results = prog.emit(
example,
(name, text) => {
console.log({ name, text });
},
undefined,
undefined,
{},
);

console.log(results);
console.log({
global: prog.getGlobalDiagnostics(),
options: prog.getOptionsDiagnostics(),
semantic: prog.getSemanticDiagnostics(),
syntactic: prog.getSyntacticDiagnostics(),
declaration: prog.getDeclarationDiagnostics(),
});
Loading