@nifrajs/cli 5.5.0 → 5.6.0

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 (143) hide show
  1. package/CHANGELOG.md +172 -0
  2. package/dist/cdn-check.d.ts +7 -6
  3. package/dist/cdn-check.d.ts.map +1 -1
  4. package/dist/cdn-check.js +13 -1
  5. package/dist/cdn-check.js.map +1 -1
  6. package/dist/check-diagnostics.d.ts.map +1 -1
  7. package/dist/check-diagnostics.js +5 -3
  8. package/dist/check-diagnostics.js.map +1 -1
  9. package/dist/check-scan.d.ts.map +1 -1
  10. package/dist/check-scan.js +29 -2
  11. package/dist/check-scan.js.map +1 -1
  12. package/dist/cli.d.ts.map +1 -1
  13. package/dist/cli.js +14 -5
  14. package/dist/cli.js.map +1 -1
  15. package/dist/command-catalog.d.ts +14 -1
  16. package/dist/command-catalog.d.ts.map +1 -1
  17. package/dist/command-catalog.js +93 -0
  18. package/dist/command-catalog.js.map +1 -1
  19. package/dist/doctor.d.ts +9 -1
  20. package/dist/doctor.d.ts.map +1 -1
  21. package/dist/doctor.js +52 -0
  22. package/dist/doctor.js.map +1 -1
  23. package/dist/fix-recipes.d.ts.map +1 -1
  24. package/dist/fix-recipes.js +27 -0
  25. package/dist/fix-recipes.js.map +1 -1
  26. package/dist/frontend-guidance.d.ts +1 -1
  27. package/dist/frontend-guidance.d.ts.map +1 -1
  28. package/dist/frontend-guidance.js +17 -8
  29. package/dist/frontend-guidance.js.map +1 -1
  30. package/dist/internal/source-facts.d.ts +2 -1
  31. package/dist/internal/source-facts.d.ts.map +1 -1
  32. package/dist/internal/source-facts.js +17 -11
  33. package/dist/internal/source-facts.js.map +1 -1
  34. package/dist/introspect.js +1 -1
  35. package/dist/introspect.js.map +1 -1
  36. package/dist/load.d.ts +4 -1
  37. package/dist/load.d.ts.map +1 -1
  38. package/dist/load.js +8 -0
  39. package/dist/load.js.map +1 -1
  40. package/dist/mcp-exec.d.ts.map +1 -1
  41. package/dist/mcp-exec.js +57 -3
  42. package/dist/mcp-exec.js.map +1 -1
  43. package/dist/mcp-http.d.ts +2 -1
  44. package/dist/mcp-http.d.ts.map +1 -1
  45. package/dist/mcp-http.js +3 -2
  46. package/dist/mcp-http.js.map +1 -1
  47. package/dist/mcp-tool-meta.d.ts +21 -0
  48. package/dist/mcp-tool-meta.d.ts.map +1 -0
  49. package/dist/mcp-tool-meta.js +246 -0
  50. package/dist/mcp-tool-meta.js.map +1 -0
  51. package/dist/mcp.d.ts +3 -2
  52. package/dist/mcp.d.ts.map +1 -1
  53. package/dist/mcp.js +36 -98
  54. package/dist/mcp.js.map +1 -1
  55. package/dist/output-schemas.d.ts +8 -0
  56. package/dist/output-schemas.d.ts.map +1 -1
  57. package/dist/output-schemas.js +14 -0
  58. package/dist/output-schemas.js.map +1 -1
  59. package/dist/ppr-read-fix.d.ts +8 -0
  60. package/dist/ppr-read-fix.d.ts.map +1 -0
  61. package/dist/ppr-read-fix.js +356 -0
  62. package/dist/ppr-read-fix.js.map +1 -0
  63. package/dist/recipes/5.6.0.d.ts +10 -0
  64. package/dist/recipes/5.6.0.d.ts.map +1 -0
  65. package/dist/recipes/5.6.0.js +68 -0
  66. package/dist/recipes/5.6.0.js.map +1 -0
  67. package/dist/recipes/index.d.ts.map +1 -1
  68. package/dist/recipes/index.js +2 -0
  69. package/dist/recipes/index.js.map +1 -1
  70. package/dist/review.d.ts.map +1 -1
  71. package/dist/review.js +7 -2
  72. package/dist/review.js.map +1 -1
  73. package/dist/rules/codes.d.ts +8 -3
  74. package/dist/rules/codes.d.ts.map +1 -1
  75. package/dist/rules/codes.js +9 -4
  76. package/dist/rules/codes.js.map +1 -1
  77. package/dist/rules/explanations.d.ts.map +1 -1
  78. package/dist/rules/explanations.js +39 -13
  79. package/dist/rules/explanations.js.map +1 -1
  80. package/dist/rules/islands.d.ts +91 -0
  81. package/dist/rules/islands.d.ts.map +1 -1
  82. package/dist/rules/islands.js +757 -3
  83. package/dist/rules/islands.js.map +1 -1
  84. package/dist/rules/ppr.d.ts +16 -0
  85. package/dist/rules/ppr.d.ts.map +1 -0
  86. package/dist/rules/ppr.js +114 -0
  87. package/dist/rules/ppr.js.map +1 -0
  88. package/dist/rules/swap.d.ts +7 -0
  89. package/dist/rules/swap.d.ts.map +1 -0
  90. package/dist/rules/swap.js +338 -0
  91. package/dist/rules/swap.js.map +1 -0
  92. package/dist/scaffold-hole.d.ts +105 -0
  93. package/dist/scaffold-hole.d.ts.map +1 -0
  94. package/dist/scaffold-hole.js +688 -0
  95. package/dist/scaffold-hole.js.map +1 -0
  96. package/dist/scaffold.d.ts +5 -3
  97. package/dist/scaffold.d.ts.map +1 -1
  98. package/dist/scaffold.js +56 -46
  99. package/dist/scaffold.js.map +1 -1
  100. package/dist/verification-plan.d.ts.map +1 -1
  101. package/dist/verification-plan.js +12 -1
  102. package/dist/verification-plan.js.map +1 -1
  103. package/dist/web-app-options.d.ts +1 -1
  104. package/dist/web-app-options.d.ts.map +1 -1
  105. package/dist/web-app-options.js.map +1 -1
  106. package/docs/examples.json +63 -9
  107. package/docs/llms-full.txt +2070 -437
  108. package/docs/types.json +1300 -270
  109. package/package.json +11 -11
  110. package/src/cdn-check.ts +21 -7
  111. package/src/check-diagnostics.ts +5 -3
  112. package/src/check-scan.ts +34 -3
  113. package/src/cli.ts +14 -5
  114. package/src/command-catalog.ts +126 -0
  115. package/src/doctor.ts +44 -1
  116. package/src/fix-recipes.ts +28 -0
  117. package/src/frontend-guidance.ts +20 -8
  118. package/src/internal/source-facts.ts +27 -13
  119. package/src/introspect.ts +1 -1
  120. package/src/load.ts +13 -0
  121. package/src/mcp-exec.ts +74 -3
  122. package/src/mcp-http.ts +3 -2
  123. package/src/mcp-tool-meta.ts +268 -0
  124. package/src/mcp.ts +35 -99
  125. package/src/output-schemas.ts +28 -0
  126. package/src/ppr-read-fix.ts +426 -0
  127. package/src/recipes/5.6.0.ts +69 -0
  128. package/src/recipes/index.ts +2 -0
  129. package/src/review.ts +7 -2
  130. package/src/rules/codes.ts +9 -4
  131. package/src/rules/explanations.ts +39 -13
  132. package/src/rules/islands.ts +875 -3
  133. package/src/rules/ppr.ts +143 -0
  134. package/src/rules/swap.ts +402 -0
  135. package/src/scaffold-hole.ts +880 -0
  136. package/src/scaffold.ts +60 -49
  137. package/src/verification-plan.ts +20 -1
  138. package/src/web-app-options.ts +1 -0
  139. package/dist/rules/nano.d.ts +0 -6
  140. package/dist/rules/nano.d.ts.map +0 -1
  141. package/dist/rules/nano.js +0 -273
  142. package/dist/rules/nano.js.map +0 -1
  143. package/src/rules/nano.ts +0 -307
