@justscale/typescript 0.1.1
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/LICENSE +21 -0
- package/README.md +128 -0
- package/dist/api.d.ts +144 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +380 -0
- package/dist/api.js.map +1 -0
- package/dist/compiler/analyzer.d.ts +247 -0
- package/dist/compiler/analyzer.d.ts.map +1 -0
- package/dist/compiler/analyzer.js +3201 -0
- package/dist/compiler/analyzer.js.map +1 -0
- package/dist/compiler/cli.d.ts +12 -0
- package/dist/compiler/cli.d.ts.map +1 -0
- package/dist/compiler/cli.js +209 -0
- package/dist/compiler/cli.js.map +1 -0
- package/dist/compiler/compile.d.ts +26 -0
- package/dist/compiler/compile.d.ts.map +1 -0
- package/dist/compiler/compile.js +121 -0
- package/dist/compiler/compile.js.map +1 -0
- package/dist/compiler/errors.d.ts +336 -0
- package/dist/compiler/errors.d.ts.map +1 -0
- package/dist/compiler/errors.js +466 -0
- package/dist/compiler/errors.js.map +1 -0
- package/dist/compiler/exports-prepass.d.ts +31 -0
- package/dist/compiler/exports-prepass.d.ts.map +1 -0
- package/dist/compiler/exports-prepass.js +249 -0
- package/dist/compiler/exports-prepass.js.map +1 -0
- package/dist/compiler/hmr-change-detector.d.ts +47 -0
- package/dist/compiler/hmr-change-detector.d.ts.map +1 -0
- package/dist/compiler/hmr-change-detector.js +395 -0
- package/dist/compiler/hmr-change-detector.js.map +1 -0
- package/dist/compiler/hmr-transformer.d.ts +54 -0
- package/dist/compiler/hmr-transformer.d.ts.map +1 -0
- package/dist/compiler/hmr-transformer.js +535 -0
- package/dist/compiler/hmr-transformer.js.map +1 -0
- package/dist/compiler/index.d.ts +19 -0
- package/dist/compiler/index.d.ts.map +1 -0
- package/dist/compiler/index.js +16 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/compiler/primitive-detector.d.ts +70 -0
- package/dist/compiler/primitive-detector.d.ts.map +1 -0
- package/dist/compiler/primitive-detector.js +338 -0
- package/dist/compiler/primitive-detector.js.map +1 -0
- package/dist/compiler/ptsc.d.ts +40 -0
- package/dist/compiler/ptsc.d.ts.map +1 -0
- package/dist/compiler/ptsc.js +462 -0
- package/dist/compiler/ptsc.js.map +1 -0
- package/dist/compiler/rewriter.d.ts +96 -0
- package/dist/compiler/rewriter.d.ts.map +1 -0
- package/dist/compiler/rewriter.js +418 -0
- package/dist/compiler/rewriter.js.map +1 -0
- package/dist/compiler/step-hash.d.ts +43 -0
- package/dist/compiler/step-hash.d.ts.map +1 -0
- package/dist/compiler/step-hash.js +83 -0
- package/dist/compiler/step-hash.js.map +1 -0
- package/dist/compiler/switch-codegen.d.ts +84 -0
- package/dist/compiler/switch-codegen.d.ts.map +1 -0
- package/dist/compiler/switch-codegen.js +1540 -0
- package/dist/compiler/switch-codegen.js.map +1 -0
- package/dist/compiler/transformer.d.ts +29 -0
- package/dist/compiler/transformer.d.ts.map +1 -0
- package/dist/compiler/transformer.js +216 -0
- package/dist/compiler/transformer.js.map +1 -0
- package/dist/config/index.d.ts +122 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +215 -0
- package/dist/config/index.js.map +1 -0
- package/dist/di-errors/formatter.d.ts +126 -0
- package/dist/di-errors/formatter.d.ts.map +1 -0
- package/dist/di-errors/formatter.js +384 -0
- package/dist/di-errors/formatter.js.map +1 -0
- package/dist/di-errors/index.d.ts +5 -0
- package/dist/di-errors/index.d.ts.map +1 -0
- package/dist/di-errors/index.js +13 -0
- package/dist/di-errors/index.js.map +1 -0
- package/dist/editor.d.ts +11 -0
- package/dist/editor.d.ts.map +1 -0
- package/dist/editor.js +2 -0
- package/dist/editor.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/language-service/index.d.ts +52 -0
- package/dist/language-service/index.d.ts.map +1 -0
- package/dist/language-service/index.js +366 -0
- package/dist/language-service/index.js.map +1 -0
- package/dist/language-service/process-quick-fixes.d.ts +20 -0
- package/dist/language-service/process-quick-fixes.d.ts.map +1 -0
- package/dist/language-service/process-quick-fixes.js +114 -0
- package/dist/language-service/process-quick-fixes.js.map +1 -0
- package/dist/language-service/quick-fix-discovery.d.ts +39 -0
- package/dist/language-service/quick-fix-discovery.d.ts.map +1 -0
- package/dist/language-service/quick-fix-discovery.js +124 -0
- package/dist/language-service/quick-fix-discovery.js.map +1 -0
- package/dist/loader/incremental.d.ts +50 -0
- package/dist/loader/incremental.d.ts.map +1 -0
- package/dist/loader/incremental.js +151 -0
- package/dist/loader/incremental.js.map +1 -0
- package/dist/loader/index.d.ts +25 -0
- package/dist/loader/index.d.ts.map +1 -0
- package/dist/loader/index.js +24 -0
- package/dist/loader/index.js.map +1 -0
- package/dist/loader/loader.d.ts +52 -0
- package/dist/loader/loader.d.ts.map +1 -0
- package/dist/loader/loader.js +248 -0
- package/dist/loader/loader.js.map +1 -0
- package/dist/loader/register.d.ts +14 -0
- package/dist/loader/register.d.ts.map +1 -0
- package/dist/loader/register.js +20 -0
- package/dist/loader/register.js.map +1 -0
- package/dist/plugins/index.d.ts +13 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +13 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/index.public.d.ts +13 -0
- package/dist/plugins/index.public.d.ts.map +1 -0
- package/dist/plugins/index.public.js +13 -0
- package/dist/plugins/index.public.js.map +1 -0
- package/dist/plugins/types.d.ts +83 -0
- package/dist/plugins/types.d.ts.map +1 -0
- package/dist/plugins/types.js +24 -0
- package/dist/plugins/types.js.map +1 -0
- package/dist/server/index.d.ts +33 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +42 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/tsserver.d.ts +28 -0
- package/dist/server/tsserver.d.ts.map +1 -0
- package/dist/server/tsserver.js +126 -0
- package/dist/server/tsserver.js.map +1 -0
- package/lib/lib.d.ts +20 -0
- package/lib/lib.decorators.d.ts +382 -0
- package/lib/lib.decorators.legacy.d.ts +20 -0
- package/lib/lib.dom.asynciterable.d.ts +18 -0
- package/lib/lib.dom.d.ts +45125 -0
- package/lib/lib.dom.iterable.d.ts +18 -0
- package/lib/lib.es2015.collection.d.ts +150 -0
- package/lib/lib.es2015.core.d.ts +595 -0
- package/lib/lib.es2015.d.ts +26 -0
- package/lib/lib.es2015.generator.d.ts +75 -0
- package/lib/lib.es2015.iterable.d.ts +603 -0
- package/lib/lib.es2015.promise.d.ts +79 -0
- package/lib/lib.es2015.proxy.d.ts +126 -0
- package/lib/lib.es2015.reflect.d.ts +142 -0
- package/lib/lib.es2015.symbol.d.ts +44 -0
- package/lib/lib.es2015.symbol.wellknown.d.ts +324 -0
- package/lib/lib.es2016.array.include.d.ts +114 -0
- package/lib/lib.es2016.d.ts +19 -0
- package/lib/lib.es2016.full.d.ts +21 -0
- package/lib/lib.es2016.intl.d.ts +29 -0
- package/lib/lib.es2017.arraybuffer.d.ts +19 -0
- package/lib/lib.es2017.d.ts +24 -0
- package/lib/lib.es2017.date.d.ts +29 -0
- package/lib/lib.es2017.full.d.ts +21 -0
- package/lib/lib.es2017.intl.d.ts +42 -0
- package/lib/lib.es2017.object.d.ts +47 -0
- package/lib/lib.es2017.sharedmemory.d.ts +133 -0
- package/lib/lib.es2017.string.d.ts +43 -0
- package/lib/lib.es2017.typedarrays.d.ts +51 -0
- package/lib/lib.es2018.asyncgenerator.d.ts +75 -0
- package/lib/lib.es2018.asynciterable.d.ts +51 -0
- package/lib/lib.es2018.d.ts +22 -0
- package/lib/lib.es2018.full.d.ts +22 -0
- package/lib/lib.es2018.intl.d.ts +81 -0
- package/lib/lib.es2018.promise.d.ts +28 -0
- package/lib/lib.es2018.regexp.d.ts +35 -0
- package/lib/lib.es2019.array.d.ts +77 -0
- package/lib/lib.es2019.d.ts +22 -0
- package/lib/lib.es2019.full.d.ts +22 -0
- package/lib/lib.es2019.intl.d.ts +21 -0
- package/lib/lib.es2019.object.d.ts +31 -0
- package/lib/lib.es2019.string.d.ts +35 -0
- package/lib/lib.es2019.symbol.d.ts +22 -0
- package/lib/lib.es2020.bigint.d.ts +763 -0
- package/lib/lib.es2020.d.ts +25 -0
- package/lib/lib.es2020.date.d.ts +40 -0
- package/lib/lib.es2020.full.d.ts +22 -0
- package/lib/lib.es2020.intl.d.ts +472 -0
- package/lib/lib.es2020.number.d.ts +26 -0
- package/lib/lib.es2020.promise.d.ts +45 -0
- package/lib/lib.es2020.sharedmemory.d.ts +97 -0
- package/lib/lib.es2020.string.d.ts +42 -0
- package/lib/lib.es2020.symbol.wellknown.d.ts +39 -0
- package/lib/lib.es2021.d.ts +21 -0
- package/lib/lib.es2021.full.d.ts +22 -0
- package/lib/lib.es2021.intl.d.ts +164 -0
- package/lib/lib.es2021.promise.d.ts +46 -0
- package/lib/lib.es2021.string.d.ts +31 -0
- package/lib/lib.es2021.weakref.d.ts +76 -0
- package/lib/lib.es2022.array.d.ts +119 -0
- package/lib/lib.es2022.d.ts +23 -0
- package/lib/lib.es2022.error.d.ts +73 -0
- package/lib/lib.es2022.full.d.ts +22 -0
- package/lib/lib.es2022.intl.d.ts +143 -0
- package/lib/lib.es2022.object.d.ts +24 -0
- package/lib/lib.es2022.regexp.d.ts +37 -0
- package/lib/lib.es2022.string.d.ts +23 -0
- package/lib/lib.es2023.array.d.ts +922 -0
- package/lib/lib.es2023.collection.d.ts +19 -0
- package/lib/lib.es2023.d.ts +20 -0
- package/lib/lib.es2023.full.d.ts +22 -0
- package/lib/lib.es2023.intl.d.ts +62 -0
- package/lib/lib.es2024.arraybuffer.d.ts +63 -0
- package/lib/lib.es2024.collection.d.ts +27 -0
- package/lib/lib.es2024.d.ts +24 -0
- package/lib/lib.es2024.full.d.ts +22 -0
- package/lib/lib.es2024.object.d.ts +27 -0
- package/lib/lib.es2024.promise.d.ts +33 -0
- package/lib/lib.es2024.regexp.d.ts +23 -0
- package/lib/lib.es2024.sharedmemory.d.ts +66 -0
- package/lib/lib.es2024.string.d.ts +27 -0
- package/lib/lib.es2025.collection.d.ts +94 -0
- package/lib/lib.es2025.d.ts +23 -0
- package/lib/lib.es2025.float16.d.ts +443 -0
- package/lib/lib.es2025.full.d.ts +22 -0
- package/lib/lib.es2025.intl.d.ts +200 -0
- package/lib/lib.es2025.iterator.d.ts +146 -0
- package/lib/lib.es2025.promise.d.ts +32 -0
- package/lib/lib.es2025.regexp.d.ts +30 -0
- package/lib/lib.es5.d.ts +4599 -0
- package/lib/lib.es6.d.ts +21 -0
- package/lib/lib.esnext.array.d.ts +33 -0
- package/lib/lib.esnext.collection.d.ts +47 -0
- package/lib/lib.esnext.d.ts +27 -0
- package/lib/lib.esnext.date.d.ts +21 -0
- package/lib/lib.esnext.decorators.d.ts +26 -0
- package/lib/lib.esnext.disposable.d.ts +191 -0
- package/lib/lib.esnext.error.d.ts +22 -0
- package/lib/lib.esnext.full.d.ts +22 -0
- package/lib/lib.esnext.intl.d.ts +107 -0
- package/lib/lib.esnext.sharedmemory.d.ts +23 -0
- package/lib/lib.esnext.temporal.d.ts +485 -0
- package/lib/lib.esnext.typedarrays.d.ts +90 -0
- package/lib/lib.scripthost.d.ts +320 -0
- package/lib/lib.webworker.asynciterable.d.ts +18 -0
- package/lib/lib.webworker.d.ts +15606 -0
- package/lib/lib.webworker.importscripts.d.ts +21 -0
- package/lib/lib.webworker.iterable.d.ts +18 -0
- package/lib/logger.js +144 -0
- package/lib/package.json +7 -0
- package/lib/tsserver.js +57 -0
- package/lib/tsserverlibrary.js +171 -0
- package/lib/typesMap.json +497 -0
- package/lib/typescript.js +373 -0
- package/package.json +115 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JustScale
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# @justscale/typescript
|
|
2
|
+
|
|
3
|
+
Drop-in TypeScript replacement (`ptsc`, `ptscserver`) with two extensions your normal `tsc` can't do:
|
|
4
|
+
|
|
5
|
+
1. **Durable-process handler compilation** — handlers passed to `createProcess(...)` are rewritten into an opcode-driven shape that can be serialized at any suspension point, survive a restart, and resume mid-flight.
|
|
6
|
+
2. **TSPxxxx diagnostics** — process-specific errors (unhandled `await`, non-serializable `const`, unsupported control flow) surface as TS diagnostics with stable error codes.
|
|
7
|
+
|
|
8
|
+
Everything else behaves exactly like `tsc` / `tsserver`.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add -D @justscale/typescript typescript
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`typescript` (>=6.0) is a peer dep — bring your own, `@justscale/typescript` wraps it.
|
|
17
|
+
|
|
18
|
+
## CLI
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npx ptsc # compile using tsconfig.json
|
|
22
|
+
npx ptsc --noEmit # type-check only
|
|
23
|
+
npx ptsc -w # watch
|
|
24
|
+
npx ptsc -b tsconfig.build.json
|
|
25
|
+
npx ptsc --init # scaffold a tsconfig.json
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Every `tsc` flag passes through. Also installed: `ptscserver` (tsserver equivalent), plus `justscale-tsc` / `justscale-tsserver` aliases.
|
|
29
|
+
|
|
30
|
+
## IDE
|
|
31
|
+
|
|
32
|
+
### VS Code
|
|
33
|
+
|
|
34
|
+
Point the TypeScript SDK at this package:
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
// .vscode/settings.json
|
|
38
|
+
{ "typescript.tsdk": "node_modules/@justscale/typescript/lib" }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Full TypeScript IntelliSense plus JustScale diagnostics in the editor.
|
|
42
|
+
|
|
43
|
+
## tsconfig
|
|
44
|
+
|
|
45
|
+
A `justscale` section at the root of `tsconfig.json` configures the extensions:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"compilerOptions": {
|
|
50
|
+
"target": "ES2022",
|
|
51
|
+
"module": "NodeNext",
|
|
52
|
+
"moduleResolution": "NodeNext",
|
|
53
|
+
"strict": true
|
|
54
|
+
},
|
|
55
|
+
"justscale": {
|
|
56
|
+
"processFilePattern": "*.process.ts",
|
|
57
|
+
"processModules": ["@justscale/core/process"],
|
|
58
|
+
"strict": true,
|
|
59
|
+
"verbose": false
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Defaults are almost always right; you only need the `justscale` section if you're customising the process-file convention or pointing at a non-standard process module.
|
|
65
|
+
|
|
66
|
+
## TSPxxxx diagnostics
|
|
67
|
+
|
|
68
|
+
Stable error codes for process-specific issues:
|
|
69
|
+
|
|
70
|
+
| Code | Meaning |
|
|
71
|
+
|-|-|
|
|
72
|
+
| TSP0001 | Missing `await` on a suspension point (`delay`, `signal`, `race`, ...) |
|
|
73
|
+
| TSP0002 | Variable captured incorrectly across a suspension point |
|
|
74
|
+
| TSP0003 | Invalid process handler signature |
|
|
75
|
+
| TSP0004 | Unsupported control flow in a process handler |
|
|
76
|
+
| TSP1001 | Awaited service call needs `using` instead of `const` so the value is re-fetched on resume |
|
|
77
|
+
|
|
78
|
+
TSP1001 in practice:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
// fails — const captures the value; after a resume it's stale
|
|
82
|
+
const order = await orders.findById(orderId);
|
|
83
|
+
|
|
84
|
+
// works — using tells the runtime to re-hydrate on resume
|
|
85
|
+
using order = await orders.findById(orderId);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Full list: https://justscale.sh/docs/processes/compiler.
|
|
89
|
+
|
|
90
|
+
## Programmatic API
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { transpile, transpileProject } from '@justscale/typescript/api';
|
|
94
|
+
|
|
95
|
+
const result = transpile(source, 'example.process.ts');
|
|
96
|
+
if (result.success) console.log(result.code);
|
|
97
|
+
|
|
98
|
+
const project = transpileProject('./tsconfig.json');
|
|
99
|
+
for (const [file] of project.files) console.log('compiled:', file);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`createProcessTransformer(program, opts)` is also exported for wiring the transform into a custom `ts.createProgram` pipeline or ts-patch setup.
|
|
103
|
+
|
|
104
|
+
## Subpath exports
|
|
105
|
+
|
|
106
|
+
- `@justscale/typescript` — main programmatic API (`transpile`, `transpileProject`, `createProgram`, `createProcessTransformer`, `formatDiagnostics`).
|
|
107
|
+
- `@justscale/typescript/api` — the narrower `transpile*` surface if that's all you need.
|
|
108
|
+
- `@justscale/typescript/config` — config parsing (`parseConfig`, `findConfig`, `defaultConfig`).
|
|
109
|
+
- `@justscale/typescript/register` — Node `--import` hook; used by the monorepo's test runner (`node --import @justscale/typescript/register ...`) so tests see the same transform as production.
|
|
110
|
+
- `@justscale/typescript/loader` — lower-level loader primitives if you're building your own runtime.
|
|
111
|
+
- `@justscale/typescript/server` — embeddable `ptscserver` internals.
|
|
112
|
+
- `@justscale/typescript/editor` — editor-side helpers for refactors, quick fixes.
|
|
113
|
+
|
|
114
|
+
## How the process compilation works
|
|
115
|
+
|
|
116
|
+
The transformer walks each `createProcess(...)` handler AST:
|
|
117
|
+
|
|
118
|
+
1. **Analyses** suspension points (`delay`, `signal`, `race`, `for await`).
|
|
119
|
+
2. **Splits** the handler into synchronous blocks separated by suspension points.
|
|
120
|
+
3. **Generates opcodes** (WAIT, BLOCK, JUMP, RACE_START, ...) that the runtime executes.
|
|
121
|
+
4. **Tracks rehydration** for `using` declarations so the runtime knows what to re-fetch when resuming.
|
|
122
|
+
5. **Emits** a compiled process descriptor that replaces the handler at runtime.
|
|
123
|
+
|
|
124
|
+
The runtime pairs with `@justscale/core/process` to execute the opcodes, serialise state at any checkpoint, and resume from a saved state when a signal fires or a timer expires.
|
|
125
|
+
|
|
126
|
+
## Docs
|
|
127
|
+
|
|
128
|
+
https://justscale.sh/docs/processes/signals · https://justscale.sh/docs/processes/compiler · [VSCODE.md](./VSCODE.md) for detailed editor setup.
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Programmatic API for the JustScale TypeScript compiler.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { transpile, transpileProject } from '@justscale/typescript/api'
|
|
7
|
+
* const result = transpile(source, 'example.process.ts')
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
import ts from 'typescript';
|
|
11
|
+
import { type JustScaleConfig } from './config/index.js';
|
|
12
|
+
/**
|
|
13
|
+
* Result of transpiling a single file
|
|
14
|
+
*/
|
|
15
|
+
export interface TranspileResult {
|
|
16
|
+
/** Transpiled JavaScript code */
|
|
17
|
+
code: string;
|
|
18
|
+
/** Source map (if enabled) */
|
|
19
|
+
sourceMap?: string;
|
|
20
|
+
/** Declaration file content (if enabled) */
|
|
21
|
+
declaration?: string;
|
|
22
|
+
/** Diagnostics from compilation */
|
|
23
|
+
diagnostics: ts.Diagnostic[];
|
|
24
|
+
/** Whether compilation was successful (no errors) */
|
|
25
|
+
success: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Result of transpiling a project
|
|
29
|
+
*/
|
|
30
|
+
export interface TranspileProjectResult {
|
|
31
|
+
/** Results for each file */
|
|
32
|
+
files: Map<string, TranspileResult>;
|
|
33
|
+
/** Project-wide diagnostics */
|
|
34
|
+
diagnostics: ts.Diagnostic[];
|
|
35
|
+
/** Whether compilation was successful */
|
|
36
|
+
success: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Options for transpilation
|
|
40
|
+
*/
|
|
41
|
+
export interface TranspileOptions {
|
|
42
|
+
/** TypeScript compiler options */
|
|
43
|
+
compilerOptions?: ts.CompilerOptions;
|
|
44
|
+
/** JustScale-specific options */
|
|
45
|
+
justscale?: JustScaleConfig;
|
|
46
|
+
/** Custom transformers to apply before JustScale transformer */
|
|
47
|
+
transformers?: ts.CustomTransformers;
|
|
48
|
+
/** Generate source maps */
|
|
49
|
+
sourceMap?: boolean;
|
|
50
|
+
/** Generate declaration files */
|
|
51
|
+
declaration?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Transpile a single TypeScript source string
|
|
55
|
+
*
|
|
56
|
+
* @param source - TypeScript source code
|
|
57
|
+
* @param fileName - Virtual file name (affects process detection)
|
|
58
|
+
* @param options - Compilation options
|
|
59
|
+
* @returns Transpilation result
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const result = transpile(`
|
|
64
|
+
* import { createProcess, delay } from '@justscale/core/process'
|
|
65
|
+
*
|
|
66
|
+
* export const myProcess = createProcess({
|
|
67
|
+
* name: 'my-process',
|
|
68
|
+
* async handler() {
|
|
69
|
+
* await delay(1000)
|
|
70
|
+
* return 'done'
|
|
71
|
+
* }
|
|
72
|
+
* })
|
|
73
|
+
* `, 'example.process.ts')
|
|
74
|
+
*
|
|
75
|
+
* console.log(result.code)
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function transpile(source: string, fileName?: string, options?: TranspileOptions): TranspileResult;
|
|
79
|
+
/**
|
|
80
|
+
* Transpile a TypeScript project from a tsconfig.json
|
|
81
|
+
*
|
|
82
|
+
* @param configPath - Path to tsconfig.json
|
|
83
|
+
* @param options - Additional options to override config
|
|
84
|
+
* @returns Results for all files in the project
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const result = transpileProject('./tsconfig.json')
|
|
89
|
+
*
|
|
90
|
+
* if (result.success) {
|
|
91
|
+
* for (const [file, output] of result.files) {
|
|
92
|
+
* console.log(`${file}: ${output.code.length} bytes`)
|
|
93
|
+
* }
|
|
94
|
+
* } else {
|
|
95
|
+
* console.error('Compilation failed:', result.diagnostics)
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function transpileProject(configPath: string, options?: TranspileOptions): TranspileProjectResult;
|
|
100
|
+
/**
|
|
101
|
+
* Create a TypeScript Program with JustScale extensions
|
|
102
|
+
*
|
|
103
|
+
* Use this for advanced use cases where you need full control over
|
|
104
|
+
* the compilation process.
|
|
105
|
+
*
|
|
106
|
+
* @param fileNames - Files to include in the program
|
|
107
|
+
* @param options - Compiler options
|
|
108
|
+
* @returns A TypeScript Program
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* const program = createProgram(['./src/index.ts'], {
|
|
113
|
+
* target: ts.ScriptTarget.ES2022,
|
|
114
|
+
* module: ts.ModuleKind.NodeNext,
|
|
115
|
+
* })
|
|
116
|
+
*
|
|
117
|
+
* // Get process diagnostics
|
|
118
|
+
* const diagnostics = getProcessDiagnostics(program)
|
|
119
|
+
*
|
|
120
|
+
* // Custom emit with transformer
|
|
121
|
+
* program.emit(undefined, undefined, undefined, undefined, {
|
|
122
|
+
* before: [createProcessTransformer(program)]
|
|
123
|
+
* })
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function createProgram(fileNames: string[], options?: ts.CompilerOptions): ts.Program;
|
|
127
|
+
/**
|
|
128
|
+
* Get JustScale process diagnostics for a program
|
|
129
|
+
*
|
|
130
|
+
* @param program - TypeScript program
|
|
131
|
+
* @param config - JustScale configuration
|
|
132
|
+
* @returns Array of diagnostics
|
|
133
|
+
*/
|
|
134
|
+
export declare function getProcessDiagnostics(program: ts.Program, config?: JustScaleConfig): ts.Diagnostic[];
|
|
135
|
+
/**
|
|
136
|
+
* Format diagnostics for console output
|
|
137
|
+
*/
|
|
138
|
+
export declare function formatDiagnostics(diagnostics: ts.Diagnostic[]): string;
|
|
139
|
+
export { createProcessTransformer } from './compiler/transformer.js';
|
|
140
|
+
export { analyzeHandler } from './compiler/analyzer.js';
|
|
141
|
+
export { formatErrorCode, ProcessErrorCode } from './compiler/errors.js';
|
|
142
|
+
export { parseConfig, defaultConfig } from './config/index.js';
|
|
143
|
+
export type { JustScaleConfig, ParsedConfig } from './config/index.js';
|
|
144
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,MAAM,YAAY,CAAC;AAM5B,OAAO,EAA8B,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAErF;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,8BAA8B;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,4CAA4C;IAC5C,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,mCAAmC;IACnC,WAAW,EAAE,EAAE,CAAC,UAAU,EAAE,CAAA;IAC5B,qDAAqD;IACrD,OAAO,EAAE,OAAO,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,4BAA4B;IAC5B,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IACnC,+BAA+B;IAC/B,WAAW,EAAE,EAAE,CAAC,UAAU,EAAE,CAAA;IAC5B,yCAAyC;IACzC,OAAO,EAAE,OAAO,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,kCAAkC;IAClC,eAAe,CAAC,EAAE,EAAE,CAAC,eAAe,CAAA;IACpC,iCAAiC;IACjC,SAAS,CAAC,EAAE,eAAe,CAAA;IAC3B,gEAAgE;IAChE,YAAY,CAAC,EAAE,EAAE,CAAC,kBAAkB,CAAA;IACpC,2BAA2B;IAC3B,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,iCAAiC;IACjC,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,QAAQ,SAAa,EACrB,OAAO,GAAE,gBAAqB,GAC7B,eAAe,CA8GjB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,gBAAqB,GAC7B,sBAAsB,CAmGxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,MAAM,EAAE,EACnB,OAAO,CAAC,EAAE,EAAE,CAAC,eAAe,GAC3B,EAAE,CAAC,OAAO,CAYZ;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,EAAE,CAAC,OAAO,EACnB,MAAM,GAAE,eAA+B,GACtC,EAAE,CAAC,UAAU,EAAE,CAWjB;AAoFD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,EAAE,CAAC,UAAU,EAAE,GAAG,MAAM,CAQtE;AAGD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAC/D,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Programmatic API for the JustScale TypeScript compiler.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { transpile, transpileProject } from '@justscale/typescript/api'
|
|
7
|
+
* const result = transpile(source, 'example.process.ts')
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
import ts from 'typescript';
|
|
11
|
+
import { resolve } from 'node:path';
|
|
12
|
+
import { analyzeHandler } from './compiler/analyzer.js';
|
|
13
|
+
import { filterUsingExportsDiagnostics } from './compiler/errors.js';
|
|
14
|
+
import { createProcessTransformer } from './compiler/transformer.js';
|
|
15
|
+
import { extractAndInjectExportsTypes, createModifiedHost } from './compiler/exports-prepass.js';
|
|
16
|
+
import { parseConfig, defaultConfig } from './config/index.js';
|
|
17
|
+
/**
|
|
18
|
+
* Transpile a single TypeScript source string
|
|
19
|
+
*
|
|
20
|
+
* @param source - TypeScript source code
|
|
21
|
+
* @param fileName - Virtual file name (affects process detection)
|
|
22
|
+
* @param options - Compilation options
|
|
23
|
+
* @returns Transpilation result
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const result = transpile(`
|
|
28
|
+
* import { createProcess, delay } from '@justscale/core/process'
|
|
29
|
+
*
|
|
30
|
+
* export const myProcess = createProcess({
|
|
31
|
+
* name: 'my-process',
|
|
32
|
+
* async handler() {
|
|
33
|
+
* await delay(1000)
|
|
34
|
+
* return 'done'
|
|
35
|
+
* }
|
|
36
|
+
* })
|
|
37
|
+
* `, 'example.process.ts')
|
|
38
|
+
*
|
|
39
|
+
* console.log(result.code)
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function transpile(source, fileName = 'input.ts', options = {}) {
|
|
43
|
+
const compilerOptions = {
|
|
44
|
+
target: ts.ScriptTarget.ES2022,
|
|
45
|
+
module: ts.ModuleKind.NodeNext,
|
|
46
|
+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
47
|
+
// Include ES2022 lib by default for Promise, async/await, etc.
|
|
48
|
+
lib: ['lib.es2022.d.ts'],
|
|
49
|
+
...options.compilerOptions,
|
|
50
|
+
sourceMap: options.sourceMap ?? options.compilerOptions?.sourceMap,
|
|
51
|
+
declaration: options.declaration ?? options.compilerOptions?.declaration,
|
|
52
|
+
};
|
|
53
|
+
const jsConfig = options.justscale ?? defaultConfig;
|
|
54
|
+
const sourceFile = ts.createSourceFile(fileName, source, compilerOptions.target ?? ts.ScriptTarget.ES2022, true);
|
|
55
|
+
const defaultHost = ts.createCompilerHost(compilerOptions);
|
|
56
|
+
const host = {
|
|
57
|
+
...defaultHost,
|
|
58
|
+
getSourceFile: (name, languageVersion) => {
|
|
59
|
+
if (name === fileName)
|
|
60
|
+
return sourceFile;
|
|
61
|
+
// Use the default host to load lib files and other dependencies
|
|
62
|
+
return defaultHost.getSourceFile(name, languageVersion);
|
|
63
|
+
},
|
|
64
|
+
writeFile: () => { }, // We'll capture output differently
|
|
65
|
+
fileExists: (name) => name === fileName || defaultHost.fileExists(name),
|
|
66
|
+
readFile: (name) => {
|
|
67
|
+
if (name === fileName)
|
|
68
|
+
return source;
|
|
69
|
+
return defaultHost.readFile(name);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
const program1 = ts.createProgram([fileName], compilerOptions, host);
|
|
73
|
+
const modifiedSources = extractAndInjectExportsTypes(program1);
|
|
74
|
+
let program;
|
|
75
|
+
if (modifiedSources.size > 0) {
|
|
76
|
+
const modifiedText = modifiedSources.get(fileName);
|
|
77
|
+
if (modifiedText) {
|
|
78
|
+
const host2 = {
|
|
79
|
+
...host,
|
|
80
|
+
getSourceFile: (name, languageVersionOrOptions, onError) => {
|
|
81
|
+
if (name === fileName) {
|
|
82
|
+
return ts.createSourceFile(name, modifiedText, languageVersionOrOptions, true);
|
|
83
|
+
}
|
|
84
|
+
return host.getSourceFile(name, languageVersionOrOptions, onError);
|
|
85
|
+
},
|
|
86
|
+
readFile: (name) => {
|
|
87
|
+
if (name === fileName)
|
|
88
|
+
return modifiedText;
|
|
89
|
+
return host.readFile(name);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
program = ts.createProgram([fileName], compilerOptions, host2, program1);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
program = program1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
program = program1;
|
|
100
|
+
}
|
|
101
|
+
const finalSourceFile = program.getSourceFile(fileName);
|
|
102
|
+
const diagnostics = filterUsingExportsDiagnostics([
|
|
103
|
+
...program.getSyntacticDiagnostics(finalSourceFile),
|
|
104
|
+
...program.getSemanticDiagnostics(finalSourceFile),
|
|
105
|
+
]);
|
|
106
|
+
const processDiagnostics = getProcessDiagnosticsForFile(program, finalSourceFile, jsConfig);
|
|
107
|
+
diagnostics.push(...processDiagnostics);
|
|
108
|
+
let code = '';
|
|
109
|
+
let sourceMap;
|
|
110
|
+
let declaration;
|
|
111
|
+
program.emit(finalSourceFile, (name, text) => {
|
|
112
|
+
if (name.endsWith('.map')) {
|
|
113
|
+
sourceMap = text;
|
|
114
|
+
}
|
|
115
|
+
else if (name.endsWith('.d.ts')) {
|
|
116
|
+
declaration = text;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
code = text;
|
|
120
|
+
}
|
|
121
|
+
}, undefined, undefined, {
|
|
122
|
+
before: [
|
|
123
|
+
...(options.transformers?.before ?? []),
|
|
124
|
+
createProcessTransformer(program, { verbose: jsConfig.verbose ?? false }),
|
|
125
|
+
],
|
|
126
|
+
after: options.transformers?.after,
|
|
127
|
+
afterDeclarations: options.transformers?.afterDeclarations,
|
|
128
|
+
});
|
|
129
|
+
const hasErrors = diagnostics.some((d) => d.category === ts.DiagnosticCategory.Error);
|
|
130
|
+
return {
|
|
131
|
+
code,
|
|
132
|
+
sourceMap,
|
|
133
|
+
declaration,
|
|
134
|
+
diagnostics,
|
|
135
|
+
success: !hasErrors,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Transpile a TypeScript project from a tsconfig.json
|
|
140
|
+
*
|
|
141
|
+
* @param configPath - Path to tsconfig.json
|
|
142
|
+
* @param options - Additional options to override config
|
|
143
|
+
* @returns Results for all files in the project
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const result = transpileProject('./tsconfig.json')
|
|
148
|
+
*
|
|
149
|
+
* if (result.success) {
|
|
150
|
+
* for (const [file, output] of result.files) {
|
|
151
|
+
* console.log(`${file}: ${output.code.length} bytes`)
|
|
152
|
+
* }
|
|
153
|
+
* } else {
|
|
154
|
+
* console.error('Compilation failed:', result.diagnostics)
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export function transpileProject(configPath, options = {}) {
|
|
159
|
+
const resolvedPath = resolve(configPath);
|
|
160
|
+
const config = parseConfig(resolvedPath, options.compilerOptions);
|
|
161
|
+
const mergedOptions = {
|
|
162
|
+
...config.compilerOptions,
|
|
163
|
+
...options.compilerOptions,
|
|
164
|
+
};
|
|
165
|
+
const jsConfig = {
|
|
166
|
+
...config.justscale,
|
|
167
|
+
...options.justscale,
|
|
168
|
+
};
|
|
169
|
+
const program1 = ts.createProgram({
|
|
170
|
+
rootNames: config.fileNames,
|
|
171
|
+
options: mergedOptions,
|
|
172
|
+
projectReferences: config.projectReferences,
|
|
173
|
+
});
|
|
174
|
+
const modifiedSources = extractAndInjectExportsTypes(program1);
|
|
175
|
+
let program;
|
|
176
|
+
if (modifiedSources.size > 0) {
|
|
177
|
+
const defaultHost = ts.createCompilerHost(mergedOptions);
|
|
178
|
+
const host2 = createModifiedHost(defaultHost, modifiedSources);
|
|
179
|
+
program = ts.createProgram({
|
|
180
|
+
rootNames: config.fileNames,
|
|
181
|
+
options: mergedOptions,
|
|
182
|
+
projectReferences: config.projectReferences,
|
|
183
|
+
host: host2,
|
|
184
|
+
oldProgram: program1,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
program = program1;
|
|
189
|
+
}
|
|
190
|
+
const allDiagnostics = filterUsingExportsDiagnostics([
|
|
191
|
+
...program.getConfigFileParsingDiagnostics(),
|
|
192
|
+
...program.getSyntacticDiagnostics(),
|
|
193
|
+
...program.getOptionsDiagnostics(),
|
|
194
|
+
...program.getGlobalDiagnostics(),
|
|
195
|
+
...program.getSemanticDiagnostics(),
|
|
196
|
+
]);
|
|
197
|
+
for (const sourceFile of program.getSourceFiles()) {
|
|
198
|
+
if (sourceFile.isDeclarationFile)
|
|
199
|
+
continue;
|
|
200
|
+
if (sourceFile.fileName.includes('node_modules'))
|
|
201
|
+
continue;
|
|
202
|
+
const processDiagnostics = getProcessDiagnosticsForFile(program, sourceFile, jsConfig);
|
|
203
|
+
allDiagnostics.push(...processDiagnostics);
|
|
204
|
+
}
|
|
205
|
+
const files = new Map();
|
|
206
|
+
const emitResult = program.emit(undefined, (fileName, text, writeByteOrderMark, onError, sourceFiles) => {
|
|
207
|
+
if (!sourceFiles || sourceFiles.length === 0)
|
|
208
|
+
return;
|
|
209
|
+
const sourceFile = sourceFiles[0];
|
|
210
|
+
const originalName = sourceFile.fileName;
|
|
211
|
+
const existing = files.get(originalName) ?? {
|
|
212
|
+
code: '',
|
|
213
|
+
diagnostics: [],
|
|
214
|
+
success: true,
|
|
215
|
+
};
|
|
216
|
+
if (fileName.endsWith('.map')) {
|
|
217
|
+
existing.sourceMap = text;
|
|
218
|
+
}
|
|
219
|
+
else if (fileName.endsWith('.d.ts')) {
|
|
220
|
+
existing.declaration = text;
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
existing.code = text;
|
|
224
|
+
}
|
|
225
|
+
files.set(originalName, existing);
|
|
226
|
+
}, undefined, undefined, {
|
|
227
|
+
before: [
|
|
228
|
+
...(options.transformers?.before ?? []),
|
|
229
|
+
createProcessTransformer(program, { verbose: jsConfig.verbose ?? false }),
|
|
230
|
+
],
|
|
231
|
+
after: options.transformers?.after,
|
|
232
|
+
afterDeclarations: options.transformers?.afterDeclarations,
|
|
233
|
+
});
|
|
234
|
+
allDiagnostics.push(...emitResult.diagnostics);
|
|
235
|
+
const hasErrors = allDiagnostics.some((d) => d.category === ts.DiagnosticCategory.Error);
|
|
236
|
+
return {
|
|
237
|
+
files,
|
|
238
|
+
diagnostics: allDiagnostics,
|
|
239
|
+
success: !hasErrors,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Create a TypeScript Program with JustScale extensions
|
|
244
|
+
*
|
|
245
|
+
* Use this for advanced use cases where you need full control over
|
|
246
|
+
* the compilation process.
|
|
247
|
+
*
|
|
248
|
+
* @param fileNames - Files to include in the program
|
|
249
|
+
* @param options - Compiler options
|
|
250
|
+
* @returns A TypeScript Program
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* const program = createProgram(['./src/index.ts'], {
|
|
255
|
+
* target: ts.ScriptTarget.ES2022,
|
|
256
|
+
* module: ts.ModuleKind.NodeNext,
|
|
257
|
+
* })
|
|
258
|
+
*
|
|
259
|
+
* // Get process diagnostics
|
|
260
|
+
* const diagnostics = getProcessDiagnostics(program)
|
|
261
|
+
*
|
|
262
|
+
* // Custom emit with transformer
|
|
263
|
+
* program.emit(undefined, undefined, undefined, undefined, {
|
|
264
|
+
* before: [createProcessTransformer(program)]
|
|
265
|
+
* })
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
export function createProgram(fileNames, options) {
|
|
269
|
+
const compilerOptions = {
|
|
270
|
+
target: ts.ScriptTarget.ES2022,
|
|
271
|
+
module: ts.ModuleKind.NodeNext,
|
|
272
|
+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
273
|
+
...options,
|
|
274
|
+
};
|
|
275
|
+
return ts.createProgram({
|
|
276
|
+
rootNames: fileNames,
|
|
277
|
+
options: compilerOptions,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Get JustScale process diagnostics for a program
|
|
282
|
+
*
|
|
283
|
+
* @param program - TypeScript program
|
|
284
|
+
* @param config - JustScale configuration
|
|
285
|
+
* @returns Array of diagnostics
|
|
286
|
+
*/
|
|
287
|
+
export function getProcessDiagnostics(program, config = defaultConfig) {
|
|
288
|
+
const diagnostics = [];
|
|
289
|
+
for (const sourceFile of program.getSourceFiles()) {
|
|
290
|
+
if (sourceFile.isDeclarationFile)
|
|
291
|
+
continue;
|
|
292
|
+
if (sourceFile.fileName.includes('node_modules'))
|
|
293
|
+
continue;
|
|
294
|
+
diagnostics.push(...getProcessDiagnosticsForFile(program, sourceFile, config));
|
|
295
|
+
}
|
|
296
|
+
return diagnostics;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Get process diagnostics for a single file
|
|
300
|
+
*/
|
|
301
|
+
function getProcessDiagnosticsForFile(program, sourceFile, config) {
|
|
302
|
+
const diagnostics = [];
|
|
303
|
+
const typeChecker = program.getTypeChecker();
|
|
304
|
+
const processModules = config.processModules ?? defaultConfig.processModules;
|
|
305
|
+
// Check if this is a process file
|
|
306
|
+
if (!isProcessFile(sourceFile, config, processModules)) {
|
|
307
|
+
return diagnostics;
|
|
308
|
+
}
|
|
309
|
+
// Find createProcess calls and analyze
|
|
310
|
+
ts.forEachChild(sourceFile, function visit(node) {
|
|
311
|
+
if (ts.isCallExpression(node)) {
|
|
312
|
+
const expr = node.expression;
|
|
313
|
+
if (ts.isIdentifier(expr) && expr.text === 'createProcess') {
|
|
314
|
+
const configArg = node.arguments[0];
|
|
315
|
+
if (configArg && ts.isObjectLiteralExpression(configArg)) {
|
|
316
|
+
const handler = findHandler(configArg);
|
|
317
|
+
if (handler) {
|
|
318
|
+
const analysis = analyzeHandler(handler, typeChecker);
|
|
319
|
+
diagnostics.push(...analysis.diagnostics);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
ts.forEachChild(node, visit);
|
|
325
|
+
});
|
|
326
|
+
return diagnostics;
|
|
327
|
+
}
|
|
328
|
+
function isProcessFile(sourceFile, config, processModules) {
|
|
329
|
+
const pattern = config.processFilePattern;
|
|
330
|
+
if (pattern && sourceFile.fileName.includes(pattern.replace('*', ''))) {
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
if (sourceFile.fileName.includes('.process.'))
|
|
334
|
+
return true;
|
|
335
|
+
for (const stmt of sourceFile.statements) {
|
|
336
|
+
if (ts.isImportDeclaration(stmt)) {
|
|
337
|
+
const moduleSpecifier = stmt.moduleSpecifier;
|
|
338
|
+
if (ts.isStringLiteral(moduleSpecifier)) {
|
|
339
|
+
if (processModules.includes(moduleSpecifier.text)) {
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
function findHandler(configObj) {
|
|
348
|
+
for (const prop of configObj.properties) {
|
|
349
|
+
if (ts.isMethodDeclaration(prop)) {
|
|
350
|
+
if (ts.isIdentifier(prop.name) && prop.name.text === 'handler') {
|
|
351
|
+
return prop;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
else if (ts.isPropertyAssignment(prop)) {
|
|
355
|
+
if (ts.isIdentifier(prop.name) && prop.name.text === 'handler') {
|
|
356
|
+
if (ts.isFunctionExpression(prop.initializer) || ts.isArrowFunction(prop.initializer)) {
|
|
357
|
+
return prop.initializer;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Format diagnostics for console output
|
|
366
|
+
*/
|
|
367
|
+
export function formatDiagnostics(diagnostics) {
|
|
368
|
+
const host = {
|
|
369
|
+
getCanonicalFileName: (path) => path,
|
|
370
|
+
getCurrentDirectory: () => process.cwd(),
|
|
371
|
+
getNewLine: () => '\n',
|
|
372
|
+
};
|
|
373
|
+
return ts.formatDiagnosticsWithColorAndContext(diagnostics, host);
|
|
374
|
+
}
|
|
375
|
+
// Re-export the transformer for custom usage
|
|
376
|
+
export { createProcessTransformer } from './compiler/transformer.js';
|
|
377
|
+
export { analyzeHandler } from './compiler/analyzer.js';
|
|
378
|
+
export { formatErrorCode, ProcessErrorCode } from './compiler/errors.js';
|
|
379
|
+
export { parseConfig, defaultConfig } from './config/index.js';
|
|
380
|
+
//# sourceMappingURL=api.js.map
|