@tsrx/content-mapper 0.1.1 → 0.1.3
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 +139 -58
- package/dist/{mapper-DATqGboo.js → mapper-Gc2yuGNf.js} +843 -55
- package/dist/mapper-Gc2yuGNf.js.map +1 -0
- package/dist/mapper.js +2 -2
- package/dist/protocol.js +39 -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 +9 -8
- package/platform-globals/android.d.cts +17 -0
- package/platform-globals/ios.d.cts +17 -0
- package/platform-globals/unconfigured.d.cts +17 -0
- package/platform-globals/web.d.cts +17 -0
- package/src/diagnostic-directives.js +239 -0
- package/src/export-stub.js +94 -7
- package/src/mapper.js +65 -46
- package/src/platform-globals.js +76 -0
- package/src/protocol.js +41 -13
- package/src/rpc.js +11 -2
- package/src/span-mappings.js +379 -8
- package/dist/mapper-DATqGboo.js.map +0 -1
package/dist/mapper.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as validate_options, n as require, r as to_diagnostic, t as create_tsrx_content_mapper } from "./mapper-Gc2yuGNf.js";
|
|
2
2
|
|
|
3
|
-
export { create_tsrx_content_mapper,
|
|
3
|
+
export { create_tsrx_content_mapper, require, to_diagnostic, validate_options };
|
package/dist/protocol.js
CHANGED
|
@@ -37,21 +37,15 @@ const SpanMapFeature = {
|
|
|
37
37
|
None: 0,
|
|
38
38
|
All: (1 << 20) - 1
|
|
39
39
|
};
|
|
40
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Diagnostic-directive policies: `Ignore` hides TypeScript's errors in a
|
|
42
|
+
* generated range; `Expect` also reports a diagnostic of the mapper's when
|
|
43
|
+
* there are none there.
|
|
44
|
+
*/
|
|
41
45
|
const DiagnosticDirectivePolicy = {
|
|
42
46
|
Ignore: 0,
|
|
43
47
|
Expect: 1
|
|
44
48
|
};
|
|
45
|
-
/** Prefix for mapper-authored diagnostics: `error tsrx(1000): ...`. */
|
|
46
|
-
const DIAGNOSTIC_SOURCE = "tsrx";
|
|
47
|
-
/** Numeric code for a fatal compile error (the file could not be transformed). */
|
|
48
|
-
const DIAGNOSTIC_CODE_COMPILE_ERROR = 1e3;
|
|
49
|
-
/** Numeric code for a usage error that carries no string code. */
|
|
50
|
-
const DIAGNOSTIC_CODE_USAGE_ERROR = 1001;
|
|
51
|
-
/** Numeric code when no TSRX compiler can be resolved for a file. */
|
|
52
|
-
const DIAGNOSTIC_CODE_NO_COMPILER = 1002;
|
|
53
|
-
/** Numeric code when the project's TSRX configuration is invalid. */
|
|
54
|
-
const DIAGNOSTIC_CODE_INVALID_CONFIG = 1003;
|
|
55
49
|
/**
|
|
56
50
|
* @typedef {[
|
|
57
51
|
* generatedStart: number,
|
|
@@ -63,6 +57,22 @@ const DIAGNOSTIC_CODE_INVALID_CONFIG = 1003;
|
|
|
63
57
|
* ]} SpanMapping
|
|
64
58
|
*/
|
|
65
59
|
/**
|
|
60
|
+
* Not part of the TypeScript 7 protocol: what the `verificationMappings` mapper
|
|
61
|
+
* option adds to a transform result for command-line checkers. Every compiler
|
|
62
|
+
* mapping marked for diagnostics, in Volar's `CodeMapping` shape and in the
|
|
63
|
+
* transform's order, so `new SourceMap(verificationMappings).toSourceRange(start,
|
|
64
|
+
* end, true)` from `@volar/source-map` maps a diagnostic as `tsrx-tsc` does.
|
|
65
|
+
* Points (offsets with zero length) belong to the mapping they are listed in.
|
|
66
|
+
* Empty when the transform has a mapping whose verification is a `shouldReport`
|
|
67
|
+
* callback, which can't be serialized.
|
|
68
|
+
* @typedef {object} VerificationMapping
|
|
69
|
+
* @property {number[]} sourceOffsets
|
|
70
|
+
* @property {number[]} generatedOffsets
|
|
71
|
+
* @property {number[]} lengths
|
|
72
|
+
* @property {number[]} generatedLengths
|
|
73
|
+
* @property {{ verification: true }} data
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
66
76
|
* @typedef {object} MapperDiagnostic
|
|
67
77
|
* @property {number} start Offset into the original text (UTF-16 code units).
|
|
68
78
|
* @property {number} length
|
|
@@ -76,9 +86,26 @@ const DIAGNOSTIC_CODE_INVALID_CONFIG = 1003;
|
|
|
76
86
|
* @property {SpanMapping[]} mappings
|
|
77
87
|
*/
|
|
78
88
|
/**
|
|
89
|
+
* @typedef {[
|
|
90
|
+
* originalStart: number,
|
|
91
|
+
* originalLength: number,
|
|
92
|
+
* generatedStart: number,
|
|
93
|
+
* generatedEnd: number,
|
|
94
|
+
* policy: 0 | 1,
|
|
95
|
+
* unusedExpectDirectiveIndex?: number,
|
|
96
|
+
* ]} DiagnosticDirective
|
|
97
|
+
*/
|
|
98
|
+
/**
|
|
99
|
+
* @typedef {object} DiagnosticDirectives
|
|
100
|
+
* @property {Array<{ code: number, messageText: string }>} unusedExpectDirectiveDiagnostics What an `Expect` directive reports when TypeScript has no error in its range.
|
|
101
|
+
* @property {DiagnosticDirective[]} directives
|
|
102
|
+
*/
|
|
103
|
+
/**
|
|
79
104
|
* @typedef {MappedOutput & {
|
|
80
105
|
* diagnostics?: MapperDiagnostic[],
|
|
106
|
+
* diagnosticDirectives?: DiagnosticDirectives,
|
|
81
107
|
* supplemental?: MappedOutput[],
|
|
108
|
+
* verificationMappings?: VerificationMapping[],
|
|
82
109
|
* }} TransformResult
|
|
83
110
|
*/
|
|
84
111
|
/**
|
|
@@ -112,5 +139,5 @@ const DIAGNOSTIC_CODE_INVALID_CONFIG = 1003;
|
|
|
112
139
|
*/
|
|
113
140
|
|
|
114
141
|
//#endregion
|
|
115
|
-
export {
|
|
142
|
+
export { DiagnosticDirectivePolicy, SpanMapFeature, SpanMapKind };
|
|
116
143
|
//# sourceMappingURL=protocol.js.map
|
package/dist/protocol.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.js","names":[],"sources":["../src/protocol.js"],"sourcesContent":["/**\n * Constants of the TypeScript 7 content-mapper protocol, kept in sync with\n * `tsc/internal/contentmapper` and `tsc/internal/spanmap` in microsoft/TypeScript.\n */\n\n/** Span kinds. */\nexport const SpanMapKind = /** @type {const} */ ({\n\t/** Same length and identical text; the only kind that supports text edits. */\n\tVerbatim: 0,\n\t/** Same entity, different text or length. */\n\tAtom: 1,\n\t/** Different text; diagnostics covering the whole span exactly show the original text. */\n\tAlias: 2,\n});\n\n/** Feature bits a span opts into. Diagnostics ignore them. */\nexport const SpanMapFeature = /** @type {const} */ ({\n\tHover: 1 << 0,\n\tSignatureHelp: 1 << 1,\n\tCompletion: 1 << 2,\n\tDefinition: 1 << 3,\n\tTypeDefinition: 1 << 4,\n\tImplementation: 1 << 5,\n\tReferences: 1 << 6,\n\tDocumentHighlights: 1 << 7,\n\tRename: 1 << 8,\n\tCallHierarchy: 1 << 9,\n\tCodeActions: 1 << 10,\n\tFormatting: 1 << 11,\n\tInlayHints: 1 << 12,\n\tSemanticTokens: 1 << 13,\n\tFoldingRanges: 1 << 14,\n\tSelectionRanges: 1 << 15,\n\tLinkedEditing: 1 << 16,\n\tAutoInsert: 1 << 17,\n\tDocumentSymbols: 1 << 18,\n\tCodeLens: 1 << 19,\n\tNone: 0,\n\tAll: (1 << 20) - 1,\n});\n\n
|
|
1
|
+
{"version":3,"file":"protocol.js","names":[],"sources":["../src/protocol.js"],"sourcesContent":["/**\n * Constants of the TypeScript 7 content-mapper protocol, kept in sync with\n * `tsc/internal/contentmapper` and `tsc/internal/spanmap` in microsoft/TypeScript.\n */\n\n/** Span kinds. */\nexport const SpanMapKind = /** @type {const} */ ({\n\t/** Same length and identical text; the only kind that supports text edits. */\n\tVerbatim: 0,\n\t/** Same entity, different text or length. */\n\tAtom: 1,\n\t/** Different text; diagnostics covering the whole span exactly show the original text. */\n\tAlias: 2,\n});\n\n/** Feature bits a span opts into. Diagnostics ignore them. */\nexport const SpanMapFeature = /** @type {const} */ ({\n\tHover: 1 << 0,\n\tSignatureHelp: 1 << 1,\n\tCompletion: 1 << 2,\n\tDefinition: 1 << 3,\n\tTypeDefinition: 1 << 4,\n\tImplementation: 1 << 5,\n\tReferences: 1 << 6,\n\tDocumentHighlights: 1 << 7,\n\tRename: 1 << 8,\n\tCallHierarchy: 1 << 9,\n\tCodeActions: 1 << 10,\n\tFormatting: 1 << 11,\n\tInlayHints: 1 << 12,\n\tSemanticTokens: 1 << 13,\n\tFoldingRanges: 1 << 14,\n\tSelectionRanges: 1 << 15,\n\tLinkedEditing: 1 << 16,\n\tAutoInsert: 1 << 17,\n\tDocumentSymbols: 1 << 18,\n\tCodeLens: 1 << 19,\n\tNone: 0,\n\tAll: (1 << 20) - 1,\n});\n\n/**\n * Diagnostic-directive policies: `Ignore` hides TypeScript's errors in a\n * generated range; `Expect` also reports a diagnostic of the mapper's when\n * there are none there.\n */\nexport const DiagnosticDirectivePolicy = /** @type {const} */ ({ Ignore: 0, Expect: 1 });\n\n/**\n * @typedef {[\n * \tgeneratedStart: number,\n * \tgeneratedLength: number,\n * \toriginalStart: number,\n * \toriginalLength: number,\n * \tkind: 0 | 1 | 2,\n * \tfeatures: number,\n * ]} SpanMapping\n */\n\n/**\n * Not part of the TypeScript 7 protocol: what the `verificationMappings` mapper\n * option adds to a transform result for command-line checkers. Every compiler\n * mapping marked for diagnostics, in Volar's `CodeMapping` shape and in the\n * transform's order, so `new SourceMap(verificationMappings).toSourceRange(start,\n * end, true)` from `@volar/source-map` maps a diagnostic as `tsrx-tsc` does.\n * Points (offsets with zero length) belong to the mapping they are listed in.\n * Empty when the transform has a mapping whose verification is a `shouldReport`\n * callback, which can't be serialized.\n * @typedef {object} VerificationMapping\n * @property {number[]} sourceOffsets\n * @property {number[]} generatedOffsets\n * @property {number[]} lengths\n * @property {number[]} generatedLengths\n * @property {{ verification: true }} data\n */\n\n/**\n * @typedef {object} MapperDiagnostic\n * @property {number} start Offset into the original text (UTF-16 code units).\n * @property {number} length\n * @property {number} code\n * @property {string} messageText\n */\n\n/**\n * @typedef {object} MappedOutput\n * @property {string} text\n * @property {'.js' | '.jsx' | '.mjs' | '.cjs' | '.ts' | '.tsx' | '.mts' | '.cts' | '.json'} extension\n * @property {SpanMapping[]} mappings\n */\n\n/**\n * @typedef {[\n * \toriginalStart: number,\n * \toriginalLength: number,\n * \tgeneratedStart: number,\n * \tgeneratedEnd: number,\n * \tpolicy: 0 | 1,\n * \tunusedExpectDirectiveIndex?: number,\n * ]} DiagnosticDirective\n */\n\n/**\n * @typedef {object} DiagnosticDirectives\n * @property {Array<{ code: number, messageText: string }>} unusedExpectDirectiveDiagnostics What an `Expect` directive reports when TypeScript has no error in its range.\n * @property {DiagnosticDirective[]} directives\n */\n\n/**\n * @typedef {MappedOutput & {\n * \tdiagnostics?: MapperDiagnostic[],\n * \tdiagnosticDirectives?: DiagnosticDirectives,\n * \tsupplemental?: MappedOutput[],\n * \tverificationMappings?: VerificationMapping[],\n * }} TransformResult\n */\n\n/**\n * @typedef {object} InitializeParams\n * @property {string} [locale]\n * @property {Array<'utf-8' | 'utf-16'>} positionEncodings\n */\n\n/**\n * @typedef {object} InitializeResult\n * @property {'utf-8' | 'utf-16'} positionEncoding\n * @property {string} diagnosticSource\n */\n\n/**\n * @typedef {object} OpenProjectParams\n * @property {string} configFileName Empty for inferred projects.\n * @property {string} projectHandle\n * @property {Record<string, unknown>} [options] The mapper entry's `options` object.\n * @property {Record<string, unknown>} compilerOptions Effective compiler options (wire format).\n */\n\n/**\n * @typedef {object} OpenProjectResult\n * @property {string} configIdentity\n * @property {string[]} [watchedFiles]\n * @property {Array<{ path: Array<string | number>, messageText: string, code: number }>} [optionDiagnostics]\n */\n\n/**\n * @typedef {object} TransformParams\n * @property {string} fileName\n * @property {string} content\n * @property {string} [projectHandle]\n */\n"],"mappings":";;;;;;AAMA,MAAa,cAAoC;;CAEhD,UAAU;;CAEV,MAAM;;CAEN,OAAO;AACR;;AAGA,MAAa,iBAAuC;CACnD,OAAO;CACP,eAAe;CACf,YAAY;CACZ,YAAY;CACZ,gBAAgB;CAChB,gBAAgB;CAChB,YAAY;CACZ,oBAAoB;CACpB,QAAQ;CACR,eAAe;CACf,aAAa;CACb,YAAY;CACZ,YAAY;CACZ,gBAAgB;CAChB,eAAe;CACf,iBAAiB;CACjB,eAAe;CACf,YAAY,KAAK;CACjB,iBAAiB,KAAK;CACtB,UAAU,KAAK;CACf,MAAM;CACN,MAAM,KAAK,MAAM;AAClB;;;;;;AAOA,MAAa,4BAAkD;CAAE,QAAQ;CAAG,QAAQ;AAAE"}
|
package/dist/rpc.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Console } from "node:console";
|
|
2
|
+
|
|
1
3
|
//#region src/rpc.js
|
|
2
4
|
/**
|
|
3
5
|
* Content-mapper transport: `Content-Length`-framed JSON-RPC 2.0 over stdio.
|
|
@@ -117,10 +119,17 @@ function run_mapper_server(mapper, streams = {}) {
|
|
|
117
119
|
}
|
|
118
120
|
/**
|
|
119
121
|
* Route every console method to stderr so stray logging (from a target
|
|
120
|
-
* compiler, for example) can never corrupt the protocol stream on stdout
|
|
122
|
+
* compiler, for example) can never corrupt the protocol stream on stdout: not
|
|
123
|
+
* only `log` but `dir`, `table`, `trace`, `group` and the rest write there, so
|
|
124
|
+
* each takes the method of a console whose stdout is stderr.
|
|
121
125
|
*/
|
|
122
126
|
function redirect_console_to_stderr() {
|
|
123
|
-
|
|
127
|
+
const to_stderr = new Console({
|
|
128
|
+
stdout: process.stderr,
|
|
129
|
+
stderr: process.stderr
|
|
130
|
+
});
|
|
131
|
+
for (const [name, method] of Object.entries(to_stderr)) if (typeof method === "function")
|
|
132
|
+
/** @type {Record<string, unknown>} */ console[name] = method;
|
|
124
133
|
}
|
|
125
134
|
|
|
126
135
|
//#endregion
|
package/dist/rpc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc.js","names":[],"sources":["../src/rpc.js"],"sourcesContent":["/**\n * Content-mapper transport: `Content-Length`-framed JSON-RPC 2.0 over stdio.\n * TypeScript is the only requester; the mapper never sends requests or\n * notifications. stdout is reserved for protocol messages, so everything\n * else (including `console.log`) goes to stderr.\n *\n * Kept separate from the executable entry (`server.js`) so hosts that bundle\n * the mapper (the VS Code extension ships it for inferred projects) can start\n * it from their own entry file.\n */\n\n/**\n * @typedef {{ jsonrpc: '2.0', id?: number | string | null, method: string, params?: unknown }} RequestMessage\n */\n\n/**\n * Run the mapper server on the given streams. Installs the stream handlers\n * and returns; the process exits (with `process.exitCode`) once the input\n * stream ends, which is how TypeScript shuts a mapper down.\n * @param {import('./mapper.js').ContentMapper} mapper\n * @param {{ input?: NodeJS.ReadableStream, output?: NodeJS.WritableStream }} [streams]\n */\nexport function run_mapper_server(mapper, streams = {}) {\n\tconst input = streams.input ?? process.stdin;\n\tconst output = streams.output ?? process.stdout;\n\tlet buffered = Buffer.alloc(0);\n\n\tinput.on('data', (chunk) => {\n\t\tbuffered = Buffer.concat([buffered, typeof chunk === 'string' ? Buffer.from(chunk) : chunk]);\n\t\tread_messages();\n\t});\n\tinput.on('error', (error) => {\n\t\tconsole.error(error);\n\t\tprocess.exitCode = 1;\n\t});\n\tinput.on('end', () => {\n\t\tprocess.exit(process.exitCode ?? 0);\n\t});\n\n\tfunction read_messages() {\n\t\twhile (true) {\n\t\t\tconst header_end = buffered.indexOf('\\r\\n\\r\\n');\n\t\t\tif (header_end < 0) return;\n\t\t\tconst header = buffered.subarray(0, header_end).toString('ascii');\n\t\t\tconst length_match = /^Content-Length:\\s*(\\d+)\\s*$/im.exec(header);\n\t\t\tif (!length_match) {\n\t\t\t\tconsole.error('@tsrx/content-mapper: missing Content-Length header');\n\t\t\t\tprocess.exit(1);\n\t\t\t}\n\t\t\tconst content_length = Number(length_match[1]);\n\t\t\tconst message_start = header_end + 4;\n\t\t\tconst message_end = message_start + content_length;\n\t\t\tif (buffered.length < message_end) return;\n\t\t\tconst body = buffered.subarray(message_start, message_end).toString('utf8');\n\t\t\tbuffered = buffered.subarray(message_end);\n\t\t\t/** @type {RequestMessage} */\n\t\t\tlet message;\n\t\t\ttry {\n\t\t\t\tmessage = JSON.parse(body);\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error('@tsrx/content-mapper: invalid JSON-RPC message', error);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\thandle_message(message);\n\t\t}\n\t}\n\n\t/** @param {RequestMessage} message */\n\tfunction handle_message(message) {\n\t\tif (message.id === undefined || message.id === null) {\n\t\t\t// Notifications need no response.\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\t/** @type {unknown} */\n\t\t\tlet result;\n\t\t\tswitch (message.method) {\n\t\t\t\tcase 'initialize':\n\t\t\t\t\tresult = mapper.initialize(/** @type {any} */ (message.params));\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'openProject':\n\t\t\t\t\tresult = mapper.openProject(/** @type {any} */ (message.params));\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'closeProject':\n\t\t\t\t\tmapper.closeProject(/** @type {any} */ (message.params));\n\t\t\t\t\tresult = null;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'transform':\n\t\t\t\t\tresult = mapper.transform(/** @type {any} */ (message.params));\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\twrite({\n\t\t\t\t\t\tjsonrpc: '2.0',\n\t\t\t\t\t\tid: message.id,\n\t\t\t\t\t\terror: { code: -32601, message: `Unknown method ${message.method}` },\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\t\t\t}\n\t\t\twrite({ jsonrpc: '2.0', id: message.id, result });\n\t\t} catch (error) {\n\t\t\twrite({\n\t\t\t\tjsonrpc: '2.0',\n\t\t\t\tid: message.id,\n\t\t\t\terror: {\n\t\t\t\t\tcode: -32603,\n\t\t\t\t\tmessage: error instanceof Error ? (error.stack ?? error.message) : String(error),\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n\n\t/** @param {unknown} message */\n\tfunction write(message) {\n\t\tconst body = Buffer.from(JSON.stringify(message), 'utf8');\n\t\toutput.write(`Content-Length: ${body.length}\\r\\n\\r\\n`);\n\t\toutput.write(body);\n\t}\n}\n\n/**\n * Route every console method to stderr so stray logging (from a target\n * compiler, for example) can never corrupt the protocol stream on stdout.\n */\nexport function redirect_console_to_stderr() {\n\
|
|
1
|
+
{"version":3,"file":"rpc.js","names":[],"sources":["../src/rpc.js"],"sourcesContent":["/**\n * Content-mapper transport: `Content-Length`-framed JSON-RPC 2.0 over stdio.\n * TypeScript is the only requester; the mapper never sends requests or\n * notifications. stdout is reserved for protocol messages, so everything\n * else (including `console.log`) goes to stderr.\n *\n * Kept separate from the executable entry (`server.js`) so hosts that bundle\n * the mapper (the VS Code extension ships it for inferred projects) can start\n * it from their own entry file.\n */\n\nimport { Console } from 'node:console';\n\n/**\n * @typedef {{ jsonrpc: '2.0', id?: number | string | null, method: string, params?: unknown }} RequestMessage\n */\n\n/**\n * Run the mapper server on the given streams. Installs the stream handlers\n * and returns; the process exits (with `process.exitCode`) once the input\n * stream ends, which is how TypeScript shuts a mapper down.\n * @param {import('./mapper.js').ContentMapper} mapper\n * @param {{ input?: NodeJS.ReadableStream, output?: NodeJS.WritableStream }} [streams]\n */\nexport function run_mapper_server(mapper, streams = {}) {\n\tconst input = streams.input ?? process.stdin;\n\tconst output = streams.output ?? process.stdout;\n\tlet buffered = Buffer.alloc(0);\n\n\tinput.on('data', (chunk) => {\n\t\tbuffered = Buffer.concat([buffered, typeof chunk === 'string' ? Buffer.from(chunk) : chunk]);\n\t\tread_messages();\n\t});\n\tinput.on('error', (error) => {\n\t\tconsole.error(error);\n\t\tprocess.exitCode = 1;\n\t});\n\tinput.on('end', () => {\n\t\tprocess.exit(process.exitCode ?? 0);\n\t});\n\n\tfunction read_messages() {\n\t\twhile (true) {\n\t\t\tconst header_end = buffered.indexOf('\\r\\n\\r\\n');\n\t\t\tif (header_end < 0) return;\n\t\t\tconst header = buffered.subarray(0, header_end).toString('ascii');\n\t\t\tconst length_match = /^Content-Length:\\s*(\\d+)\\s*$/im.exec(header);\n\t\t\tif (!length_match) {\n\t\t\t\tconsole.error('@tsrx/content-mapper: missing Content-Length header');\n\t\t\t\tprocess.exit(1);\n\t\t\t}\n\t\t\tconst content_length = Number(length_match[1]);\n\t\t\tconst message_start = header_end + 4;\n\t\t\tconst message_end = message_start + content_length;\n\t\t\tif (buffered.length < message_end) return;\n\t\t\tconst body = buffered.subarray(message_start, message_end).toString('utf8');\n\t\t\tbuffered = buffered.subarray(message_end);\n\t\t\t/** @type {RequestMessage} */\n\t\t\tlet message;\n\t\t\ttry {\n\t\t\t\tmessage = JSON.parse(body);\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error('@tsrx/content-mapper: invalid JSON-RPC message', error);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\thandle_message(message);\n\t\t}\n\t}\n\n\t/** @param {RequestMessage} message */\n\tfunction handle_message(message) {\n\t\tif (message.id === undefined || message.id === null) {\n\t\t\t// Notifications need no response.\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\t/** @type {unknown} */\n\t\t\tlet result;\n\t\t\tswitch (message.method) {\n\t\t\t\tcase 'initialize':\n\t\t\t\t\tresult = mapper.initialize(/** @type {any} */ (message.params));\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'openProject':\n\t\t\t\t\tresult = mapper.openProject(/** @type {any} */ (message.params));\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'closeProject':\n\t\t\t\t\tmapper.closeProject(/** @type {any} */ (message.params));\n\t\t\t\t\tresult = null;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'transform':\n\t\t\t\t\tresult = mapper.transform(/** @type {any} */ (message.params));\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\twrite({\n\t\t\t\t\t\tjsonrpc: '2.0',\n\t\t\t\t\t\tid: message.id,\n\t\t\t\t\t\terror: { code: -32601, message: `Unknown method ${message.method}` },\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\t\t\t}\n\t\t\twrite({ jsonrpc: '2.0', id: message.id, result });\n\t\t} catch (error) {\n\t\t\twrite({\n\t\t\t\tjsonrpc: '2.0',\n\t\t\t\tid: message.id,\n\t\t\t\terror: {\n\t\t\t\t\tcode: -32603,\n\t\t\t\t\tmessage: error instanceof Error ? (error.stack ?? error.message) : String(error),\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n\n\t/** @param {unknown} message */\n\tfunction write(message) {\n\t\tconst body = Buffer.from(JSON.stringify(message), 'utf8');\n\t\toutput.write(`Content-Length: ${body.length}\\r\\n\\r\\n`);\n\t\toutput.write(body);\n\t}\n}\n\n/**\n * Route every console method to stderr so stray logging (from a target\n * compiler, for example) can never corrupt the protocol stream on stdout: not\n * only `log` but `dir`, `table`, `trace`, `group` and the rest write there, so\n * each takes the method of a console whose stdout is stderr.\n */\nexport function redirect_console_to_stderr() {\n\tconst to_stderr = new Console({ stdout: process.stderr, stderr: process.stderr });\n\tfor (const [name, method] of Object.entries(to_stderr)) {\n\t\tif (typeof method === 'function') {\n\t\t\t/** @type {Record<string, unknown>} */ (/** @type {unknown} */ (console))[name] = method;\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,kBAAkB,QAAQ,UAAU,CAAC,GAAG;CACvD,MAAM,QAAQ,QAAQ,SAAS,QAAQ;CACvC,MAAM,SAAS,QAAQ,UAAU,QAAQ;CACzC,IAAI,WAAW,OAAO,MAAM,CAAC;CAE7B,MAAM,GAAG,SAAS,UAAU;EAC3B,WAAW,OAAO,OAAO,CAAC,UAAU,OAAO,UAAU,WAAW,OAAO,KAAK,KAAK,IAAI,KAAK,CAAC;EAC3F,cAAc;CACf,CAAC;CACD,MAAM,GAAG,UAAU,UAAU;EAC5B,QAAQ,MAAM,KAAK;EACnB,QAAQ,WAAW;CACpB,CAAC;CACD,MAAM,GAAG,aAAa;EACrB,QAAQ,KAAK,QAAQ,YAAY,CAAC;CACnC,CAAC;CAED,SAAS,gBAAgB;EACxB,OAAO,MAAM;GACZ,MAAM,aAAa,SAAS,QAAQ,UAAU;GAC9C,IAAI,aAAa,GAAG;GACpB,MAAM,SAAS,SAAS,SAAS,GAAG,UAAU,CAAC,CAAC,SAAS,OAAO;GAChE,MAAM,eAAe,iCAAiC,KAAK,MAAM;GACjE,IAAI,CAAC,cAAc;IAClB,QAAQ,MAAM,qDAAqD;IACnE,QAAQ,KAAK,CAAC;GACf;GACA,MAAM,iBAAiB,OAAO,aAAa,EAAE;GAC7C,MAAM,gBAAgB,aAAa;GACnC,MAAM,cAAc,gBAAgB;GACpC,IAAI,SAAS,SAAS,aAAa;GACnC,MAAM,OAAO,SAAS,SAAS,eAAe,WAAW,CAAC,CAAC,SAAS,MAAM;GAC1E,WAAW,SAAS,SAAS,WAAW;;GAExC,IAAI;GACJ,IAAI;IACH,UAAU,KAAK,MAAM,IAAI;GAC1B,SAAS,OAAO;IACf,QAAQ,MAAM,kDAAkD,KAAK;IACrE;GACD;GACA,eAAe,OAAO;EACvB;CACD;;CAGA,SAAS,eAAe,SAAS;EAChC,IAAI,QAAQ,OAAO,UAAa,QAAQ,OAAO,MAE9C;EAED,IAAI;;GAEH,IAAI;GACJ,QAAQ,QAAQ,QAAhB;IACC,KAAK;KACJ,SAAS,OAAO,WAA+B,QAAQ,MAAO;KAC9D;IACD,KAAK;KACJ,SAAS,OAAO,YAAgC,QAAQ,MAAO;KAC/D;IACD,KAAK;KACJ,OAAO,aAAiC,QAAQ,MAAO;KACvD,SAAS;KACT;IACD,KAAK;KACJ,SAAS,OAAO,UAA8B,QAAQ,MAAO;KAC7D;IACD;KACC,MAAM;MACL,SAAS;MACT,IAAI,QAAQ;MACZ,OAAO;OAAE,MAAM;OAAQ,SAAS,kBAAkB,QAAQ;MAAS;KACpE,CAAC;KACD;GACF;GACA,MAAM;IAAE,SAAS;IAAO,IAAI,QAAQ;IAAI;GAAO,CAAC;EACjD,SAAS,OAAO;GACf,MAAM;IACL,SAAS;IACT,IAAI,QAAQ;IACZ,OAAO;KACN,MAAM;KACN,SAAS,iBAAiB,QAAS,MAAM,SAAS,MAAM,UAAW,OAAO,KAAK;IAChF;GACD,CAAC;EACF;CACD;;CAGA,SAAS,MAAM,SAAS;EACvB,MAAM,OAAO,OAAO,KAAK,KAAK,UAAU,OAAO,GAAG,MAAM;EACxD,OAAO,MAAM,mBAAmB,KAAK,OAAO,SAAS;EACrD,OAAO,MAAM,IAAI;CAClB;AACD;;;;;;;AAQA,SAAgB,6BAA6B;CAC5C,MAAM,YAAY,IAAI,QAAQ;EAAE,QAAQ,QAAQ;EAAQ,QAAQ,QAAQ;CAAO,CAAC;CAChF,KAAK,MAAM,CAAC,MAAM,WAAW,OAAO,QAAQ,SAAS,GACpD,IAAI,OAAO,WAAW;wCACkB,AAAyB,QAAU,QAAQ;AAGrF"}
|
package/dist/server.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as create_tsrx_content_mapper } from "./mapper-
|
|
2
|
+
import { t as create_tsrx_content_mapper } from "./mapper-Gc2yuGNf.js";
|
|
3
3
|
import { redirect_console_to_stderr, run_mapper_server } from "./rpc.js";
|
|
4
4
|
|
|
5
5
|
//#region src/server.js
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsrx/content-mapper",
|
|
3
3
|
"description": "TypeScript 7 content mapper for .tsrx files",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Dominic Gannaway",
|
|
7
7
|
"type": "module",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
19
|
+
"platform-globals",
|
|
19
20
|
"src"
|
|
20
21
|
],
|
|
21
22
|
"exports": {
|
|
@@ -48,13 +49,13 @@
|
|
|
48
49
|
"solid-js": "2.0.0-rc.3",
|
|
49
50
|
"tsdown": "^0.23.0",
|
|
50
51
|
"typescript": "^5.9.3",
|
|
51
|
-
"vue": "3.6.0-
|
|
52
|
-
"@tsrx/core": "0.
|
|
53
|
-
"@tsrx/
|
|
54
|
-
"@tsrx/react": "0.
|
|
55
|
-
"@tsrx/
|
|
56
|
-
"@tsrx/typescript-plugin": "0.
|
|
57
|
-
"@tsrx/
|
|
52
|
+
"vue": "3.6.0-rc.14",
|
|
53
|
+
"@tsrx/core": "0.6.0",
|
|
54
|
+
"@tsrx/preact": "0.4.0",
|
|
55
|
+
"@tsrx/react": "0.5.0",
|
|
56
|
+
"@tsrx/solid": "0.4.0",
|
|
57
|
+
"@tsrx/typescript-plugin": "0.7.0",
|
|
58
|
+
"@tsrx/vue": "0.4.0"
|
|
58
59
|
},
|
|
59
60
|
"publishConfig": {
|
|
60
61
|
"access": "public"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Editor-only `import.meta.env.platform` types for the `android` platform.
|
|
2
|
+
// The content mapper references this file in place of the copy that
|
|
3
|
+
// `withPlatformTypes` in `@tsrx/core` appends to each module that keeps a
|
|
4
|
+
// flag (see `src/platform-globals.js`).
|
|
5
|
+
export {};
|
|
6
|
+
declare global {
|
|
7
|
+
interface ImportMetaEnv {
|
|
8
|
+
readonly platform: string & {
|
|
9
|
+
readonly web: false;
|
|
10
|
+
readonly ios: false;
|
|
11
|
+
readonly android: true;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
interface ImportMeta {
|
|
15
|
+
readonly [key: `env${string}`]: ImportMetaEnv;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Editor-only `import.meta.env.platform` types for the `ios` platform.
|
|
2
|
+
// The content mapper references this file in place of the copy that
|
|
3
|
+
// `withPlatformTypes` in `@tsrx/core` appends to each module that keeps a
|
|
4
|
+
// flag (see `src/platform-globals.js`).
|
|
5
|
+
export {};
|
|
6
|
+
declare global {
|
|
7
|
+
interface ImportMetaEnv {
|
|
8
|
+
readonly platform: string & {
|
|
9
|
+
readonly web: false;
|
|
10
|
+
readonly ios: true;
|
|
11
|
+
readonly android: false;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
interface ImportMeta {
|
|
15
|
+
readonly [key: `env${string}`]: ImportMetaEnv;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Editor-only `import.meta.env.platform` types for a project without a platform.
|
|
2
|
+
// The content mapper references this file in place of the copy that
|
|
3
|
+
// `withPlatformTypes` in `@tsrx/core` appends to each module that keeps a
|
|
4
|
+
// flag (see `src/platform-globals.js`).
|
|
5
|
+
export {};
|
|
6
|
+
declare global {
|
|
7
|
+
interface ImportMetaEnv {
|
|
8
|
+
readonly platform: string & {
|
|
9
|
+
readonly web: boolean;
|
|
10
|
+
readonly ios: boolean;
|
|
11
|
+
readonly android: boolean;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
interface ImportMeta {
|
|
15
|
+
readonly [key: `env${string}`]: ImportMetaEnv;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Editor-only `import.meta.env.platform` types for the `web` platform.
|
|
2
|
+
// The content mapper references this file in place of the copy that
|
|
3
|
+
// `withPlatformTypes` in `@tsrx/core` appends to each module that keeps a
|
|
4
|
+
// flag (see `src/platform-globals.js`).
|
|
5
|
+
export {};
|
|
6
|
+
declare global {
|
|
7
|
+
interface ImportMetaEnv {
|
|
8
|
+
readonly platform: string & {
|
|
9
|
+
readonly web: true;
|
|
10
|
+
readonly ios: false;
|
|
11
|
+
readonly android: false;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
interface ImportMeta {
|
|
15
|
+
readonly [key: `env${string}`]: ImportMetaEnv;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/** @import * as AST from 'estree' */
|
|
2
|
+
/** @import {CodeMapping, CompileError} from '@tsrx/core/types' */
|
|
3
|
+
/** @import {DiagnosticDirectives} from './protocol.js' */
|
|
4
|
+
|
|
5
|
+
import { DIAGNOSTIC_CODES } from '@tsrx/core/diagnostics';
|
|
6
|
+
import { DiagnosticDirectivePolicy } from './protocol.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The nodes whose generated code an error's directive covers: the innermost
|
|
10
|
+
* statement, declaration, class or type member, or element that holds the
|
|
11
|
+
* error.
|
|
12
|
+
*/
|
|
13
|
+
const HOLDER_TYPE =
|
|
14
|
+
/(?:Statement|Declaration|Definition|Signature)$|^(?:StaticBlock|AccessorProperty|JSXElement|JSXFragment|JSXStyleElement)$/;
|
|
15
|
+
|
|
16
|
+
/** Keys of a tree node that hold no child nodes, or nodes that aren't its children. */
|
|
17
|
+
const NOT_CHILDREN = new Set([
|
|
18
|
+
'metadata',
|
|
19
|
+
'loc',
|
|
20
|
+
'range',
|
|
21
|
+
'parent',
|
|
22
|
+
'comments',
|
|
23
|
+
'leadingComments',
|
|
24
|
+
'trailingComments',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An `ignore` directive for each error the mapper reports, so that a mistake
|
|
29
|
+
* shows once. TypeScript reports some mistakes itself from the generated code,
|
|
30
|
+
* with a code of its own, and would show them next to the mapper's error. The
|
|
31
|
+
* directive hides TypeScript's errors in the generated code of the statement,
|
|
32
|
+
* class or type member, or element that holds the error, until the error is
|
|
33
|
+
* fixed and the mapper stops sending it. The mapper's own errors are not
|
|
34
|
+
* TypeScript's, so they stay.
|
|
35
|
+
*
|
|
36
|
+
* The ranges come from what every TSRX compiler returns, whatever its target:
|
|
37
|
+
* the source tree (`sourceAst`) finds the node that holds an error, and the
|
|
38
|
+
* mappings give that node's generated code. Without a source tree the error's
|
|
39
|
+
* own span stands in. A node without mappings gets no directive.
|
|
40
|
+
* @param {readonly CompileError[]} errors
|
|
41
|
+
* @param {AST.Program | null} source_ast
|
|
42
|
+
* @param {readonly CodeMapping[]} mappings
|
|
43
|
+
* @param {number} original_length
|
|
44
|
+
* @param {number} generated_length
|
|
45
|
+
* @returns {DiagnosticDirectives | null}
|
|
46
|
+
*/
|
|
47
|
+
export function ignore_directives(errors, source_ast, mappings, original_length, generated_length) {
|
|
48
|
+
/** @type {Array<[original_start: number, original_end: number, generated_start: number, generated_end: number]>} */
|
|
49
|
+
const ranges = [];
|
|
50
|
+
for (const error of errors) {
|
|
51
|
+
if (typeof error.pos !== 'number') {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const on_tag = reported_on_tag(error);
|
|
55
|
+
const position = on_tag ? /** @type {number} */ (error.raisedAt) - 1 : error.pos;
|
|
56
|
+
const holder = source_ast ? holder_at(source_ast, position) : null;
|
|
57
|
+
const start = holder ? /** @type {number} */ (holder.start) : error.pos;
|
|
58
|
+
const end = holder
|
|
59
|
+
? /** @type {number} */ (holder.end)
|
|
60
|
+
: typeof error.end === 'number' && error.end > error.pos
|
|
61
|
+
? error.end
|
|
62
|
+
: error.pos + 1;
|
|
63
|
+
// With nothing holding an unclosed tag, only its own code is covered. Its
|
|
64
|
+
// anchor isn't that: it sits on whatever else is on its generated line.
|
|
65
|
+
const own_code = on_tag && !holder;
|
|
66
|
+
for (const generated of generated_ranges(mappings, start, end, own_code)) {
|
|
67
|
+
ranges.push([
|
|
68
|
+
Math.min(start, original_length),
|
|
69
|
+
Math.min(end, original_length),
|
|
70
|
+
Math.min(generated[0], generated_length),
|
|
71
|
+
Math.min(generated[1], generated_length),
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (ranges.length === 0) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// TypeScript rejects directives whose generated ranges overlap.
|
|
80
|
+
ranges.sort((a, b) => a[2] - b[2]);
|
|
81
|
+
/** @type {typeof ranges} */
|
|
82
|
+
const merged = [];
|
|
83
|
+
for (const range of ranges) {
|
|
84
|
+
const last = merged.at(-1);
|
|
85
|
+
if (last && range[2] < last[3]) {
|
|
86
|
+
last[0] = Math.min(last[0], range[0]);
|
|
87
|
+
last[1] = Math.max(last[1], range[1]);
|
|
88
|
+
last[3] = Math.max(last[3], range[3]);
|
|
89
|
+
} else {
|
|
90
|
+
merged.push([...range]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
unusedExpectDirectiveDiagnostics: [],
|
|
95
|
+
directives: merged.map(([original_start, original_end, generated_start, generated_end]) => [
|
|
96
|
+
original_start,
|
|
97
|
+
original_end - original_start,
|
|
98
|
+
generated_start,
|
|
99
|
+
generated_end,
|
|
100
|
+
DiagnosticDirectivePolicy.Ignore,
|
|
101
|
+
]),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Whether `error` is an unclosed tag reported on the tag and found where the
|
|
107
|
+
* parser stopped, one before `raisedAt`: the end of its template, or the start
|
|
108
|
+
* of a `<style>` or `<script>` body. Its directive holds there, not at the error:
|
|
109
|
+
* the closing tag that recovery adds has no mapping, so TypeScript's error on it
|
|
110
|
+
* could only show at the top of the file, and the tag's own statement can end
|
|
111
|
+
* before it. At the end of the file nothing holds that place, as the tag's
|
|
112
|
+
* declaration has stretched around the code that follows, which the directive
|
|
113
|
+
* must not hide. A compiler from before tags were reported on themselves has its
|
|
114
|
+
* error where the parser stopped already, with `raisedAt` no further than `end`,
|
|
115
|
+
* and the mapper runs whichever compiler the project has.
|
|
116
|
+
* @param {CompileError} error
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
*/
|
|
119
|
+
function reported_on_tag({ code, end, raisedAt }) {
|
|
120
|
+
return (
|
|
121
|
+
code === DIAGNOSTIC_CODES.UNCLOSED_TAG &&
|
|
122
|
+
typeof raisedAt === 'number' &&
|
|
123
|
+
typeof end === 'number' &&
|
|
124
|
+
raisedAt > end
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The innermost holder node (see {@link HOLDER_TYPE}) that contains `position`,
|
|
130
|
+
* found by descending from the root through the child that contains it.
|
|
131
|
+
* @param {AST.Node} root
|
|
132
|
+
* @param {number} position
|
|
133
|
+
* @returns {AST.Node | null}
|
|
134
|
+
*/
|
|
135
|
+
function holder_at(root, position) {
|
|
136
|
+
/** @type {AST.Node | null} */
|
|
137
|
+
let holder = null;
|
|
138
|
+
/** @type {AST.Node | null} */
|
|
139
|
+
let node = root;
|
|
140
|
+
while (node) {
|
|
141
|
+
if (HOLDER_TYPE.test(node.type)) {
|
|
142
|
+
holder = node;
|
|
143
|
+
}
|
|
144
|
+
node = child_at(node, position);
|
|
145
|
+
}
|
|
146
|
+
return holder;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {AST.Node} node
|
|
151
|
+
* @param {number} position
|
|
152
|
+
* @returns {AST.Node | null}
|
|
153
|
+
*/
|
|
154
|
+
function child_at(node, position) {
|
|
155
|
+
for (const [key, value] of Object.entries(node)) {
|
|
156
|
+
if (NOT_CHILDREN.has(key) || !value || typeof value !== 'object') {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
for (const child of Array.isArray(value) ? value : [value]) {
|
|
160
|
+
if (
|
|
161
|
+
child &&
|
|
162
|
+
typeof child.type === 'string' &&
|
|
163
|
+
typeof child.start === 'number' &&
|
|
164
|
+
typeof child.end === 'number' &&
|
|
165
|
+
child.start <= position &&
|
|
166
|
+
position < child.end
|
|
167
|
+
) {
|
|
168
|
+
return child;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The generated ranges of the mapped code between `start` and `end` in the
|
|
177
|
+
* original. A node's generated code need not be in one piece: a compiler can
|
|
178
|
+
* hoist part of it (a static element above the component), and a `<script>`
|
|
179
|
+
* body is checked as a block at the end of the file. So each range runs from
|
|
180
|
+
* the first to the last generated offset of the node's segments until code
|
|
181
|
+
* mapped from outside the node follows, and the next of its segments starts a
|
|
182
|
+
* new range; a range never covers another component's code. A segment of the
|
|
183
|
+
* same length on both sides that encloses the original range, as a verbatim
|
|
184
|
+
* `<script>` body does, gives the matching part of its generated text.
|
|
185
|
+
* @param {readonly CodeMapping[]} mappings
|
|
186
|
+
* @param {number} start
|
|
187
|
+
* @param {number} end
|
|
188
|
+
* @param {boolean} [own_code] Count only segments inside the range, and no
|
|
189
|
+
* error's anchor. A segment that encloses the range doesn't count: a mapping of
|
|
190
|
+
* the whole file can have the same length on both sides by chance. An anchor
|
|
191
|
+
* still ends a range, as any code from outside the node does.
|
|
192
|
+
* @returns {Array<[number, number]>}
|
|
193
|
+
*/
|
|
194
|
+
function generated_ranges(mappings, start, end, own_code = false) {
|
|
195
|
+
/** @type {Array<[generated_start: number, generated_end: number, inside: boolean]>} */
|
|
196
|
+
const segments = [];
|
|
197
|
+
for (const mapping of mappings) {
|
|
198
|
+
for (let i = 0; i < mapping.sourceOffsets.length; i++) {
|
|
199
|
+
const source = mapping.sourceOffsets[i];
|
|
200
|
+
const length = mapping.lengths[i];
|
|
201
|
+
const generated = mapping.generatedOffsets[i];
|
|
202
|
+
const generated_length = mapping.generatedLengths?.[i] ?? length;
|
|
203
|
+
if (own_code && mapping.data?.customData?.diagnosticAnchor) {
|
|
204
|
+
segments.push([generated, generated + generated_length, false]);
|
|
205
|
+
} else if (source >= start && source + length <= end) {
|
|
206
|
+
segments.push([generated, generated + generated_length, true]);
|
|
207
|
+
} else if (
|
|
208
|
+
!own_code &&
|
|
209
|
+
source <= start &&
|
|
210
|
+
end <= source + length &&
|
|
211
|
+
generated_length === length
|
|
212
|
+
) {
|
|
213
|
+
segments.push([generated + start - source, generated + end - source, true]);
|
|
214
|
+
} else {
|
|
215
|
+
segments.push([generated, generated + generated_length, false]);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
segments.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
220
|
+
/** @type {Array<[number, number]>} */
|
|
221
|
+
const ranges = [];
|
|
222
|
+
/** @type {[number, number] | null} */
|
|
223
|
+
let range = null;
|
|
224
|
+
for (const [segment_start, segment_end, inside] of segments) {
|
|
225
|
+
if (inside) {
|
|
226
|
+
if (range) {
|
|
227
|
+
range[1] = Math.max(range[1], segment_end);
|
|
228
|
+
} else {
|
|
229
|
+
range = [segment_start, segment_end];
|
|
230
|
+
}
|
|
231
|
+
} else if (range && segment_start >= range[1]) {
|
|
232
|
+
// Code from elsewhere follows: the node's next segment starts a new range.
|
|
233
|
+
if (range[1] > range[0]) ranges.push(range);
|
|
234
|
+
range = null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (range && range[1] > range[0]) ranges.push(range);
|
|
238
|
+
return ranges;
|
|
239
|
+
}
|
package/src/export-stub.js
CHANGED
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
* Build a TypeScript stub that preserves a module's export surface while the
|
|
5
5
|
* file cannot be compiled.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* type errors of its own) to the checker would produce noise. Instead every
|
|
7
|
+
* Raw TSRX, or the last good TSX (which may carry type errors of its own),
|
|
8
|
+
* would give the checker noise to report. Instead every
|
|
10
9
|
* export from the last successful transform's source AST is re-declared as
|
|
11
10
|
* `any` (both as a value and as a type, so `new X()`, `X.y` and `let v: X` all
|
|
12
11
|
* keep resolving), re-exports from other modules are kept as written (including
|
|
@@ -69,10 +68,21 @@ export function build_export_stub(program) {
|
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
|
|
71
|
+
// A name no declaration can have (`null`, `if`, `string` for a type, an
|
|
72
|
+
// arbitrary module namespace name such as `"foo-bar"`) is exported from a
|
|
73
|
+
// local binding of another name.
|
|
74
|
+
let next_local = 0;
|
|
72
75
|
for (const name of names) {
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
if (identifier_pattern.test(name) && !UNDECLARABLE_NAMES.has(name)) {
|
|
77
|
+
lines.push(`export declare const ${name}: any;`);
|
|
78
|
+
lines.push(`export type ${name} = any;`);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
let local = `_$_export_${next_local++}`;
|
|
82
|
+
while (names.has(local)) local = `_$_export_${next_local++}`;
|
|
83
|
+
lines.push(`declare const ${local}: any;`);
|
|
84
|
+
lines.push(`type ${local} = any;`);
|
|
85
|
+
lines.push(`export { ${local} as ${module_export_name(name)} };`);
|
|
76
86
|
}
|
|
77
87
|
if (has_default) {
|
|
78
88
|
lines.push('declare const _default: any;');
|
|
@@ -96,6 +106,74 @@ function type_modifier(node) {
|
|
|
96
106
|
|
|
97
107
|
const identifier_pattern = /^[\p{ID_Start}_$][\p{ID_Continue}$]*$/u;
|
|
98
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Identifier names that `export declare const x: any; export type x = any;`
|
|
111
|
+
* can't declare in a module: reserved words (TS1389), words reserved in strict
|
|
112
|
+
* mode (TS1214), `await`, `let`, `arguments` and `eval`, the names of
|
|
113
|
+
* TypeScript's own types (TS2457), and `as`, which `export type as` misreads.
|
|
114
|
+
*/
|
|
115
|
+
const UNDECLARABLE_NAMES = new Set([
|
|
116
|
+
'break',
|
|
117
|
+
'case',
|
|
118
|
+
'catch',
|
|
119
|
+
'class',
|
|
120
|
+
'const',
|
|
121
|
+
'continue',
|
|
122
|
+
'debugger',
|
|
123
|
+
'default',
|
|
124
|
+
'delete',
|
|
125
|
+
'do',
|
|
126
|
+
'else',
|
|
127
|
+
'enum',
|
|
128
|
+
'export',
|
|
129
|
+
'extends',
|
|
130
|
+
'false',
|
|
131
|
+
'finally',
|
|
132
|
+
'for',
|
|
133
|
+
'function',
|
|
134
|
+
'if',
|
|
135
|
+
'import',
|
|
136
|
+
'in',
|
|
137
|
+
'instanceof',
|
|
138
|
+
'new',
|
|
139
|
+
'null',
|
|
140
|
+
'return',
|
|
141
|
+
'super',
|
|
142
|
+
'switch',
|
|
143
|
+
'this',
|
|
144
|
+
'throw',
|
|
145
|
+
'true',
|
|
146
|
+
'try',
|
|
147
|
+
'typeof',
|
|
148
|
+
'var',
|
|
149
|
+
'void',
|
|
150
|
+
'while',
|
|
151
|
+
'with',
|
|
152
|
+
'implements',
|
|
153
|
+
'interface',
|
|
154
|
+
'package',
|
|
155
|
+
'private',
|
|
156
|
+
'protected',
|
|
157
|
+
'public',
|
|
158
|
+
'static',
|
|
159
|
+
'yield',
|
|
160
|
+
'await',
|
|
161
|
+
'let',
|
|
162
|
+
'arguments',
|
|
163
|
+
'eval',
|
|
164
|
+
'any',
|
|
165
|
+
'bigint',
|
|
166
|
+
'boolean',
|
|
167
|
+
'never',
|
|
168
|
+
'number',
|
|
169
|
+
'object',
|
|
170
|
+
'string',
|
|
171
|
+
'symbol',
|
|
172
|
+
'undefined',
|
|
173
|
+
'unknown',
|
|
174
|
+
'as',
|
|
175
|
+
]);
|
|
176
|
+
|
|
99
177
|
/**
|
|
100
178
|
* The name an export binds, as a plain string: `a` for `export { a }` and
|
|
101
179
|
* `foo-bar` for `export { "foo-bar" as a } from`.
|
|
@@ -116,7 +194,16 @@ function export_name(node) {
|
|
|
116
194
|
* @returns {string}
|
|
117
195
|
*/
|
|
118
196
|
function print_name(node) {
|
|
119
|
-
|
|
197
|
+
return module_export_name(export_name(node));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* A name as an export specifier takes it: any identifier name, reserved words
|
|
202
|
+
* too, as is, and anything else quoted.
|
|
203
|
+
* @param {string} name
|
|
204
|
+
* @returns {string}
|
|
205
|
+
*/
|
|
206
|
+
function module_export_name(name) {
|
|
120
207
|
return identifier_pattern.test(name) ? name : JSON.stringify(name);
|
|
121
208
|
}
|
|
122
209
|
|