@nifrajs/cli 5.17.0 → 5.18.1

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 (178) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/dist/check-diagnostics.d.ts +7 -0
  3. package/dist/check-diagnostics.d.ts.map +1 -1
  4. package/dist/check-diagnostics.js +3 -0
  5. package/dist/check-diagnostics.js.map +1 -1
  6. package/dist/check-scan-sql.d.ts +95 -0
  7. package/dist/check-scan-sql.d.ts.map +1 -0
  8. package/dist/check-scan-sql.js +1190 -0
  9. package/dist/check-scan-sql.js.map +1 -0
  10. package/dist/check-scan.d.ts +5 -93
  11. package/dist/check-scan.d.ts.map +1 -1
  12. package/dist/check-scan.js +20 -1207
  13. package/dist/check-scan.js.map +1 -1
  14. package/dist/check.d.ts.map +1 -1
  15. package/dist/check.js +9 -0
  16. package/dist/check.js.map +1 -1
  17. package/dist/cli.d.ts +2 -6
  18. package/dist/cli.d.ts.map +1 -1
  19. package/dist/cli.js +249 -80
  20. package/dist/cli.js.map +1 -1
  21. package/dist/command-catalog.d.ts +3 -0
  22. package/dist/command-catalog.d.ts.map +1 -1
  23. package/dist/command-catalog.js +18 -4
  24. package/dist/command-catalog.js.map +1 -1
  25. package/dist/frontend-guidance.js +1 -1
  26. package/dist/frontend-guidance.js.map +1 -1
  27. package/dist/internal/a11y-element.d.ts +33 -0
  28. package/dist/internal/a11y-element.d.ts.map +1 -0
  29. package/dist/internal/a11y-element.js +124 -0
  30. package/dist/internal/a11y-element.js.map +1 -0
  31. package/dist/internal/a11y-jsx.d.ts +12 -0
  32. package/dist/internal/a11y-jsx.d.ts.map +1 -0
  33. package/dist/internal/a11y-jsx.js +42 -0
  34. package/dist/internal/a11y-jsx.js.map +1 -0
  35. package/dist/internal/a11y-template.d.ts +11 -0
  36. package/dist/internal/a11y-template.d.ts.map +1 -0
  37. package/dist/internal/a11y-template.js +56 -0
  38. package/dist/internal/a11y-template.js.map +1 -0
  39. package/dist/internal/aria-data.d.ts +23 -0
  40. package/dist/internal/aria-data.d.ts.map +1 -0
  41. package/dist/internal/aria-data.js +220 -0
  42. package/dist/internal/aria-data.js.map +1 -0
  43. package/dist/internal/edit-distance.d.ts +3 -0
  44. package/dist/internal/edit-distance.d.ts.map +1 -0
  45. package/dist/internal/edit-distance.js +12 -0
  46. package/dist/internal/edit-distance.js.map +1 -0
  47. package/dist/internal/imported-as.d.ts +10 -0
  48. package/dist/internal/imported-as.d.ts.map +1 -0
  49. package/dist/internal/imported-as.js +23 -0
  50. package/dist/internal/imported-as.js.map +1 -0
  51. package/dist/internal/source-facts.d.ts +3 -0
  52. package/dist/internal/source-facts.d.ts.map +1 -1
  53. package/dist/internal/source-facts.js +2 -0
  54. package/dist/internal/source-facts.js.map +1 -1
  55. package/dist/internal/template-markup.d.ts +58 -0
  56. package/dist/internal/template-markup.d.ts.map +1 -0
  57. package/dist/internal/template-markup.js +221 -0
  58. package/dist/internal/template-markup.js.map +1 -0
  59. package/dist/internal/typescript-import.d.ts.map +1 -1
  60. package/dist/internal/typescript-import.js +11 -0
  61. package/dist/internal/typescript-import.js.map +1 -1
  62. package/dist/mcp-exec.d.ts.map +1 -1
  63. package/dist/mcp-exec.js +37 -23
  64. package/dist/mcp-exec.js.map +1 -1
  65. package/dist/mcp-http.js +1 -1
  66. package/dist/mcp-io.d.ts.map +1 -1
  67. package/dist/mcp-io.js +5 -3
  68. package/dist/mcp-io.js.map +1 -1
  69. package/dist/mcp-root.js +1 -1
  70. package/dist/mcp-root.js.map +1 -1
  71. package/dist/mcp-ws.d.ts.map +1 -1
  72. package/dist/mcp-ws.js +3 -2
  73. package/dist/mcp-ws.js.map +1 -1
  74. package/dist/migrate-layout-config.d.ts +63 -0
  75. package/dist/migrate-layout-config.d.ts.map +1 -0
  76. package/dist/migrate-layout-config.js +507 -0
  77. package/dist/migrate-layout-config.js.map +1 -0
  78. package/dist/migrate-layout-split.d.ts +46 -0
  79. package/dist/migrate-layout-split.d.ts.map +1 -0
  80. package/dist/migrate-layout-split.js +687 -0
  81. package/dist/migrate-layout-split.js.map +1 -0
  82. package/dist/migrate-layout.d.ts +4 -24
  83. package/dist/migrate-layout.d.ts.map +1 -1
  84. package/dist/migrate-layout.js +8 -1183
  85. package/dist/migrate-layout.js.map +1 -1
  86. package/dist/output-schemas.d.ts.map +1 -1
  87. package/dist/output-schemas.js +78 -10
  88. package/dist/output-schemas.js.map +1 -1
  89. package/dist/recipes/5.18.0.d.ts +9 -0
  90. package/dist/recipes/5.18.0.d.ts.map +1 -0
  91. package/dist/recipes/5.18.0.js +56 -0
  92. package/dist/recipes/5.18.0.js.map +1 -0
  93. package/dist/recipes/index.d.ts.map +1 -1
  94. package/dist/recipes/index.js +2 -0
  95. package/dist/recipes/index.js.map +1 -1
  96. package/dist/review.d.ts.map +1 -1
  97. package/dist/review.js +6 -0
  98. package/dist/review.js.map +1 -1
  99. package/dist/rules/a11y.d.ts +5 -0
  100. package/dist/rules/a11y.d.ts.map +1 -0
  101. package/dist/rules/a11y.js +78 -0
  102. package/dist/rules/a11y.js.map +1 -0
  103. package/dist/rules/codes.d.ts +6 -0
  104. package/dist/rules/codes.d.ts.map +1 -1
  105. package/dist/rules/codes.js +6 -0
  106. package/dist/rules/codes.js.map +1 -1
  107. package/dist/rules/comment-markers.js +1 -1
  108. package/dist/rules/comment-markers.js.map +1 -1
  109. package/dist/rules/explanations.d.ts.map +1 -1
  110. package/dist/rules/explanations.js +30 -0
  111. package/dist/rules/explanations.js.map +1 -1
  112. package/dist/rules/host-exposure.d.ts +23 -0
  113. package/dist/rules/host-exposure.d.ts.map +1 -0
  114. package/dist/rules/host-exposure.js +380 -0
  115. package/dist/rules/host-exposure.js.map +1 -0
  116. package/dist/rules/internal-links.d.ts.map +1 -1
  117. package/dist/rules/internal-links.js +16 -141
  118. package/dist/rules/internal-links.js.map +1 -1
  119. package/dist/rules/islands.d.ts +54 -0
  120. package/dist/rules/islands.d.ts.map +1 -1
  121. package/dist/rules/islands.js +577 -80
  122. package/dist/rules/islands.js.map +1 -1
  123. package/dist/rules/security.d.ts +1 -0
  124. package/dist/rules/security.d.ts.map +1 -1
  125. package/dist/rules/security.js +34 -7
  126. package/dist/rules/security.js.map +1 -1
  127. package/dist/runtime-differences.js +3 -3
  128. package/dist/runtime-differences.js.map +1 -1
  129. package/dist/scaffold.d.ts +1 -1
  130. package/dist/scaffold.d.ts.map +1 -1
  131. package/dist/scaffold.js +2 -2
  132. package/dist/scaffold.js.map +1 -1
  133. package/dist/source-rewrites.d.ts.map +1 -1
  134. package/dist/source-rewrites.js +2 -19
  135. package/dist/source-rewrites.js.map +1 -1
  136. package/docs/examples.json +4 -4
  137. package/docs/llms-full.txt +422 -42
  138. package/docs/types.json +466 -56
  139. package/package.json +12 -12
  140. package/src/check-diagnostics.ts +11 -0
  141. package/src/check-scan-sql.ts +1746 -0
  142. package/src/check-scan.ts +34 -1754
  143. package/src/check.ts +10 -1
  144. package/src/cli.ts +258 -98
  145. package/src/command-catalog.ts +21 -4
  146. package/src/frontend-guidance.ts +1 -1
  147. package/src/internal/a11y-element.ts +227 -0
  148. package/src/internal/a11y-jsx.ts +54 -0
  149. package/src/internal/a11y-template.ts +74 -0
  150. package/src/internal/aria-data.ts +242 -0
  151. package/src/internal/edit-distance.ts +16 -0
  152. package/src/internal/imported-as.ts +30 -0
  153. package/src/internal/source-facts.ts +4 -0
  154. package/src/internal/template-markup.ts +270 -0
  155. package/src/internal/typescript-import.ts +15 -0
  156. package/src/mcp-exec.ts +43 -22
  157. package/src/mcp-http.ts +1 -1
  158. package/src/mcp-io.ts +5 -3
  159. package/src/mcp-root.ts +1 -1
  160. package/src/mcp-ws.ts +3 -2
  161. package/src/migrate-layout-config.ts +615 -0
  162. package/src/migrate-layout-split.ts +870 -0
  163. package/src/migrate-layout.ts +37 -1468
  164. package/src/output-schemas.ts +85 -17
  165. package/src/recipes/5.18.0.ts +63 -0
  166. package/src/recipes/index.ts +2 -0
  167. package/src/review.ts +6 -0
  168. package/src/rules/a11y.ts +119 -0
  169. package/src/rules/codes.ts +6 -0
  170. package/src/rules/comment-markers.ts +2 -2
  171. package/src/rules/explanations.ts +30 -0
  172. package/src/rules/host-exposure.ts +531 -0
  173. package/src/rules/internal-links.ts +21 -154
  174. package/src/rules/islands.ts +698 -86
  175. package/src/rules/security.ts +48 -7
  176. package/src/runtime-differences.ts +3 -3
  177. package/src/scaffold.ts +3 -3
  178. package/src/source-rewrites.ts +2 -15
