@tsrx/content-mapper 0.1.1 → 0.1.2
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/README.md +83 -51
- package/dist/{mapper-DATqGboo.js → mapper-B4f--1ac.js} +309 -41
- package/dist/mapper-B4f--1ac.js.map +1 -0
- package/dist/mapper.js +2 -2
- package/dist/protocol.js +22 -12
- package/dist/protocol.js.map +1 -1
- package/dist/rpc.js +11 -2
- package/dist/rpc.js.map +1 -1
- package/dist/server.js +1 -1
- package/package.json +7 -7
- package/src/diagnostic-directives.js +199 -0
- package/src/export-stub.js +94 -7
- package/src/mapper.js +43 -40
- package/src/protocol.js +23 -13
- package/src/rpc.js +11 -2
- package/dist/mapper-DATqGboo.js.map +0 -1
package/README.md
CHANGED
|
@@ -12,11 +12,13 @@ https://github.com/tsrx-org/tsrx/issues/136.
|
|
|
12
12
|
|
|
13
13
|
## Requirements
|
|
14
14
|
|
|
15
|
-
- **TypeScript `7.1.0-dev.
|
|
15
|
+
- **TypeScript `7.1.0-dev.20260923.1` or newer.** The content-mapper protocol is
|
|
16
16
|
not in the stable 7.0 line: `typescript@7.0.2` rejects `--runExternalCode`
|
|
17
|
-
(TS5023) and ignores `contentMappers`.
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
(TS5023) and ignores `contentMappers`. The protocol arrived in
|
|
18
|
+
`7.1.0-dev.20260822.1` (`7.1.0-dev.20260821.1` fails this package's test suite),
|
|
19
|
+
but `tsc --watch` never recompiled on macOS until `7.1.0-dev.20260923.1`
|
|
20
|
+
(microsoft/TypeScript#64351), the oldest nightly that passes the whole suite.
|
|
21
|
+
The repository runs `7.1.0-dev.20260930.4`. Run the suite against another build
|
|
20
22
|
with `TSRX_NATIVE_TSC=<path to tsc>`.
|
|
21
23
|
- **`--runExternalCode`.** TypeScript only spawns the mapper when the user opts
|
|
22
24
|
in; the mapper never enables it. Without the flag, a `contentMappers` entry is
|
|
@@ -89,6 +91,29 @@ One precedence rule, highest first:
|
|
|
89
91
|
|
|
90
92
|
`platform` follows the same rule with `tsrx.platform`.
|
|
91
93
|
|
|
94
|
+
### What a compiler returns
|
|
95
|
+
|
|
96
|
+
The mapper runs any TSRX compiler, this repository's targets, Ripple, Octane or a
|
|
97
|
+
third-party one, through
|
|
98
|
+
`compile_to_volar_mappings(source, filename, { loose: true, platform })`, and
|
|
99
|
+
relies on what it returns:
|
|
100
|
+
|
|
101
|
+
- `code`: the generated TSX. Keep it valid TSX: a directive hides only
|
|
102
|
+
TypeScript's checker errors, so a syntax error TypeScript finds in the generated
|
|
103
|
+
code shows as it is, next to the compiler's own error.
|
|
104
|
+
- `mappings`: Volar mappings from the source to `code`. Map every token of a
|
|
105
|
+
statement, keywords and punctuation too, so that TypeScript's errors land on the
|
|
106
|
+
source and a directive covers the statement's whole generated code.
|
|
107
|
+
- `errors`: the collected errors, each with `pos` and `end` offsets into the
|
|
108
|
+
source and a `code`, TSRX's (`TSRX2002`) or TypeScript's (`TS1186`). An error
|
|
109
|
+
without a code is sent as `771001`.
|
|
110
|
+
- `sourceAst`: the source tree, with `start` and `end` on its nodes. The mapper
|
|
111
|
+
finds the statement, class or type member, or element that holds an error in it.
|
|
112
|
+
Without a tree, a directive covers only the error's own span.
|
|
113
|
+
|
|
114
|
+
A mistake the compiler cannot recover from is thrown, with `pos` and `code`: the
|
|
115
|
+
mapper then sends that error alone, and a stub of the file's exports.
|
|
116
|
+
|
|
92
117
|
### What TypeScript sees
|
|
93
118
|
|
|
94
119
|
- The generated TSX of the type-only transform, with a span map. Spans whose text
|
|
@@ -106,12 +131,23 @@ One precedence rule, highest first:
|
|
|
106
131
|
or with the component's, and contributes nothing to declaration output. `import`
|
|
107
132
|
declarations of a `<script type="module">` body are hoisted to module level in
|
|
108
133
|
front of the block, where TypeScript resolves them like any other import.
|
|
109
|
-
- TSRX compile
|
|
110
|
-
`error
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
134
|
+
- Every TSRX compile error as a mapper diagnostic in the original file, printed as
|
|
135
|
+
`error TSRX<number>`, in capitals like TypeScript's `TS2322`, so a TSRX error
|
|
136
|
+
shows with its own code (`TSRX2002`). An error with a TypeScript code is `11`
|
|
137
|
+
and its number (`TS1005` → `TSRX111005`), with the code in brackets in the
|
|
138
|
+
message. The mapper's own errors are `771000` (a file that doesn't compile, with
|
|
139
|
+
an error without a code), `771001` (an error without a TSRX or TypeScript code),
|
|
140
|
+
`771002` (no compiler found) and `771003` (invalid configuration). The source,
|
|
141
|
+
codes and prefixes are in `@tsrx/core/diagnostics`.
|
|
142
|
+
- For each of those errors, an `ignore` diagnostic directive over the generated
|
|
143
|
+
code of the statement, class or type member, or element that holds it, so a
|
|
144
|
+
mistake shows once: TypeScript's own report of it there (TS1186 for a rest
|
|
145
|
+
element's default, a follow-on error of a TSRX mistake) is hidden until the
|
|
146
|
+
mapper's error is fixed. TypeScript's other errors in that code come back then
|
|
147
|
+
too. `tests/error-examples.js` has an example of every error in
|
|
148
|
+
`@tsrx/core/diagnostics`, and `tests/error-examples.test.js` checks that each
|
|
149
|
+
shows exactly once on TypeScript 7, or that its known problem (with the issue
|
|
150
|
+
that tracks it) still stands.
|
|
115
151
|
- While a file cannot be compiled, a stub that re-declares its exports as `any`
|
|
116
152
|
(values and types), so importers keep resolving and the author sees exactly one
|
|
117
153
|
error at the failing construct. `<style>` bodies are never TypeScript's concern
|
|
@@ -167,23 +203,20 @@ what editors get:
|
|
|
167
203
|
- Without `runExternalCode` the mapper process is never spawned, `.tsrx` is never
|
|
168
204
|
registered and `.ts` importers report TS2307 for `.tsrx` modules: that is the
|
|
169
205
|
untrusted-workspace behaviour.
|
|
170
|
-
- `custom/setContentMapperContributions
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
206
|
+
- `custom/setContentMapperContributions`, exposed by the TypeScript 7 VS Code
|
|
207
|
+
extension as `registerContentMappers`, discovers configured projects for
|
|
208
|
+
contributed file extensions. TSRX registers only `.tsrx`, so opening a `.tsrx`
|
|
209
|
+
file starts TypeScript features without opening a `.ts` or `.js` file. The
|
|
210
|
+
mapper comes from the project's `contentMappers` entry. The API also supports an
|
|
211
|
+
optional inferred-project mapper for files outside configured projects; the TSRX
|
|
212
|
+
extension does not provide one.
|
|
176
213
|
|
|
177
214
|
The TSRX language server runs beside it with `--typescript-backend=native`; see
|
|
178
|
-
[`@tsrx/language-server`](
|
|
179
|
-
extension's README for the per-editor setup.
|
|
215
|
+
[`@tsrx/language-server`](https://github.com/tsrx-org/tsrx/tree/main/packages/language-server)
|
|
216
|
+
and the VS Code extension's README for the per-editor setup.
|
|
180
217
|
|
|
181
|
-
### Known limitations (TypeScript 7.1.0-dev.
|
|
218
|
+
### Known limitations (TypeScript 7.1.0-dev.20260930.4)
|
|
182
219
|
|
|
183
|
-
- `--watch` compiles once and never recompiles after an edit on macOS
|
|
184
|
-
(microsoft/TypeScript#64351, a nightly regression since `7.1.0-dev.20260811.1`
|
|
185
|
-
that reproduces without a content mapper and with every `--watchFile` strategy).
|
|
186
|
-
The watch test only asserts the initial pass.
|
|
187
220
|
- `--runExternalCode` is required and is never enabled by the mapper; `tsrx-tsc`
|
|
188
221
|
passes it.
|
|
189
222
|
- Editors: no auto-import when a new import statement is needed
|
|
@@ -232,34 +265,33 @@ and installs next to the package.
|
|
|
232
265
|
|
|
233
266
|
### Native TypeScript binary
|
|
234
267
|
|
|
235
|
-
The
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
`lib/
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
-
|
|
247
|
-
`typescript`
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
Tests locate the binary
|
|
256
|
-
`
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
(`minimumReleaseAgeExclude`), then run `pnpm install`.
|
|
268
|
+
The repository's root `typescript` is the exact TypeScript 7.1 nightly the mapper
|
|
269
|
+
is tested against: the `native` catalog in `pnpm-workspace.yaml` (`ts_native`).
|
|
270
|
+
The `typescript@7.x` package is only a thin launcher: its `bin/tsc` calls
|
|
271
|
+
`lib/getExePath.js`, which resolves the platform package
|
|
272
|
+
`@typescript/typescript-<os>-<arch>` (an optional dependency of the launcher, so
|
|
273
|
+
pnpm installs only the one matching the current platform) and runs `lib/tsc` (or
|
|
274
|
+
`lib/tsc.exe`) from it.
|
|
275
|
+
|
|
276
|
+
- `pnpm typecheck` runs that `tsc` directly, with `--runExternalCode` for the
|
|
277
|
+
projects that declare the mapper in `contentMappers`.
|
|
278
|
+
- The packages whose code or tests use TypeScript's JavaScript API
|
|
279
|
+
(`@tsrx/typescript-plugin`, `@tsrx/language-server`, `@tsrx/core`'s tests and
|
|
280
|
+
the others that list it) keep `typescript` from the `default` catalog, the 5.x
|
|
281
|
+
line.
|
|
282
|
+
- Packages whose declaration files import `typescript` without declaring it get it
|
|
283
|
+
as an optional peer through `packageExtensions`, so they take the `typescript`
|
|
284
|
+
of the package that uses them rather than the root launcher.
|
|
285
|
+
- The nightly is also listed under `minimumReleaseAgeExclude` because it is newer
|
|
286
|
+
than the workspace's release-age policy.
|
|
287
|
+
|
|
288
|
+
Tests locate the binary the way the launcher does, through the platform package of
|
|
289
|
+
the `typescript` resolved from the repository root. A missing binary fails the
|
|
290
|
+
test instead of skipping it.
|
|
291
|
+
|
|
292
|
+
To move to a newer nightly, update `ts_native` and the matching
|
|
293
|
+
`minimumReleaseAgeExclude` entries in `pnpm-workspace.yaml`, then run
|
|
294
|
+
`pnpm install`.
|
|
263
295
|
|
|
264
296
|
### Tests
|
|
265
297
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DiagnosticDirectivePolicy, SpanMapFeature, SpanMapKind } from "./protocol.js";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
import fs from "node:fs";
|
|
@@ -2338,6 +2338,198 @@ function invalidateCompilerResolutionCaches() {
|
|
|
2338
2338
|
loggedCompilationFailures.clear();
|
|
2339
2339
|
}
|
|
2340
2340
|
|
|
2341
|
+
//#endregion
|
|
2342
|
+
//#region ../tsrx/src/diagnostics.js
|
|
2343
|
+
/**
|
|
2344
|
+
* The source TSRX's errors are reported under, in capitals like TypeScript's
|
|
2345
|
+
* own `TS2322`: `@tsrx/content-mapper` declares it to TypeScript 7, which shows
|
|
2346
|
+
* its errors as `TSRX2002`, and the TSRX language server reports its compile
|
|
2347
|
+
* errors with it.
|
|
2348
|
+
*/
|
|
2349
|
+
const DIAGNOSTIC_SOURCE = "TSRX";
|
|
2350
|
+
/** See {@link TYPESCRIPT_CODE_PREFIX}. */
|
|
2351
|
+
const MAPPER_CODE_PREFIX = "77";
|
|
2352
|
+
/** @param {number} number */
|
|
2353
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
2354
|
+
function mapper_code(number) {
|
|
2355
|
+
return Number(`${"77"}${number}`);
|
|
2356
|
+
}
|
|
2357
|
+
/** The errors `@tsrx/content-mapper` reports itself. */
|
|
2358
|
+
const MAPPER_CODES = {
|
|
2359
|
+
/** A file that doesn't compile, with an error that has no code. */
|
|
2360
|
+
COMPILE_ERROR: /* @__PURE__ */ mapper_code(1e3),
|
|
2361
|
+
/** An error without a TSRX or TypeScript code. */
|
|
2362
|
+
USAGE_ERROR: /* @__PURE__ */ mapper_code(1001),
|
|
2363
|
+
/** No TSRX compiler found for a file. */
|
|
2364
|
+
NO_COMPILER: /* @__PURE__ */ mapper_code(1002),
|
|
2365
|
+
/** The project's TSRX configuration is invalid. */
|
|
2366
|
+
INVALID_CONFIG: /* @__PURE__ */ mapper_code(1003)
|
|
2367
|
+
};
|
|
2368
|
+
|
|
2369
|
+
//#endregion
|
|
2370
|
+
//#region src/diagnostic-directives.js
|
|
2371
|
+
/** @import * as AST from 'estree' */
|
|
2372
|
+
/** @import {CodeMapping, CompileError} from '@tsrx/core/types' */
|
|
2373
|
+
/** @import {DiagnosticDirectives} from './protocol.js' */
|
|
2374
|
+
/**
|
|
2375
|
+
* The nodes whose generated code an error's directive covers: the innermost
|
|
2376
|
+
* statement, declaration, class or type member, or element that holds the
|
|
2377
|
+
* error.
|
|
2378
|
+
*/
|
|
2379
|
+
const HOLDER_TYPE = /(?:Statement|Declaration|Definition|Signature)$|^(?:StaticBlock|AccessorProperty|JSXElement|JSXFragment|JSXStyleElement)$/;
|
|
2380
|
+
/** Keys of a tree node that hold no child nodes, or nodes that aren't its children. */
|
|
2381
|
+
const NOT_CHILDREN = /* @__PURE__ */ new Set([
|
|
2382
|
+
"metadata",
|
|
2383
|
+
"loc",
|
|
2384
|
+
"range",
|
|
2385
|
+
"parent",
|
|
2386
|
+
"comments",
|
|
2387
|
+
"leadingComments",
|
|
2388
|
+
"trailingComments"
|
|
2389
|
+
]);
|
|
2390
|
+
/**
|
|
2391
|
+
* An `ignore` directive for each error the mapper reports, so that a mistake
|
|
2392
|
+
* shows once. TypeScript reports some mistakes itself from the generated code,
|
|
2393
|
+
* with a code of its own, and would show them next to the mapper's error. The
|
|
2394
|
+
* directive hides TypeScript's errors in the generated code of the statement,
|
|
2395
|
+
* class or type member, or element that holds the error, until the error is
|
|
2396
|
+
* fixed and the mapper stops sending it. The mapper's own errors are not
|
|
2397
|
+
* TypeScript's, so they stay.
|
|
2398
|
+
*
|
|
2399
|
+
* The ranges come from what every TSRX compiler returns, whatever its target:
|
|
2400
|
+
* the source tree (`sourceAst`) finds the node that holds an error, and the
|
|
2401
|
+
* mappings give that node's generated code. Without a source tree the error's
|
|
2402
|
+
* own span stands in. A node without mappings gets no directive.
|
|
2403
|
+
* @param {readonly CompileError[]} errors
|
|
2404
|
+
* @param {AST.Program | null} source_ast
|
|
2405
|
+
* @param {readonly CodeMapping[]} mappings
|
|
2406
|
+
* @param {number} original_length
|
|
2407
|
+
* @param {number} generated_length
|
|
2408
|
+
* @returns {DiagnosticDirectives | null}
|
|
2409
|
+
*/
|
|
2410
|
+
function ignore_directives(errors, source_ast, mappings, original_length, generated_length) {
|
|
2411
|
+
/** @type {Array<[original_start: number, original_end: number, generated_start: number, generated_end: number]>} */
|
|
2412
|
+
const ranges = [];
|
|
2413
|
+
for (const error of errors) {
|
|
2414
|
+
if (typeof error.pos !== "number") continue;
|
|
2415
|
+
const holder = source_ast ? holder_at(source_ast, error.pos) : null;
|
|
2416
|
+
const start = holder ? holder.start : error.pos;
|
|
2417
|
+
const end = holder ? holder.end : typeof error.end === "number" && error.end > error.pos ? error.end : error.pos + 1;
|
|
2418
|
+
for (const generated of generated_ranges(mappings, start, end)) ranges.push([
|
|
2419
|
+
Math.min(start, original_length),
|
|
2420
|
+
Math.min(end, original_length),
|
|
2421
|
+
Math.min(generated[0], generated_length),
|
|
2422
|
+
Math.min(generated[1], generated_length)
|
|
2423
|
+
]);
|
|
2424
|
+
}
|
|
2425
|
+
if (ranges.length === 0) return null;
|
|
2426
|
+
ranges.sort((a, b) => a[2] - b[2]);
|
|
2427
|
+
/** @type {typeof ranges} */
|
|
2428
|
+
const merged = [];
|
|
2429
|
+
for (const range of ranges) {
|
|
2430
|
+
const last = merged.at(-1);
|
|
2431
|
+
if (last && range[2] < last[3]) {
|
|
2432
|
+
last[0] = Math.min(last[0], range[0]);
|
|
2433
|
+
last[1] = Math.max(last[1], range[1]);
|
|
2434
|
+
last[3] = Math.max(last[3], range[3]);
|
|
2435
|
+
} else merged.push([...range]);
|
|
2436
|
+
}
|
|
2437
|
+
return {
|
|
2438
|
+
unusedExpectDirectiveDiagnostics: [],
|
|
2439
|
+
directives: merged.map(([original_start, original_end, generated_start, generated_end]) => [
|
|
2440
|
+
original_start,
|
|
2441
|
+
original_end - original_start,
|
|
2442
|
+
generated_start,
|
|
2443
|
+
generated_end,
|
|
2444
|
+
DiagnosticDirectivePolicy.Ignore
|
|
2445
|
+
])
|
|
2446
|
+
};
|
|
2447
|
+
}
|
|
2448
|
+
/**
|
|
2449
|
+
* The innermost holder node (see {@link HOLDER_TYPE}) that contains `position`,
|
|
2450
|
+
* found by descending from the root through the child that contains it.
|
|
2451
|
+
* @param {AST.Node} root
|
|
2452
|
+
* @param {number} position
|
|
2453
|
+
* @returns {AST.Node | null}
|
|
2454
|
+
*/
|
|
2455
|
+
function holder_at(root, position) {
|
|
2456
|
+
/** @type {AST.Node | null} */
|
|
2457
|
+
let holder = null;
|
|
2458
|
+
/** @type {AST.Node | null} */
|
|
2459
|
+
let node = root;
|
|
2460
|
+
while (node) {
|
|
2461
|
+
if (HOLDER_TYPE.test(node.type)) holder = node;
|
|
2462
|
+
node = child_at(node, position);
|
|
2463
|
+
}
|
|
2464
|
+
return holder;
|
|
2465
|
+
}
|
|
2466
|
+
/**
|
|
2467
|
+
* @param {AST.Node} node
|
|
2468
|
+
* @param {number} position
|
|
2469
|
+
* @returns {AST.Node | null}
|
|
2470
|
+
*/
|
|
2471
|
+
function child_at(node, position) {
|
|
2472
|
+
for (const [key, value] of Object.entries(node)) {
|
|
2473
|
+
if (NOT_CHILDREN.has(key) || !value || typeof value !== "object") continue;
|
|
2474
|
+
for (const child of Array.isArray(value) ? value : [value]) if (child && typeof child.type === "string" && typeof child.start === "number" && typeof child.end === "number" && child.start <= position && position < child.end) return child;
|
|
2475
|
+
}
|
|
2476
|
+
return null;
|
|
2477
|
+
}
|
|
2478
|
+
/**
|
|
2479
|
+
* The generated ranges of the mapped code between `start` and `end` in the
|
|
2480
|
+
* original. A node's generated code need not be in one piece: a compiler can
|
|
2481
|
+
* hoist part of it (a static element above the component), and a `<script>`
|
|
2482
|
+
* body is checked as a block at the end of the file. So each range runs from
|
|
2483
|
+
* the first to the last generated offset of the node's segments until code
|
|
2484
|
+
* mapped from outside the node follows, and the next of its segments starts a
|
|
2485
|
+
* new range; a range never covers another component's code. A segment of the
|
|
2486
|
+
* same length on both sides that encloses the original range, as a verbatim
|
|
2487
|
+
* `<script>` body does, gives the matching part of its generated text.
|
|
2488
|
+
* @param {readonly CodeMapping[]} mappings
|
|
2489
|
+
* @param {number} start
|
|
2490
|
+
* @param {number} end
|
|
2491
|
+
* @returns {Array<[number, number]>}
|
|
2492
|
+
*/
|
|
2493
|
+
function generated_ranges(mappings, start, end) {
|
|
2494
|
+
/** @type {Array<[generated_start: number, generated_end: number, inside: boolean]>} */
|
|
2495
|
+
const segments = [];
|
|
2496
|
+
for (const mapping of mappings) for (let i = 0; i < mapping.sourceOffsets.length; i++) {
|
|
2497
|
+
const source = mapping.sourceOffsets[i];
|
|
2498
|
+
const length = mapping.lengths[i];
|
|
2499
|
+
const generated = mapping.generatedOffsets[i];
|
|
2500
|
+
const generated_length = mapping.generatedLengths?.[i] ?? length;
|
|
2501
|
+
if (source >= start && source + length <= end) segments.push([
|
|
2502
|
+
generated,
|
|
2503
|
+
generated + generated_length,
|
|
2504
|
+
true
|
|
2505
|
+
]);
|
|
2506
|
+
else if (source <= start && end <= source + length && generated_length === length) segments.push([
|
|
2507
|
+
generated + start - source,
|
|
2508
|
+
generated + end - source,
|
|
2509
|
+
true
|
|
2510
|
+
]);
|
|
2511
|
+
else segments.push([
|
|
2512
|
+
generated,
|
|
2513
|
+
generated + generated_length,
|
|
2514
|
+
false
|
|
2515
|
+
]);
|
|
2516
|
+
}
|
|
2517
|
+
segments.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
2518
|
+
/** @type {Array<[number, number]>} */
|
|
2519
|
+
const ranges = [];
|
|
2520
|
+
/** @type {[number, number] | null} */
|
|
2521
|
+
let range = null;
|
|
2522
|
+
for (const [segment_start, segment_end, inside] of segments) if (inside) {
|
|
2523
|
+
if (range) range[1] = Math.max(range[1], segment_end);
|
|
2524
|
+
else range = [segment_start, segment_end];
|
|
2525
|
+
} else if (range && segment_start >= range[1]) {
|
|
2526
|
+
if (range[1] > range[0]) ranges.push(range);
|
|
2527
|
+
range = null;
|
|
2528
|
+
}
|
|
2529
|
+
if (range && range[1] > range[0]) ranges.push(range);
|
|
2530
|
+
return ranges;
|
|
2531
|
+
}
|
|
2532
|
+
|
|
2341
2533
|
//#endregion
|
|
2342
2534
|
//#region src/export-stub.js
|
|
2343
2535
|
/** @import * as AST from 'estree' */
|
|
@@ -2345,9 +2537,8 @@ function invalidateCompilerResolutionCaches() {
|
|
|
2345
2537
|
* Build a TypeScript stub that preserves a module's export surface while the
|
|
2346
2538
|
* file cannot be compiled.
|
|
2347
2539
|
*
|
|
2348
|
-
*
|
|
2349
|
-
*
|
|
2350
|
-
* type errors of its own) to the checker would produce noise. Instead every
|
|
2540
|
+
* Raw TSRX, or the last good TSX (which may carry type errors of its own),
|
|
2541
|
+
* would give the checker noise to report. Instead every
|
|
2351
2542
|
* export from the last successful transform's source AST is re-declared as
|
|
2352
2543
|
* `any` (both as a value and as a type, so `new X()`, `X.y` and `let v: X` all
|
|
2353
2544
|
* keep resolving), re-exports from other modules are kept as written (including
|
|
@@ -2391,10 +2582,18 @@ function build_export_stub(program) {
|
|
|
2391
2582
|
}
|
|
2392
2583
|
if (statement.declaration) for (const name of declaration_names(statement.declaration)) names.add(name);
|
|
2393
2584
|
}
|
|
2585
|
+
let next_local = 0;
|
|
2394
2586
|
for (const name of names) {
|
|
2395
|
-
if (
|
|
2396
|
-
|
|
2397
|
-
|
|
2587
|
+
if (identifier_pattern$1.test(name) && !UNDECLARABLE_NAMES.has(name)) {
|
|
2588
|
+
lines.push(`export declare const ${name}: any;`);
|
|
2589
|
+
lines.push(`export type ${name} = any;`);
|
|
2590
|
+
continue;
|
|
2591
|
+
}
|
|
2592
|
+
let local = `_$_export_${next_local++}`;
|
|
2593
|
+
while (names.has(local)) local = `_$_export_${next_local++}`;
|
|
2594
|
+
lines.push(`declare const ${local}: any;`);
|
|
2595
|
+
lines.push(`type ${local} = any;`);
|
|
2596
|
+
lines.push(`export { ${local} as ${module_export_name(name)} };`);
|
|
2398
2597
|
}
|
|
2399
2598
|
if (has_default) {
|
|
2400
2599
|
lines.push("declare const _default: any;");
|
|
@@ -2414,6 +2613,73 @@ function type_modifier(node) {
|
|
|
2414
2613
|
}
|
|
2415
2614
|
const identifier_pattern$1 = /^[\p{ID_Start}_$][\p{ID_Continue}$]*$/u;
|
|
2416
2615
|
/**
|
|
2616
|
+
* Identifier names that `export declare const x: any; export type x = any;`
|
|
2617
|
+
* can't declare in a module: reserved words (TS1389), words reserved in strict
|
|
2618
|
+
* mode (TS1214), `await`, `let`, `arguments` and `eval`, the names of
|
|
2619
|
+
* TypeScript's own types (TS2457), and `as`, which `export type as` misreads.
|
|
2620
|
+
*/
|
|
2621
|
+
const UNDECLARABLE_NAMES = /* @__PURE__ */ new Set([
|
|
2622
|
+
"break",
|
|
2623
|
+
"case",
|
|
2624
|
+
"catch",
|
|
2625
|
+
"class",
|
|
2626
|
+
"const",
|
|
2627
|
+
"continue",
|
|
2628
|
+
"debugger",
|
|
2629
|
+
"default",
|
|
2630
|
+
"delete",
|
|
2631
|
+
"do",
|
|
2632
|
+
"else",
|
|
2633
|
+
"enum",
|
|
2634
|
+
"export",
|
|
2635
|
+
"extends",
|
|
2636
|
+
"false",
|
|
2637
|
+
"finally",
|
|
2638
|
+
"for",
|
|
2639
|
+
"function",
|
|
2640
|
+
"if",
|
|
2641
|
+
"import",
|
|
2642
|
+
"in",
|
|
2643
|
+
"instanceof",
|
|
2644
|
+
"new",
|
|
2645
|
+
"null",
|
|
2646
|
+
"return",
|
|
2647
|
+
"super",
|
|
2648
|
+
"switch",
|
|
2649
|
+
"this",
|
|
2650
|
+
"throw",
|
|
2651
|
+
"true",
|
|
2652
|
+
"try",
|
|
2653
|
+
"typeof",
|
|
2654
|
+
"var",
|
|
2655
|
+
"void",
|
|
2656
|
+
"while",
|
|
2657
|
+
"with",
|
|
2658
|
+
"implements",
|
|
2659
|
+
"interface",
|
|
2660
|
+
"package",
|
|
2661
|
+
"private",
|
|
2662
|
+
"protected",
|
|
2663
|
+
"public",
|
|
2664
|
+
"static",
|
|
2665
|
+
"yield",
|
|
2666
|
+
"await",
|
|
2667
|
+
"let",
|
|
2668
|
+
"arguments",
|
|
2669
|
+
"eval",
|
|
2670
|
+
"any",
|
|
2671
|
+
"bigint",
|
|
2672
|
+
"boolean",
|
|
2673
|
+
"never",
|
|
2674
|
+
"number",
|
|
2675
|
+
"object",
|
|
2676
|
+
"string",
|
|
2677
|
+
"symbol",
|
|
2678
|
+
"undefined",
|
|
2679
|
+
"unknown",
|
|
2680
|
+
"as"
|
|
2681
|
+
]);
|
|
2682
|
+
/**
|
|
2417
2683
|
* The name an export binds, as a plain string: `a` for `export { a }` and
|
|
2418
2684
|
* `foo-bar` for `export { "foo-bar" as a } from`.
|
|
2419
2685
|
* @param {AST.Identifier | AST.Literal | AST.Expression} node
|
|
@@ -2432,7 +2698,15 @@ function export_name(node) {
|
|
|
2432
2698
|
* @returns {string}
|
|
2433
2699
|
*/
|
|
2434
2700
|
function print_name(node) {
|
|
2435
|
-
|
|
2701
|
+
return module_export_name(export_name(node));
|
|
2702
|
+
}
|
|
2703
|
+
/**
|
|
2704
|
+
* A name as an export specifier takes it: any identifier name, reserved words
|
|
2705
|
+
* too, as is, and anything else quoted.
|
|
2706
|
+
* @param {string} name
|
|
2707
|
+
* @returns {string}
|
|
2708
|
+
*/
|
|
2709
|
+
function module_export_name(name) {
|
|
2436
2710
|
return identifier_pattern$1.test(name) ? name : JSON.stringify(name);
|
|
2437
2711
|
}
|
|
2438
2712
|
/**
|
|
@@ -2813,14 +3087,14 @@ function create_tsrx_content_mapper(context = {}) {
|
|
|
2813
3087
|
return failure(state, file_name, content, {
|
|
2814
3088
|
start: 0,
|
|
2815
3089
|
length: 0,
|
|
2816
|
-
code:
|
|
3090
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
2817
3091
|
messageText: error_message(error)
|
|
2818
3092
|
});
|
|
2819
3093
|
}
|
|
2820
3094
|
if (!compiler) return failure(state, file_name, content, {
|
|
2821
3095
|
start: 0,
|
|
2822
3096
|
length: 0,
|
|
2823
|
-
code:
|
|
3097
|
+
code: MAPPER_CODES.NO_COMPILER,
|
|
2824
3098
|
messageText: `No TSRX compiler found for ${file_name}. Install a target compiler (for example @tsrx/react) next to the project, or select one with "tsrx": { "compiler": "<package>" } in tsconfig.json or "options": { "compiler": "<package>" } on the content mapper entry.`
|
|
2825
3099
|
});
|
|
2826
3100
|
/** @type {'web' | 'ios' | 'android' | undefined} */
|
|
@@ -2836,21 +3110,23 @@ function create_tsrx_content_mapper(context = {}) {
|
|
|
2836
3110
|
return failure(state, file_name, content, {
|
|
2837
3111
|
start: 0,
|
|
2838
3112
|
length: 0,
|
|
2839
|
-
code:
|
|
3113
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
2840
3114
|
messageText: error_message(error)
|
|
2841
3115
|
});
|
|
2842
3116
|
}
|
|
2843
3117
|
const result = transform_tsrx(compiler, file_name, content, { platform });
|
|
2844
|
-
if (result.fatalError) return failure(state, file_name, content, to_diagnostic(result.fatalError, content.length,
|
|
3118
|
+
if (result.fatalError) return failure(state, file_name, content, to_diagnostic(result.fatalError, content.length, MAPPER_CODES.COMPILE_ERROR));
|
|
2845
3119
|
if (result.sourceAst) state.lastGood.set(file_name, result.sourceAst);
|
|
2846
3120
|
const language_features = state.options.languageFeatures !== false;
|
|
2847
3121
|
const mappings = to_span_mappings(result.mappings, result.text, content, { languageFeatures: language_features });
|
|
2848
|
-
const diagnostics = result.errors.map((error) => to_diagnostic(error, content.length,
|
|
3122
|
+
const diagnostics = result.errors.map((error) => to_diagnostic(error, content.length, MAPPER_CODES.USAGE_ERROR));
|
|
3123
|
+
const diagnosticDirectives = ignore_directives(result.errors, result.sourceAst, result.mappings, content.length, result.text.length);
|
|
2849
3124
|
return {
|
|
2850
3125
|
text: result.text,
|
|
2851
3126
|
extension: ".tsx",
|
|
2852
3127
|
mappings,
|
|
2853
|
-
...diagnostics.length > 0 ? { diagnostics } : null
|
|
3128
|
+
...diagnostics.length > 0 ? { diagnostics } : null,
|
|
3129
|
+
...diagnosticDirectives ? { diagnosticDirectives } : null
|
|
2854
3130
|
};
|
|
2855
3131
|
}
|
|
2856
3132
|
/**
|
|
@@ -2971,7 +3247,7 @@ function validate_options(raw) {
|
|
|
2971
3247
|
optionDiagnostics.push({
|
|
2972
3248
|
path: [],
|
|
2973
3249
|
messageText: "Content mapper options must be an object.",
|
|
2974
|
-
code:
|
|
3250
|
+
code: MAPPER_CODES.INVALID_CONFIG
|
|
2975
3251
|
});
|
|
2976
3252
|
return {
|
|
2977
3253
|
options,
|
|
@@ -2987,7 +3263,7 @@ function validate_options(raw) {
|
|
|
2987
3263
|
else optionDiagnostics.push({
|
|
2988
3264
|
path: [key],
|
|
2989
3265
|
messageText: "\"compiler\" must be a bare package specifier such as \"@tsrx/react\".",
|
|
2990
|
-
code:
|
|
3266
|
+
code: MAPPER_CODES.INVALID_CONFIG
|
|
2991
3267
|
});
|
|
2992
3268
|
break;
|
|
2993
3269
|
case "platform":
|
|
@@ -2995,7 +3271,7 @@ function validate_options(raw) {
|
|
|
2995
3271
|
else optionDiagnostics.push({
|
|
2996
3272
|
path: [key],
|
|
2997
3273
|
messageText: "\"platform\" must be \"web\", \"ios\", or \"android\".",
|
|
2998
|
-
code:
|
|
3274
|
+
code: MAPPER_CODES.INVALID_CONFIG
|
|
2999
3275
|
});
|
|
3000
3276
|
break;
|
|
3001
3277
|
case "languageFeatures":
|
|
@@ -3003,13 +3279,13 @@ function validate_options(raw) {
|
|
|
3003
3279
|
else optionDiagnostics.push({
|
|
3004
3280
|
path: [key],
|
|
3005
3281
|
messageText: "\"languageFeatures\" must be a boolean.",
|
|
3006
|
-
code:
|
|
3282
|
+
code: MAPPER_CODES.INVALID_CONFIG
|
|
3007
3283
|
});
|
|
3008
3284
|
break;
|
|
3009
3285
|
default: optionDiagnostics.push({
|
|
3010
3286
|
path: [key],
|
|
3011
3287
|
messageText: `Unknown content mapper option "${key}".`,
|
|
3012
|
-
code:
|
|
3288
|
+
code: MAPPER_CODES.INVALID_CONFIG
|
|
3013
3289
|
});
|
|
3014
3290
|
}
|
|
3015
3291
|
}
|
|
@@ -3018,10 +3294,15 @@ function validate_options(raw) {
|
|
|
3018
3294
|
optionDiagnostics
|
|
3019
3295
|
};
|
|
3020
3296
|
}
|
|
3297
|
+
const TSRX_CODE = /^TSRX(\d+)$/;
|
|
3298
|
+
const TYPESCRIPT_CODE = /^TS(\d+)$/;
|
|
3021
3299
|
/**
|
|
3022
3300
|
* Translate a compiler `CompileError` into a mapper diagnostic in original
|
|
3023
|
-
* offsets.
|
|
3024
|
-
*
|
|
3301
|
+
* offsets. TypeScript shows the mapper's diagnostics as `TSRX<number>`, so a
|
|
3302
|
+
* TSRX code goes as its number (`TSRX2002` → `2002`) and a TypeScript code as
|
|
3303
|
+
* `TYPESCRIPT_CODE_PREFIX` then its number (`TS1005` → `111005`), which keeps
|
|
3304
|
+
* the code in the message too. An error without either uses `fallback_code`
|
|
3305
|
+
* and keeps any other code in the message.
|
|
3025
3306
|
* @param {CompileError} error
|
|
3026
3307
|
* @param {number} content_length
|
|
3027
3308
|
* @param {number} fallback_code
|
|
@@ -3030,31 +3311,18 @@ function validate_options(raw) {
|
|
|
3030
3311
|
function to_diagnostic(error, content_length, fallback_code) {
|
|
3031
3312
|
const start = typeof error.pos === "number" ? error.pos : 0;
|
|
3032
3313
|
const end = typeof error.end === "number" && error.end > start ? error.end : start + 1;
|
|
3033
|
-
const code = typeof error.code === "string"
|
|
3314
|
+
const code = typeof error.code === "string" ? error.code : "";
|
|
3315
|
+
const tsrx = TSRX_CODE.exec(code);
|
|
3316
|
+
const typescript = TYPESCRIPT_CODE.exec(code);
|
|
3034
3317
|
const message = String(error.message ?? error);
|
|
3035
3318
|
return clamp_diagnostic({
|
|
3036
3319
|
start,
|
|
3037
3320
|
length: end - start,
|
|
3038
|
-
code,
|
|
3039
|
-
messageText:
|
|
3321
|
+
code: tsrx ? Number(tsrx[1]) : typescript ? Number(`${"11"}${typescript[1]}`) : fallback_code,
|
|
3322
|
+
messageText: code && !tsrx ? `${message} [${code}]` : message
|
|
3040
3323
|
}, content_length);
|
|
3041
3324
|
}
|
|
3042
3325
|
/**
|
|
3043
|
-
* Stable numeric code for a TSRX string diagnostic code (FNV-1a 32-bit,
|
|
3044
|
-
* folded into the 10000..99999 range so it never collides with the fixed
|
|
3045
|
-
* codes below 10000).
|
|
3046
|
-
* @param {string} code
|
|
3047
|
-
* @returns {number}
|
|
3048
|
-
*/
|
|
3049
|
-
function numeric_code(code) {
|
|
3050
|
-
let hash = 2166136261;
|
|
3051
|
-
for (let index = 0; index < code.length; index++) {
|
|
3052
|
-
hash ^= code.charCodeAt(index);
|
|
3053
|
-
hash = Math.imul(hash, 16777619);
|
|
3054
|
-
}
|
|
3055
|
-
return 1e4 + (hash >>> 0) % 9e4;
|
|
3056
|
-
}
|
|
3057
|
-
/**
|
|
3058
3326
|
* @param {MapperDiagnostic} diagnostic
|
|
3059
3327
|
* @param {number} content_length
|
|
3060
3328
|
* @returns {MapperDiagnostic}
|
|
@@ -3094,5 +3362,5 @@ function nearest_package_manifest(entry) {
|
|
|
3094
3362
|
}
|
|
3095
3363
|
|
|
3096
3364
|
//#endregion
|
|
3097
|
-
export { validate_options as
|
|
3098
|
-
//# sourceMappingURL=mapper-
|
|
3365
|
+
export { validate_options as i, require$1 as n, to_diagnostic as r, create_tsrx_content_mapper as t };
|
|
3366
|
+
//# sourceMappingURL=mapper-B4f--1ac.js.map
|