@tsrx/content-mapper 0.1.3 → 0.1.5

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/dist/mapper.js CHANGED
@@ -1,3 +1,3 @@
1
- import { i as validate_options, n as require, r as to_diagnostic, t as create_tsrx_content_mapper } from "./mapper-Gc2yuGNf.js";
1
+ import { i as validate_options, n as require, r as to_diagnostic, t as create_tsrx_content_mapper } from "./mapper-CsFz-OPS.js";
2
2
 
3
3
  export { create_tsrx_content_mapper, require, to_diagnostic, validate_options };
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-Gc2yuGNf.js";
2
+ import { t as create_tsrx_content_mapper } from "./mapper-CsFz-OPS.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.3",
4
+ "version": "0.1.5",
5
5
  "license": "MIT",
6
6
  "author": "Dominic Gannaway",
7
7
  "type": "module",
@@ -45,17 +45,17 @@
45
45
  "@types/react": "^19.2.2",
46
46
  "preact": "^10.27.0",
47
47
  "react": "^19.2.0",
48
- "ripple": "^0.4.0",
48
+ "ripple": "^0.6.1",
49
49
  "solid-js": "2.0.0-rc.3",
50
50
  "tsdown": "^0.23.0",
51
51
  "typescript": "^5.9.3",
52
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"
53
+ "@tsrx/solid": "0.4.12",
54
+ "@tsrx/typescript-plugin": "0.7.12",
55
+ "@tsrx/core": "0.8.0",
56
+ "@tsrx/preact": "0.4.12",
57
+ "@tsrx/vue": "0.4.12",
58
+ "@tsrx/react": "0.5.12"
59
59
  },
