@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/src/mapper.js
CHANGED
|
@@ -16,14 +16,9 @@ import {
|
|
|
16
16
|
source_uses_platform_flag,
|
|
17
17
|
} from '@tsrx/typescript-plugin/src/language.js';
|
|
18
18
|
import { transform_tsrx } from '@tsrx/typescript-plugin/src/transform.js';
|
|
19
|
+
import { DIAGNOSTIC_SOURCE, MAPPER_CODES, TYPESCRIPT_CODE_PREFIX } from '@tsrx/core/diagnostics';
|
|
20
|
+
import { ignore_directives } from './diagnostic-directives.js';
|
|
19
21
|
import { build_export_stub } from './export-stub.js';
|
|
20
|
-
import {
|
|
21
|
-
DIAGNOSTIC_CODE_COMPILE_ERROR,
|
|
22
|
-
DIAGNOSTIC_CODE_INVALID_CONFIG,
|
|
23
|
-
DIAGNOSTIC_CODE_NO_COMPILER,
|
|
24
|
-
DIAGNOSTIC_CODE_USAGE_ERROR,
|
|
25
|
-
DIAGNOSTIC_SOURCE,
|
|
26
|
-
} from './protocol.js';
|
|
27
22
|
import { to_span_mappings } from './span-mappings.js';
|
|
28
23
|
|
|
29
24
|
const require = createRequire(import.meta.url);
|
|
@@ -136,7 +131,7 @@ export function create_tsrx_content_mapper(context = {}) {
|
|
|
136
131
|
return failure(state, file_name, content, {
|
|
137
132
|
start: 0,
|
|
138
133
|
length: 0,
|
|
139
|
-
code:
|
|
134
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
140
135
|
messageText: error_message(error),
|
|
141
136
|
});
|
|
142
137
|
}
|
|
@@ -144,7 +139,7 @@ export function create_tsrx_content_mapper(context = {}) {
|
|
|
144
139
|
return failure(state, file_name, content, {
|
|
145
140
|
start: 0,
|
|
146
141
|
length: 0,
|
|
147
|
-
code:
|
|
142
|
+
code: MAPPER_CODES.NO_COMPILER,
|
|
148
143
|
messageText:
|
|
149
144
|
`No TSRX compiler found for ${file_name}. Install a target compiler ` +
|
|
150
145
|
'(for example @tsrx/react) next to the project, or select one with ' +
|
|
@@ -168,18 +163,20 @@ export function create_tsrx_content_mapper(context = {}) {
|
|
|
168
163
|
return failure(state, file_name, content, {
|
|
169
164
|
start: 0,
|
|
170
165
|
length: 0,
|
|
171
|
-
code:
|
|
166
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
172
167
|
messageText: error_message(error),
|
|
173
168
|
});
|
|
174
169
|
}
|
|
175
170
|
|
|
176
171
|
const result = transform_tsrx(compiler, file_name, content, { platform });
|
|
177
172
|
if (result.fatalError) {
|
|
173
|
+
// The file has no generated code for TypeScript to check, so the error
|
|
174
|
+
// goes to TypeScript whatever its code.
|
|
178
175
|
return failure(
|
|
179
176
|
state,
|
|
180
177
|
file_name,
|
|
181
178
|
content,
|
|
182
|
-
to_diagnostic(result.fatalError, content.length,
|
|
179
|
+
to_diagnostic(result.fatalError, content.length, MAPPER_CODES.COMPILE_ERROR),
|
|
183
180
|
);
|
|
184
181
|
}
|
|
185
182
|
if (result.sourceAst) {
|
|
@@ -190,14 +187,26 @@ export function create_tsrx_content_mapper(context = {}) {
|
|
|
190
187
|
const mappings = to_span_mappings(result.mappings, result.text, content, {
|
|
191
188
|
languageFeatures: language_features,
|
|
192
189
|
});
|
|
190
|
+
// Every error goes to TypeScript, even one with a TypeScript code: the
|
|
191
|
+
// generated code can lose the mistake (a repeated modifier, a rest
|
|
192
|
+
// parameter's `?`), and then TypeScript never reports it. Where TypeScript
|
|
193
|
+
// does, the directives hide its copy until the mapper's error is fixed.
|
|
193
194
|
const diagnostics = result.errors.map((error) =>
|
|
194
|
-
to_diagnostic(error, content.length,
|
|
195
|
+
to_diagnostic(error, content.length, MAPPER_CODES.USAGE_ERROR),
|
|
196
|
+
);
|
|
197
|
+
const diagnosticDirectives = ignore_directives(
|
|
198
|
+
result.errors,
|
|
199
|
+
result.sourceAst,
|
|
200
|
+
result.mappings,
|
|
201
|
+
content.length,
|
|
202
|
+
result.text.length,
|
|
195
203
|
);
|
|
196
204
|
return {
|
|
197
205
|
text: result.text,
|
|
198
206
|
extension: '.tsx',
|
|
199
207
|
mappings,
|
|
200
208
|
...(diagnostics.length > 0 ? { diagnostics } : null),
|
|
209
|
+
...(diagnosticDirectives ? { diagnosticDirectives } : null),
|
|
201
210
|
};
|
|
202
211
|
}
|
|
203
212
|
|
|
@@ -337,7 +346,7 @@ export function validate_options(raw) {
|
|
|
337
346
|
optionDiagnostics.push({
|
|
338
347
|
path: [],
|
|
339
348
|
messageText: 'Content mapper options must be an object.',
|
|
340
|
-
code:
|
|
349
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
341
350
|
});
|
|
342
351
|
return { options, optionDiagnostics };
|
|
343
352
|
}
|
|
@@ -352,7 +361,7 @@ export function validate_options(raw) {
|
|
|
352
361
|
optionDiagnostics.push({
|
|
353
362
|
path: [key],
|
|
354
363
|
messageText: '"compiler" must be a bare package specifier such as "@tsrx/react".',
|
|
355
|
-
code:
|
|
364
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
356
365
|
});
|
|
357
366
|
}
|
|
358
367
|
break;
|
|
@@ -363,7 +372,7 @@ export function validate_options(raw) {
|
|
|
363
372
|
optionDiagnostics.push({
|
|
364
373
|
path: [key],
|
|
365
374
|
messageText: '"platform" must be "web", "ios", or "android".',
|
|
366
|
-
code:
|
|
375
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
367
376
|
});
|
|
368
377
|
}
|
|
369
378
|
break;
|
|
@@ -374,7 +383,7 @@ export function validate_options(raw) {
|
|
|
374
383
|
optionDiagnostics.push({
|
|
375
384
|
path: [key],
|
|
376
385
|
messageText: '"languageFeatures" must be a boolean.',
|
|
377
|
-
code:
|
|
386
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
378
387
|
});
|
|
379
388
|
}
|
|
380
389
|
break;
|
|
@@ -382,17 +391,23 @@ export function validate_options(raw) {
|
|
|
382
391
|
optionDiagnostics.push({
|
|
383
392
|
path: [key],
|
|
384
393
|
messageText: `Unknown content mapper option "${key}".`,
|
|
385
|
-
code:
|
|
394
|
+
code: MAPPER_CODES.INVALID_CONFIG,
|
|
386
395
|
});
|
|
387
396
|
}
|
|
388
397
|
}
|
|
389
398
|
return { options, optionDiagnostics };
|
|
390
399
|
}
|
|
391
400
|
|
|
401
|
+
const TSRX_CODE = /^TSRX(\d+)$/;
|
|
402
|
+
const TYPESCRIPT_CODE = /^TS(\d+)$/;
|
|
403
|
+
|
|
392
404
|
/**
|
|
393
405
|
* Translate a compiler `CompileError` into a mapper diagnostic in original
|
|
394
|
-
* offsets.
|
|
395
|
-
*
|
|
406
|
+
* offsets. TypeScript shows the mapper's diagnostics as `TSRX<number>`, so a
|
|
407
|
+
* TSRX code goes as its number (`TSRX2002` → `2002`) and a TypeScript code as
|
|
408
|
+
* `TYPESCRIPT_CODE_PREFIX` then its number (`TS1005` → `111005`), which keeps
|
|
409
|
+
* the code in the message too. An error without either uses `fallback_code`
|
|
410
|
+
* and keeps any other code in the message.
|
|
396
411
|
* @param {CompileError} error
|
|
397
412
|
* @param {number} content_length
|
|
398
413
|
* @param {number} fallback_code
|
|
@@ -401,37 +416,25 @@ export function validate_options(raw) {
|
|
|
401
416
|
export function to_diagnostic(error, content_length, fallback_code) {
|
|
402
417
|
const start = typeof error.pos === 'number' ? error.pos : 0;
|
|
403
418
|
const end = typeof error.end === 'number' && error.end > start ? error.end : start + 1;
|
|
404
|
-
const code =
|
|
405
|
-
|
|
419
|
+
const code = typeof error.code === 'string' ? error.code : '';
|
|
420
|
+
const tsrx = TSRX_CODE.exec(code);
|
|
421
|
+
const typescript = TYPESCRIPT_CODE.exec(code);
|
|
406
422
|
const message = String(error.message ?? error);
|
|
407
423
|
return clamp_diagnostic(
|
|
408
424
|
{
|
|
409
425
|
start,
|
|
410
426
|
length: end - start,
|
|
411
|
-
code
|
|
412
|
-
|
|
413
|
-
|
|
427
|
+
code: tsrx
|
|
428
|
+
? Number(tsrx[1])
|
|
429
|
+
: typescript
|
|
430
|
+
? Number(`${TYPESCRIPT_CODE_PREFIX}${typescript[1]}`)
|
|
431
|
+
: fallback_code,
|
|
432
|
+
messageText: code && !tsrx ? `${message} [${code}]` : message,
|
|
414
433
|
},
|
|
415
434
|
content_length,
|
|
416
435
|
);
|
|
417
436
|
}
|
|
418
437
|
|
|
419
|
-
/**
|
|
420
|
-
* Stable numeric code for a TSRX string diagnostic code (FNV-1a 32-bit,
|
|
421
|
-
* folded into the 10000..99999 range so it never collides with the fixed
|
|
422
|
-
* codes below 10000).
|
|
423
|
-
* @param {string} code
|
|
424
|
-
* @returns {number}
|
|
425
|
-
*/
|
|
426
|
-
export function numeric_code(code) {
|
|
427
|
-
let hash = 0x811c9dc5;
|
|
428
|
-
for (let index = 0; index < code.length; index++) {
|
|
429
|
-
hash ^= code.charCodeAt(index);
|
|
430
|
-
hash = Math.imul(hash, 0x01000193);
|
|
431
|
-
}
|
|
432
|
-
return 10000 + ((hash >>> 0) % 90000);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
438
|
/**
|
|
436
439
|
* @param {MapperDiagnostic} diagnostic
|
|
437
440
|
* @param {number} content_length
|
package/src/protocol.js
CHANGED
|
@@ -39,21 +39,13 @@ export const SpanMapFeature = /** @type {const} */ ({
|
|
|
39
39
|
All: (1 << 20) - 1,
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Diagnostic-directive policies: `Ignore` hides TypeScript's errors in a
|
|
44
|
+
* generated range; `Expect` also reports a diagnostic of the mapper's when
|
|
45
|
+
* there are none there.
|
|
46
|
+
*/
|
|
43
47
|
export const DiagnosticDirectivePolicy = /** @type {const} */ ({ Ignore: 0, Expect: 1 });
|
|
44
48
|
|
|
45
|
-
/** Prefix for mapper-authored diagnostics: `error tsrx(1000): ...`. */
|
|
46
|
-
export const DIAGNOSTIC_SOURCE = 'tsrx';
|
|
47
|
-
|
|
48
|
-
/** Numeric code for a fatal compile error (the file could not be transformed). */
|
|
49
|
-
export const DIAGNOSTIC_CODE_COMPILE_ERROR = 1000;
|
|
50
|
-
/** Numeric code for a usage error that carries no string code. */
|
|
51
|
-
export const DIAGNOSTIC_CODE_USAGE_ERROR = 1001;
|
|
52
|
-
/** Numeric code when no TSRX compiler can be resolved for a file. */
|
|
53
|
-
export const DIAGNOSTIC_CODE_NO_COMPILER = 1002;
|
|
54
|
-
/** Numeric code when the project's TSRX configuration is invalid. */
|
|
55
|
-
export const DIAGNOSTIC_CODE_INVALID_CONFIG = 1003;
|
|
56
|
-
|
|
57
49
|
/**
|
|
58
50
|
* @typedef {[
|
|
59
51
|
* generatedStart: number,
|
|
@@ -80,9 +72,27 @@ export const DIAGNOSTIC_CODE_INVALID_CONFIG = 1003;
|
|
|
80
72
|
* @property {SpanMapping[]} mappings
|
|
81
73
|
*/
|
|
82
74
|
|
|
75
|
+
/**
|
|
76
|
+
* @typedef {[
|
|
77
|
+
* originalStart: number,
|
|
78
|
+
* originalLength: number,
|
|
79
|
+
* generatedStart: number,
|
|
80
|
+
* generatedEnd: number,
|
|
81
|
+
* policy: 0 | 1,
|
|
82
|
+
* unusedExpectDirectiveIndex?: number,
|
|
83
|
+
* ]} DiagnosticDirective
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @typedef {object} DiagnosticDirectives
|
|
88
|
+
* @property {Array<{ code: number, messageText: string }>} unusedExpectDirectiveDiagnostics What an `Expect` directive reports when TypeScript has no error in its range.
|
|
89
|
+
* @property {DiagnosticDirective[]} directives
|
|
90
|
+
*/
|
|
91
|
+
|
|
83
92
|
/**
|
|
84
93
|
* @typedef {MappedOutput & {
|
|
85
94
|
* diagnostics?: MapperDiagnostic[],
|
|
95
|
+
* diagnosticDirectives?: DiagnosticDirectives,
|
|
86
96
|
* supplemental?: MappedOutput[],
|
|
87
97
|
* }} TransformResult
|
|
88
98
|
*/
|
package/src/rpc.js
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* it from their own entry file.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import { Console } from 'node:console';
|
|
13
|
+
|
|
12
14
|
/**
|
|
13
15
|
* @typedef {{ jsonrpc: '2.0', id?: number | string | null, method: string, params?: unknown }} RequestMessage
|
|
14
16
|
*/
|
|
@@ -119,8 +121,15 @@ export function run_mapper_server(mapper, streams = {}) {
|
|
|
119
121
|
|
|
120
122
|
/**
|
|
121
123
|
* Route every console method to stderr so stray logging (from a target
|
|
122
|
-
* compiler, for example) can never corrupt the protocol stream on stdout
|
|
124
|
+
* compiler, for example) can never corrupt the protocol stream on stdout: not
|
|
125
|
+
* only `log` but `dir`, `table`, `trace`, `group` and the rest write there, so
|
|
126
|
+
* each takes the method of a console whose stdout is stderr.
|
|
123
127
|
*/
|
|
124
128
|
export function redirect_console_to_stderr() {
|
|
125
|
-
|
|
129
|
+
const to_stderr = new Console({ stdout: process.stderr, stderr: process.stderr });
|
|
130
|
+
for (const [name, method] of Object.entries(to_stderr)) {
|
|
131
|
+
if (typeof method === 'function') {
|
|
132
|
+
/** @type {Record<string, unknown>} */ (/** @type {unknown} */ (console))[name] = method;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
126
135
|
}
|