typenative 0.0.21 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,7 @@ jobs:
27
27
  node-version: 24
28
28
  - uses: actions/setup-go@v5
29
29
  with:
30
- go-version: '1.21'
30
+ go-version: '1.23'
31
31
  - run: npm ci
32
32
  - run: npm ci
33
33
  working-directory: test
package/CHANGELOG.md CHANGED
@@ -5,6 +5,44 @@ All notable changes to TypeNative will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.1.0] - 2026-10-09
9
+
10
+ TypeNative compiles itself: `src/main.ts` builds into a native `typenative` binary that passes the
11
+ whole test suite and regenerates its own Go source identically (stage-1/stage-2 bootstrap).
12
+
13
+ ### Added
14
+
15
+ - **Native compiler**: `npm run build:native` builds `native/typenative` and `native/tsparser`
16
+ - **`any` / `unknown` at runtime**: property and index access, truthiness, equality (objects by
17
+ identity), method calls, spreads and conversions to typed values via a small Go runtime
18
+ (`TnGet`, `TnSet`, `TnAs`, …) — no guessed types
19
+ - **Language**: object spread, `??=`, `in`, `typeof`, `continue`, optional and destructured
20
+ parameters, constructor parameter properties, `return` inside `try`/`catch`, object type
21
+ aliases and inline object types → Go structs, `Record<K, V>` and dictionaries → Go maps
22
+ - **Typing**: contextual types for object/array/function literals, narrowing of `T | undefined`,
23
+ JS truthiness, `??` chains, nullable ternaries, optional chains across calls
24
+ - **Library**: `toFixed`, `Math.max(...arr)`, `sort` with comparators, `flat`, typed
25
+ `Object.keys`/`values`, regex `replace` (`/g`, `$1`/`$&`, function replacers), `match`/`split`
26
+ with regexes, `Array.isArray`, `console.warn`, `fs.unlinkSync`, `process.env['X']`
27
+ - Tests 33–50
28
+
29
+ ### Changed
30
+
31
+ - **Go 1.23 or newer is required** (generated code uses `slices`/`maps` iterator helpers)
32
+ - `process.platform` reports Node's names (`win32`, `darwin`, `linux`)
33
+ - `JSON.stringify` no longer escapes `<`, `>`, `&` (like JS)
34
+ - Out-of-range array/string reads give zero values instead of panicking (JS `undefined`)
35
+ - Imports and helpers are emitted in sorted order
36
+ - Map, Set and Record keep insertion order (like JS); objects are references (Go pointers)
37
+
38
+ ### Fixed
39
+
40
+ - The test suite never ran (scripts called `bin/index.js`, which has no entry point); it now runs
41
+ through `bin/cli.js`
42
+ - Phase 3c regressions (19 tests): AST kind names, missing parent links, `extends` detection
43
+ - Main-file top-level variables were invisible to functions
44
+ - `spawnSync` with `stdio: 'inherit'` dropped its arguments
45
+
8
46
  ## [0.0.21] - 2026-09-06
9
47
 
10
48
  Self-hosting milestone: the compiler no longer depends on `execa`, `fs-extra`, or `nanoid`, and the
package/README.md CHANGED
@@ -5,7 +5,7 @@ Build native applications using Typescript.
5
5
  ## PreRequisites
6
6
 
