@tsrx/content-mapper 0.1.2 → 0.1.4
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 +56 -7
- package/dist/{mapper-B4f--1ac.js → mapper-SyX-87Od.js} +651 -28
- package/dist/mapper-SyX-87Od.js.map +1 -0
- package/dist/mapper.js +1 -1
- package/dist/protocol.js +17 -0
- package/dist/protocol.js.map +1 -1
- package/dist/server.js +1 -1
- package/package.json +10 -9
- 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 +194 -7
- package/src/mapper.js +25 -9
- package/src/platform-globals.js +76 -0
- package/src/protocol.js +18 -0
- package/src/span-mappings.js +379 -8
- package/dist/mapper-B4f--1ac.js.map +0 -1
|
@@ -1950,8 +1950,6 @@ function regions_from_mappings(mappings) {
|
|
|
1950
1950
|
const require$2 = createRequire$1(import.meta.url);
|
|
1951
1951
|
const root_dirname = path$1.dirname(fileURLToPath$1(import.meta.url));
|
|
1952
1952
|
const { log, logWarning, logError } = createLogging("[TSRX Language]");
|
|
1953
|
-
/** @type {Set<string>} */
|
|
1954
|
-
const loggedCompilationFailures = /* @__PURE__ */ new Set();
|
|
1955
1953
|
/**
|
|
1956
1954
|
* `[package name, package directory parts, supported extensions, package hints, entry candidates?]`.
|
|
1957
1955
|
* Entry candidates are relative to the package directory and are probed in order,
|
|
@@ -2335,12 +2333,156 @@ function invalidateCompilerResolutionCaches() {
|
|
|
2335
2333
|
path2TsrxCompilerPathMap.clear();
|
|
2336
2334
|
pathToPackageManifestCache.clear();
|
|
2337
2335
|
reset_consumer_compiler_resolution_caches();
|
|
2338
|
-
loggedCompilationFailures.clear();
|
|
2339
2336
|
}
|
|
2340
2337
|
|
|
2341
2338
|
//#endregion
|
|
2342
2339
|
//#region ../tsrx/src/diagnostics.js
|
|
2343
2340
|
/**
|
|
2341
|
+
* TSRX's own error codes, one per mistake only TSRX reports. The TSRX
|
|
2342
|
+
* specification lists each with its message and an example (its appendix,
|
|
2343
|
+
* "Error codes"). A mistake TypeScript also reports has TypeScript's code
|
|
2344
|
+
* instead (`TS1005`), from {@link TS_ERRORS}.
|
|
2345
|
+
*
|
|
2346
|
+
* - `TSRX1xxx`: markup syntax only TSRX has
|
|
2347
|
+
* - `TSRX2xxx`: template rules
|
|
2348
|
+
* - `TSRX3xxx`: `<style>` and CSS
|
|
2349
|
+
* - `TSRX4xxx`: everything else
|
|
2350
|
+
*/
|
|
2351
|
+
const DIAGNOSTIC_CODES = {
|
|
2352
|
+
/** An element has no closing tag before the end of its template. */
|
|
2353
|
+
UNCLOSED_TAG: "TSRX1001",
|
|
2354
|
+
/** A closing tag doesn't match the element it closes. */
|
|
2355
|
+
MISMATCHED_CLOSING_TAG: "TSRX1002",
|
|
2356
|
+
/** A closing tag with no element open. */
|
|
2357
|
+
UNEXPECTED_CLOSING_TAG: "TSRX1003",
|
|
2358
|
+
/** A `<\/script` in a `<script>` body that isn't its closing tag, where HTML would end the script. */
|
|
2359
|
+
SCRIPT_END_TAG_IN_BODY: "TSRX1004",
|
|
2360
|
+
/** A namespaced element name (`<foo:bar>`). */
|
|
2361
|
+
NAMESPACED_ELEMENT: "TSRX1005",
|
|
2362
|
+
/** A spread as an attribute's value (`a={...b}`). */
|
|
2363
|
+
ATTRIBUTE_VALUE_SPREAD: "TSRX1006",
|
|
2364
|
+
/** A `;` at the end of an expression container (`{a;}`). */
|
|
2365
|
+
TEMPLATE_EXPRESSION_TRAILING_SEMICOLON: "TSRX1007",
|
|
2366
|
+
/** A directive without its `{ … }` body. */
|
|
2367
|
+
DIRECTIVE_BODY_EXPECTED: "TSRX1008",
|
|
2368
|
+
/** A directive's next branch written without its `@` (`else`, `empty`, `pending`, `catch`). */
|
|
2369
|
+
DIRECTIVE_BRANCH_EXPECTED: "TSRX1009",
|
|
2370
|
+
/** An `@try` with neither `@catch` nor `@pending`. */
|
|
2371
|
+
TRY_HANDLER_MISSING: "TSRX1010",
|
|
2372
|
+
/** An `@for`'s `index` or `key` clause is incomplete or out of order. */
|
|
2373
|
+
FOR_CLAUSE: "TSRX1011",
|
|
2374
|
+
/** A directive's clause (`@else {`, `@catch (e) {`, `@case 1:`) in element children, where no directive takes it. */
|
|
2375
|
+
DETACHED_DIRECTIVE_CLAUSE: "TSRX1012",
|
|
2376
|
+
/** A `return` in a template. */
|
|
2377
|
+
TEMPLATE_RETURN_STATEMENT: "TSRX2001",
|
|
2378
|
+
/** A `return` in an `@if` body. */
|
|
2379
|
+
IF_RETURN_STATEMENT: "TSRX2002",
|
|
2380
|
+
/** A `break` in an `@if` body. */
|
|
2381
|
+
IF_BREAK_STATEMENT: "TSRX2003",
|
|
2382
|
+
/** A `continue` in an `@if` body. */
|
|
2383
|
+
IF_CONTINUE_STATEMENT: "TSRX2004",
|
|
2384
|
+
/** A `return` in an `@for` body. */
|
|
2385
|
+
FOR_RETURN_STATEMENT: "TSRX2005",
|
|
2386
|
+
/** A `break` in an `@for` body. */
|
|
2387
|
+
FOR_BREAK_STATEMENT: "TSRX2006",
|
|
2388
|
+
/** A `continue` in an `@for` body. */
|
|
2389
|
+
FOR_CONTINUE_STATEMENT: "TSRX2007",
|
|
2390
|
+
/** A `break` in an `@case` body. */
|
|
2391
|
+
SWITCH_CASE_BREAK_STATEMENT: "TSRX2008",
|
|
2392
|
+
/** A `return` in an `@case` body. */
|
|
2393
|
+
SWITCH_CASE_RETURN_STATEMENT: "TSRX2009",
|
|
2394
|
+
/** Template output nothing renders: not returned, assigned, or part of the output. */
|
|
2395
|
+
FORGOTTEN_STATEMENT_CONTAINER: "TSRX2010",
|
|
2396
|
+
/** A `@{ … }` body with more than one output node. */
|
|
2397
|
+
CODE_BLOCK_SINGLE_OUTPUT: "TSRX2011",
|
|
2398
|
+
/** A statement after a `@{ … }` body's output. */
|
|
2399
|
+
CODE_BLOCK_STATEMENT_AFTER_OUTPUT: "TSRX2012",
|
|
2400
|
+
/** A JSX spread child (`{...items}`), which parses but no target supports. */
|
|
2401
|
+
JSX_SPREAD_CHILD: "TSRX2013",
|
|
2402
|
+
/** A dynamic tag expression (`<{expr}>`) other than an identifier, a member access, or a string literal. */
|
|
2403
|
+
DYNAMIC_TAG_EXPRESSION: "TSRX2014",
|
|
2404
|
+
/** An `@for` over something other than `for…of`. */
|
|
2405
|
+
FOR_OF_ONLY: "TSRX2015",
|
|
2406
|
+
/** An element with more than one `ref` attribute. */
|
|
2407
|
+
MULTIPLE_REFS: "TSRX2016",
|
|
2408
|
+
/** An element nested where HTML doesn't allow it (`<p>` in `<p>`). */
|
|
2409
|
+
INVALID_HTML_NESTING: "TSRX2017",
|
|
2410
|
+
/** A JavaScript `try…finally` in a template. */
|
|
2411
|
+
TEMPLATE_TRY_FINALLY: "TSRX2018",
|
|
2412
|
+
/** A `try` in a template with neither a `pending` nor a `catch` block. */
|
|
2413
|
+
TEMPLATE_TRY_HANDLER: "TSRX2019",
|
|
2414
|
+
/** An `@pending` block on a target without it. */
|
|
2415
|
+
TARGET_PENDING_UNSUPPORTED: "TSRX2020",
|
|
2416
|
+
/** An `await` where the target can't await (in a callback, or in its components). */
|
|
2417
|
+
TARGET_AWAIT_UNSUPPORTED: "TSRX2021",
|
|
2418
|
+
/** A `yield` in a part of the template that runs in a callback. */
|
|
2419
|
+
TARGET_YIELD_UNSUPPORTED: "TSRX2022",
|
|
2420
|
+
/** A `super` in a part of the template that's lowered into a generator function. */
|
|
2421
|
+
TARGET_SUPER_UNSUPPORTED: "TSRX2023",
|
|
2422
|
+
/** A `for await…of` in a template on a target without it. */
|
|
2423
|
+
TARGET_FOR_AWAIT_UNSUPPORTED: "TSRX2024",
|
|
2424
|
+
/** A top-level `await` in a TSRX function without a module-level `"use server"`. */
|
|
2425
|
+
TOP_LEVEL_AWAIT_USE_SERVER: "TSRX2025",
|
|
2426
|
+
/** An `@catch` reset parameter on a target whose error boundary has none. */
|
|
2427
|
+
TARGET_CATCH_RESET_UNSUPPORTED: "TSRX2026",
|
|
2428
|
+
/** An `@for` head that assigns to existing variables, or declares its loop variable with `var` or `using`. */
|
|
2429
|
+
FOR_HEAD: "TSRX2027",
|
|
2430
|
+
/** A component read above its declaration on a target that compiles it to a constant, where moving the declaration up would change what it reads. */
|
|
2431
|
+
TARGET_COMPONENT_READ_BEFORE_DECLARATION: "TSRX2028",
|
|
2432
|
+
/** A destructured prop default the target's component compiler drops. */
|
|
2433
|
+
TARGET_PROP_DEFAULT_UNSUPPORTED: "TSRX2029",
|
|
2434
|
+
/** A `var` declared in a nested `@{ … }` block or an `@if`, `@for`, `@switch` or `@try` body, outside any function or class in it. */
|
|
2435
|
+
DIRECTIVE_BODY_VAR: "TSRX2030",
|
|
2436
|
+
/** A destructured prop key the target's component compiler reads as another prop, or leaves out. */
|
|
2437
|
+
TARGET_PROP_KEY_UNSUPPORTED: "TSRX2031",
|
|
2438
|
+
/** A binding of a name the target's toolchain finds by name: Vue's `defineVaporComponent`. */
|
|
2439
|
+
TARGET_RESERVED_NAME: "TSRX2032",
|
|
2440
|
+
/** Type arguments after a dynamic tag (`<{Tag}<T> />`). */
|
|
2441
|
+
DYNAMIC_TAG_TYPE_ARGUMENTS: "TSRX2033",
|
|
2442
|
+
/** A `yield` in a template value the target's JSX compiler moves into an effect, getter or callback. */
|
|
2443
|
+
TARGET_TEMPLATE_YIELD_UNSUPPORTED: "TSRX2034",
|
|
2444
|
+
/**
|
|
2445
|
+
* A warning: a `@{ … }` with no output whose only statement is an
|
|
2446
|
+
* expression there only for its value, such as `<p>@{user.name}</p>`, so
|
|
2447
|
+
* it renders nothing.
|
|
2448
|
+
*/
|
|
2449
|
+
CODE_BLOCK_EXPRESSION_ONLY: "TSRX2035",
|
|
2450
|
+
/** `<style apply>` carries no expression value. */
|
|
2451
|
+
STYLE_APPLY_VALUE: "TSRX3001",
|
|
2452
|
+
/** An `apply` entry is not an identifier, member, or array of those, or does not resolve to a style block. */
|
|
2453
|
+
STYLE_APPLY_TARGET: "TSRX3002",
|
|
2454
|
+
/** An `apply` target is declared after the applying block in source order. */
|
|
2455
|
+
STYLE_APPLY_BEFORE_DECLARATION: "TSRX3003",
|
|
2456
|
+
/** Two `apply` attributes on one `<style>` block. */
|
|
2457
|
+
STYLE_APPLY_DUPLICATE: "TSRX3004",
|
|
2458
|
+
/** `apply` on a `<head>` style or a resource (`href`) style. */
|
|
2459
|
+
STYLE_APPLY_UNSUPPORTED_HOST: "TSRX3005",
|
|
2460
|
+
/** An assigned style block authors a `.$class` class selector. */
|
|
2461
|
+
STYLE_RESERVED_CLASS_KEY: "TSRX3006",
|
|
2462
|
+
/** A standalone `<style>` block at module scope. */
|
|
2463
|
+
STYLE_STANDALONE_AT_MODULE_SCOPE: "TSRX3007",
|
|
2464
|
+
/** A standalone `<style>` block with CSS text outside any `@{ … }` or control-flow body. */
|
|
2465
|
+
STYLE_STANDALONE_OUTSIDE_TEMPLATE: "TSRX3008",
|
|
2466
|
+
/** A standalone `<style>` block in a statement slot: the lone output of a `@{ … }` or control-flow body, or a statement. */
|
|
2467
|
+
STYLE_STANDALONE_NEEDS_FRAGMENT: "TSRX3009",
|
|
2468
|
+
/** A `<style>` attribute other than `ref` and `apply`. */
|
|
2469
|
+
STYLE_UNKNOWN_ATTRIBUTE: "TSRX3010",
|
|
2470
|
+
/** `:global` used where the scoping rules do not allow it. */
|
|
2471
|
+
CSS_GLOBAL_PLACEMENT: "TSRX3011",
|
|
2472
|
+
/** An `@import` rule in a `<style>` block, whose rules would not be scoped. */
|
|
2473
|
+
CSS_IMPORT: "TSRX3012",
|
|
2474
|
+
/** CSS in a `<style>` block that doesn't parse. */
|
|
2475
|
+
CSS_SYNTAX: "TSRX3013",
|
|
2476
|
+
/** A `<style>` block in a child expression container (`{<style>…</style>}`), which renders its class map. */
|
|
2477
|
+
STYLE_IN_EXPRESSION_CONTAINER: "TSRX3014",
|
|
2478
|
+
/** A recognized `import.meta.env.platform.*` flag is used without a selected platform. */
|
|
2479
|
+
PLATFORM_REQUIRED: "TSRX4001",
|
|
2480
|
+
/** A name that starts with the prefix the compiler reserves for its own identifiers. */
|
|
2481
|
+
RESERVED_IDENTIFIER_PREFIX: "TSRX4002",
|
|
2482
|
+
/** JavaScript syntax the parser rejects that TypeScript accepts (an escaped `import.meta`). */
|
|
2483
|
+
JAVASCRIPT_SYNTAX: "TSRX4003"
|
|
2484
|
+
};
|
|
2485
|
+
/**
|
|
2344
2486
|
* The source TSRX's errors are reported under, in capitals like TypeScript's
|
|
2345
2487
|
* own `TS2322`: `@tsrx/content-mapper` declares it to TypeScript 7, which shows
|
|
2346
2488
|
* its errors as `TSRX2002`, and the TSRX language server reports its compile
|
|
@@ -2404,18 +2546,35 @@ const NOT_CHILDREN = /* @__PURE__ */ new Set([
|
|
|
2404
2546
|
* @param {AST.Program | null} source_ast
|
|
2405
2547
|
* @param {readonly CodeMapping[]} mappings
|
|
2406
2548
|
* @param {number} original_length
|
|
2407
|
-
* @param {
|
|
2549
|
+
* @param {string} generated_text
|
|
2408
2550
|
* @returns {DiagnosticDirectives | null}
|
|
2409
2551
|
*/
|
|
2410
|
-
function ignore_directives(errors, source_ast, mappings, original_length,
|
|
2552
|
+
function ignore_directives(errors, source_ast, mappings, original_length, generated_text) {
|
|
2553
|
+
const generated_length = generated_text.length;
|
|
2411
2554
|
/** @type {Array<[original_start: number, original_end: number, generated_start: number, generated_end: number]>} */
|
|
2412
2555
|
const ranges = [];
|
|
2556
|
+
if (source_ast && errors.some((error) => error.code === DIAGNOSTIC_CODES.UNCLOSED_TAG && typeof error.pos === "number")) {
|
|
2557
|
+
/** @type {Set<number>} */
|
|
2558
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
2559
|
+
for (const opening of unclosed_openings(source_ast)) {
|
|
2560
|
+
const closing_tag = recovered_closing_tag(opening, mappings, generated_text, claimed);
|
|
2561
|
+
if (closing_tag) ranges.push([
|
|
2562
|
+
Math.min(opening.start, original_length),
|
|
2563
|
+
Math.min(opening.end, original_length),
|
|
2564
|
+
closing_tag[0],
|
|
2565
|
+
closing_tag[1]
|
|
2566
|
+
]);
|
|
2567
|
+
}
|
|
2568
|
+
}
|
|
2413
2569
|
for (const error of errors) {
|
|
2414
2570
|
if (typeof error.pos !== "number") continue;
|
|
2415
|
-
const
|
|
2571
|
+
const on_tag = reported_on_tag(error);
|
|
2572
|
+
const position = on_tag ? error.raisedAt - 1 : error.pos;
|
|
2573
|
+
const holder = source_ast ? holder_at(source_ast, position) : null;
|
|
2416
2574
|
const start = holder ? holder.start : error.pos;
|
|
2417
2575
|
const end = holder ? holder.end : typeof error.end === "number" && error.end > error.pos ? error.end : error.pos + 1;
|
|
2418
|
-
|
|
2576
|
+
const own_code = on_tag && !holder;
|
|
2577
|
+
for (const generated of generated_ranges(mappings, start, end, own_code)) ranges.push([
|
|
2419
2578
|
Math.min(start, original_length),
|
|
2420
2579
|
Math.min(end, original_length),
|
|
2421
2580
|
Math.min(generated[0], generated_length),
|
|
@@ -2446,6 +2605,113 @@ function ignore_directives(errors, source_ast, mappings, original_length, genera
|
|
|
2446
2605
|
};
|
|
2447
2606
|
}
|
|
2448
2607
|
/**
|
|
2608
|
+
* The generated range of the closing tag that recovery adds to an unclosed
|
|
2609
|
+
* element, or `null`. The React, Preact, Vue and Hono targets give it no
|
|
2610
|
+
* mapping, so TypeScript's error on it, such as TS2304 for a name that isn't a
|
|
2611
|
+
* value, has no place in the file and shows at its top as TS18080. An ignore
|
|
2612
|
+
* directive over just that tag holds no authored code, so it can't hide errors
|
|
2613
|
+
* in the code that follows. The tag is the first `</name>` from the opening
|
|
2614
|
+
* tag on that no other element has claimed and that no mapping ties to
|
|
2615
|
+
* authored code: the only mapping it may have is its name's to the opening
|
|
2616
|
+
* tag, as the Solid target maps it.
|
|
2617
|
+
* @param {{ start: number, end: number, name: unknown }} opening
|
|
2618
|
+
* @param {readonly CodeMapping[]} mappings
|
|
2619
|
+
* @param {string} generated_text
|
|
2620
|
+
* @param {Set<number>} claimed
|
|
2621
|
+
* @returns {[number, number] | null}
|
|
2622
|
+
*/
|
|
2623
|
+
function recovered_closing_tag(opening, mappings, generated_text, claimed) {
|
|
2624
|
+
const name = jsx_name(opening.name);
|
|
2625
|
+
if (!name) return null;
|
|
2626
|
+
/** @type {number | undefined} */
|
|
2627
|
+
let from;
|
|
2628
|
+
for (const mapping of mappings) for (let i = 0; i < mapping.sourceOffsets.length; i++) {
|
|
2629
|
+
const source = mapping.sourceOffsets[i];
|
|
2630
|
+
if (source >= opening.start && source < opening.end) from = Math.min(from ?? Infinity, mapping.generatedOffsets[i]);
|
|
2631
|
+
}
|
|
2632
|
+
if (from === void 0) return null;
|
|
2633
|
+
const text = `</${name}>`;
|
|
2634
|
+
for (let at = generated_text.indexOf(text, from); at !== -1; at = generated_text.indexOf(text, at + 1)) {
|
|
2635
|
+
const name_start = at + 2;
|
|
2636
|
+
if (claimed.has(at) || maps_authored_code(mappings, at, at + text.length, opening, name_start, name.length)) continue;
|
|
2637
|
+
claimed.add(at);
|
|
2638
|
+
return [at, at + text.length];
|
|
2639
|
+
}
|
|
2640
|
+
return null;
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* The opening tags of the elements the parser found unclosed, outermost first.
|
|
2644
|
+
* @param {AST.Node} node
|
|
2645
|
+
* @returns {Array<{ start: number, end: number, name: unknown }>}
|
|
2646
|
+
*/
|
|
2647
|
+
function unclosed_openings(node) {
|
|
2648
|
+
/** @type {Array<{ start: number, end: number, name: unknown }>} */
|
|
2649
|
+
const openings = [];
|
|
2650
|
+
/** @param {AST.Node} current */
|
|
2651
|
+
const visit = (current) => {
|
|
2652
|
+
const element = current;
|
|
2653
|
+
if (element.type === "JSXElement" && element.unclosed && typeof element.openingElement?.start === "number" && typeof element.openingElement?.end === "number") openings.push(element.openingElement);
|
|
2654
|
+
for (const [key, value] of Object.entries(current)) {
|
|
2655
|
+
if (NOT_CHILDREN.has(key) || !value || typeof value !== "object") continue;
|
|
2656
|
+
for (const child of Array.isArray(value) ? value : [value]) if (child && typeof child.type === "string") visit(child);
|
|
2657
|
+
}
|
|
2658
|
+
};
|
|
2659
|
+
visit(node);
|
|
2660
|
+
return openings;
|
|
2661
|
+
}
|
|
2662
|
+
/**
|
|
2663
|
+
* The text of a JSX tag name, as the recovered closing tag prints it.
|
|
2664
|
+
* @param {any} name
|
|
2665
|
+
* @returns {string | null}
|
|
2666
|
+
*/
|
|
2667
|
+
function jsx_name(name) {
|
|
2668
|
+
if (!name) return null;
|
|
2669
|
+
if (name.type === "JSXIdentifier" || name.type === "Identifier") return name.name;
|
|
2670
|
+
if (name.type === "JSXMemberExpression") {
|
|
2671
|
+
const object = jsx_name(name.object);
|
|
2672
|
+
return object ? `${object}.${name.property.name}` : null;
|
|
2673
|
+
}
|
|
2674
|
+
if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
|
|
2675
|
+
return null;
|
|
2676
|
+
}
|
|
2677
|
+
/**
|
|
2678
|
+
* Whether a mapping ties generated code in `[start, end)` to authored code:
|
|
2679
|
+
* any mapping there but one of just the name, at `name_start`, to the opening
|
|
2680
|
+
* tag, as the Solid target maps a recovered closing tag's name.
|
|
2681
|
+
* @param {readonly CodeMapping[]} mappings
|
|
2682
|
+
* @param {number} start
|
|
2683
|
+
* @param {number} end
|
|
2684
|
+
* @param {{ start: number, end: number }} opening
|
|
2685
|
+
* @param {number} name_start
|
|
2686
|
+
* @param {number} name_length
|
|
2687
|
+
* @returns {boolean}
|
|
2688
|
+
*/
|
|
2689
|
+
function maps_authored_code(mappings, start, end, opening, name_start, name_length) {
|
|
2690
|
+
return mappings.some((mapping) => mapping.generatedOffsets.some((offset, i) => {
|
|
2691
|
+
const length = mapping.generatedLengths?.[i] ?? mapping.lengths[i];
|
|
2692
|
+
if (offset >= end || start >= offset + length) return false;
|
|
2693
|
+
const source = mapping.sourceOffsets[i];
|
|
2694
|
+
return !(offset >= name_start && offset + length <= name_start + name_length && source >= opening.start && source + mapping.lengths[i] <= opening.end);
|
|
2695
|
+
}));
|
|
2696
|
+
}
|
|
2697
|
+
/**
|
|
2698
|
+
* Whether `error` is an unclosed tag reported on the tag and found where the
|
|
2699
|
+
* parser stopped, one before `raisedAt`: the end of its template, or the start
|
|
2700
|
+
* of a `<style>` or `<script>` body. Its directive holds there, not at the error:
|
|
2701
|
+
* the closing tag that recovery adds has no mapping, so TypeScript's error on it
|
|
2702
|
+
* could only show at the top of the file, and the tag's own statement can end
|
|
2703
|
+
* before it. At the end of the file nothing holds that place, as the tag's
|
|
2704
|
+
* declaration has stretched around the code that follows, which the directive
|
|
2705
|
+
* must not hide. A compiler from before tags were reported on themselves has its
|
|
2706
|
+
* error where the parser stopped already, with `raisedAt` no further than `end`,
|
|
2707
|
+
* and the mapper runs whichever compiler the project has.
|
|
2708
|
+
* @param {CompileError} error
|
|
2709
|
+
* @returns {boolean}
|
|
2710
|
+
*/
|
|
2711
|
+
function reported_on_tag({ code, end, raisedAt }) {
|
|
2712
|
+
return code === DIAGNOSTIC_CODES.UNCLOSED_TAG && typeof raisedAt === "number" && typeof end === "number" && raisedAt > end;
|
|
2713
|
+
}
|
|
2714
|
+
/**
|
|
2449
2715
|
* The innermost holder node (see {@link HOLDER_TYPE}) that contains `position`,
|
|
2450
2716
|
* found by descending from the root through the child that contains it.
|
|
2451
2717
|
* @param {AST.Node} root
|
|
@@ -2488,9 +2754,13 @@ function child_at(node, position) {
|
|
|
2488
2754
|
* @param {readonly CodeMapping[]} mappings
|
|
2489
2755
|
* @param {number} start
|
|
2490
2756
|
* @param {number} end
|
|
2757
|
+
* @param {boolean} [own_code] Count only segments inside the range, and no
|
|
2758
|
+
* error's anchor. A segment that encloses the range doesn't count: a mapping of
|
|
2759
|
+
* the whole file can have the same length on both sides by chance. An anchor
|
|
2760
|
+
* still ends a range, as any code from outside the node does.
|
|
2491
2761
|
* @returns {Array<[number, number]>}
|
|
2492
2762
|
*/
|
|
2493
|
-
function generated_ranges(mappings, start, end) {
|
|
2763
|
+
function generated_ranges(mappings, start, end, own_code = false) {
|
|
2494
2764
|
/** @type {Array<[generated_start: number, generated_end: number, inside: boolean]>} */
|
|
2495
2765
|
const segments = [];
|
|
2496
2766
|
for (const mapping of mappings) for (let i = 0; i < mapping.sourceOffsets.length; i++) {
|
|
@@ -2498,12 +2768,17 @@ function generated_ranges(mappings, start, end) {
|
|
|
2498
2768
|
const length = mapping.lengths[i];
|
|
2499
2769
|
const generated = mapping.generatedOffsets[i];
|
|
2500
2770
|
const generated_length = mapping.generatedLengths?.[i] ?? length;
|
|
2501
|
-
if (
|
|
2771
|
+
if (own_code && mapping.data?.customData?.diagnosticAnchor) segments.push([
|
|
2772
|
+
generated,
|
|
2773
|
+
generated + generated_length,
|
|
2774
|
+
false
|
|
2775
|
+
]);
|
|
2776
|
+
else if (source >= start && source + length <= end) segments.push([
|
|
2502
2777
|
generated,
|
|
2503
2778
|
generated + generated_length,
|
|
2504
2779
|
true
|
|
2505
2780
|
]);
|
|
2506
|
-
else if (source <= start && end <= source + length && generated_length === length) segments.push([
|
|
2781
|
+
else if (!own_code && source <= start && end <= source + length && generated_length === length) segments.push([
|
|
2507
2782
|
generated + start - source,
|
|
2508
2783
|
generated + end - source,
|
|
2509
2784
|
true
|
|
@@ -2739,10 +3014,83 @@ function pattern_names(pattern) {
|
|
|
2739
3014
|
}
|
|
2740
3015
|
}
|
|
2741
3016
|
|
|
3017
|
+
//#endregion
|
|
3018
|
+
//#region src/platform-globals.js
|
|
3019
|
+
/** @import {CodeMapping} from '@tsrx/core/types' */
|
|
3020
|
+
/**
|
|
3021
|
+
* The editor-only `ImportMetaEnv` / `ImportMeta` globals that `withPlatformTypes`
|
|
3022
|
+
* in `@tsrx/core` appends to the generated TSX of a module that keeps a platform
|
|
3023
|
+
* flag, exactly as it prints them, with the three flag value types captured.
|
|
3024
|
+
*/
|
|
3025
|
+
const PLATFORM_GLOBALS = /\ndeclare global \{\n\tinterface ImportMetaEnv \{\n\t\treadonly platform: string & \{\n\t\t\treadonly web: (true|false|boolean);\n\t\t\treadonly ios: (true|false|boolean);\n\t\t\treadonly android: (true|false|boolean);\n\t\t\};\n\t\}\n\tinterface ImportMeta \{\n\t\treadonly \[key: `env\$\{string\}`\]: ImportMetaEnv;\n\t\}\n\}\n/;
|
|
3026
|
+
/** The `platform-globals/` file that declares each set of flag value types. */
|
|
3027
|
+
const PLATFORM_FILES = /* @__PURE__ */ new Map([
|
|
3028
|
+
["true,false,false", "web"],
|
|
3029
|
+
["false,true,false", "ios"],
|
|
3030
|
+
["false,false,true", "android"],
|
|
3031
|
+
["boolean,boolean,boolean", "unconfigured"]
|
|
3032
|
+
]);
|
|
3033
|
+
/** @type {Map<string, string | null>} */
|
|
3034
|
+
const files = /* @__PURE__ */ new Map();
|
|
3035
|
+
/**
|
|
3036
|
+
* Replace a module's copy of the platform globals in its generated TSX with a
|
|
3037
|
+
* reference to the shared file that declares the same globals. Native
|
|
3038
|
+
* TypeScript merges every module's copy into one `ImportMeta` and reports the
|
|
3039
|
+
* repeated index signature (TS2374), and declaration output keeps every
|
|
3040
|
+
* `declare global` block. The shared file is in the program once, and
|
|
3041
|
+
* declaration output drops a `/// <reference path>` directive.
|
|
3042
|
+
*
|
|
3043
|
+
* The copy is blanked in place, and the directive goes at the start of the
|
|
3044
|
+
* text, where TypeScript reads it, so every generated offset moves by its
|
|
3045
|
+
* length. A directive is not an import: organize imports and auto-import still
|
|
3046
|
+
* see only the module's own imports, and TypeScript 7 maps their edits only
|
|
3047
|
+
* when the last of them is the module's last import. Text without the exact
|
|
3048
|
+
* copy (another TSRX version, a third-party compiler), or that starts with a
|
|
3049
|
+
* hashbang, comes back unchanged.
|
|
3050
|
+
* @param {string} text
|
|
3051
|
+
* @param {readonly CodeMapping[]} mappings
|
|
3052
|
+
* @returns {{ text: string, mappings: readonly CodeMapping[] }}
|
|
3053
|
+
*/
|
|
3054
|
+
function share_platform_globals(text, mappings) {
|
|
3055
|
+
const match = PLATFORM_GLOBALS.exec(text);
|
|
3056
|
+
const name = match && PLATFORM_FILES.get(match.slice(1).join(","));
|
|
3057
|
+
const file = name && !text.startsWith("#!") ? platform_globals_file(name) : null;
|
|
3058
|
+
if (!match || !file) return {
|
|
3059
|
+
text,
|
|
3060
|
+
mappings
|
|
3061
|
+
};
|
|
3062
|
+
const directive = `/// <reference path="${file}" />\n`;
|
|
3063
|
+
const end = match.index + match[0].length;
|
|
3064
|
+
return {
|
|
3065
|
+
text: `${directive}${text.slice(0, match.index)}${match[0].replace(/[^\n]/g, " ")}${text.slice(end)}`,
|
|
3066
|
+
mappings: mappings.map((mapping) => ({
|
|
3067
|
+
...mapping,
|
|
3068
|
+
generatedOffsets: mapping.generatedOffsets.map((offset) => offset + directive.length)
|
|
3069
|
+
}))
|
|
3070
|
+
};
|
|
3071
|
+
}
|
|
3072
|
+
/**
|
|
3073
|
+
* The path of a `platform-globals/` file, with forward slashes. The directory
|
|
3074
|
+
* sits next to `src/` and `dist/`, so one relative URL serves both. A file
|
|
3075
|
+
* missing from the installed package, or a path a directive cannot quote,
|
|
3076
|
+
* leaves the copy in place.
|
|
3077
|
+
* @param {string} name
|
|
3078
|
+
* @returns {string | null}
|
|
3079
|
+
*/
|
|
3080
|
+
function platform_globals_file(name) {
|
|
3081
|
+
let file = files.get(name);
|
|
3082
|
+
if (file === void 0) {
|
|
3083
|
+
const path = fileURLToPath(new URL(`../platform-globals/${name}.d.cts`, import.meta.url));
|
|
3084
|
+
file = fs.existsSync(path) && !/["\r\n]/.test(path) ? path.replace(/\\/g, "/") : null;
|
|
3085
|
+
files.set(name, file);
|
|
3086
|
+
}
|
|
3087
|
+
return file;
|
|
3088
|
+
}
|
|
3089
|
+
|
|
2742
3090
|
//#endregion
|
|
2743
3091
|
//#region src/span-mappings.js
|
|
2744
3092
|
/** @import {CodeMapping} from '@tsrx/core/types' */
|
|
2745
|
-
/** @import {SpanMapping} from './protocol.js' */
|
|
3093
|
+
/** @import {SpanMapping, VerificationMapping} from './protocol.js' */
|
|
2746
3094
|
const semantic_features = SpanMapFeature.Hover | SpanMapFeature.SignatureHelp | SpanMapFeature.InlayHints | SpanMapFeature.SemanticTokens;
|
|
2747
3095
|
const completion_features = SpanMapFeature.Completion | SpanMapFeature.AutoInsert;
|
|
2748
3096
|
const navigation_features = SpanMapFeature.Definition | SpanMapFeature.TypeDefinition | SpanMapFeature.Implementation | SpanMapFeature.References | SpanMapFeature.DocumentHighlights | SpanMapFeature.Rename | SpanMapFeature.CallHierarchy | SpanMapFeature.CodeActions | SpanMapFeature.LinkedEditing;
|
|
@@ -2756,6 +3104,9 @@ const identifier_pattern = /^[\p{ID_Start}_$][\p{ID_Continue}$]*$/u;
|
|
|
2756
3104
|
* @property {number} originalEnd
|
|
2757
3105
|
* @property {0 | 1 | 2} kind
|
|
2758
3106
|
* @property {number} features
|
|
3107
|
+
* @property {boolean} [positionOnly] From a mapping with no Volar feature, not even
|
|
3108
|
+
* `verification`: it only places diagnostics, so coalescing may join it to
|
|
3109
|
+
* its neighbours like an unmapped gap.
|
|
2759
3110
|
*/
|
|
2760
3111
|
/**
|
|
2761
3112
|
* Translate a Volar `CodeMapping['data']` into content-mapper feature bits.
|
|
@@ -2813,6 +3164,11 @@ function features_from_mapping_data(data) {
|
|
|
2813
3164
|
* `import { a } from './x';` is one edit-safe span rather than four tokens
|
|
2814
3165
|
* with unmapped punctuation between them. TypeScript only applies text edits
|
|
2815
3166
|
* (auto-import, rename, code actions) that fit inside a single Verbatim span.
|
|
3167
|
+
* A dropped candidate then keeps its uncovered edges (see
|
|
3168
|
+
* {@link map_dropped_edges}), so a diagnostic over a whole container starts and
|
|
3169
|
+
* ends where the container does in the original. Last, identical text left
|
|
3170
|
+
* between spans with different feature bits maps without features (see
|
|
3171
|
+
* {@link map_identical_gaps}).
|
|
2816
3172
|
* @param {readonly CodeMapping[]} mappings
|
|
2817
3173
|
* @param {string} generated_text
|
|
2818
3174
|
* @param {string} original_text
|
|
@@ -2848,20 +3204,21 @@ function to_span_mappings(mappings, generated_text, original_text, options = {})
|
|
|
2848
3204
|
originalStart: original_start,
|
|
2849
3205
|
originalEnd: original_end,
|
|
2850
3206
|
kind,
|
|
2851
|
-
features: language_features ? features_from_mapping_data(mapping.data) : SpanMapFeature.None
|
|
3207
|
+
features: language_features ? features_from_mapping_data(mapping.data) : SpanMapFeature.None,
|
|
3208
|
+
positionOnly: is_position_only(mapping.data)
|
|
2852
3209
|
});
|
|
2853
3210
|
}
|
|
2854
3211
|
}
|
|
2855
3212
|
candidates.sort((left, right) => Number(left.kind !== SpanMapKind.Verbatim) - Number(right.kind !== SpanMapKind.Verbatim) || left.generatedEnd - left.generatedStart - (right.generatedEnd - right.generatedStart) || left.generatedStart - right.generatedStart || left.originalStart - right.originalStart);
|
|
2856
3213
|
/** @type {Candidate[]} */
|
|
2857
3214
|
const selected = [];
|
|
3215
|
+
/** @type {Candidate[]} */
|
|
3216
|
+
const dropped = [];
|
|
2858
3217
|
const occupied = new IntervalSet();
|
|
2859
|
-
for (const candidate of candidates)
|
|
2860
|
-
|
|
2861
|
-
selected.push(candidate);
|
|
2862
|
-
}
|
|
3218
|
+
for (const candidate of candidates) if (occupied.add(candidate.generatedStart, candidate.generatedEnd)) selected.push(candidate);
|
|
3219
|
+
else dropped.push(candidate);
|
|
2863
3220
|
selected.sort((left, right) => left.generatedStart - right.generatedStart);
|
|
2864
|
-
return extend_verbatim_spans_over_whitespace(coalesce_verbatim_spans(selected, generated_text, original_text), generated_text, original_text).map((candidate) => [
|
|
3221
|
+
return map_identical_gaps(map_dropped_edges(extend_verbatim_spans_over_whitespace(coalesce_verbatim_spans(selected, generated_text, original_text), generated_text, original_text), dropped, generated_text, original_text), generated_text, original_text).map((candidate) => [
|
|
2865
3222
|
candidate.generatedStart,
|
|
2866
3223
|
candidate.generatedEnd - candidate.generatedStart,
|
|
2867
3224
|
candidate.originalStart,
|
|
@@ -2871,11 +3228,54 @@ function to_span_mappings(mappings, generated_text, original_text, options = {})
|
|
|
2871
3228
|
]);
|
|
2872
3229
|
}
|
|
2873
3230
|
/**
|
|
3231
|
+
* Whether a mapping has no Volar feature at all, not even `verification`. The
|
|
3232
|
+
* transform emits such mappings only to give TypeScript 7 a place for
|
|
3233
|
+
* diagnostics (the brackets of a respaced literal); Volar ignores them.
|
|
3234
|
+
* @param {CodeMapping['data'] | undefined} data
|
|
3235
|
+
* @returns {boolean}
|
|
3236
|
+
*/
|
|
3237
|
+
function is_position_only(data) {
|
|
3238
|
+
return !!data && typeof data === "object" && !data.verification && !data.completion && !data.semantic && !data.navigation && !data.structure && !data.format;
|
|
3239
|
+
}
|
|
3240
|
+
/**
|
|
3241
|
+
* The transform's own mappings for diagnostics, for command-line checkers (the
|
|
3242
|
+
* `verificationMappings` mapper option): every mapping whose `data.verification`
|
|
3243
|
+
* is set, in the transform's order, with its offsets and lengths as emitted.
|
|
3244
|
+
* This is what `tsrx-tsc` hands Volar, so Volar's `SourceMap#toSourceRange`
|
|
3245
|
+
* (with `fallbackToAnyMatch`) over it maps a diagnostic exactly as `tsrx-tsc`
|
|
3246
|
+
* does. The grouping matters: Volar maps a range through one mapping that holds
|
|
3247
|
+
* both of its ends, such as the start and end points of a literal, and tries
|
|
3248
|
+
* mappings in this order, narrower first. When any mapping's verification is a
|
|
3249
|
+
* `shouldReport` callback, the result is empty: the callback depends on the
|
|
3250
|
+
* diagnostic and doesn't serialize, and leaving out only that mapping could
|
|
3251
|
+
* let an overlapping one map a diagnostic somewhere else.
|
|
3252
|
+
* @param {readonly CodeMapping[]} mappings
|
|
3253
|
+
* @returns {VerificationMapping[]}
|
|
3254
|
+
*/
|
|
3255
|
+
function to_verification_mappings(mappings) {
|
|
3256
|
+
/** @type {VerificationMapping[]} */
|
|
3257
|
+
const result = [];
|
|
3258
|
+
for (const mapping of mappings) {
|
|
3259
|
+
const verification = mapping.data?.verification;
|
|
3260
|
+
if (!verification) continue;
|
|
3261
|
+
if (typeof verification === "object" && typeof verification.shouldReport === "function") return [];
|
|
3262
|
+
result.push({
|
|
3263
|
+
sourceOffsets: [...mapping.sourceOffsets],
|
|
3264
|
+
generatedOffsets: [...mapping.generatedOffsets],
|
|
3265
|
+
lengths: [...mapping.lengths],
|
|
3266
|
+
generatedLengths: [...mapping.generatedLengths ?? mapping.lengths],
|
|
3267
|
+
data: { verification: true }
|
|
3268
|
+
});
|
|
3269
|
+
}
|
|
3270
|
+
return result;
|
|
3271
|
+
}
|
|
3272
|
+
/**
|
|
2874
3273
|
* Merge runs of Verbatim spans, sorted by generated start, when the two spans
|
|
2875
3274
|
* carry the same feature bits and the text between them is identical (and of
|
|
2876
3275
|
* equal length) in the generated and the original file. The merged span is
|
|
2877
3276
|
* Verbatim by construction: every position inside it maps with the same
|
|
2878
|
-
* offset and the same text.
|
|
3277
|
+
* offset and the same text. A position-only span joins a neighbour whatever
|
|
3278
|
+
* its features, and takes them, as the unmapped text it covers would.
|
|
2879
3279
|
* @param {Candidate[]} spans
|
|
2880
3280
|
* @param {string} generated_text
|
|
2881
3281
|
* @param {string} original_text
|
|
@@ -2886,9 +3286,13 @@ function coalesce_verbatim_spans(spans, generated_text, original_text) {
|
|
|
2886
3286
|
const merged = [];
|
|
2887
3287
|
for (const span of spans) {
|
|
2888
3288
|
const previous = merged[merged.length - 1];
|
|
2889
|
-
if (previous && previous.kind === SpanMapKind.Verbatim && span.kind === SpanMapKind.Verbatim && previous.features === span.features && span.originalStart >= previous.originalEnd && span.generatedStart - previous.generatedEnd === span.originalStart - previous.originalEnd && generated_text.slice(previous.generatedEnd, span.generatedStart) === original_text.slice(previous.originalEnd, span.originalStart)) {
|
|
3289
|
+
if (previous && previous.kind === SpanMapKind.Verbatim && span.kind === SpanMapKind.Verbatim && (previous.features === span.features || previous.positionOnly || span.positionOnly) && span.originalStart >= previous.originalEnd && span.generatedStart - previous.generatedEnd === span.originalStart - previous.originalEnd && generated_text.slice(previous.generatedEnd, span.generatedStart) === original_text.slice(previous.originalEnd, span.originalStart)) {
|
|
2890
3290
|
previous.generatedEnd = span.generatedEnd;
|
|
2891
3291
|
previous.originalEnd = span.originalEnd;
|
|
3292
|
+
if (previous.positionOnly && !span.positionOnly) {
|
|
3293
|
+
previous.features = span.features;
|
|
3294
|
+
previous.positionOnly = false;
|
|
3295
|
+
}
|
|
2892
3296
|
continue;
|
|
2893
3297
|
}
|
|
2894
3298
|
merged.push({ ...span });
|
|
@@ -2924,6 +3328,219 @@ function extend_verbatim_spans_over_whitespace(spans, generated_text, original_t
|
|
|
2924
3328
|
}
|
|
2925
3329
|
return spans;
|
|
2926
3330
|
}
|
|
3331
|
+
/**
|
|
3332
|
+
* Map the uncovered edges of each candidate that lost to the spans it
|
|
3333
|
+
* overlaps: the part before its first span and the part after its last.
|
|
3334
|
+
*
|
|
3335
|
+
* A container (a whole import type, a statement) encloses the token spans that
|
|
3336
|
+
* beat it, and TypeScript reports some diagnostics over the whole container.
|
|
3337
|
+
* When the printed text differs from the source (`import( './x' )` printed
|
|
3338
|
+
* as `import('./x')`), the container's start lies in unmapped text, and
|
|
3339
|
+
* TypeScript would report the diagnostic at the nearest earlier mapped
|
|
3340
|
+
* position instead. An edge runs from the container's boundary to the
|
|
3341
|
+
* neighbouring span, in both files, so it maps only when that span's original
|
|
3342
|
+
* range starts (or ends) inside the container's. Each edge is split by
|
|
3343
|
+
* {@link split_edge} so that a node inside it without a span of its own keeps
|
|
3344
|
+
* its position. Edges carry no feature bits: they place diagnostics and change
|
|
3345
|
+
* no editor feature. Gaps between a container's spans get no edge;
|
|
3346
|
+
* {@link map_identical_gaps} maps the ones whose text is identical.
|
|
3347
|
+
* Candidates are visited shortest first, so a nested container's edge bounds
|
|
3348
|
+
* the edge of the one around it.
|
|
3349
|
+
* @param {Candidate[]} spans Sorted by generated start, non-overlapping.
|
|
3350
|
+
* @param {readonly Candidate[]} dropped
|
|
3351
|
+
* @param {string} generated_text
|
|
3352
|
+
* @param {string} original_text
|
|
3353
|
+
* @returns {Candidate[]}
|
|
3354
|
+
*/
|
|
3355
|
+
function map_dropped_edges(spans, dropped, generated_text, original_text) {
|
|
3356
|
+
const result = spans.slice();
|
|
3357
|
+
/**
|
|
3358
|
+
* @param {Candidate | null} edge
|
|
3359
|
+
* @param {boolean} trailing
|
|
3360
|
+
*/
|
|
3361
|
+
const add = (edge, trailing) => {
|
|
3362
|
+
if (!edge) return;
|
|
3363
|
+
for (const span of split_edge(edge, generated_text, original_text, trailing)) {
|
|
3364
|
+
if (span.kind === SpanMapKind.Atom && is_partial_identifier(original_text, span.originalStart, span.originalEnd)) continue;
|
|
3365
|
+
result.splice(last_span_at_or_before(result, span.generatedStart) + 1, 0, span);
|
|
3366
|
+
}
|
|
3367
|
+
};
|
|
3368
|
+
for (const candidate of dropped) {
|
|
3369
|
+
add(leading_edge(result, candidate), false);
|
|
3370
|
+
add(trailing_edge(result, candidate), true);
|
|
3371
|
+
}
|
|
3372
|
+
return result;
|
|
3373
|
+
}
|
|
3374
|
+
/**
|
|
3375
|
+
* @param {readonly Candidate[]} spans
|
|
3376
|
+
* @param {Candidate} candidate
|
|
3377
|
+
* @returns {Candidate | null}
|
|
3378
|
+
*/
|
|
3379
|
+
function leading_edge(spans, candidate) {
|
|
3380
|
+
const index = last_span_at_or_before(spans, candidate.generatedStart);
|
|
3381
|
+
if (index >= 0 && spans[index].generatedEnd > candidate.generatedStart) return null;
|
|
3382
|
+
const next = spans[index + 1];
|
|
3383
|
+
if (!next || next.generatedStart >= candidate.generatedEnd || next.originalStart <= candidate.originalStart || next.originalStart > candidate.originalEnd) return null;
|
|
3384
|
+
return edge(candidate.generatedStart, next.generatedStart, candidate.originalStart, next.originalStart, SpanMapKind.Atom);
|
|
3385
|
+
}
|
|
3386
|
+
/**
|
|
3387
|
+
* @param {readonly Candidate[]} spans
|
|
3388
|
+
* @param {Candidate} candidate
|
|
3389
|
+
* @returns {Candidate | null}
|
|
3390
|
+
*/
|
|
3391
|
+
function trailing_edge(spans, candidate) {
|
|
3392
|
+
const previous = spans[last_span_at_or_before(spans, candidate.generatedEnd - 1)];
|
|
3393
|
+
if (!previous || previous.generatedEnd >= candidate.generatedEnd || previous.generatedEnd <= candidate.generatedStart || previous.originalEnd < candidate.originalStart || previous.originalEnd >= candidate.originalEnd) return null;
|
|
3394
|
+
return edge(previous.generatedEnd, candidate.generatedEnd, previous.originalEnd, candidate.originalEnd, SpanMapKind.Atom);
|
|
3395
|
+
}
|
|
3396
|
+
/**
|
|
3397
|
+
* Split an edge into `Verbatim` runs of the text it shares with the source,
|
|
3398
|
+
* matched from the container's boundary (the start of a leading edge, the end
|
|
3399
|
+
* of a trailing one) with whitespace that only one side has skipped, and one
|
|
3400
|
+
* `Atom` for whatever follows the first other difference. The printer mostly
|
|
3401
|
+
* changes spacing: in `Record< {}, 1 >`, printed `Record<{ }, 1>`, the `{` of
|
|
3402
|
+
* the `{}` that has no span of its own then maps to itself instead of to the
|
|
3403
|
+
* container's `<`. An edge whose boundary character differs stays one `Atom`,
|
|
3404
|
+
* and no run starts or ends inside an identifier.
|
|
3405
|
+
* @param {Candidate} whole
|
|
3406
|
+
* @param {string} generated_text
|
|
3407
|
+
* @param {string} original_text
|
|
3408
|
+
* @param {boolean} from_end
|
|
3409
|
+
* @returns {Candidate[]}
|
|
3410
|
+
*/
|
|
3411
|
+
function split_edge(whole, generated_text, original_text, from_end) {
|
|
3412
|
+
const generated = generated_text.slice(whole.generatedStart, whole.generatedEnd);
|
|
3413
|
+
const original = original_text.slice(whole.originalStart, whole.originalEnd);
|
|
3414
|
+
/**
|
|
3415
|
+
* The character `index` steps from the boundary.
|
|
3416
|
+
* @param {string} text
|
|
3417
|
+
* @param {number} index
|
|
3418
|
+
*/
|
|
3419
|
+
const at = (text, index) => text[from_end ? text.length - 1 - index : index];
|
|
3420
|
+
if (at(generated, 0) !== at(original, 0)) return [whole];
|
|
3421
|
+
/** Runs as `[generated step, original step, length]`, in steps from the boundary. */
|
|
3422
|
+
/** @type {Array<[number, number, number]>} */
|
|
3423
|
+
const runs = [];
|
|
3424
|
+
let i = 0;
|
|
3425
|
+
let j = 0;
|
|
3426
|
+
while (i < generated.length && j < original.length) {
|
|
3427
|
+
const g = at(generated, i);
|
|
3428
|
+
const o = at(original, j);
|
|
3429
|
+
if (g === o) {
|
|
3430
|
+
const run = runs.at(-1);
|
|
3431
|
+
if (run && run[0] + run[2] === i && run[1] + run[2] === j) run[2]++;
|
|
3432
|
+
else runs.push([
|
|
3433
|
+
i,
|
|
3434
|
+
j,
|
|
3435
|
+
1
|
|
3436
|
+
]);
|
|
3437
|
+
i++;
|
|
3438
|
+
j++;
|
|
3439
|
+
} else if (/\s/.test(g)) i++;
|
|
3440
|
+
else if (/\s/.test(o)) j++;
|
|
3441
|
+
else break;
|
|
3442
|
+
}
|
|
3443
|
+
/** @param {string} text @param {number} index */
|
|
3444
|
+
const splits = (text, index) => identifier_part.test(at(text, index - 1) ?? "") && identifier_part.test(at(text, index) ?? "");
|
|
3445
|
+
/** @type {Array<[number, number, number]>} */
|
|
3446
|
+
const kept = [];
|
|
3447
|
+
for (const [g, o, length] of runs) {
|
|
3448
|
+
if (splits(generated, g) || splits(original, o)) break;
|
|
3449
|
+
let end = length;
|
|
3450
|
+
while (end > 0 && (splits(generated, g + end) || splits(original, o + end))) end--;
|
|
3451
|
+
if (end > 0) kept.push([
|
|
3452
|
+
g,
|
|
3453
|
+
o,
|
|
3454
|
+
end
|
|
3455
|
+
]);
|
|
3456
|
+
if (end < length) break;
|
|
3457
|
+
}
|
|
3458
|
+
const last = kept.at(-1);
|
|
3459
|
+
if (!last) return [whole];
|
|
3460
|
+
i = last[0] + last[2];
|
|
3461
|
+
j = last[1] + last[2];
|
|
3462
|
+
/** @type {Candidate[]} */
|
|
3463
|
+
const spans = kept.map(([g, o, length]) => from_end ? edge_span(whole.generatedEnd - g - length, whole.originalEnd - o - length, length) : edge_span(whole.generatedStart + g, whole.originalStart + o, length));
|
|
3464
|
+
if (i < generated.length && j < original.length) spans.push(from_end ? edge(whole.generatedStart, whole.generatedEnd - i, whole.originalStart, whole.originalEnd - j, SpanMapKind.Atom) : edge(whole.generatedStart + i, whole.generatedEnd, whole.originalStart + j, whole.originalEnd, SpanMapKind.Atom));
|
|
3465
|
+
return spans;
|
|
3466
|
+
}
|
|
3467
|
+
/**
|
|
3468
|
+
* @param {number} generated_start
|
|
3469
|
+
* @param {number} original_start
|
|
3470
|
+
* @param {number} length
|
|
3471
|
+
* @returns {Candidate}
|
|
3472
|
+
*/
|
|
3473
|
+
function edge_span(generated_start, original_start, length) {
|
|
3474
|
+
return edge(generated_start, generated_start + length, original_start, original_start + length, SpanMapKind.Verbatim);
|
|
3475
|
+
}
|
|
3476
|
+
/**
|
|
3477
|
+
* @param {number} generated_start
|
|
3478
|
+
* @param {number} generated_end
|
|
3479
|
+
* @param {number} original_start
|
|
3480
|
+
* @param {number} original_end
|
|
3481
|
+
* @param {0 | 1 | 2} kind
|
|
3482
|
+
* @returns {Candidate}
|
|
3483
|
+
*/
|
|
3484
|
+
function edge(generated_start, generated_end, original_start, original_end, kind) {
|
|
3485
|
+
return {
|
|
3486
|
+
generatedStart: generated_start,
|
|
3487
|
+
generatedEnd: generated_end,
|
|
3488
|
+
originalStart: original_start,
|
|
3489
|
+
originalEnd: original_end,
|
|
3490
|
+
kind,
|
|
3491
|
+
features: SpanMapFeature.None
|
|
3492
|
+
};
|
|
3493
|
+
}
|
|
3494
|
+
/**
|
|
3495
|
+
* The index of the last span that starts at or before `position`, or -1.
|
|
3496
|
+
* @param {readonly Candidate[]} spans Sorted by generated start.
|
|
3497
|
+
* @param {number} position
|
|
3498
|
+
*/
|
|
3499
|
+
function last_span_at_or_before(spans, position) {
|
|
3500
|
+
let low = 0;
|
|
3501
|
+
let high = spans.length;
|
|
3502
|
+
while (low < high) {
|
|
3503
|
+
const middle = low + high >>> 1;
|
|
3504
|
+
if (spans[middle].generatedStart <= position) low = middle + 1;
|
|
3505
|
+
else high = middle;
|
|
3506
|
+
}
|
|
3507
|
+
return low - 1;
|
|
3508
|
+
}
|
|
3509
|
+
/**
|
|
3510
|
+
* Give the text that follows a Verbatim span, and is identical in the generated
|
|
3511
|
+
* and the original file up to the next span in either file, a Verbatim span of
|
|
3512
|
+
* its own with no feature bits. TypeScript 7 offers no auto-import completion at
|
|
3513
|
+
* the end of a span that unmapped text separates from the next span, such as at
|
|
3514
|
+
* `helperB` in `{helperB()}<Button />`: the `<` of a tag maps with other features
|
|
3515
|
+
* than `helperB`, so `()}` joins neither span (#1028). The text keeps no features
|
|
3516
|
+
* of its neighbours, as when it was unmapped.
|
|
3517
|
+
* @param {Candidate[]} spans Sorted by generated start, non-overlapping.
|
|
3518
|
+
* @param {string} generated_text
|
|
3519
|
+
* @param {string} original_text
|
|
3520
|
+
* @returns {Candidate[]}
|
|
3521
|
+
*/
|
|
3522
|
+
function map_identical_gaps(spans, generated_text, original_text) {
|
|
3523
|
+
/** @type {Candidate[]} */
|
|
3524
|
+
const result = [];
|
|
3525
|
+
for (let index = 0; index < spans.length; index++) {
|
|
3526
|
+
const span = spans[index];
|
|
3527
|
+
result.push(span);
|
|
3528
|
+
const next = spans[index + 1];
|
|
3529
|
+
if (span.kind !== SpanMapKind.Verbatim || !next) continue;
|
|
3530
|
+
let length = 0;
|
|
3531
|
+
while (span.generatedEnd + length < next.generatedStart && span.originalEnd + length < next.originalStart && generated_text[span.generatedEnd + length] === original_text[span.originalEnd + length]) length++;
|
|
3532
|
+
if (length === 0) continue;
|
|
3533
|
+
result.push({
|
|
3534
|
+
generatedStart: span.generatedEnd,
|
|
3535
|
+
generatedEnd: span.generatedEnd + length,
|
|
3536
|
+
originalStart: span.originalEnd,
|
|
3537
|
+
originalEnd: span.originalEnd + length,
|
|
3538
|
+
kind: SpanMapKind.Verbatim,
|
|
3539
|
+
features: SpanMapFeature.None
|
|
3540
|
+
});
|
|
3541
|
+
}
|
|
3542
|
+
return result;
|
|
3543
|
+
}
|
|
2927
3544
|
const identifier_part = /[\p{ID_Continue}$\u200c\u200d]/u;
|
|
2928
3545
|
const token_part = /[\p{ID_Continue}$#@\u200c\u200d]/u;
|
|
2929
3546
|
/**
|
|
@@ -3006,6 +3623,7 @@ const bare_package_specifier_pattern = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a
|
|
|
3006
3623
|
* @property {string} [compiler] Bare package specifier of the TSRX compiler. Takes precedence over `tsrx.compiler` in the tsconfig chain.
|
|
3007
3624
|
* @property {'web' | 'ios' | 'android'} [platform] Platform for `import.meta.env.platform` flags. Takes precedence over `tsrx.platform`.
|
|
3008
3625
|
* @property {boolean} [languageFeatures] `false` clears every editor feature bit while keeping spans for diagnostics (CLI-only projects).
|
|
3626
|
+
* @property {boolean} [verificationMappings] `true` adds `verificationMappings` to each transform result: the transform's own diagnostics mappings, in Volar's shape and order, for command-line checkers. Off by default; TypeScript ignores the field.
|
|
3009
3627
|
*/
|
|
3010
3628
|
/**
|
|
3011
3629
|
* @typedef {object} ProjectState
|
|
@@ -3117,16 +3735,19 @@ function create_tsrx_content_mapper(context = {}) {
|
|
|
3117
3735
|
const result = transform_tsrx(compiler, file_name, content, { platform });
|
|
3118
3736
|
if (result.fatalError) return failure(state, file_name, content, to_diagnostic(result.fatalError, content.length, MAPPER_CODES.COMPILE_ERROR));
|
|
3119
3737
|
if (result.sourceAst) state.lastGood.set(file_name, result.sourceAst);
|
|
3738
|
+
const generated = share_platform_globals(result.text, result.mappings);
|
|
3120
3739
|
const language_features = state.options.languageFeatures !== false;
|
|
3121
|
-
const mappings = to_span_mappings(
|
|
3122
|
-
const
|
|
3123
|
-
const
|
|
3740
|
+
const mappings = to_span_mappings(generated.mappings, generated.text, content, { languageFeatures: language_features });
|
|
3741
|
+
const errors = result.errors.filter((error) => error.severity !== "warning");
|
|
3742
|
+
const diagnostics = errors.map((error) => to_diagnostic(error, content.length, MAPPER_CODES.USAGE_ERROR));
|
|
3743
|
+
const diagnosticDirectives = ignore_directives(errors, result.sourceAst, generated.mappings, content.length, generated.text);
|
|
3124
3744
|
return {
|
|
3125
|
-
text:
|
|
3745
|
+
text: generated.text,
|
|
3126
3746
|
extension: ".tsx",
|
|
3127
3747
|
mappings,
|
|
3128
3748
|
...diagnostics.length > 0 ? { diagnostics } : null,
|
|
3129
|
-
...diagnosticDirectives ? { diagnosticDirectives } : null
|
|
3749
|
+
...diagnosticDirectives ? { diagnosticDirectives } : null,
|
|
3750
|
+
...state.options.verificationMappings ? { verificationMappings: to_verification_mappings(generated.mappings) } : null
|
|
3130
3751
|
};
|
|
3131
3752
|
}
|
|
3132
3753
|
/**
|
|
@@ -3220,7 +3841,8 @@ function create_tsrx_content_mapper(context = {}) {
|
|
|
3220
3841
|
text: build_export_stub(state.lastGood.get(file_name)),
|
|
3221
3842
|
extension: ".ts",
|
|
3222
3843
|
mappings: [],
|
|
3223
|
-
diagnostics: [clamp_diagnostic(diagnostic, content.length)]
|
|
3844
|
+
diagnostics: [clamp_diagnostic(diagnostic, content.length)],
|
|
3845
|
+
...state.options.verificationMappings ? { verificationMappings: [] } : null
|
|
3224
3846
|
};
|
|
3225
3847
|
}
|
|
3226
3848
|
return {
|
|
@@ -3275,10 +3897,11 @@ function validate_options(raw) {
|
|
|
3275
3897
|
});
|
|
3276
3898
|
break;
|
|
3277
3899
|
case "languageFeatures":
|
|
3278
|
-
|
|
3900
|
+
case "verificationMappings":
|
|
3901
|
+
if (typeof value === "boolean") options[key] = value;
|
|
3279
3902
|
else optionDiagnostics.push({
|
|
3280
3903
|
path: [key],
|
|
3281
|
-
messageText: "
|
|
3904
|
+
messageText: `"${key}" must be a boolean.`,
|
|
3282
3905
|
code: MAPPER_CODES.INVALID_CONFIG
|
|
3283
3906
|
});
|
|
3284
3907
|
break;
|
|
@@ -3363,4 +3986,4 @@ function nearest_package_manifest(entry) {
|
|
|
3363
3986
|
|
|
3364
3987
|
//#endregion
|
|
3365
3988
|
export { validate_options as i, require$1 as n, to_diagnostic as r, create_tsrx_content_mapper as t };
|
|
3366
|
-
//# sourceMappingURL=mapper-
|
|
3989
|
+
//# sourceMappingURL=mapper-SyX-87Od.js.map
|