@@ -37,6 +37,8 @@ function parsedTree(ts, sources, file) {
37
37
  files.set(file, { tree });
38
38
  return tree;
39
39
  }
40
+ // The calls that mount islands from an enhancer map.
41
+ const MOUNT_CALLS = new Set(["mountIslands", "enableSwap"]);
40
42
  function isEnhancerFn(ts, node) {
41
43
  return ts.isArrowFunction(node) || ts.isFunctionExpression(node);
42
44
  }
@@ -80,7 +82,7 @@ function returnsCleanup(ts, fn) {
80
82
  return found;
81
83
  }
82
84
  /** The inline enhancer functions authored in `file`: the argument to `defineIsland(...)` and every
83
- * function-typed value of a `mountIslands({ ... })` object literal. */
85
+ * function-typed value of a `mountIslands({ ... })` or `enableSwap({ ... })` object literal. */
84
86
  function enhancerFns(ts, tree) {
85
87
  const out = [];
86
88
  const visit = (node) => {
@@ -91,7 +93,7 @@ function enhancerFns(ts, tree) {
91
93
  if (arg !== undefined && isEnhancerFn(ts, arg))
92
94
  out.push(arg);
93
95
  }
94
- else if (callee === "mountIslands") {
96
+ else if (MOUNT_CALLS.has(callee)) {
95
97
  const arg = node.arguments[0];
96
98
  if (arg !== undefined && ts.isObjectLiteralExpression(arg)) {
97
99
  for (const prop of arg.properties) {
@@ -154,5 +156,757 @@ export const islandCleanupRule = {
154
156
  return findings;
155
157
  },
156
158
  };
157
- export const islandRules = Object.freeze([islandCleanupRule]);
159
+ export const lineOf = (tree, node) => tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1;
160
+ export function toolchainMissing(code, title) {
161
+ return diagnostic({
162
+ code,
163
+ severity: "warn",
164
+ message: `${title} (${code}) did NOT run - TypeScript is not installed, so this report says nothing about it`,
165
+ fix: { recipe: "toolchain.install-typescript", command: "bun add -d typescript" },
166
+ verify: "nifra check --lints-only",
167
+ });
168
+ }
169
+ /** The names a parameter binds: its identifier, or each identifier its destructuring pattern binds. */
170
+ function boundNames(ts, name, out = new Set()) {
171
+ if (ts.isIdentifier(name))
172
+ out.add(name.text);
173
+ else
174
+ for (const element of name.elements)
175
+ if (!ts.isOmittedExpression(element))
176
+ boundNames(ts, element.name, out);
177
+ return out;
178
+ }
179
+ /** Whether `node` reads any of `names` (a property name such as the `id` of `x.id` is not a read). */
180
+ function readsAny(ts, node, names) {
181
+ if (ts.isIdentifier(node))
182
+ return names.has(node.text);
183
+ if (ts.isPropertyAccessExpression(node))
184
+ return readsAny(ts, node.expression, names);
185
+ return ts.forEachChild(node, (child) => readsAny(ts, child, names) || undefined) ?? false;
186
+ }
187
+ const POSITION_CALLS = new Set(["indexOf", "lastIndexOf", "findIndex", "findLastIndex"]);
188
+ /** A `bindList` key that reads a position: a second parameter it reads (`bindList` passes none, so
189
+ * it is always `undefined`) or an `indexOf`-family call handed the item, which shifts on every insert
190
+ * and removal. An `indexOf` over the item's own data, such as `u.email.indexOf("@")`, is not one. */
191
+ function positionalKey(ts, call) {
192
+ const options = call.arguments[2];
193
+ if (options === undefined || !ts.isObjectLiteralExpression(options))
194
+ return undefined;
195
+ for (const prop of options.properties) {
196
+ if (!ts.isPropertyAssignment(prop) ||
197
+ !ts.isIdentifier(prop.name) ||
198
+ prop.name.text !== "key" ||
199
+ !isEnhancerFn(ts, prop.initializer))
200
+ continue;
201
+ const key = prop.initializer;
202
+ const [item, position] = key.parameters;
203
+ if (position !== undefined && readsAny(ts, key.body, boundNames(ts, position.name)))
204
+ return key;
205
+ // A destructured second parameter throws on the `undefined` bindList passes.
206
+ if (position !== undefined && !ts.isIdentifier(position.name))
207
+ return key;
208
+ if (item === undefined)
209
+ continue;
210
+ const itemNames = boundNames(ts, item.name);
211
+ const handedTheItem = (node) => (ts.isCallExpression(node) &&
212
+ ts.isPropertyAccessExpression(node.expression) &&
213
+ POSITION_CALLS.has(node.expression.name.text) &&
214
+ node.arguments.some((arg) => readsAny(ts, arg, itemNames))) ||
215
+ ts.forEachChild(node, handedTheItem);
216
+ if (ts.forEachChild(key, handedTheItem))
217
+ return key;
218
+ }
219
+ return undefined;
220
+ }
221
+ export const islandListKeyRule = {
222
+ code: "NF-C022",
223
+ title: "bindList key check",
224
+ async scan(ctx) {
225
+ const loaded = await loadRuleTypeScript(ctx);
226
+ if (loaded.diagnostics !== undefined)
227
+ return loaded.diagnostics;
228
+ const ts = loaded.compiler;
229
+ if (ts === undefined)
230
+ return [toolchainMissing("NF-C022", "bindList key check")];
231
+ const findings = [];
232
+ for (const file of ctx.project.source.files) {
233
+ const tree = parsedTree(ts, ctx.project.source, file);
234
+ if (tree === undefined)
235
+ continue;
236
+ const visit = (node) => {
237
+ if (ts.isCallExpression(node) &&
238
+ ts.isIdentifier(node.expression) &&
239
+ node.expression.text === "bindList") {
240
+ const key = positionalKey(ts, node);
241
+ if (key !== undefined) {
242
+ findings.push(diagnostic({
243
+ code: "NF-C022",
244
+ severity: "warn",
245
+ file,
246
+ line: lineOf(tree, key),
247
+ message: "bindList key reads the item's position - an insert, removal or reorder hands a node to the wrong item. Key by a stable id on the item instead",
248
+ evidence: ["key: (item, i) => i or key: (item) => list.indexOf(item)"],
249
+ fix: { recipe: "islands.stable-key", command: "nifra_docs islands" },
250
+ verify: "nifra check --lints-only",
251
+ }));
252
+ }
253
+ }
254
+ ts.forEachChild(node, visit);
255
+ };
256
+ visit(tree);
257
+ }
258
+ return findings;
259
+ },
260
+ };
261
+ export const UNKNOWN = Symbol("unknown");
262
+ function literalKeys(ts, node) {
263
+ const keys = new Set();
264
+ for (const prop of node.properties) {
265
+ if (ts.isSpreadAssignment(prop) || prop.name === undefined)
266
+ return undefined;
267
+ if (ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name))
268
+ keys.add(prop.name.text);
269
+ else
270
+ return undefined;
271
+ }
272
+ return keys;
273
+ }
274
+ /** Whether `node` names a property (`x.state`, `{ state: 1 }`, `{ state() {} }`) rather than a value. */
275
+ function isPropertyName(ts, node) {
276
+ const parent = node.parent;
277
+ return (((ts.isPropertyAccessExpression(parent) ||
278
+ ts.isPropertyAssignment(parent) ||
279
+ ts.isMethodDeclaration(parent) ||
280
+ ts.isGetAccessorDeclaration(parent) ||
281
+ ts.isSetAccessorDeclaration(parent) ||
282
+ ts.isPropertyDeclaration(parent)) &&
283
+ parent.name === node) ||
284
+ (ts.isBindingElement(parent) && parent.propertyName === node));
285
+ }
286
+ /** The state keys and handler names of a `bindIsland(setup)` whose setup is an inline function. */
287
+ function bindIslandNames(ts, setup) {
288
+ if (setup === undefined || !isEnhancerFn(ts, setup))
289
+ return UNKNOWN;
290
+ const param = setup.parameters[0];
291
+ let stateName;
292
+ let ctxName;
293
+ if (param !== undefined) {
294
+ if (ts.isIdentifier(param.name))
295
+ ctxName = param.name.text;
296
+ else if (ts.isObjectBindingPattern(param.name)) {
297
+ for (const element of param.name.elements) {
298
+ const prop = element.propertyName ?? element.name;
299
+ if (ts.isIdentifier(prop) && prop.text === "state" && ts.isIdentifier(element.name))
300
+ stateName = element.name.text;
301
+ else if (element.dotDotDotToken !== undefined && ts.isIdentifier(element.name))
302
+ ctxName = element.name.text;
303
+ }
304
+ }
305
+ else
306
+ return UNKNOWN;
307
+ }
308
+ let state = new Set();
309
+ const ctxProperty = (node) => ctxName !== undefined &&
310
+ ts.isPropertyAccessExpression(node) &&
311
+ ts.isIdentifier(node.expression) &&
312
+ node.expression.text === ctxName;
313
+ // The keys are known only while `state` is called directly: an alias, a destructuring or a helper
314
+ // that receives `state` or the context may declare keys this walk cannot see.
315
+ const walk = (node) => {
316
+ if (state === undefined)
317
+ return;
318
+ if (ts.isCallExpression(node)) {
319
+ const callee = node.expression;
320
+ if ((ts.isIdentifier(callee) && callee.text === stateName) ||
321
+ (ctxProperty(callee) && callee.name.text === "state")) {
322
+ const key = node.arguments[0];
323
+ if (key !== undefined && ts.isStringLiteralLike(key))
324
+ state.add(key.text);
325
+ else
326
+ state = undefined;
327
+ for (const arg of node.arguments)
328
+ walk(arg);
329
+ return;
330
+ }
331
+ }
332
+ // `ctx.root`, `ctx.props` and `ctx.onDispose` reach no state; `ctx.state` read as a value does.
333
+ if (ctxProperty(node)) {
334
+ if (node.name.text === "state")
335
+ state = undefined;
336
+ return;
337
+ }
338
+ if (ts.isIdentifier(node)) {
339
+ if ((node.text === stateName || node.text === ctxName) && !isPropertyName(ts, node))
340
+ state = undefined;
341
+ return;
342
+ }
343
+ ts.forEachChild(node, walk);
344
+ };
345
+ ts.forEachChild(setup.body, walk);
346
+ const returned = (expr) => {
347
+ if (expr === undefined)
348
+ return new Set();
349
+ let inner = expr;
350
+ while (ts.isParenthesizedExpression(inner))
351
+ inner = inner.expression;
352
+ if (ts.isObjectLiteralExpression(inner))
353
+ return literalKeys(ts, inner);
354
+ if (ts.isIdentifier(inner) && inner.text === "undefined")
355
+ return new Set();
356
+ if (ts.isVoidExpression(inner))
357
+ return new Set();
358
+ return undefined;
359
+ };
360
+ let handlers;
361
+ if (!ts.isBlock(setup.body))
362
+ handlers = returned(setup.body);
363
+ else {
364
+ handlers = new Set();
365
+ const walkReturns = (node) => {
366
+ if (isEnhancerFn(ts, node) || ts.isFunctionDeclaration(node))
367
+ return;
368
+ if (ts.isReturnStatement(node)) {
369
+ const keys = returned(node.expression);
370
+ if (keys === undefined)
371
+ handlers = undefined;
372
+ else
373
+ for (const key of keys)
374
+ handlers?.add(key);
375
+ }
376
+ ts.forEachChild(node, walkReturns);
377
+ };
378
+ ts.forEachChild(setup.body, walkReturns);
379
+ }
380
+ return { kind: "bind", state, handlers };
381
+ }
382
+ /** What an enhancer expression mounts: a `bindIsland(...)`, an imperative enhancer, or unknown. */
383
+ function enhancerNames(ts, expr, locals) {
384
+ let target = expr;
385
+ if (ts.isIdentifier(expr))
386
+ target = locals.get(expr.text);
387
+ if (target === undefined)
388
+ return UNKNOWN;
389
+ if (isEnhancerFn(ts, target))
390
+ return { kind: "imperative" };
391
+ if (ts.isCallExpression(target) && ts.isIdentifier(target.expression)) {
392
+ if (target.expression.text === "defineIsland")
393
+ return { kind: "imperative" };
394
+ if (target.expression.text === "bindIsland")
395
+ return bindIslandNames(ts, target.arguments[0]);
396
+ }
397
+ return UNKNOWN;
398
+ }
399
+ /** Every island id mounted in the project, with what its markup may name. An id mounted with
400
+ * different or unresolvable enhancers maps to UNKNOWN. */
401
+ function mountedIslands(ts, trees) {
402
+ const ids = new Map();
403
+ for (const tree of trees) {
404
+ const locals = new Map();
405
+ const mounts = [];
406
+ const visit = (node) => {
407
+ if (ts.isVariableDeclaration(node) &&
408
+ ts.isIdentifier(node.name) &&
409
+ node.initializer !== undefined)
410
+ locals.set(node.name.text, node.initializer);
411
+ if (ts.isCallExpression(node) &&
412
+ ts.isIdentifier(node.expression) &&
413
+ MOUNT_CALLS.has(node.expression.text)) {
414
+ const map = node.arguments[0];
415
+ if (map !== undefined && ts.isObjectLiteralExpression(map))
416
+ mounts.push(map);
417
+ }
418
+ ts.forEachChild(node, visit);
419
+ };
420
+ visit(tree);
421
+ for (const map of mounts) {
422
+ for (const prop of map.properties) {
423
+ let id;
424
+ let names = UNKNOWN;
425
+ if (ts.isShorthandPropertyAssignment(prop)) {
426
+ id = prop.name.text;
427
+ names = enhancerNames(ts, prop.name, locals);
428
+ }
429
+ else if (ts.isPropertyAssignment(prop) &&
430
+ (ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name))) {
431
+ id = prop.name.text;
432
+ names = enhancerNames(ts, prop.initializer, locals);
433
+ }
434
+ if (id === undefined)
435
+ continue;
436
+ ids.set(id, ids.has(id) ? UNKNOWN : names);
437
+ }
438
+ }
439
+ }
440
+ return ids;
441
+ }
442
+ // Where an interpolation sat in a template; an attribute value holding one is not literal.
443
+ export const HOLE = "\u0000";
444
+ function bindingNames(attribute, value) {
445
+ if (value.includes(HOLE))
446
+ return undefined;
447
+ const kind = attribute.slice("data-bind-".length);
448
+ if (kind === "text" || kind === "show" || kind === "value")
449
+ return value.trim() === "" ? [] : [{ name: value.trim(), handler: false }];
450
+ if (kind !== "class" && kind !== "attr" && kind !== "on")
451
+ return undefined;
452
+ const out = [];
453
+ for (const part of value.split(",")) {
454
+ const sep = part.indexOf(":");
455
+ if (sep <= 0)
456
+ continue;
457
+ const name = part.slice(sep + 1).trim();
458
+ if (part.slice(0, sep).trim() && name)
459
+ out.push({ name, handler: kind === "on" });
460
+ }
461
+ return out;
462
+ }
463
+ const VOID_ELEMENTS = new Set([
464
+ "area",
465
+ "base",
466
+ "br",
467
+ "col",
468
+ "embed",
469
+ "hr",
470
+ "img",
471
+ "input",
472
+ "link",
473
+ "meta",
474
+ "source",
475
+ "track",
476
+ "wbr",
477
+ ]);
478
+ /** The attributes of one tag's inside (`name a="1" b`), in order. */
479
+ function tagAttributes(inside) {
480
+ const out = [];
481
+ let i = 0;
482
+ while (i < inside.length) {
483
+ while (i < inside.length && /[\s/]/.test(inside.charAt(i)))
484
+ i++;
485
+ const start = i;
486
+ while (i < inside.length && !/[\s/=>]/.test(inside.charAt(i)))
487
+ i++;
488
+ const name = inside.slice(start, i).toLowerCase();
489
+ if (name === "")
490
+ break;
491
+ while (i < inside.length && /\s/.test(inside.charAt(i)))
492
+ i++;
493
+ let value = "";
494
+ if (inside.charAt(i) === "=") {
495
+ i++;
496
+ while (i < inside.length && /\s/.test(inside.charAt(i)))
497
+ i++;
498
+ const quote = inside.charAt(i);
499
+ if (quote === '"' || quote === "'") {
500
+ const end = inside.indexOf(quote, i + 1);
501
+ const stop = end < 0 ? inside.length : end;
502
+ value = inside.slice(i + 1, stop);
503
+ i = stop + 1;
504
+ }
505
+ else {
506
+ const begin = i;
507
+ while (i < inside.length && !/\s/.test(inside.charAt(i)))
508
+ i++;
509
+ value = inside.slice(begin, i);
510
+ }
511
+ }
512
+ out.push([name, value]);
513
+ }
514
+ return out;
515
+ }
516
+ /** The island a binding belongs to, and a slot between them, from the elements open around it. */
517
+ function ownerOf(stack) {
518
+ let slot = null;
519
+ for (let i = stack.length - 1; i >= 0; i--) {
520
+ const entry = stack[i];
521
+ if (entry === undefined)
522
+ continue;
523
+ if (entry.slot !== undefined && slot === null)
524
+ slot = entry.slot.value;
525
+ if (entry.island !== null)
526
+ return { island: entry.island, slot };
527
+ }
528
+ return { island: null, slot: null };
529
+ }
530
+ /**
531
+ * The bindings, swap links, forms and slots of an `html` template's text, and whether it holds an
532
+ * island host. `holes` are the template's interpolations, in order; `placement` learns where each
533
+ * one sits. A linear scan: an unclosed tag ends it.
534
+ */
535
+ function templateMarkup(text, holes, node, placement, lineAt) {
536
+ const stack = [];
537
+ const bindings = [];
538
+ const swaps = [];
539
+ const positions = [];
540
+ for (let i = text.indexOf(HOLE); i >= 0; i = text.indexOf(HOLE, i + 1))
541
+ positions.push(i);
542
+ const holesIn = (from, to) => {
543
+ const out = [];
544
+ positions.forEach((position, i) => {
545
+ const hole = holes[i];
546
+ if (position >= from && position < to && hole !== undefined)
547
+ out.push(hole);
548
+ });
549
+ return out;
550
+ };
551
+ // Innermost first, so each interpolation is placed in the element nearest to it.
552
+ const close = (from, at) => {
553
+ for (let i = stack.length - 1; i >= from; i--) {
554
+ const entry = stack[i];
555
+ if (entry === undefined)
556
+ continue;
557
+ const inside = holesIn(entry.start, at);
558
+ const sole = entry.slot !== undefined && text.slice(entry.start, at).trim() === HOLE;
559
+ for (const hole of inside)
560
+ if (!placement.has(hole))
561
+ placement.set(hole, { slot: entry.slot?.value ?? null, sole });
562
+ if (entry.slot !== undefined)
563
+ entry.slot.content = { text: text.slice(entry.start, at), holes: inside };
564
+ }
565
+ stack.length = from;
566
+ };
567
+ let hasHost = false;
568
+ let at = text.indexOf("<");
569
+ while (at >= 0) {
570
+ let end = at + 1;
571
+ let quote = "";
572
+ while (end < text.length) {
573
+ const c = text.charAt(end);
574
+ if (quote !== "") {
575
+ if (c === quote)
576
+ quote = "";
577
+ }
578
+ else if (c === '"' || c === "'")
579
+ quote = c;
580
+ else if (c === ">")
581
+ break;
582
+ end++;
583
+ }
584
+ if (end >= text.length)
585
+ break;
586
+ const inside = text.slice(at + 1, end);
587
+ // An interpolation in a tag is an attribute value, never content.
588
+ for (const hole of holesIn(at, end))
589
+ placement.set(hole, { slot: null, sole: false });
590
+ const tagAt = at;
591
+ at = text.indexOf("<", end + 1);
592
+ if (inside.startsWith("/")) {
593
+ const tag = inside.slice(1).trim().toLowerCase();
594
+ const open = stack.map((entry) => entry.tag).lastIndexOf(tag);
595
+ if (open >= 0)
596
+ close(open, tagAt);
597
+ continue;
598
+ }
599
+ const tag = /^[a-zA-Z][\w-]*/.exec(inside)?.[0]?.toLowerCase();
600
+ if (tag === undefined)
601
+ continue;
602
+ const attributes = tagAttributes(inside.slice(tag.length));
603
+ const attribute = (name) => attributes.find(([key]) => key === name)?.[1];
604
+ const literal = (value) => value.includes(HOLE) ? UNKNOWN : value;
605
+ const hostId = attribute("data-island");
606
+ const island = hostId === undefined ? null : literal(hostId);
607
+ if (island !== null)
608
+ hasHost = true;
609
+ const owner = ownerOf(stack);
610
+ const line = lineAt(tagAt);
611
+ for (const [name, value] of attributes) {
612
+ if (!name.startsWith("data-bind-"))
613
+ continue;
614
+ const names = bindingNames(name, value);
615
+ if (names !== undefined)
616
+ bindings.push({ attribute: name, names, ...owner, node, line });
617
+ }
618
+ const swap = attribute("data-swap");
619
+ const url = tag === "a" ? attribute("href") : tag === "form" ? attribute("action") : undefined;
620
+ if (swap !== undefined)
621
+ swaps.push({ kind: "swap", tag, value: literal(swap), url, jsx: false, node, line });
622
+ const slotName = attribute("data-swap-slot");
623
+ const slot = slotName === undefined
624
+ ? undefined
625
+ : { kind: "slot", tag, value: literal(slotName), url: undefined, jsx: false, node, line };
626
+ if (slot !== undefined)
627
+ swaps.push(slot);
628
+ if (!VOID_ELEMENTS.has(tag) && !inside.trimEnd().endsWith("/"))
629
+ stack.push({ tag, island, slot, start: end + 1 });
630
+ }
631
+ return { hasHost, bindings, swaps };
632
+ }
633
+ /** The source line of an offset in a template's text: its newlines, and the lines each
634
+ * interpolation spans in the source. */
635
+ function templateLines(tree, template, text, spans) {
636
+ const lineOfPosition = (position) => tree.getLineAndCharacterOfPosition(position).line + 1;
637
+ const first = lineOfPosition(template.getStart(tree));
638
+ return (offset) => {
639
+ let line = first;
640
+ let hole = 0;
641
+ for (let i = 0; i < offset && i < text.length; i++) {
642
+ const c = text.charAt(i);
643
+ if (c === "\n")
644
+ line++;
645
+ else if (c === HOLE) {
646
+ const span = spans[hole++];
647
+ if (span !== undefined)
648
+ line +=
649
+ lineOfPosition(span.literal.getStart(tree)) -
650
+ lineOfPosition(span.expression.getStart(tree));
651
+ }
652
+ }
653
+ return line;
654
+ };
655
+ }
656
+ /** The text of an `html\`...\`` template, each interpolation replaced by HOLE. */
657
+ function templateText(ts, template) {
658
+ if (ts.isNoSubstitutionTemplateLiteral(template))
659
+ return template.text;
660
+ let text = template.head.text;
661
+ for (const span of template.templateSpans)
662
+ text += HOLE + span.literal.text;
663
+ return text;
664
+ }
665
+ /** The literal string value of a JSX attribute, `undefined` when absent, UNKNOWN when not literal. */
666
+ function jsxAttribute(ts, element, name) {
667
+ for (const attr of element.attributes.properties) {
668
+ if (ts.isJsxSpreadAttribute(attr))
669
+ continue;
670
+ if (attr.name.getText() !== name)
671
+ continue;
672
+ const init = attr.initializer;
673
+ if (init === undefined)
674
+ return "";
675
+ if (ts.isStringLiteral(init))
676
+ return init.text;
677
+ if (ts.isJsxExpression(init) &&
678
+ init.expression !== undefined &&
679
+ ts.isStringLiteralLike(init.expression))
680
+ return init.expression.text;
681
+ return UNKNOWN;
682
+ }
683
+ return undefined;
684
+ }
685
+ /** The bindings, swap links, forms and slots of one outermost JSX tree, and whether it holds an
686
+ * island host. */
687
+ function jsxMarkup(ts, root) {
688
+ const bindings = [];
689
+ const swaps = [];
690
+ let hasHost = false;
691
+ const walk = (node, owner) => {
692
+ let opening;
693
+ if (ts.isJsxElement(node))
694
+ opening = node.openingElement;
695
+ else if (ts.isJsxSelfClosingElement(node))
696
+ opening = node;
697
+ let inner = owner;
698
+ if (opening !== undefined) {
699
+ const tag = opening.tagName.getText();
700
+ const host = tag === "Island"
701
+ ? jsxAttribute(ts, opening, "id")
702
+ : jsxAttribute(ts, opening, "data-island");
703
+ if (host !== undefined) {
704
+ hasHost = true;
705
+ inner = { island: host, slot: null };
706
+ }
707
+ for (const attr of opening.attributes.properties) {
708
+ if (ts.isJsxSpreadAttribute(attr))
709
+ continue;
710
+ const name = attr.name.getText();
711
+ if (!name.startsWith("data-bind-"))
712
+ continue;
713
+ const value = jsxAttribute(ts, opening, name);
714
+ if (value === UNKNOWN || value === undefined)
715
+ continue;
716
+ const names = bindingNames(name, value);
717
+ if (names !== undefined)
718
+ bindings.push({ attribute: name, names, ...owner, node: attr });
719
+ }
720
+ const swap = jsxAttribute(ts, opening, "data-swap");
721
+ if (swap !== undefined) {
722
+ const url = tag === "a"
723
+ ? jsxAttribute(ts, opening, "href")
724
+ : tag === "form"
725
+ ? jsxAttribute(ts, opening, "action")
726
+ : undefined;
727
+ swaps.push({ kind: "swap", tag, value: swap, url, jsx: true, node: opening });
728
+ }
729
+ const slot = jsxAttribute(ts, opening, "data-swap-slot");
730
+ if (slot !== undefined) {
731
+ swaps.push({ kind: "slot", tag, value: slot, url: undefined, jsx: true, node: opening });
732
+ inner = { island: inner.island, slot };
733
+ }
734
+ }
735
+ ts.forEachChild(node, (child) => walk(child, inner));
736
+ };
737
+ walk(root, { island: null, slot: null });
738
+ return { hasHost, bindings, swaps };
739
+ }
740
+ /** The `slot` names a file imports from `@nifrajs/web-vanilla`. */
741
+ function slotImports(ts, tree) {
742
+ const names = new Set();
743
+ for (const statement of tree.statements) {
744
+ if (!ts.isImportDeclaration(statement) ||
745
+ !ts.isStringLiteral(statement.moduleSpecifier) ||
746
+ statement.moduleSpecifier.text !== "@nifrajs/web-vanilla")
747
+ continue;
748
+ const bound = statement.importClause?.namedBindings;
749
+ if (bound === undefined || !ts.isNamedImports(bound))
750
+ continue;
751
+ for (const element of bound.elements)
752
+ if ((element.propertyName ?? element.name).text === "slot")
753
+ names.add(element.name.text);
754
+ }
755
+ return names;
756
+ }
757
+ /** The markup of one file: every tree's bindings (those with a host), swap uses and slot calls. */
758
+ function fileMarkup(ts, tree) {
759
+ const bindings = [];
760
+ const swaps = [];
761
+ const slotCalls = [];
762
+ const placement = new Map();
763
+ const slotNames = slotImports(ts, tree);
764
+ const take = (found) => {
765
+ // A fragment with no host of its own may render inside one elsewhere, so it is not judged.
766
+ if (found.hasHost)
767
+ bindings.push(...found.bindings);
768
+ swaps.push(...found.swaps);
769
+ };
770
+ const visit = (node) => {
771
+ if (ts.isTaggedTemplateExpression(node) &&
772
+ ts.isIdentifier(node.tag) &&
773
+ node.tag.text === "html") {
774
+ const spans = ts.isTemplateExpression(node.template) ? node.template.templateSpans : [];
775
+ const text = templateText(ts, node.template);
776
+ const holes = spans.map((span) => span.expression);
777
+ const lineAt = templateLines(tree, node.template, text, spans);
778
+ take(templateMarkup(text, holes, node, placement, lineAt));
779
+ }
780
+ else if (ts.isJsxElement(node) ||
781
+ ts.isJsxSelfClosingElement(node) ||
782
+ ts.isJsxFragment(node)) {
783
+ take(jsxMarkup(ts, node));
784
+ return;
785
+ }
786
+ else if (ts.isCallExpression(node) &&
787
+ ts.isIdentifier(node.expression) &&
788
+ slotNames.has(node.expression.text)) {
789
+ const first = node.arguments[0];
790
+ slotCalls.push({
791
+ name: first !== undefined && ts.isStringLiteralLike(first) ? first.text : UNKNOWN,
792
+ node,
793
+ });
794
+ }
795
+ ts.forEachChild(node, visit);
796
+ };
797
+ visit(tree);
798
+ return { bindings, swaps, slotCalls, placement };
799
+ }
800
+ // NF-C047, NF-C048 and the swap rules judge the same markup, so one walk of the project serves all.
801
+ const markupCache = new WeakMap();
802
+ /** Markup across the project: each binding with the island map it is judged against, and each
803
+ * file's swap facts. */
804
+ export async function projectMarkup(ctx, code, title) {
805
+ const empty = { bindings: [], files: [] };
806
+ const loaded = await loadRuleTypeScript(ctx);
807
+ if (loaded.diagnostics !== undefined)
808
+ return { diagnostics: loaded.diagnostics, markup: empty };
809
+ const ts = loaded.compiler;
810
+ if (ts === undefined)
811
+ return { diagnostics: [toolchainMissing(code, title)], markup: empty };
812
+ const cached = markupCache.get(ctx.project.source);
813
+ if (cached !== undefined)
814
+ return { ts, markup: cached };
815
+ const trees = [];
816
+ for (const file of ctx.project.source.files) {
817
+ const tree = parsedTree(ts, ctx.project.source, file);
818
+ if (tree !== undefined)
819
+ trees.push({ file, tree });
820
+ }
821
+ const islands = mountedIslands(ts, trees.map((entry) => entry.tree));
822
+ const bindings = [];
823
+ const files = [];
824
+ for (const { file, tree } of trees) {
825
+ const found = fileMarkup(ts, tree);
826
+ for (const binding of found.bindings)
827
+ bindings.push({ file, line: binding.line ?? lineOf(tree, binding.node), binding, islands });
828
+ files.push({ file, tree, ...found });
829
+ }
830
+ const markup = { bindings, files };
831
+ markupCache.set(ctx.project.source, markup);
832
+ return { ts, markup };
833
+ }
834
+ export const islandBindingNameRule = {
835
+ code: "NF-C047",
836
+ title: "Island binding name check",
837
+ async scan(ctx) {
838
+ const { diagnostics, markup } = await projectMarkup(ctx, "NF-C047", "Island binding name check");
839
+ if (diagnostics !== undefined)
840
+ return diagnostics;
841
+ const findings = [];
842
+ for (const { file, line, binding, islands } of markup.bindings) {
843
+ if (typeof binding.island !== "string")
844
+ continue;
845
+ const names = islands.get(binding.island);
846
+ if (names === undefined || names === UNKNOWN || names.kind !== "bind")
847
+ continue;
848
+ for (const { name, handler } of binding.names) {
849
+ const known = handler ? names.handlers : names.state;
850
+ if (known === undefined || known.has(name))
851
+ continue;
852
+ findings.push(diagnostic({
853
+ code: "NF-C047",
854
+ severity: "warn",
855
+ file,
856
+ line,
857
+ message: handler
858
+ ? `${binding.attribute} names handler "${name}", which the bindIsland setup mounted as "${binding.island}" does not return - the listener is never added`
859
+ : `${binding.attribute} names "${name}", which the bindIsland setup mounted as "${binding.island}" never declares with state("${name}", ...) - the binding is skipped`,
860
+ evidence: [`${binding.attribute} -> ${name}`],
861
+ fix: { recipe: "islands.binding-names", command: "nifra_docs islands" },
862
+ verify: "nifra check --lints-only",
863
+ }));
864
+ }
865
+ }
866
+ return findings;
867
+ },
868
+ };
869
+ export const islandBindingHostRule = {
870
+ code: "NF-C048",
871
+ title: "Island binding outside a bindIsland host",
872
+ async scan(ctx) {
873
+ const { diagnostics, markup } = await projectMarkup(ctx, "NF-C048", "Island binding outside a bindIsland host");
874
+ if (diagnostics !== undefined)
875
+ return diagnostics;
876
+ const findings = [];
877
+ for (const { file, line, binding, islands } of markup.bindings) {
878
+ let message;
879
+ if (binding.island === null)
880
+ message = `${binding.attribute} never binds: it sits outside every data-island host. Move it inside a host mounted with bindIsland`;
881
+ else if (typeof binding.island === "string") {
882
+ const names = islands.get(binding.island);
883
+ if (names !== undefined && names !== UNKNOWN && names.kind === "imperative")
884
+ message = `${binding.attribute} never binds: island "${binding.island}" is mounted with an imperative enhancer, which runs no bindings. Move it inside a host mounted with bindIsland`;
885
+ }
886
+ if (message === undefined && binding.island !== null && binding.slot !== null) {
887
+ const slot = typeof binding.slot === "string" ? `"${binding.slot}"` : "around it";
888
+ message = `${binding.attribute} sits in data-swap-slot ${slot} but belongs to an island outside it: a swap replaces its element and the binding stops. Bind it from an island inside the slot`;
889
+ }
890
+ if (message === undefined)
891
+ continue;
892
+ findings.push(diagnostic({
893
+ code: "NF-C048",
894
+ severity: "warn",
895
+ file,
896
+ line,
897
+ message,
898
+ evidence: [binding.attribute],
899
+ fix: { recipe: "islands.binding-host", command: "nifra_docs islands" },
900
+ verify: "nifra check --lints-only",
901
+ }));
902
+ }
903
+ return findings;
904
+ },
905
+ };
906
+ export const islandRules = Object.freeze([
907
+ islandCleanupRule,
908
+ islandListKeyRule,
909
+ islandBindingNameRule,
910
+ islandBindingHostRule,
911
+ ]);
158
912
  //# sourceMappingURL=islands.js.map