7
7
  - [Nodejs v24](https://nodejs.org/en) or newer.
8
- - [Go 1.21](https://go.dev/doc/install) or newer.
8
+ - [Go 1.23](https://go.dev/doc/install) or newer.
9
9
 
10
10
  ## Get Started
11
11
 
@@ -24,7 +24,7 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
24
24
  | boolean | ✅ | Transpiled to `bool` |
25
25
  | string | ✅ | |
26
26
  | null | ✅ | |
27
- | any | ✅ | Used for type inference |
27
+ | any / unknown | ✅ | Dynamic values handled at runtime (JSON-like objects, arrays, primitives) |
28
28
  | Nullable types | ✅ | `T \| null` / `T \| undefined` transpiled to Go pointer types |
29
29
 
30
30
  **Variables & Objects**
@@ -36,6 +36,9 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
36
36
  | Object shorthand | ✅ | `{ name }` → `{ name: name }` |
37
37
  | Array destructuring | ✅ | `const [a, b] = arr` |
38
38
  | Object destructuring | ✅ | `const { x, y } = obj` |
39
+ | Object spread | ✅ | `{ ...base, x: 1 }` |
40
+ | Records / dictionaries | ✅ | `Record<K, V>` and objects indexed by key → Go maps |
41
+ | Object type aliases | ✅ | `type P = { x: number }` → Go struct |
39
42
 
40
43
  **Operators**
41
44
 
@@ -50,6 +53,9 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
50
53
  | Ternary expressions | ✅ | `condition ? a : b` |
51
54
  | Nullish coalescing | ✅ | `??` operator |
52
55
  | Optional chaining | ✅ | `obj?.prop`, `arr?.[i]` |
56
+ | Nullish assignment | ✅ | `x ??= value` |
57
+ | `in` / `typeof` | ✅ | `"key" in obj`, `typeof x === "string"` |
58
+ | JS truthiness | ✅ | `if (str)`, `!count`, `a \|\| fallback` on any type |
53
59
 
54
60
  **Control Flow**
55
61
 
@@ -58,11 +64,13 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
58
64
  | If/Else statements | ✅ | Fully supported |
59
65
  | Switch statements | ✅ | Case and default statements |
60
66
  | For loops | ✅ | Standard `for` loops |
67
+ | Continue | ✅ | `continue` in all loop kinds |
61
68
  | For...of loops | ✅ | Arrays, Maps, Sets; `Object.entries()` unwrapping |
62
69
  | For...in loops | ✅ | Iterates keys via Go `range` |
63
70
  | While loops | ✅ | Transpiled to Go's `for` loops |
64
71
  | Do...while loops | ✅ | Implemented with conditional break |
65
72
  | Try/Catch/Finally | ✅ | `throw` → `panic`; catch/finally via `defer`/`recover` |
73
+ | Narrowing | ✅ | `T \| undefined` narrowed after `if (x)`, `x !== undefined`, early returns |
66
74
 
67
75
  **Data Structures & Array Methods**
68
76
 
@@ -74,6 +82,8 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
74
82
  | Method chaining | ✅ | e.g. `.map(...).filter(...).join(...)` |
75
83
  | Map | ✅ | `Map<K, V>` → Go `map[K]V`; `.set()`, `.get()`, `.has()`, `.delete()`, `.clear()`, `.size` |
76
84
  | Set | ✅ | `Set<T>` → Go `map[T]struct{}`; `.add()`, `.has()`, `.delete()`, `.clear()`, `.size` |
85
+ | Object.keys / values | ✅ | Typed results for maps and records |
86
+ | Array sort / flat | ✅ | `sort((a, b) => a - b)` in place, `flat()` one level |
77
87
 
78
88
  **Functions**
79
89
 
@@ -87,12 +97,15 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
87
97
  | Generics (functions/classes) | ✅ | Type parameters via Go generics |
88
98
  | Default parameter values | ✅ | `function(x = defaultValue)` |
89
99
  | Rest parameters | ✅ | `function(...args: T[])` → Go variadic |
100
+ | Optional parameters | ✅ | `function f(x?: string)` |
101
+ | Destructured parameters | ✅ | `function f({ a, b }: T)`, `([x, y]: number[])` |
90
102
 
91
103
  **Classes & Interfaces**
92
104
 
93
105
  | Feature | Supported | Notes |
94
106
  | ------------------- | :-------: | -------------------------------------------------------------- |
95
107
  | Classes | ✅ | Transpiled to Go structs with constructor and receiver methods |
108
+ | Parameter properties | ✅ | `constructor(private x: number)` |
96
109
  | Class inheritance | ✅ | `extends` via embedded structs, `super()` supported |
97
110
  | Static members | ✅ | `static method()` / `static prop` → package-level declarations |
98
111
  | Getters / Setters | ✅ | `get prop()` → `Get_prop()`, `set prop(v)` → `Set_prop(v)` |
@@ -131,7 +144,8 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
131
144
  | Math.random | ✅ | Mapped to `rand.Float64()` |
132
145
  | Math.floor / ceil / round | ✅ | Mapped to `math.Floor`, `math.Ceil`, `math.Round` |
133
146
  | Math.abs / sqrt / pow | ✅ | Mapped to corresponding `math` functions |
134
- | Math.min / max | ✅ | Mapped to `math.Min`, `math.Max` |
147
+ | Math.min / max | ✅ | Go builtins `min` / `max` |
148
+ | Math.max(...arr) | ✅ | Spread and any number of arguments via `slices.Max` / `max` |
135
149
  | Math.log / log2 / log10 | ✅ | Mapped to `math.Log`, `math.Log2`, `math.Log10` |
136
150
  | Math.sin / cos / tan | ✅ | Mapped to `math.Sin`, `math.Cos`, `math.Tan` |
137
151
  | Math.trunc / sign | ✅ | Mapped to `math.Trunc` and inline sign check |
@@ -146,6 +160,7 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
146
160
  | split / includes / indexOf | ✅ | Via `strings` package |
147
161
  | startsWith / endsWith | ✅ | Via `strings` package |
148
162
  | replace / replaceAll | ✅ | Via `strings` package |
163
+ | replace with RegExp | ✅ | `/g` vs first match, `$1`/`$&` patterns, function replacers |
149
164
  | charAt / substring / slice | ✅ | Direct Go string indexing/slicing |
150
165
  | concat / repeat | ✅ | String concatenation and `strings.Repeat` |
151
166
  | padStart / padEnd | ✅ | Via `strings.Repeat` |
@@ -157,6 +172,7 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
157
172
  | Feature | Supported | Notes |
158
173
  | -------- | :-------: | ----------------------------------------------------- |
159
174
  | toString | ✅ | Universal `toString()` via `fmt.Sprintf` for any type |
175
+ | toFixed | ✅ | `n.toFixed(2)` via `strconv.FormatFloat` |
160
176
 
161
177
  **RegExp**
162
178
 
@@ -180,4 +196,25 @@ TypeNative currently supports a focused subset of TypeScript syntax elements tha
180
196
  | npm package imports | ✅ | `import { x } from 'pkg'` mapped to Go module imports |
181
197
  | Named exports | ✅ | `export function` / `export const` declarations |
182
198
 
183
- TypeNative is currently in early development and new features are being added regularly. The goal for `1.0` release is for TypeNative to transpile itself.
199
+ ## Native (self-hosted) compiler
200
+
201
+ TypeNative compiles itself: `src/main.ts` transpiles to Go and builds into a native `typenative`
202
+ binary that needs no Node.js at runtime and produces the same Go output as the npm package.
203
+
204
+ ```sh
205
+ npm run build # compile the TypeScript sources
206
+ npm run build:native # build native/typenative and native/tsparser
207
+ native/typenative --source app.ts --script
208
+ ```
209
+
210
+ Building the native compiler requires Go 1.26 (for the TypeScript parser, `tsparser/`). The
211
+ `typenative` binary finds `tsparser` next to itself, or via the `TYPENATIVE_TSPARSER`
212
+ environment variable.
213
+
214
+ ### Known limitations
215
+
216
+ - Stateful `/g` regex loops (`while ((m = re.exec(s)))`) are not supported — use `matchAll`
217
+ - `import.meta.url` is not lowered
218
+ - Object types compile to Go structs, which are copied by value (JS objects are references)
219
+
220
+ TypeNative is in early development and new features are being added regularly.
package/TODO.md CHANGED
@@ -26,9 +26,13 @@ Goal: TypeNative transpiles itself into a native Go binary (1.0 milestone).
26
26
  via an injected parse function (ts shim keeps predicate call sites); operator
27
27
  tokens, process.env, stdin-exec mappings added; verified identical Go output
28
28
  via npm parser and Go-parser paths on all 33 test specs
29
- - [ ] Phase 4: dogfood src/transpiler.ts (2.6k lines): regex /g semantics,
30
- String.replace with function replacer, JSON.parse result typing, object spread,
31
- Array.includes on untyped values, default-import namespace stripping (ts.x → x)
29
+ - [x] Phase 4: src/main.ts (incl. transpiler.ts) compiles to a native binary;
30
+ `any` values use a small Go runtime (TnGet/TnSet/TnAs/...) instead of guessed types
31
+ - [x] Phase 5: stage-1/stage-2 bootstrap — the native compiler passes all test specs
32
+ and reproduces its own Go source identically (fixpoint)
33
+
34
+ ## Next
35
+ - [ ] CI self-hosting job (stage 1 → stage 2 → compare); README/CHANGELOG updates
36
+ - [ ] Stateful RegExp /g (`lastIndex`, `while ((m = re.exec(s)))` loops)
32
37
  - [ ] import.meta.url lowering
33
- - [ ] Phase 5: stage-1/stage-2 bootstrap + run test suite through the native binary
34
- - [ ] Phase 5: CI self-hosting job; README/CHANGELOG updates
38
+ - [ ] Gradually type the AST in transpiler.ts (fewer dynamic lookups, faster binary)
package/bin/index.js CHANGED
@@ -149,8 +149,7 @@ function tryInjectDtsTypes(jsContent, packageName, searchDir) {
149
149
  // Extract typed function signatures from the .d.ts module body
150
150
  const signatures = new Map();
151
151
  const sigRegex = /export function (\w+)\(([^)]*)\)\s*:\s*([^\n;]+)/g;
152
- let m;
153
- while ((m = sigRegex.exec(dtsBody)) !== null) {
152
+ for (const m of dtsBody.matchAll(sigRegex)) {
154
153
  signatures.set(m[1], { params: m[2].trim(), returnType: m[3].trim() });
155
154
  }
156
155
  if (signatures.size === 0)
package/bin/parse-node.js CHANGED
@@ -92,8 +92,8 @@ function convertNode(node) {
92
92
  if (arr.length > 0)
93
93
  obj[key] = arr;
94
94
  }
95
- else if (typeof v === 'number' && key === 'operator') {
96
- // unary operator: emit the kind name (matches the Go producer)
95
+ else if (typeof v === 'number' && (key === 'operator' || key === 'token')) {
96
+ // operator / heritage-clause token: emit the kind name (matches the Go producer)
97
97
  obj[key] = KIND_NAMES[v] ?? String(v);
98
98
  }
99
99
  else if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') {