@tsrx/core 0.1.34 → 0.1.35

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/plugin.js +29 -0
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Core compiler infrastructure for TSRX syntax",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.1.34",
6
+ "version": "0.1.35",
7
7
  "type": "module",
8
8
  "repository": {
9
9
  "type": "git",
package/src/plugin.js CHANGED
@@ -259,6 +259,13 @@ export function TSRXPlugin(config) {
259
259
  // `#filterTemplateScriptContexts`.
260
260
  /** @type {number[]} */
261
261
  #expressionContainerContextBaselines = [];
262
+ // `#path` length at the start of each open `{ … }` expression container.
263
+ // Raw template text inside a container belongs only to an element opened
264
+ // inside it (`{<div> a</div>}`); at the container's own expression level
265
+ // (`{cond ? (<Outer>…</Outer>) : null}` after the `)`) the next characters
266
+ // are JS, and reading them as raw text would swallow tokens like `: null`.
267
+ /** @type {number[]} */
268
+ #expressionContainerPathBaselines = [];
262
269
  #consumeContainerBraceAfterScope = false;
263
270
  #scriptJSXElementDepth = 0;
264
271
  #forceScriptJSXElementDepth = 0;
@@ -936,6 +943,26 @@ export function TSRXPlugin(config) {
936
943
  if (!current_template_node || this.#isJSXControlFlowDirectiveAt(this.pos)) {
937
944
  return false;
938
945
  }
946
+ // Inside an expression container (only reachable with
947
+ // `allow_inside_expression_container`), raw text belongs to an element
948
+ // opened inside the container. When the innermost native template element
949
+ // sits below the container's path baseline we are at the container's own
950
+ // expression level — e.g. after `(<Outer>…</Outer>)` in
951
+ // `{cond ? (<Outer>…</Outer>) : null}` — and the following characters are
952
+ // JS tokens, not template text.
953
+ if (this.#jsxExpressionContainerDepth > 0 && !this.#openingNativeTemplateNode) {
954
+ const path_baseline = this.#expressionContainerPathBaselines.at(-1) ?? 0;
955
+ let inside_container = false;
956
+ for (let i = this.#path.length - 1; i >= path_baseline; i--) {
957
+ if (this.#isNativeTemplateNode(this.#path[i])) {
958
+ inside_container = true;
959
+ break;
960
+ }
961
+ }
962
+ if (!inside_container) {
963
+ return false;
964
+ }
965
+ }
939
966
  if (this.#isTemplateLineCommentStart(this.pos)) {
940
967
  return false;
941
968
  }
@@ -3377,6 +3404,7 @@ export function TSRXPlugin(config) {
3377
3404
  // container must not strip anything below this floor (see
3378
3405
  // `#filterTemplateScriptContexts`).
3379
3406
  this.#expressionContainerContextBaselines.push(this.context.length);
3407
+ this.#expressionContainerPathBaselines.push(this.#path.length);
3380
3408
  pushed_context_baseline = true;
3381
3409
 
3382
3410
  node.expression =
@@ -3398,6 +3426,7 @@ export function TSRXPlugin(config) {
3398
3426
  this.#jsxExpressionContainerDepth--;
3399
3427
  if (pushed_context_baseline) {
3400
3428
  this.#expressionContainerContextBaselines.pop();
3429
+ this.#expressionContainerPathBaselines.pop();
3401
3430
  }
3402
3431
  }
3403
3432