@@ -1,6 +1,7 @@
1
1
  import type * as TSApi from "typescript"
2
2
  import { type Diagnostic, diagnostic } from "../diagnostics.ts"
3
3
  import { scriptKindOf } from "../internal/script-kind.ts"
4
+ import { commentBlockExpectation, MISSING_EXPECTATION_REASON } from "./comment-markers.ts"
4
5
  import type { CheckRule, SourceIndex } from "./index.ts"
5
6
  import { loadRuleTypeScript } from "./typescript.ts"
6
7
 
@@ -296,11 +297,174 @@ type IslandNames =
296
297
  readonly kind: "bind"
297
298
  readonly state: ReadonlySet<string> | undefined
298
299
  readonly handlers: ReadonlySet<string> | undefined
300
+ readonly props?: PropsShape | undefined
299
301
  }
300
- | { readonly kind: "imperative" }
302
+ | { readonly kind: "imperative"; readonly props?: PropsShape | undefined }
303
+
304
+ /** The primitive a prop's type or literal value is, when it is one and only one. */
305
+ type PropKind = "string" | "number" | "boolean"
306
+ const OMITTED = Symbol("omitted from JSON")
307
+ type PropValue = PropKind | "null" | typeof OMITTED | undefined
308
+
309
+ /** The props an enhancer's type literal declares: `undefined` where the type is not a plain literal. */
310
+ type PropsShape = ReadonlyMap<
311
+ string,
312
+ { readonly optional: boolean; readonly kind: PropKind | undefined }
313
+ >
314
+
315
+ /** Explicit prop keys, their known serialized values, and whether a spread may add more. */
316
+ interface PropsPassed {
317
+ readonly entries: ReadonlyMap<string, PropValue>
318
+ readonly open: boolean
319
+ }
301
320
 
302
321
  export const UNKNOWN = Symbol("unknown")
303
322
 
