@tsrx/content-mapper 0.1.3 → 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/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-SyX-87Od.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-SyX-87Od.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.4",
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/core": "0.6.1",
54
+ "@tsrx/preact": "0.4.1",
55
+ "@tsrx/react": "0.5.1",
56
+ "@tsrx/solid": "0.4.1",
57
+ "@tsrx/typescript-plugin": "0.7.1",
58
+ "@tsrx/vue": "0.4.1"
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,