tjs-lang 0.7.8 → 0.8.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.
- package/CLAUDE.md +9 -0
- package/demo/docs.json +64 -16
- package/demo/src/ts-examples.ts +8 -8
- package/package.json +7 -3
- package/src/lang/docs.test.ts +148 -0
- package/src/lang/docs.ts +49 -15
- package/src/lang/emitters/js-wasm.ts +57 -65
- package/src/lang/emitters/js.ts +16 -1
- package/src/lang/features.test.ts +4 -3
- package/src/lang/index.ts +9 -0
- package/src/lang/module-loader.test.ts +318 -0
- package/src/lang/module-loader.ts +419 -0
- package/src/lang/parser-transforms.ts +336 -0
- package/src/lang/parser-types.ts +33 -0
- package/src/lang/parser.ts +43 -2
- package/src/lang/wasm.test.ts +1293 -2
- package/src/lang/wasm.ts +470 -87
- package/src/linalg/index.tjs +119 -0
- package/src/linalg/linalg.test.ts +294 -0
- package/src/linalg/vector-search.bench.test.ts +395 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the transpile-time module loader.
|
|
3
|
+
*
|
|
4
|
+
* These use an in-memory filesystem via `inMemoryFileSystem` so the tests are
|
|
5
|
+
* hermetic — no real disk I/O, no node_modules dependencies.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect } from 'bun:test'
|
|
9
|
+
import { sep } from 'node:path'
|
|
10
|
+
import {
|
|
11
|
+
ModuleLoader,
|
|
12
|
+
inMemoryFileSystem,
|
|
13
|
+
type FileSystem,
|
|
14
|
+
} from './module-loader'
|
|
15
|
+
|
|
16
|
+
// All paths use forward slashes in test fixtures; the helper normalizes for us.
|
|
17
|
+
const p = (parts: TemplateStringsArray) => parts.join('').split('/').join(sep)
|
|
18
|
+
|
|
19
|
+
function loaderWith(
|
|
20
|
+
files: Record<string, string>,
|
|
21
|
+
baseDir = '/proj',
|
|
22
|
+
extra: Partial<ConstructorParameters<typeof ModuleLoader>[0]> = {}
|
|
23
|
+
) {
|
|
24
|
+
// Normalize keys to platform-native separators
|
|
25
|
+
const normalized: Record<string, string> = {}
|
|
26
|
+
for (const [k, v] of Object.entries(files)) {
|
|
27
|
+
normalized[k.split('/').join(sep)] = v
|
|
28
|
+
}
|
|
29
|
+
return new ModuleLoader({
|
|
30
|
+
fs: inMemoryFileSystem(normalized),
|
|
31
|
+
baseDir: baseDir.split('/').join(sep),
|
|
32
|
+
...extra,
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('ModuleLoader.resolve', () => {
|
|
37
|
+
it('resolves relative paths against the importer directory', () => {
|
|
38
|
+
const loader = loaderWith({
|
|
39
|
+
'/proj/app.tjs': 'import { x } from "./math.tjs"',
|
|
40
|
+
'/proj/math.tjs': 'export const x = 1',
|
|
41
|
+
})
|
|
42
|
+
expect(loader.resolve('./math.tjs', p`/proj/app.tjs`)).toBe(p`/proj/math.tjs`)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('resolves relative paths against baseDir when no importer is given', () => {
|
|
46
|
+
const loader = loaderWith({
|
|
47
|
+
'/proj/math.tjs': 'export const x = 1',
|
|
48
|
+
})
|
|
49
|
+
expect(loader.resolve('./math.tjs')).toBe(p`/proj/math.tjs`)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('resolves parent-relative paths', () => {
|
|
53
|
+
const loader = loaderWith({
|
|
54
|
+
'/proj/lib/inner.tjs': 'import { y } from "../math.tjs"',
|
|
55
|
+
'/proj/math.tjs': 'export const y = 2',
|
|
56
|
+
})
|
|
57
|
+
expect(loader.resolve('../math.tjs', p`/proj/lib/inner.tjs`)).toBe(
|
|
58
|
+
p`/proj/math.tjs`
|
|
59
|
+
)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('resolves absolute paths', () => {
|
|
63
|
+
const loader = loaderWith({
|
|
64
|
+
'/abs/foo.tjs': 'export const z = 3',
|
|
65
|
+
})
|
|
66
|
+
expect(loader.resolve(p`/abs/foo.tjs`)).toBe(p`/abs/foo.tjs`)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('tries .tjs, .ts, .js extensions in order', () => {
|
|
70
|
+
// Only .ts exists — should still resolve when specifier has no extension
|
|
71
|
+
const loader = loaderWith({
|
|
72
|
+
'/proj/legacy.ts': 'export const a = 1',
|
|
73
|
+
})
|
|
74
|
+
expect(loader.resolve('./legacy', p`/proj/app.tjs`)).toBe(p`/proj/legacy.ts`)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('prefers .tjs when multiple extensions exist', () => {
|
|
78
|
+
const loader = loaderWith({
|
|
79
|
+
'/proj/foo.tjs': 'export const a = 1',
|
|
80
|
+
'/proj/foo.ts': 'export const a = 2',
|
|
81
|
+
'/proj/foo.js': 'export const a = 3',
|
|
82
|
+
})
|
|
83
|
+
expect(loader.resolve('./foo', p`/proj/app.tjs`)).toBe(p`/proj/foo.tjs`)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('resolves directory imports via index.<ext>', () => {
|
|
87
|
+
const loader = loaderWith({
|
|
88
|
+
'/proj/utils/index.tjs': 'export const u = 1',
|
|
89
|
+
})
|
|
90
|
+
expect(loader.resolve('./utils', p`/proj/app.tjs`)).toBe(
|
|
91
|
+
p`/proj/utils/index.tjs`
|
|
92
|
+
)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('walks up looking for node_modules for bare specifiers', () => {
|
|
96
|
+
const loader = loaderWith({
|
|
97
|
+
'/proj/node_modules/tjs-lang/linalg/index.tjs': 'export const dot = 1',
|
|
98
|
+
'/proj/src/inner/app.tjs': 'import { dot } from "tjs-lang/linalg"',
|
|
99
|
+
})
|
|
100
|
+
expect(
|
|
101
|
+
loader.resolve('tjs-lang/linalg', p`/proj/src/inner/app.tjs`)
|
|
102
|
+
).toBe(p`/proj/node_modules/tjs-lang/linalg/index.tjs`)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('checks bareSpecifierRoots before walking node_modules', () => {
|
|
106
|
+
const loader = loaderWith(
|
|
107
|
+
{
|
|
108
|
+
'/proj/local-libs/mylib/index.tjs': 'export const x = 1',
|
|
109
|
+
},
|
|
110
|
+
'/proj',
|
|
111
|
+
{ bareSpecifierRoots: [p`/proj/local-libs`] }
|
|
112
|
+
)
|
|
113
|
+
expect(loader.resolve('mylib')).toBe(p`/proj/local-libs/mylib/index.tjs`)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('returns null for URL specifiers', () => {
|
|
117
|
+
const loader = loaderWith({})
|
|
118
|
+
expect(loader.resolve('https://esm.sh/lodash')).toBeNull()
|
|
119
|
+
expect(loader.resolve('http://example.com/foo.js')).toBeNull()
|
|
120
|
+
expect(loader.resolve('data:text/javascript,foo')).toBeNull()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('returns null for unknown bare specifiers', () => {
|
|
124
|
+
const loader = loaderWith({})
|
|
125
|
+
expect(loader.resolve('react')).toBeNull()
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('returns null for missing relative paths', () => {
|
|
129
|
+
const loader = loaderWith({
|
|
130
|
+
'/proj/app.tjs': '',
|
|
131
|
+
})
|
|
132
|
+
expect(loader.resolve('./does-not-exist', p`/proj/app.tjs`)).toBeNull()
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
describe('ModuleLoader.load', () => {
|
|
137
|
+
it('loads, parses, and surfaces imports/exports', () => {
|
|
138
|
+
const loader = loaderWith({
|
|
139
|
+
'/proj/math.tjs': `
|
|
140
|
+
export function add(a: 0, b: 0): 0 { return a + b }
|
|
141
|
+
export function sub(a: 0, b: 0): 0 { return a - b }
|
|
142
|
+
`,
|
|
143
|
+
})
|
|
144
|
+
const mod = loader.load('./math.tjs', p`/proj/app.tjs`)
|
|
145
|
+
expect(mod).not.toBeNull()
|
|
146
|
+
expect(mod!.path).toBe(p`/proj/math.tjs`)
|
|
147
|
+
expect(mod!.exports.map((e) => e.name).sort()).toEqual(['add', 'sub'])
|
|
148
|
+
expect(mod!.exports.every((e) => e.kind === 'function')).toBe(true)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('captures import declarations', () => {
|
|
152
|
+
const loader = loaderWith({
|
|
153
|
+
'/proj/app.tjs': `
|
|
154
|
+
import { add } from './math.tjs'
|
|
155
|
+
import sqrt from './sqrt.tjs'
|
|
156
|
+
import * as utils from './utils.tjs'
|
|
157
|
+
`,
|
|
158
|
+
'/proj/math.tjs': 'export const add = 0',
|
|
159
|
+
'/proj/sqrt.tjs': 'export default function sqrt() { return 0 }',
|
|
160
|
+
'/proj/utils.tjs': 'export const x = 0',
|
|
161
|
+
})
|
|
162
|
+
const mod = loader.load('./app.tjs')
|
|
163
|
+
expect(mod).not.toBeNull()
|
|
164
|
+
const i = mod!.imports
|
|
165
|
+
expect(i.find((e) => e.local === 'add')).toMatchObject({
|
|
166
|
+
specifier: './math.tjs',
|
|
167
|
+
imported: 'add',
|
|
168
|
+
namespace: false,
|
|
169
|
+
})
|
|
170
|
+
expect(i.find((e) => e.local === 'sqrt')).toMatchObject({
|
|
171
|
+
specifier: './sqrt.tjs',
|
|
172
|
+
imported: 'default',
|
|
173
|
+
namespace: false,
|
|
174
|
+
})
|
|
175
|
+
expect(i.find((e) => e.local === 'utils')).toMatchObject({
|
|
176
|
+
specifier: './utils.tjs',
|
|
177
|
+
imported: '*',
|
|
178
|
+
namespace: true,
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('handles renamed imports (import { a as b } from ...)', () => {
|
|
183
|
+
const loader = loaderWith({
|
|
184
|
+
'/proj/app.tjs': `import { add as plus } from './math.tjs'`,
|
|
185
|
+
'/proj/math.tjs': 'export const add = 0',
|
|
186
|
+
})
|
|
187
|
+
const mod = loader.load('./app.tjs')
|
|
188
|
+
expect(mod!.imports[0]).toMatchObject({
|
|
189
|
+
specifier: './math.tjs',
|
|
190
|
+
local: 'plus',
|
|
191
|
+
imported: 'add',
|
|
192
|
+
})
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
it('surfaces re-exports with kind "re-export"', () => {
|
|
196
|
+
const loader = loaderWith({
|
|
197
|
+
'/proj/index.tjs': `
|
|
198
|
+
export { add } from './math.tjs'
|
|
199
|
+
export * from './utils.tjs'
|
|
200
|
+
`,
|
|
201
|
+
'/proj/math.tjs': 'export const add = 0',
|
|
202
|
+
'/proj/utils.tjs': 'export const x = 0',
|
|
203
|
+
})
|
|
204
|
+
const mod = loader.load('./index.tjs')
|
|
205
|
+
expect(mod).not.toBeNull()
|
|
206
|
+
const reexports = mod!.exports.filter((e) => e.kind === 're-export')
|
|
207
|
+
expect(reexports).toContainEqual({
|
|
208
|
+
name: 'add',
|
|
209
|
+
kind: 're-export',
|
|
210
|
+
fromSpecifier: './math.tjs',
|
|
211
|
+
})
|
|
212
|
+
expect(reexports).toContainEqual({
|
|
213
|
+
name: '*',
|
|
214
|
+
kind: 're-export',
|
|
215
|
+
fromSpecifier: './utils.tjs',
|
|
216
|
+
})
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
it('surfaces variable exports', () => {
|
|
220
|
+
const loader = loaderWith({
|
|
221
|
+
'/proj/things.tjs': `
|
|
222
|
+
export const PI = 3.14
|
|
223
|
+
export let counter = 0
|
|
224
|
+
`,
|
|
225
|
+
})
|
|
226
|
+
const mod = loader.load('./things.tjs')
|
|
227
|
+
expect(mod!.exports).toContainEqual({ name: 'PI', kind: 'variable' })
|
|
228
|
+
expect(mod!.exports).toContainEqual({ name: 'counter', kind: 'variable' })
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('surfaces classes as variables (post-preprocessor: class → wrapClass(class))', () => {
|
|
232
|
+
// The tjs preprocessor rewrites `export class Foo {}` into something
|
|
233
|
+
// shaped like `export const Foo = wrapClass(class Foo {})`. The loader
|
|
234
|
+
// surfaces the post-preprocessor AST faithfully — downstream code can
|
|
235
|
+
// recover the class identity from the body if needed.
|
|
236
|
+
const loader = loaderWith({
|
|
237
|
+
'/proj/things.tjs': `export class Foo {}`,
|
|
238
|
+
})
|
|
239
|
+
const mod = loader.load('./things.tjs')
|
|
240
|
+
expect(mod!.exports.find((e) => e.name === 'Foo')?.kind).toBe('variable')
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('surfaces default function exports', () => {
|
|
244
|
+
const loader = loaderWith({
|
|
245
|
+
'/proj/anon.tjs': `export default function () { return 1 }`,
|
|
246
|
+
})
|
|
247
|
+
const mod = loader.load('./anon.tjs')
|
|
248
|
+
expect(mod!.exports).toContainEqual({ name: 'default', kind: 'function' })
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('returns null when the source fails to parse', () => {
|
|
252
|
+
const loader = loaderWith({
|
|
253
|
+
'/proj/broken.tjs': `this is not valid javascript {{{`,
|
|
254
|
+
})
|
|
255
|
+
expect(loader.load('./broken.tjs')).toBeNull()
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
it('caches loaded modules by resolved path', () => {
|
|
259
|
+
let reads = 0
|
|
260
|
+
const fs: FileSystem = {
|
|
261
|
+
readFile(path) {
|
|
262
|
+
if (path.endsWith('math.tjs') || path.endsWith('math' + sep + 'tjs')) {
|
|
263
|
+
reads++
|
|
264
|
+
return 'export const x = 1'
|
|
265
|
+
}
|
|
266
|
+
return null
|
|
267
|
+
},
|
|
268
|
+
exists(path) {
|
|
269
|
+
return path.endsWith('math.tjs') || path.endsWith('math' + sep + 'tjs')
|
|
270
|
+
},
|
|
271
|
+
}
|
|
272
|
+
const loader = new ModuleLoader({ fs, baseDir: p`/proj` })
|
|
273
|
+
loader.load('./math.tjs')
|
|
274
|
+
loader.load('./math.tjs')
|
|
275
|
+
loader.load('./math.tjs')
|
|
276
|
+
expect(reads).toBe(1)
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
it('clearCache forces a reload', () => {
|
|
280
|
+
let reads = 0
|
|
281
|
+
const fs: FileSystem = {
|
|
282
|
+
readFile() {
|
|
283
|
+
reads++
|
|
284
|
+
return 'export const x = 1'
|
|
285
|
+
},
|
|
286
|
+
exists: () => true,
|
|
287
|
+
}
|
|
288
|
+
const loader = new ModuleLoader({ fs, baseDir: p`/proj` })
|
|
289
|
+
loader.load('./math.tjs')
|
|
290
|
+
loader.clearCache()
|
|
291
|
+
loader.load('./math.tjs')
|
|
292
|
+
expect(reads).toBe(2)
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
it('respects cacheLimit by evicting oldest entries', () => {
|
|
296
|
+
const loader = loaderWith(
|
|
297
|
+
{
|
|
298
|
+
'/proj/a.tjs': 'export const x = 1',
|
|
299
|
+
'/proj/b.tjs': 'export const y = 2',
|
|
300
|
+
'/proj/c.tjs': 'export const z = 3',
|
|
301
|
+
},
|
|
302
|
+
'/proj',
|
|
303
|
+
{ cacheLimit: 2 }
|
|
304
|
+
)
|
|
305
|
+
loader.load('./a.tjs')
|
|
306
|
+
loader.load('./b.tjs')
|
|
307
|
+
loader.load('./c.tjs') // should evict a.tjs
|
|
308
|
+
// No public cache inspection — but loading a.tjs again with a counting fs
|
|
309
|
+
// would re-read. Easier: just confirm the load still works.
|
|
310
|
+
expect(loader.load('./a.tjs')).not.toBeNull()
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
it('returns null for unresolvable specifiers (no implicit fallback)', () => {
|
|
314
|
+
const loader = loaderWith({})
|
|
315
|
+
expect(loader.load('lodash')).toBeNull()
|
|
316
|
+
expect(loader.load('https://esm.sh/lodash')).toBeNull()
|
|
317
|
+
})
|
|
318
|
+
})
|