323
+ const memberName = (ts: typeof TSApi, name: TSApi.PropertyName): string | undefined =>
324
+ ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isNumericLiteral(name)
325
+ ? name.text
326
+ : undefined
327
+
328
+ /** The one primitive a type node is: a keyword, a literal, or a union of literals of one kind. */
329
+ function typeKind(ts: typeof TSApi, type: TSApi.TypeNode): PropKind | undefined {
330
+ if (ts.isParenthesizedTypeNode(type)) return typeKind(ts, type.type)
331
+ if (type.kind === ts.SyntaxKind.StringKeyword) return "string"
332
+ if (type.kind === ts.SyntaxKind.NumberKeyword) return "number"
333
+ if (type.kind === ts.SyntaxKind.BooleanKeyword) return "boolean"
334
+ if (ts.isLiteralTypeNode(type)) {
335
+ const literal = type.literal
336
+ if (ts.isStringLiteralLike(literal)) return "string"
337
+ if (ts.isNumericLiteral(literal)) return "number"
338
+ if (literal.kind === ts.SyntaxKind.TrueKeyword || literal.kind === ts.SyntaxKind.FalseKeyword)
339
+ return "boolean"
340
+ return undefined
341
+ }
342
+ if (ts.isUnionTypeNode(type)) {
343
+ const kinds = new Set(type.types.map((member) => typeKind(ts, member)))
344
+ const [only] = kinds
345
+ return kinds.size === 1 ? only : undefined
346
+ }
347
+ return undefined
348
+ }
349
+
350
+ /** Whether a type node admits `undefined`, which JSON drops: the prop may then be left out. */
351
+ function admitsUndefined(ts: typeof TSApi, type: TSApi.TypeNode): boolean {
352
+ if (type.kind === ts.SyntaxKind.UndefinedKeyword) return true
353
+ if (ts.isParenthesizedTypeNode(type)) return admitsUndefined(ts, type.type)
354
+ return ts.isUnionTypeNode(type) && type.types.some((member) => admitsUndefined(ts, member))
355
+ }
356
+
357
+ /** The props of a plain type literal (`{ start: number; label?: string }`); anything else - a
358
+ * reference, an index signature, a method - is not judged. */
359
+ function propsShape(ts: typeof TSApi, type: TSApi.TypeNode | undefined): PropsShape | undefined {
360
+ if (type === undefined || !ts.isTypeLiteralNode(type)) return undefined
361
+ const shape = new Map<string, { optional: boolean; kind: PropKind | undefined }>()
362
+ for (const member of type.members) {
363
+ if (!ts.isPropertySignature(member) || member.type === undefined) return undefined
364
+ const name = memberName(ts, member.name)
365
+ if (name === undefined) return undefined
366
+ shape.set(name, {
367
+ optional: member.questionToken !== undefined || admitsUndefined(ts, member.type),
368
+ kind: typeKind(ts, member.type),
369
+ })
370
+ }
371
+ return shape
372
+ }
373
+
374
+ /** What JSON writes for this value, leaving expressions it cannot settle unknown. */
375
+ function literalKind(ts: typeof TSApi, node: TSApi.Expression): PropValue {
376
+ let inner = node
377
+ while (ts.isParenthesizedExpression(inner) || ts.isAsExpression(inner)) inner = inner.expression
378
+ if (ts.isVoidExpression(inner) || (ts.isIdentifier(inner) && inner.text === "undefined"))
379
+ return OMITTED
380
+ if (ts.isStringLiteralLike(inner) || ts.isTemplateExpression(inner)) return "string"
381
+ if (ts.isNumericLiteral(inner)) return "number"
382
+ if (
383
+ ts.isPrefixUnaryExpression(inner) &&
384
+ inner.operator === ts.SyntaxKind.MinusToken &&
385
+ ts.isNumericLiteral(inner.operand)
386
+ )
387
+ return "number"
388
+ if (inner.kind === ts.SyntaxKind.TrueKeyword || inner.kind === ts.SyntaxKind.FalseKeyword)
389
+ return "boolean"
390
+ if (inner.kind === ts.SyntaxKind.NullKeyword) return "null"
391
+ return undefined
392
+ }
393
+
394
+ /** The props an object literal passes; `undefined` when a member is one this cannot read. */
395
+ function passedFromObject(
396
+ ts: typeof TSApi,
397
+ object: TSApi.ObjectLiteralExpression,
398
+ ): PropsPassed | undefined {
399
+ const entries = new Map<string, PropValue>()
400
+ const lastSpread = object.properties.findLastIndex(ts.isSpreadAssignment)
401
+ for (const [index, prop] of object.properties.entries()) {
402
+ if (ts.isSpreadAssignment(prop)) continue
403
+ if (ts.isShorthandPropertyAssignment(prop)) entries.set(prop.name.text, undefined)
404
+ else if (ts.isPropertyAssignment(prop)) {
405
+ const name = memberName(ts, prop.name)
406
+ if (name === undefined) return undefined
407
+ entries.set(name, index < lastSpread ? undefined : literalKind(ts, prop.initializer))
408
+ } else return undefined
409
+ }
410
+ return { entries, open: lastSpread !== -1 }
411
+ }
412
+
413
+ /** The props an expression passes when it is a literal object, `islandProps({...})` or
414
+ * `JSON.stringify({...})`; `undefined` otherwise. */
415
+ function passedFromExpression(
416
+ ts: typeof TSApi,
417
+ expr: TSApi.Expression | undefined,
418
+ ): PropsPassed | undefined {
419
+ let inner = expr
420
+ while (inner !== undefined && (ts.isParenthesizedExpression(inner) || ts.isAsExpression(inner)))
421
+ inner = inner.expression
422
+ if (inner === undefined) return undefined
423
+ if (ts.isObjectLiteralExpression(inner)) return passedFromObject(ts, inner)
424
+ if (!ts.isCallExpression(inner)) return undefined
425
+ const callee = inner.expression
426
+ const serializer =
427
+ (ts.isIdentifier(callee) && callee.text === "islandProps") ||
428
+ (ts.isPropertyAccessExpression(callee) &&
429
+ ts.isIdentifier(callee.expression) &&
430
+ callee.expression.text === "JSON" &&
431
+ callee.name.text === "stringify")
432
+ const arg = inner.arguments[0]
433
+ return serializer &&
434
+ arg !== undefined &&
435
+ ts.isObjectLiteralExpression(arg) &&
436
+ inner.arguments.length === 1
437
+ ? passedFromObject(ts, arg)
438
+ : undefined
439
+ }
440
+
441
+ /** The props of a literal `data-island-props` attribute value: its JSON, after the HTML entities an
442
+ * attribute value may hold. `undefined` when it is not a JSON object. */
443
+ function passedFromJson(text: string): PropsPassed | undefined {
444
+ const decoded = text
445
+ .replaceAll("&quot;", '"')
446
+ .replaceAll("&#39;", "'")
447
+ .replaceAll("&lt;", "<")
448
+ .replaceAll("&gt;", ">")
449
+ .replaceAll("&amp;", "&")
450
+ let value: unknown
451
+ try {
452
+ value = JSON.parse(decoded)
453
+ } catch {
454
+ return undefined
455
+ }
456
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined
457
+ const entries = new Map<string, PropKind | "null" | undefined>()
458
+ for (const [key, item] of Object.entries(value)) {
459
+ if (item === null) entries.set(key, "null")
460
+ else if (typeof item === "string") entries.set(key, "string")
461
+ else if (typeof item === "number") entries.set(key, "number")
462
+ else if (typeof item === "boolean") entries.set(key, "boolean")
463
+ else entries.set(key, undefined)
464
+ }
465
+ return { entries, open: false }
466
+ }
467
+
304
468
  function literalKeys(
305
469
  ts: typeof TSApi,
306
470
  node: TSApi.ObjectLiteralExpression,
@@ -413,47 +577,118 @@ function bindIslandNames(
413
577
  return { kind: "bind", state, handlers }
414
578
  }
415
579
 
580
+ /** The props annotated on an inline enhancer's second parameter. */
581
+ function parameterProps(
582
+ ts: typeof TSApi,
583
+ fn: TSApi.Expression | undefined,
584
+ ): PropsShape | undefined {
585
+ return fn !== undefined && isEnhancerFn(ts, fn)
586
+ ? propsShape(ts, fn.parameters[1]?.type)
587
+ : undefined
588
+ }
589
+
416
590
  /** What an enhancer expression mounts: a `bindIsland(...)`, an imperative enhancer, or unknown. */
417
591
  function enhancerNames(
418
592
  ts: typeof TSApi,
419
593
  expr: TSApi.Expression,
420
- locals: ReadonlyMap<string, TSApi.Expression>,
594
+ locals: ReadonlyMap<string, TSApi.Expression | undefined>,
421
595
  ): IslandNames | typeof UNKNOWN {
422
596
  let target: TSApi.Expression | undefined = expr
423
597
  if (ts.isIdentifier(expr)) target = locals.get(expr.text)
424
598
  if (target === undefined) return UNKNOWN
425
- if (isEnhancerFn(ts, target)) return { kind: "imperative" }
599
+ if (isEnhancerFn(ts, target)) return { kind: "imperative", props: parameterProps(ts, target) }
426
600
  if (ts.isCallExpression(target) && ts.isIdentifier(target.expression)) {
427
- if (target.expression.text === "defineIsland") return { kind: "imperative" }
428
- if (target.expression.text === "bindIsland") return bindIslandNames(ts, target.arguments[0])
601
+ const declared = propsShape(ts, target.typeArguments?.[0])
602
+ if (target.expression.text === "defineIsland")
603
+ return {
604
+ kind: "imperative",
605
+ props: declared ?? parameterProps(ts, target.arguments[0]),
606
+ }
607
+ if (target.expression.text === "bindIsland") {
608
+ const names = bindIslandNames(ts, target.arguments[0])
609
+ return names === UNKNOWN ? UNKNOWN : { ...names, props: declared }
610
+ }
429
611
  }
430
612
  return UNKNOWN
431
613
  }
432
614
 
615
+ function mountReference(ts: typeof TSApi, expr: TSApi.Expression | undefined): boolean {
616
+ let target = expr
617
+ while (
618
+ target !== undefined &&
619
+ (ts.isParenthesizedExpression(target) ||
620
+ ts.isAsExpression(target) ||
621
+ ts.isNonNullExpression(target) ||
622
+ ts.isSatisfiesExpression(target))
623
+ )
624
+ target = target.expression
625
+ if (target === undefined) return false
626
+ if (ts.isIdentifier(target)) return MOUNT_CALLS.has(target.text)
627
+ return ts.isPropertyAccessExpression(target) && MOUNT_CALLS.has(target.name.text)
628
+ }
629
+
630
+ /** What the project's mount calls cover. `open` is set when a call or an import hides ids this
631
+ * cannot list (a map held in a variable, a spread, or an indirect `mountIslands` call). */
632
+ interface MountSurface {
633
+ readonly ids: Map<string, IslandNames | typeof UNKNOWN>
634
+ readonly open: boolean
635
+ /** How many literal mount calls were read. */
636
+ readonly calls: number
637
+ }
638
+
433
639
  /** Every island id mounted in the project, with what its markup may name. An id mounted with
434
640
  * different or unresolvable enhancers maps to UNKNOWN. */
435
- function mountedIslands(
436
- ts: typeof TSApi,
437
- trees: ReadonlyArray<TSApi.SourceFile>,
438
- ): Map<string, IslandNames | typeof UNKNOWN> {
641
+ function mountedIslands(ts: typeof TSApi, trees: ReadonlyArray<TSApi.SourceFile>): MountSurface {
439
642
  const ids = new Map<string, IslandNames | typeof UNKNOWN>()
643
+ let open = false
644
+ let calls = 0
440
645
  for (const tree of trees) {
441
- const locals = new Map<string, TSApi.Expression>()
646
+ // The AST walk has no lexical resolver, so a name declared more than once is ambiguous.
647
+ const locals = new Map<string, TSApi.Expression | undefined>()
442
648
  const mounts: TSApi.ObjectLiteralExpression[] = []
443
649
  const visit = (node: TSApi.Node): void => {
650
+ if (ts.isVariableDeclaration(node)) {
651
+ if (ts.isIdentifier(node.name)) {
652
+ const name = node.name.text
653
+ locals.set(name, locals.has(name) ? undefined : node.initializer)
654
+ }
655
+ if (mountReference(ts, node.initializer)) open = true
656
+ } else if (
657
+ (ts.isParameter(node) ||
658
+ ts.isBindingElement(node) ||
659
+ ts.isImportSpecifier(node) ||
660
+ ts.isImportClause(node) ||
661
+ ts.isNamespaceImport(node) ||
662
+ ts.isFunctionDeclaration(node) ||
663
+ ts.isFunctionExpression(node) ||
664
+ ts.isClassDeclaration(node) ||
665
+ ts.isClassExpression(node) ||
666
+ ts.isEnumDeclaration(node)) &&
667
+ node.name !== undefined &&
668
+ ts.isIdentifier(node.name)
669
+ )
670
+ locals.set(node.name.text, undefined)
444
671
  if (
445
- ts.isVariableDeclaration(node) &&
446
- ts.isIdentifier(node.name) &&
447
- node.initializer !== undefined
672
+ ts.isBinaryExpression(node) &&
673
+ node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
674
+ mountReference(ts, node.right)
448
675
  )
449
- locals.set(node.name.text, node.initializer)
676
+ open = true
450
677
  if (
451
- ts.isCallExpression(node) &&
452
- ts.isIdentifier(node.expression) &&
453
- MOUNT_CALLS.has(node.expression.text)
454
- ) {
455
- const map = node.arguments[0]
456
- if (map !== undefined && ts.isObjectLiteralExpression(map)) mounts.push(map)
678
+ ts.isImportSpecifier(node) &&
679
+ node.propertyName !== undefined &&
680
+ MOUNT_CALLS.has(node.propertyName.text)
681
+ )
682
+ open = true
683
+ if (ts.isCallExpression(node)) {
684
+ const callee = node.expression
685
+ if (ts.isIdentifier(callee) && MOUNT_CALLS.has(callee.text)) {
686
+ const map = node.arguments[0]
687
+ if (map !== undefined && ts.isObjectLiteralExpression(map)) {
688
+ calls++
689
+ mounts.push(map)
690
+ } else open = true
691
+ } else if (mountReference(ts, callee)) open = true
457
692
  }
458
693
  ts.forEachChild(node, visit)
459
694
  }
@@ -465,26 +700,33 @@ function mountedIslands(
465
700
  if (ts.isShorthandPropertyAssignment(prop)) {
466
701
  id = prop.name.text
467
702
  names = enhancerNames(ts, prop.name, locals)
468
- } else if (
469
- ts.isPropertyAssignment(prop) &&
470
- (ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name))
471
- ) {
472
- id = prop.name.text
703
+ } else if (ts.isPropertyAssignment(prop)) {
704
+ id = memberName(ts, prop.name)
473
705
  names = enhancerNames(ts, prop.initializer, locals)
706
+ } else if (ts.isMethodDeclaration(prop)) {
707
+ id = memberName(ts, prop.name)
708
+ names = { kind: "imperative", props: propsShape(ts, prop.parameters[1]?.type) }
474
709
  }
475
- if (id === undefined) continue
476
- ids.set(id, ids.has(id) ? UNKNOWN : names)
710
+ if (id === undefined) open = true
711
+ else ids.set(id, ids.has(id) ? UNKNOWN : names)
477
712
  }
478
713
  }
479
714
  }
480
- return ids
715
+ return { ids, open, calls }
481
716
  }