60
60
  "publishConfig": {
61
61
  "access": "public"
@@ -41,12 +41,36 @@ const NOT_CHILDREN = new Set([
41
41
  * @param {AST.Program | null} source_ast
42
42
  * @param {readonly CodeMapping[]} mappings
43
43
  * @param {number} original_length
44
- * @param {number} generated_length
44
+ * @param {string} generated_text
45
45
  * @returns {DiagnosticDirectives | null}
46
46
  */
47
- export function ignore_directives(errors, source_ast, mappings, original_length, generated_length) {
47
+ export function ignore_directives(errors, source_ast, mappings, original_length, generated_text) {
48
+ const generated_length = generated_text.length;
48
49
  /** @type {Array<[original_start: number, original_end: number, generated_start: number, generated_end: number]>} */
49
50
  const ranges = [];
51
+ // The closing tags recovery adds, for every unclosed element, wherever the
52
+ // compiler reported its error: a compiler from before tags were reported on
53
+ // themselves reports them all where the parser stopped.
54
+ if (
55
+ source_ast &&
56
+ errors.some(
57
+ (error) => error.code === DIAGNOSTIC_CODES.UNCLOSED_TAG && typeof error.pos === 'number',
58
+ )
59
+ ) {
60
+ /** @type {Set<number>} */
61
+ const claimed = new Set();
62
+ for (const opening of unclosed_openings(source_ast)) {
63
+ const closing_tag = recovered_closing_tag(opening, mappings, generated_text, claimed);
64
+ if (closing_tag) {
65
+ ranges.push([
66
+ Math.min(opening.start, original_length),
67
+ Math.min(opening.end, original_length),
68
+ closing_tag[0],
69
+ closing_tag[1],
70
+ ]);
71
+ }
72
+ }
73
+ }
50
74
  for (const error of errors) {
51
75
  if (typeof error.pos !== 'number') {
52
76
  continue;
@@ -102,6 +126,129 @@ export function ignore_directives(errors, source_ast, mappings, original_length,
102
126
  };
103
127
  }
104
128
 
129
+ /**
130
+ * The generated range of the closing tag that recovery adds to an unclosed
131
+ * element, or `null`. The React, Preact, Vue and Hono targets give it no
132
+ * mapping, so TypeScript's error on it, such as TS2304 for a name that isn't a
133
+ * value, has no place in the file and shows at its top as TS18080. An ignore
134
+ * directive over just that tag holds no authored code, so it can't hide errors
135
+ * in the code that follows. The tag is the first `</name>` from the opening
136
+ * tag on that no other element has claimed and that no mapping ties to
137
+ * authored code: the only mapping it may have is its name's to the opening
138
+ * tag, as the Solid target maps it.
139
+ * @param {{ start: number, end: number, name: unknown }} opening
140
+ * @param {readonly CodeMapping[]} mappings
141
+ * @param {string} generated_text
142
+ * @param {Set<number>} claimed
143
+ * @returns {[number, number] | null}
144
+ */
145
+ function recovered_closing_tag(opening, mappings, generated_text, claimed) {
146
+ const name = jsx_name(opening.name);
147
+ if (!name) return null;
148
+ /** @type {number | undefined} */
149
+ let from;
150
+ for (const mapping of mappings) {
151
+ for (let i = 0; i < mapping.sourceOffsets.length; i++) {
152
+ const source = mapping.sourceOffsets[i];
153
+ if (source >= opening.start && source < opening.end) {
154
+ from = Math.min(from ?? Infinity, mapping.generatedOffsets[i]);
155
+ }
156
+ }
157
+ }
158
+ if (from === undefined) return null;
159
+ const text = `</${name}>`;
160
+ for (
161
+ let at = generated_text.indexOf(text, from);
162
+ at !== -1;
163
+ at = generated_text.indexOf(text, at + 1)
164
+ ) {
165
+ const name_start = at + '</'.length;
166
+ if (
167
+ claimed.has(at) ||
168
+ maps_authored_code(mappings, at, at + text.length, opening, name_start, name.length)
169
+ ) {
170
+ continue;
171
+ }
172
+ claimed.add(at);
173
+ return [at, at + text.length];
174
+ }
175
+ return null;
176
+ }
177
+
178
+ /**
179
+ * The opening tags of the elements the parser found unclosed, outermost first.
180
+ * @param {AST.Node} node
181
+ * @returns {Array<{ start: number, end: number, name: unknown }>}
182
+ */
183
+ function unclosed_openings(node) {
184
+ /** @type {Array<{ start: number, end: number, name: unknown }>} */
185
+ const openings = [];
186
+ /** @param {AST.Node} current */
187
+ const visit = (current) => {
188
+ const element = /** @type {any} */ (current);
189
+ if (
190
+ element.type === 'JSXElement' &&
191
+ element.unclosed &&
192
+ typeof element.openingElement?.start === 'number' &&
193
+ typeof element.openingElement?.end === 'number'
194
+ ) {
195
+ openings.push(element.openingElement);
196
+ }
197
+ for (const [key, value] of Object.entries(current)) {
198
+ if (NOT_CHILDREN.has(key) || !value || typeof value !== 'object') continue;
199
+ for (const child of Array.isArray(value) ? value : [value]) {
200
+ if (child && typeof child.type === 'string') visit(child);
201
+ }
202
+ }
203
+ };
204
+ visit(node);
205
+ return openings;
206
+ }
207
+
208
+ /**
209
+ * The text of a JSX tag name, as the recovered closing tag prints it.
210
+ * @param {any} name
211
+ * @returns {string | null}
212
+ */
213
+ function jsx_name(name) {
214
+ if (!name) return null;
215
+ if (name.type === 'JSXIdentifier' || name.type === 'Identifier') return name.name;
216
+ if (name.type === 'JSXMemberExpression') {
217
+ const object = jsx_name(name.object);
218
+ return object ? `${object}.${name.property.name}` : null;
219
+ }
220
+ if (name.type === 'JSXNamespacedName') return `${name.namespace.name}:${name.name.name}`;
221
+ return null;
222
+ }
223
+
224
+ /**
225
+ * Whether a mapping ties generated code in `[start, end)` to authored code:
226
+ * any mapping there but one of just the name, at `name_start`, to the opening
227
+ * tag, as the Solid target maps a recovered closing tag's name.
228
+ * @param {readonly CodeMapping[]} mappings
229
+ * @param {number} start
230
+ * @param {number} end
231
+ * @param {{ start: number, end: number }} opening
232
+ * @param {number} name_start
233
+ * @param {number} name_length
234
+ * @returns {boolean}
235
+ */
236
+ function maps_authored_code(mappings, start, end, opening, name_start, name_length) {
237
+ return mappings.some((mapping) =>
238
+ mapping.generatedOffsets.some((offset, i) => {
239
+ const length = mapping.generatedLengths?.[i] ?? mapping.lengths[i];
240
+ if (offset >= end || start >= offset + length) return false;
241
+ const source = mapping.sourceOffsets[i];
242
+ return !(
243
+ offset >= name_start &&
244
+ offset + length <= name_start + name_length &&
245
+ source >= opening.start &&
246
+ source + mapping.lengths[i] <= opening.end
247
+ );
248
+ }),
249
+ );
250
+ }
251
+
105
252
  /**
106
253
  * Whether `error` is an unclosed tag reported on the tag and found where the
107
254
  * parser stopped, one before `raisedAt`: the end of its template, or the start
package/src/mapper.js CHANGED
@@ -210,7 +210,7 @@ export function create_tsrx_content_mapper(context = {}) {
210
210
  result.sourceAst,
211
211
  generated.mappings,
212
212
  content.length,
213
- generated.text.length,
213
+ generated.text,
214
214
  );
215
215
  return {
216
216
  text: generated.text,
@@ -98,6 +98,14 @@ export function features_from_mapping_data(data) {
98
98
  * the span exactly show the authored name.
99
99
  * - Anything else → `Atom`.
100
100
  *
101
+ * Mappings over the same ranges in both files are one candidate. A container
102
+ * mapped for diagnostics only can cover exactly the text of the token inside
103
+ * it (`item` in a Vue `{item.label}`, a Solid `key i`), and the token keeps
104
+ * its hover and navigation whichever comes first: a mapping without features
105
+ * never takes the place of one with features. Of two with features, the first
106
+ * is kept, so the spans that coalesce stay the same. The candidate is
107
+ * position-only when one of its mappings is and none has a feature bit.
108
+ *
101
109
  * The transform emits container spans (statements, control-flow bodies) that
102
110
  * enclose token spans. TypeScript requires generated spans to be disjoint, so
103
111
  * candidates are ranked Verbatim first, then shorter before longer, and a
@@ -126,6 +134,13 @@ export function to_span_mappings(mappings, generated_text, original_text, option
126
134
  const language_features = options.languageFeatures !== false;
127
135
  /** @type {Candidate[]} */
128
136
  const candidates = [];
137
+ /**
138
+ * Candidates by their generated and original ranges. Kept apart, two
139
+ * candidates over the same ranges would tie in the ranking below, and the
140
+ * first in the transform's order would win even without features.
141
+ * @type {Map<string, Candidate>}
142
+ */
143
+ const by_ranges = new Map();
129
144
 
130
145
  for (const mapping of mappings) {
131
146
  const count = Math.min(mapping.sourceOffsets.length, mapping.generatedOffsets.length);
@@ -173,17 +188,30 @@ export function to_span_mappings(mappings, generated_text, original_text, option
173
188
  } else {
174
189
  kind = SpanMapKind.Atom;
175
190
  }
176
- candidates.push({
191
+ const features = language_features
192
+ ? features_from_mapping_data(mapping.data)
193
+ : SpanMapFeature.None;
194
+ const position_only = is_position_only(mapping.data);
195
+ const key = `${generated_start},${generated_end},${original_start},${original_end}`;
196
+ const same = by_ranges.get(key);
197
+ if (same) {
198
+ if (same.features === SpanMapFeature.None) same.features = features;
199
+ same.positionOnly =
200
+ (same.positionOnly || position_only) && same.features === SpanMapFeature.None;
201
+ continue;
202
+ }
203
+ /** @type {Candidate} */
204
+ const candidate = {
177
205
  generatedStart: generated_start,
178
206
  generatedEnd: generated_end,
179
207
  originalStart: original_start,
180
208
  originalEnd: original_end,
181
209
  kind,
182
- features: language_features
183
- ? features_from_mapping_data(mapping.data)
184
- : SpanMapFeature.None,
185
- positionOnly: is_position_only(mapping.data),
186
- });
210
+ features,
211
+ positionOnly: position_only,
212
+ };
213
+ by_ranges.set(key, candidate);
214
+ candidates.push(candidate);
187
215
  }
188
216
  }
189
217