482
717
 
483
718
  /** One `data-bind-*` attribute in markup, with the island host it belongs to. */
484
719
  interface MarkupBinding {
485
720
  readonly attribute: string
486
721
  /** Signal names (`handler: false`) or handler names (`handler: true`) the attribute refers to. */
487
- readonly names: ReadonlyArray<{ readonly name: string; readonly handler: boolean }>
722
+ readonly names: ReadonlyArray<{
723
+ readonly name: string
724
+ readonly handler: boolean
725
+ /** The class, attribute or event the pair names: `aria-expanded` in `aria-expanded:open`. */
726
+ readonly key: string | undefined
727
+ }>
728
+ /** The element's tag, lowercased; a JSX component keeps its own name. */
729
+ readonly tag: string | undefined
488
730
  /** The nearest enclosing host's id: `null` when there is none, UNKNOWN when it is not literal. */
489
731
  readonly island: string | null | typeof UNKNOWN
490
732
  /** A `data-swap-slot` between the binding and its host, whose swap replaces the bound element. */
@@ -519,8 +761,20 @@ export interface HolePlacement {
519
761
  readonly sole: boolean
520
762
  }
521
763
 
764
+ /** One island host in markup: a `data-island` element or an `<Island>`. */
765
+ interface MarkupHost {
766
+ /** The host's id: UNKNOWN when it is not literal. */
767
+ readonly id: string | typeof UNKNOWN
768
+ /** What it passes as props: `absent` when it has none, `unknown` when they cannot be read. */
769
+ readonly props: PropsPassed | "absent" | "unknown"
770
+ readonly node: TSApi.Node
771
+ /** The line of its tag, when `node` is the whole template. */
772
+ readonly line?: number
773
+ }
774
+
522
775
  interface MarkupFacts {
523
776
  readonly hasHost: boolean
777
+ readonly hosts: MarkupHost[]
524
778
  readonly bindings: MarkupBinding[]
525
779
  readonly swaps: SwapMarkup[]
526
780
  }
@@ -532,14 +786,15 @@ function bindingNames(attribute: string, value: string): MarkupBinding["names"]
532
786
  if (value.includes(HOLE)) return undefined
533
787
  const kind = attribute.slice("data-bind-".length)
534
788
  if (kind === "text" || kind === "show" || kind === "value")
535
- return value.trim() === "" ? [] : [{ name: value.trim(), handler: false }]
789
+ return value.trim() === "" ? [] : [{ name: value.trim(), handler: false, key: undefined }]
536
790
  if (kind !== "class" && kind !== "attr" && kind !== "on") return undefined
537
- const out: Array<{ name: string; handler: boolean }> = []
791
+ const out: Array<{ name: string; handler: boolean; key: string | undefined }> = []
538
792
  for (const part of value.split(",")) {
539
793
  const sep = part.indexOf(":")
540
794
  if (sep <= 0) continue
541
795
  const name = part.slice(sep + 1).trim()
542
- if (part.slice(0, sep).trim() && name) out.push({ name, handler: kind === "on" })
796
+ const key = part.slice(0, sep).trim()
797
+ if (key && name) out.push({ name, handler: kind === "on", key })
543
798
  }
544
799
  return out
545
800
  }
@@ -560,9 +815,35 @@ const VOID_ELEMENTS = new Set([
560
815
  "wbr",
561
816
  ])
562
817
 
563
- /** The attributes of one tag's inside (`name a="1" b`), in order. */
564
- function tagAttributes(inside: string): Array<[string, string]> {
565
- const out: Array<[string, string]> = []
818
+ const TEXT_ELEMENTS = new Set([
819
+ "iframe",
820
+ "noembed",
821
+ "noframes",
822
+ "script",
823
+ "style",
824
+ "textarea",
825
+ "title",
826
+ "xmp",
827
+ ])
828
+
829
+ function htmlCommentAt(text: string, offset: number): RegExpExecArray | null {
830
+ const pattern = /<!--(?:-?>|(.*?)--!?>)/sy
831
+ pattern.lastIndex = offset
832
+ return pattern.exec(text)
833
+ }
834
+
835
+ function textElementClose(text: string, tag: string, start: number): number {
836
+ const closer = /<\/([a-z][\w-]*)(?=[\t\n\f\r />])/gi
837
+ closer.lastIndex = start
838
+ for (let match = closer.exec(text); match !== null; match = closer.exec(text))
839
+ if (match[1]?.toLowerCase() === tag) return match.index
840
+ return -1
841
+ }
842
+
843
+ /** The attributes of one tag's inside (`name a="1" b`), in order, each with where its value starts
844
+ * in `inside` (-1 when it has none). */
845
+ function tagAttributes(inside: string): Array<[string, string, number]> {
846
+ const out: Array<[string, string, number]> = []
566
847
  let i = 0
567
848
  while (i < inside.length) {
568
849
  while (i < inside.length && /[\s/]/.test(inside.charAt(i))) i++
@@ -572,6 +853,7 @@ function tagAttributes(inside: string): Array<[string, string]> {
572
853
  if (name === "") break
573
854
  while (i < inside.length && /\s/.test(inside.charAt(i))) i++
574
855
  let value = ""
856
+ let valueAt = -1
575
857
  if (inside.charAt(i) === "=") {
576
858
  i++
577
859
  while (i < inside.length && /\s/.test(inside.charAt(i))) i++
@@ -579,15 +861,16 @@ function tagAttributes(inside: string): Array<[string, string]> {
579
861
  if (quote === '"' || quote === "'") {
580
862
  const end = inside.indexOf(quote, i + 1)
581
863
  const stop = end < 0 ? inside.length : end
582
- value = inside.slice(i + 1, stop)
864
+ valueAt = i + 1
865
+ value = inside.slice(valueAt, stop)
583
866
  i = stop + 1
584
867
  } else {
585
- const begin = i
868
+ valueAt = i
586
869
  while (i < inside.length && !/\s/.test(inside.charAt(i))) i++
587
- value = inside.slice(begin, i)
870
+ value = inside.slice(valueAt, i)
588
871
  }
589
872
  }
590
- out.push([name, value])
873
+ out.push([name, value, valueAt])
591
874
  }
592
875
  return out
593
876
  }
@@ -596,6 +879,7 @@ interface OpenElement {
596
879
  readonly tag: string
597
880
  readonly island: string | null | typeof UNKNOWN
598
881
  readonly slot: SwapMarkup | undefined
882
+ readonly ignored: boolean
599
883
  /** Where the element's content starts in the template text. */
600
884
  readonly start: number
601
885
  }
@@ -612,12 +896,35 @@ function ownerOf(stack: readonly OpenElement[]): Pick<MarkupBinding, "island" |
612
896
  return { island: null, slot: null }
613
897
  }
614
898
 
899
+ /** What a template host passes as props, read from its `data-island-props` attribute: the JSON text
900
+ * it holds, or the one interpolation that is its whole value. */
901
+ function hostProps(
902
+ ts: typeof TSApi,
903
+ attributes: ReadonlyArray<readonly [string, string, number]>,
904
+ inside: string,
905
+ nameLength: number,
906
+ holes: readonly TSApi.Expression[],
907
+ positions: readonly number[],
908
+ tagAt: number,
909
+ ): PropsPassed | "absent" | "unknown" {
910
+ const found = attributes.find(([name]) => name === "data-island-props")
911
+ if (found === undefined) return "absent"
912
+ const [, value, valueAt] = found
913
+ if (!value.includes(HOLE)) return passedFromJson(value) ?? "unknown"
914
+ if (value.trim() !== HOLE) return "unknown"
915
+ // Interpolations come in text order: those before this tag, then those inside it ahead of the value.
916
+ const before = positions.filter((position) => position < tagAt).length
917
+ const inTag = inside.slice(0, nameLength + valueAt).split(HOLE).length - 1
918
+ return passedFromExpression(ts, holes[before + inTag]) ?? "unknown"
919
+ }
920
+
615
921
  /**
616
922
  * The bindings, swap links, forms and slots of an `html` template's text, and whether it holds an
617
923
  * island host. `holes` are the template's interpolations, in order; `placement` learns where each
618
924
  * one sits. A linear scan: an unclosed tag ends it.
619
925
  */
620
926
  function templateMarkup(
927
+ ts: typeof TSApi,
621
928
  text: string,
622
929
  holes: readonly TSApi.Expression[],
623
930
  node: TSApi.Node,
@@ -627,6 +934,7 @@ function templateMarkup(
627
934
  const stack: OpenElement[] = []
628
935
  const bindings: MarkupBinding[] = []
629
936
  const swaps: SwapMarkup[] = []
937
+ const hosts: MarkupHost[] = []
630
938
  const positions: number[] = []
631
939
  for (let i = text.indexOf(HOLE); i >= 0; i = text.indexOf(HOLE, i + 1)) positions.push(i)
632
940
  const holesIn = (from: number, to: number): TSApi.Expression[] => {
@@ -654,6 +962,12 @@ function templateMarkup(
654
962
  let hasHost = false
655
963
  let at = text.indexOf("<")
656
964
  while (at >= 0) {
965
+ if (text.startsWith("<!--", at)) {
966
+ const comment = htmlCommentAt(text, at)
967
+ if (comment === null) break
968
+ at = text.indexOf("<", at + comment[0].length)
969
+ continue
970
+ }
657
971
  let end = at + 1
658
972
  let quote = ""
659
973
  while (end < text.length) {
@@ -671,7 +985,8 @@ function templateMarkup(
671
985
  const tagAt = at
672
986
  at = text.indexOf("<", end + 1)
673
987
  if (inside.startsWith("/")) {
674
- const tag = inside.slice(1).trim().toLowerCase()
988
+ const tag = /^\/([a-z][\w-]*)/i.exec(inside)?.[1]?.toLowerCase()
989
+ if (tag === undefined) continue
675
990
  const open = stack.map((entry) => entry.tag).lastIndexOf(tag)
676
991
  if (open >= 0) close(open, tagAt)
677
992
  continue
@@ -683,15 +998,26 @@ function templateMarkup(
683
998
  attributes.find(([key]) => key === name)?.[1]
684
999
  const literal = (value: string): string | typeof UNKNOWN =>
685
1000
  value.includes(HOLE) ? UNKNOWN : value
686
- const hostId = attribute("data-island")
1001
+ const ignored = stack.at(-1)?.ignored === true || attribute("data-island-ignore") !== undefined
1002
+ const hostId = ignored ? undefined : attribute("data-island")
687
1003
  const island = hostId === undefined ? null : literal(hostId)
688
1004
  if (island !== null) hasHost = true
689
1005
  const owner = ownerOf(stack)
690
1006
  const line = lineAt(tagAt)
691
- for (const [name, value] of attributes) {
692
- if (!name.startsWith("data-bind-")) continue
693
- const names = bindingNames(name, value)
694
- if (names !== undefined) bindings.push({ attribute: name, names, ...owner, node, line })
1007
+ if (island !== null) {
1008
+ // An attribute name that holds an interpolation may add props this cannot see.
1009
+ const props = attributes.some(([name]) => name.includes(HOLE))
1010
+ ? "unknown"
1011
+ : hostProps(ts, attributes, inside, tag.length, holes, positions, tagAt)
1012
+ hosts.push({ id: island, props, node, line })
1013
+ }
1014
+ if (!ignored) {
1015
+ for (const [name, value] of attributes) {
1016
+ if (!name.startsWith("data-bind-")) continue
1017
+ const names = bindingNames(name, value)
1018
+ if (names !== undefined)
1019
+ bindings.push({ attribute: name, names, tag, ...owner, node, line })
1020
+ }
695
1021
  }
696
1022
  const swap = attribute("data-swap")
697
1023
  const url = tag === "a" ? attribute("href") : tag === "form" ? attribute("action") : undefined
@@ -703,10 +1029,12 @@ function templateMarkup(
703
1029
  ? undefined
704
1030
  : { kind: "slot", tag, value: literal(slotName), url: undefined, jsx: false, node, line }
705
1031
  if (slot !== undefined) swaps.push(slot)
706
- if (!VOID_ELEMENTS.has(tag) && !inside.trimEnd().endsWith("/"))
707
- stack.push({ tag, island, slot, start: end + 1 })
1032
+ const textElement = TEXT_ELEMENTS.has(tag)
1033
+ if (!VOID_ELEMENTS.has(tag) && (textElement || !inside.trimEnd().endsWith("/")))
1034
+ stack.push({ tag, island, slot, ignored, start: end + 1 })
1035
+ if (textElement) at = textElementClose(text, tag, end + 1)
708
1036
  }
709
- return { hasHost, bindings, swaps }
1037
+ return { hasHost, hosts, bindings, swaps }
710
1038
  }
711
1039
 
712
1040
  /** The source line of an offset in a template's text: its newlines, and the lines each
@@ -746,61 +1074,115 @@ function templateText(ts: typeof TSApi, template: TSApi.TemplateLiteral): string
746
1074
  return text
747
1075
  }
748
1076
 
749
- /** The literal string value of a JSX attribute, `undefined` when absent, UNKNOWN when not literal. */
750
- function jsxAttribute(
1077
+ /** The last explicit attribute, unless a later spread can replace it. */
1078
+ function jsxAttributeNode(
751
1079
  ts: typeof TSApi,
752
1080
  element: TSApi.JsxOpeningLikeElement,
753
1081
  name: string,
754
- ): string | undefined | typeof UNKNOWN {
755
- for (const attr of element.attributes.properties) {
756
- if (ts.isJsxSpreadAttribute(attr)) continue
757
- if (attr.name.getText() !== name) continue
758
- const init = attr.initializer
759
- if (init === undefined) return ""
760
- if (ts.isStringLiteral(init)) return init.text
761
- if (
762
- ts.isJsxExpression(init) &&
763
- init.expression !== undefined &&
764
- ts.isStringLiteralLike(init.expression)
765
- )
766
- return init.expression.text
767
- return UNKNOWN
1082
+ ): TSApi.JsxAttribute | undefined | typeof UNKNOWN {
1083
+ let spread = false
1084
+ const { properties } = element.attributes
1085
+ for (let index = properties.length - 1; index >= 0; index--) {
1086
+ const attr = properties[index]
1087
+ if (attr === undefined) continue
1088
+ if (ts.isJsxSpreadAttribute(attr)) spread = true
1089
+ else if (attr.name.getText() === name) return spread ? UNKNOWN : attr
768
1090
  }
769
1091
  return undefined
770
1092
  }
771
1093
 
1094
+ /** The literal string value of a JSX attribute, `undefined` when absent, UNKNOWN when uncertain. */
1095
+ function jsxAttribute(
1096
+ ts: typeof TSApi,
1097
+ element: TSApi.JsxOpeningLikeElement,
1098
+ name: string,
1099
+ ): string | undefined | typeof UNKNOWN {
1100
+ const attr = jsxAttributeNode(ts, element, name)
1101
+ if (attr === undefined || attr === UNKNOWN) return attr
1102
+ const init = attr.initializer
1103
+ if (init === undefined) return ""
1104
+ if (ts.isStringLiteral(init)) return init.text
1105
+ if (
1106
+ ts.isJsxExpression(init) &&
1107
+ init.expression !== undefined &&
1108
+ ts.isStringLiteralLike(init.expression)
1109
+ )
1110
+ return init.expression.text
1111
+ return UNKNOWN
1112
+ }
1113
+
1114
+ /** What a JSX host passes in its `props` (an `<Island>`) or `data-island-props` attribute. */
1115
+ function jsxHostProps(
1116
+ ts: typeof TSApi,
1117
+ element: TSApi.JsxOpeningLikeElement,
1118
+ name: string,
1119
+ ): PropsPassed | "absent" | "unknown" {
1120
+ const attr = jsxAttributeNode(ts, element, name)
1121
+ if (attr === UNKNOWN) return "unknown"
1122
+ if (attr === undefined)
1123
+ return element.attributes.properties.some(ts.isJsxSpreadAttribute) ? "unknown" : "absent"
1124
+ const init = attr.initializer
1125
+ if (init === undefined) return "unknown"
1126
+ if (ts.isStringLiteral(init)) return passedFromJson(init.text) ?? "unknown"
1127
+ if (!ts.isJsxExpression(init) || init.expression === undefined) return "unknown"
1128
+ const expression = init.expression
1129
+ if (ts.isStringLiteralLike(expression)) return passedFromJson(expression.text) ?? "unknown"
1130
+ return passedFromExpression(ts, expression) ?? "unknown"
1131
+ }
1132
+
772
1133
  /** The bindings, swap links, forms and slots of one outermost JSX tree, and whether it holds an
773
1134
  * island host. */
774
1135
  function jsxMarkup(
775
1136
  ts: typeof TSApi,
776
1137
  root: TSApi.JsxElement | TSApi.JsxSelfClosingElement | TSApi.JsxFragment,
1138
+ islandNames: ReadonlySet<string>,
777
1139
  ): MarkupFacts {
778
1140
  const bindings: MarkupBinding[] = []
779
1141
  const swaps: SwapMarkup[] = []
1142
+ const hosts: MarkupHost[] = []
780
1143
  let hasHost = false
781
- const walk = (node: TSApi.Node, owner: Pick<MarkupBinding, "island" | "slot">): void => {
1144
+ const walk = (
1145
+ node: TSApi.Node,
1146
+ owner: Pick<MarkupBinding, "island" | "slot">,
1147
+ parentIgnored: boolean,
1148
+ ): void => {
782
1149
  let opening: TSApi.JsxOpeningLikeElement | undefined
783
1150
  if (ts.isJsxElement(node)) opening = node.openingElement
784
1151
  else if (ts.isJsxSelfClosingElement(node)) opening = node
1152
+ const ignored =
1153
+ parentIgnored ||
1154
+ (opening !== undefined && jsxAttribute(ts, opening, "data-island-ignore") !== undefined)
785
1155
  let inner = owner
786
1156
  if (opening !== undefined) {
787
1157
  const tag = opening.tagName.getText()
788
- const host =
789
- tag === "Island"
790
- ? jsxAttribute(ts, opening, "id")
791
- : jsxAttribute(ts, opening, "data-island")
1158
+ const nifraIsland = islandNames.has(tag)
1159
+ const host = ignored
1160
+ ? undefined
1161
+ : jsxAttribute(ts, opening, nifraIsland || tag === "Island" ? "id" : "data-island")
792
1162
  if (host !== undefined) {
793
1163
  hasHost = true
794
1164
  inner = { island: host, slot: null }
1165
+ // An `Island` that is not Nifra's own may take other props, so only Nifra's is judged.
1166
+ if (tag !== "Island" || nifraIsland)
1167
+ hosts.push({
1168
+ id: host,
1169
+ props: jsxHostProps(ts, opening, nifraIsland ? "props" : "data-island-props"),
1170
+ node: opening,
1171
+ })
795
1172
  }
796
- for (const attr of opening.attributes.properties) {
797
- if (ts.isJsxSpreadAttribute(attr)) continue
798
- const name = attr.name.getText()
799
- if (!name.startsWith("data-bind-")) continue
800
- const value = jsxAttribute(ts, opening, name)
801
- if (value === UNKNOWN || value === undefined) continue
802
- const names = bindingNames(name, value)
803
- if (names !== undefined) bindings.push({ attribute: name, names, ...owner, node: attr })
1173
+ if (!ignored) {
1174
+ const domTag =
1175
+ ts.isIdentifier(opening.tagName) && /^[a-z]/.test(tag) ? tag.toLowerCase() : undefined
1176
+ for (const attr of opening.attributes.properties) {
1177
+ if (ts.isJsxSpreadAttribute(attr)) continue
1178
+ const name = attr.name.getText()
1179
+ if (!name.startsWith("data-bind-")) continue
1180
+ const value = jsxAttribute(ts, opening, name)
1181
+ if (value === UNKNOWN || value === undefined) continue
1182
+ const names = bindingNames(name, value)
1183
+ if (names !== undefined)
1184
+ bindings.push({ attribute: name, names, tag: domTag, ...owner, node: attr })
1185
+ }
804
1186
  }
805
1187
  const swap = jsxAttribute(ts, opening, "data-swap")
806
1188
  if (swap !== undefined) {
@@ -818,10 +1200,10 @@ function jsxMarkup(
818
1200
  inner = { island: inner.island, slot }
819
1201
  }
820
1202
  }
821
- ts.forEachChild(node, (child) => walk(child, inner))
1203
+ ts.forEachChild(node, (child) => walk(child, inner, ignored))
822
1204
  }
823
- walk(root, { island: null, slot: null })
824
- return { hasHost, bindings, swaps }
1205
+ walk(root, { island: null, slot: null }, false)
1206
+ return { hasHost, hosts, bindings, swaps }
825
1207
  }
826
1208
 
827
1209
  /** The `slot` names a file imports from `@nifrajs/web-vanilla`. */
@@ -842,6 +1224,26 @@ function slotImports(ts: typeof TSApi, tree: TSApi.SourceFile): Set<string> {
842
1224
  return names
843
1225
  }
844
1226
 
1227
+ /** Local names for `Island` from Nifra packages such as `@nifrajs/web-react/island`. */
1228
+ function islandImports(ts: typeof TSApi, tree: TSApi.SourceFile): Set<string> {
1229
+ const names = new Set<string>()
1230
+ for (const statement of tree.statements) {
1231
+ if (
1232
+ !ts.isImportDeclaration(statement) ||
1233
+ !ts.isStringLiteral(statement.moduleSpecifier) ||
1234
+ !statement.moduleSpecifier.text.startsWith("@nifrajs/")
1235
+ )
1236
+ continue
1237
+ const bound = statement.importClause?.namedBindings
1238
+ if (statement.importClause?.isTypeOnly || bound === undefined || !ts.isNamedImports(bound))
1239
+ continue
1240
+ for (const element of bound.elements)
1241
+ if (!element.isTypeOnly && (element.propertyName ?? element.name).text === "Island")
1242
+ names.add(element.name.text)
1243
+ }
1244
+ return names
1245
+ }
1246
+
845
1247
  /** One `slot(name, content)` call of `@nifrajs/web-vanilla`. */
846
1248
  export interface SlotCall {
847
1249
  readonly name: string | typeof UNKNOWN
@@ -854,18 +1256,22 @@ function fileMarkup(
854
1256
  tree: TSApi.SourceFile,
855
1257
  ): {
856
1258
  readonly bindings: MarkupBinding[]
1259
+ readonly hosts: MarkupHost[]
857
1260
  readonly swaps: SwapMarkup[]
858
1261
  readonly slotCalls: SlotCall[]
859
1262
  readonly placement: Map<TSApi.Node, HolePlacement>
860
1263
  } {
861
1264
  const bindings: MarkupBinding[] = []
1265
+ const hosts: MarkupHost[] = []
862
1266
  const swaps: SwapMarkup[] = []
863
1267
  const slotCalls: SlotCall[] = []
864
1268
  const placement = new Map<TSApi.Node, HolePlacement>()
865
1269
  const slotNames = slotImports(ts, tree)
1270
+ const islandNames = islandImports(ts, tree)
866
1271
  const take = (found: MarkupFacts): void => {
867
1272
  // A fragment with no host of its own may render inside one elsewhere, so it is not judged.
868
1273
  if (found.hasHost) bindings.push(...found.bindings)
1274
+ hosts.push(...found.hosts)
869
1275
  swaps.push(...found.swaps)
870
1276
  }
871
1277
  const visit = (node: TSApi.Node): void => {
@@ -878,13 +1284,13 @@ function fileMarkup(
878
1284
  const text = templateText(ts, node.template)
879
1285
  const holes = spans.map((span) => span.expression)
880
1286
  const lineAt = templateLines(tree, node.template, text, spans)
881
- take(templateMarkup(text, holes, node, placement, lineAt))
1287
+ take(templateMarkup(ts, text, holes, node, placement, lineAt))
882
1288
  } else if (
883
1289
  ts.isJsxElement(node) ||
884
1290
  ts.isJsxSelfClosingElement(node) ||
885
1291
  ts.isJsxFragment(node)
886
1292
  ) {
887
- take(jsxMarkup(ts, node))
1293
+ take(jsxMarkup(ts, node, islandNames))
888
1294
  return
889
1295
  } else if (
890
1296
  ts.isCallExpression(node) &&
@@ -900,7 +1306,7 @@ function fileMarkup(
900
1306
  ts.forEachChild(node, visit)
901
1307
  }
902
1308
  visit(tree)
903
- return { bindings, swaps, slotCalls, placement }
1309
+ return { bindings, hosts, swaps, slotCalls, placement }
904
1310
  }
905
1311
 
906
1312
  interface MarkupFinding {
@@ -910,6 +1316,13 @@ interface MarkupFinding {
910
1316
  readonly islands: Map<string, IslandNames | typeof UNKNOWN>
911
1317
  }
912
1318
 
1319
+ interface HostFinding {
1320
+ readonly file: string
1321
+ readonly line: number
1322
+ readonly host: MarkupHost
1323
+ readonly islands: Map<string, IslandNames | typeof UNKNOWN>
1324
+ }
1325
+
913
1326
  /** A file's markup facts, for the swap rules. */
914
1327
  export interface FileMarkup {
915
1328
  readonly file: string
@@ -921,6 +1334,9 @@ export interface FileMarkup {
921
1334
 
922
1335
  export interface ProjectMarkup {
923
1336
  readonly bindings: readonly MarkupFinding[]
1337
+ readonly hosts: readonly HostFinding[]
1338
+ /** Whether every island id the project mounts could be read, and from how many mount calls. */
1339
+ readonly mounts: Pick<MountSurface, "open" | "calls">
924
1340
  readonly files: readonly FileMarkup[]
925
1341
  }
926
1342
 
@@ -938,7 +1354,12 @@ export async function projectMarkup(
938
1354
  readonly ts?: typeof TSApi
939
1355
  readonly markup: ProjectMarkup
940
1356
  }> {
941
- const empty: ProjectMarkup = { bindings: [], files: [] }
1357
+ const empty: ProjectMarkup = {
1358
+ bindings: [],
1359
+ hosts: [],
1360
+ mounts: { open: true, calls: 0 },
1361
+ files: [],
1362
+ }
942
1363
  const loaded = await loadRuleTypeScript(ctx)
943
1364
  if (loaded.diagnostics !== undefined) return { diagnostics: loaded.diagnostics, markup: empty }
944
1365
  const ts = loaded.compiler
@@ -950,19 +1371,23 @@ export async function projectMarkup(
950
1371
  const tree = parsedTree(ts, ctx.project.source, file)
951
1372
  if (tree !== undefined) trees.push({ file, tree })
952
1373
  }
953
- const islands = mountedIslands(
1374
+ const surface = mountedIslands(
954
1375
  ts,
955
1376
  trees.map((entry) => entry.tree),
956
1377
  )
1378
+ const islands = surface.ids
957
1379
  const bindings: MarkupFinding[] = []
1380
+ const hosts: HostFinding[] = []
958
1381
  const files: FileMarkup[] = []
959
1382
  for (const { file, tree } of trees) {
960
1383
  const found = fileMarkup(ts, tree)
961
1384
  for (const binding of found.bindings)
962
1385
  bindings.push({ file, line: binding.line ?? lineOf(tree, binding.node), binding, islands })
1386
+ for (const host of found.hosts)
1387
+ hosts.push({ file, line: host.line ?? lineOf(tree, host.node), host, islands })
963
1388
  files.push({ file, tree, ...found })
964
1389
  }
965
- const markup = { bindings, files }
1390
+ const markup = { bindings, hosts, mounts: { open: surface.open, calls: surface.calls }, files }
966
1391
  markupCache.set(ctx.project.source, markup)
967
1392
  return { ts, markup }
968
1393
  }
@@ -1043,9 +1468,196 @@ export const islandBindingHostRule: CheckRule = {
1043
1468
  },
1044
1469
  }
1045
1470
 
1471
+ /**
1472
+ * Whether a `nifra-expect <code>: <reason>` comment covers the finding on `line` of a file: a comment
1473
+ * on its line or in the comment block above it. In an html template, use whole-line HTML comments
1474
+ * in the block above the host.
1475
+ * `no-reason` is a marker with nothing after its colon, which silences nothing.
1476
+ */
1477
+ function expectationFor(
1478
+ ctx: Parameters<CheckRule["scan"]>[0],
1479
+ cache: Map<string, readonly string[]>,
1480
+ file: string,
1481
+ line: number,
1482
+ code: string,
1483
+ ): "silenced" | "no-reason" | undefined {
1484
+ let lines = cache.get(file)
1485
+ if (lines === undefined) {
1486
+ lines = (ctx.project.source.read(file) ?? "").split("\n").map((text) => {
1487
+ const start = text.search(/\S/)
1488
+ if (start < 0) return text
1489
+ const comment = htmlCommentAt(text, start)
1490
+ if (comment === null || text.slice(start + comment[0].length).trim() !== "") return text
1491
+ return `//${comment[1] ?? ""}`
1492
+ })
1493
+ cache.set(file, lines)
1494
+ }
1495
+ const found = commentBlockExpectation(lines, line, [code])
1496
+ if (found === undefined) return undefined
1497
+ return found.reason === undefined ? "no-reason" : "silenced"
1498
+ }
1499
+
1500
+ export const islandUnmountedRule: CheckRule = {
1501
+ code: "NF-C064",
1502
+ title: "Island host with no enhancer",
1503
+ async scan(ctx) {
1504
+ const { diagnostics, markup } = await projectMarkup(
1505
+ ctx,
1506
+ "NF-C064",
1507
+ "Island host with no enhancer",
1508
+ )
1509
+ if (diagnostics !== undefined) return diagnostics
1510
+ // Judged only when every mount call is a literal map: a map held in a variable may name any id.
1511
+ if (markup.mounts.open || markup.mounts.calls === 0) return []
1512
+ const findings: Diagnostic[] = []
1513
+ const lines = new Map<string, readonly string[]>()
1514
+ for (const { file, line, host, islands } of markup.hosts) {
1515
+ if (typeof host.id !== "string" || islands.has(host.id)) continue
1516
+ const expected = expectationFor(ctx, lines, file, line, "NF-C064")
1517
+ if (expected === "silenced") continue
1518
+ const mounted = [...islands.keys()]
1519
+ findings.push(
1520
+ diagnostic({
1521
+ code: "NF-C064",
1522
+ severity: "warn",
1523
+ file,
1524
+ line,
1525
+ message: `island "${host.id}" has no enhancer: no mountIslands call in the project names it, so the host is never enhanced${expected === undefined ? "" : `; ${MISSING_EXPECTATION_REASON}`}`,
1526
+ evidence: [
1527
+ `data-island="${host.id}"`,
1528
+ mounted.length === 0 ? "mounted: none" : `mounted: ${mounted.slice(0, 8).join(", ")}`,
1529
+ ],
1530
+ fix: { recipe: "islands.host-enhancer", command: "nifra_docs islands" },
1531
+ verify: "nifra check --lints-only",
1532
+ }),
1533
+ )
1534
+ }
1535
+ return findings
1536
+ },
1537
+ }
1538
+
1539
+ /** The way one prop disagrees with its declaration, or `undefined` when it agrees or cannot be told. */
1540
+ function propMismatch(
1541
+ key: string,
1542
+ passed: PropKind | "null" | undefined,
1543
+ declared: { readonly kind: PropKind | undefined } | undefined,
1544
+ ): string | undefined {
1545
+ if (declared === undefined)
1546
+ return `passes "${key}", which the enhancer's props type does not declare`
1547
+ if (passed === undefined || declared.kind === undefined || passed === declared.kind)
1548
+ return undefined
1549
+ return `passes "${key}" as ${passed === "null" ? "null" : `a ${passed}`}, which the enhancer declares as ${declared.kind}`
1550
+ }
1551
+
1552
+ export const islandPropsRule: CheckRule = {
1553
+ code: "NF-C065",
1554
+ title: "Island props differ from the enhancer's props type",
1555
+ async scan(ctx) {
1556
+ const { diagnostics, markup } = await projectMarkup(
1557
+ ctx,
1558
+ "NF-C065",
1559
+ "Island props differ from the enhancer's props type",
1560
+ )
1561
+ if (diagnostics !== undefined) return diagnostics
1562
+ const findings: Diagnostic[] = []
1563
+ const lines = new Map<string, readonly string[]>()
1564
+ for (const { file, line, host, islands } of markup.hosts) {
1565
+ if (typeof host.id !== "string" || host.props === "unknown") continue
1566
+ const names = islands.get(host.id)
1567
+ const shape = names === undefined || names === UNKNOWN ? undefined : names.props
1568
+ if (shape === undefined) continue
1569
+ const passed: PropsPassed =
1570
+ host.props === "absent" ? { entries: new Map(), open: false } : host.props
1571
+ const problems: string[] = []
1572
+ for (const [key, declared] of shape)
1573
+ if (
1574
+ !declared.optional &&
1575
+ (passed.entries.get(key) === OMITTED || (!passed.open && !passed.entries.has(key)))
1576
+ )
1577
+ problems.push(`does not pass "${key}", which the enhancer's props type requires`)
1578
+ for (const [key, kind] of passed.entries) {
1579
+ if (kind === OMITTED) continue
1580
+ const problem = propMismatch(key, kind, shape.get(key))
1581
+ if (problem !== undefined) problems.push(problem)
1582
+ }
1583
+ const expected =
1584
+ problems.length === 0 ? undefined : expectationFor(ctx, lines, file, line, "NF-C065")
1585
+ if (expected === "silenced") continue
1586
+ for (const problem of problems)
1587
+ findings.push(
1588
+ diagnostic({
1589
+ code: "NF-C065",
1590
+ severity: "warn",
1591
+ file,
1592
+ line,
1593
+ message: `island "${host.id}" ${problem}${expected === undefined ? "" : `; ${MISSING_EXPECTATION_REASON}`}`,
1594
+ evidence: [`data-island="${host.id}"`],
1595
+ fix: { recipe: "islands.props-contract", command: "nifra_docs islands" },
1596
+ verify: "nifra check --lints-only",
1597
+ }),
1598
+ )
1599
+ }
1600
+ return findings
1601
+ },
1602
+ }
1603
+
1604
+ // The runtime refuses the same bindings (`island-bindings.ts`): `on*` and `srcdoc` attributes, and a
1605
+ // URL attribute on an element that loads code or rebases the page.
1606
+ export const REFUSED_CODE_ATTRIBUTE = /^(?:on|srcdoc$)/i
1607
+ export const REFUSED_URL_ATTRIBUTE =
1608
+ /^(?:href|src|action|formaction|xlink:href|poster|data|background|cite|ping)$/i
1609
+ export const REFUSED_URL_TAG = new Set(["script", "iframe", "object", "embed", "link", "base"])
1610
+ export const REFUSED_SMIL_TAG = new Set(["set", "animate", "animatemotion", "animatetransform"])
1611
+
1612
+ export const islandRefusedBindingRule: CheckRule = {
1613
+ code: "NF-C066",
1614
+ title: "Island attribute binding the runtime refuses",
1615
+ async scan(ctx) {
1616
+ const { diagnostics, markup } = await projectMarkup(
1617
+ ctx,
1618
+ "NF-C066",
1619
+ "Island attribute binding the runtime refuses",
1620
+ )
1621
+ if (diagnostics !== undefined) return diagnostics
1622
+ const findings: Diagnostic[] = []
1623
+ for (const { file, line, binding } of markup.bindings) {
1624
+ if (binding.attribute !== "data-bind-attr") continue
1625
+ for (const { key } of binding.names) {
1626
+ if (key === undefined) continue
1627
+ let reason: string | undefined
1628
+ if (REFUSED_CODE_ATTRIBUTE.test(key))
1629
+ reason = `"${key}" is an event-handler or srcdoc attribute, which a binding never sets`
1630
+ else if (binding.tag !== undefined) {
1631
+ if (REFUSED_URL_ATTRIBUTE.test(key) && REFUSED_URL_TAG.has(binding.tag))
1632
+ reason = `"${key}" is a URL attribute on <${binding.tag}>, which loads code or rebases the page`
1633
+ else if (REFUSED_SMIL_TAG.has(binding.tag))
1634
+ reason = `"${key}" is bound on <${binding.tag}>: an SVG animation sets another element's attributes from its own, so a bound value can animate a link's href to a script URL`
1635
+ }
1636
+ if (reason === undefined) continue
1637
+ findings.push(
1638
+ diagnostic({
1639
+ code: "NF-C066",
1640
+ severity: "warn",
1641
+ file,
1642
+ line,
1643
+ message: `data-bind-attr never binds: ${reason}. The runtime skips it with a console warning`,
1644
+ evidence: [`data-bind-attr -> ${key}`],
1645
+ fix: { recipe: "islands.refused-binding", command: "nifra_docs islands" },
1646
+ verify: "nifra check --lints-only",
1647
+ }),
1648
+ )
1649
+ }
1650
+ }
1651
+ return findings
1652
+ },
1653
+ }
1654
+
1046
1655
  export const islandRules = Object.freeze([
1047
1656
  islandCleanupRule,
1048
1657
  islandListKeyRule,
1049
1658
  islandBindingNameRule,
1050
1659
  islandBindingHostRule,
1660
+ islandUnmountedRule,
1661
+ islandPropsRule,
1662
+ islandRefusedBindingRule,
1051
1663
  ])