@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.
- package/CHANGELOG.md +67 -0
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan-sql.d.ts +95 -0
- package/dist/check-scan-sql.d.ts.map +1 -0
- package/dist/check-scan-sql.js +1190 -0
- package/dist/check-scan-sql.js.map +1 -0
- package/dist/check-scan.d.ts +5 -93
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +20 -1207
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +9 -0
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -6
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +249 -80
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +3 -0
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +18 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/frontend-guidance.js +1 -1
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/internal/a11y-element.d.ts +33 -0
- package/dist/internal/a11y-element.d.ts.map +1 -0
- package/dist/internal/a11y-element.js +124 -0
- package/dist/internal/a11y-element.js.map +1 -0
- package/dist/internal/a11y-jsx.d.ts +12 -0
- package/dist/internal/a11y-jsx.d.ts.map +1 -0
- package/dist/internal/a11y-jsx.js +42 -0
- package/dist/internal/a11y-jsx.js.map +1 -0
- package/dist/internal/a11y-template.d.ts +11 -0
- package/dist/internal/a11y-template.d.ts.map +1 -0
- package/dist/internal/a11y-template.js +56 -0
- package/dist/internal/a11y-template.js.map +1 -0
- package/dist/internal/aria-data.d.ts +23 -0
- package/dist/internal/aria-data.d.ts.map +1 -0
- package/dist/internal/aria-data.js +220 -0
- package/dist/internal/aria-data.js.map +1 -0
- package/dist/internal/edit-distance.d.ts +3 -0
- package/dist/internal/edit-distance.d.ts.map +1 -0
- package/dist/internal/edit-distance.js +12 -0
- package/dist/internal/edit-distance.js.map +1 -0
- package/dist/internal/imported-as.d.ts +10 -0
- package/dist/internal/imported-as.d.ts.map +1 -0
- package/dist/internal/imported-as.js +23 -0
- package/dist/internal/imported-as.js.map +1 -0
- package/dist/internal/source-facts.d.ts +3 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +2 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/template-markup.d.ts +58 -0
- package/dist/internal/template-markup.d.ts.map +1 -0
- package/dist/internal/template-markup.js +221 -0
- package/dist/internal/template-markup.js.map +1 -0
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +11 -0
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +37 -23
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +5 -3
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-root.js +1 -1
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-ws.d.ts.map +1 -1
- package/dist/mcp-ws.js +3 -2
- package/dist/mcp-ws.js.map +1 -1
- package/dist/migrate-layout-config.d.ts +63 -0
- package/dist/migrate-layout-config.d.ts.map +1 -0
- package/dist/migrate-layout-config.js +507 -0
- package/dist/migrate-layout-config.js.map +1 -0
- package/dist/migrate-layout-split.d.ts +46 -0
- package/dist/migrate-layout-split.d.ts.map +1 -0
- package/dist/migrate-layout-split.js +687 -0
- package/dist/migrate-layout-split.js.map +1 -0
- package/dist/migrate-layout.d.ts +4 -24
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +8 -1183
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +78 -10
- package/dist/output-schemas.js.map +1 -1
- package/dist/recipes/5.18.0.d.ts +9 -0
- package/dist/recipes/5.18.0.d.ts.map +1 -0
- package/dist/recipes/5.18.0.js +56 -0
- package/dist/recipes/5.18.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +6 -0
- package/dist/review.js.map +1 -1
- package/dist/rules/a11y.d.ts +5 -0
- package/dist/rules/a11y.d.ts.map +1 -0
- package/dist/rules/a11y.js +78 -0
- package/dist/rules/a11y.js.map +1 -0
- package/dist/rules/codes.d.ts +6 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +6 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/comment-markers.js +1 -1
- package/dist/rules/comment-markers.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +30 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/host-exposure.d.ts +23 -0
- package/dist/rules/host-exposure.d.ts.map +1 -0
- package/dist/rules/host-exposure.js +380 -0
- package/dist/rules/host-exposure.js.map +1 -0
- package/dist/rules/internal-links.d.ts.map +1 -1
- package/dist/rules/internal-links.js +16 -141
- package/dist/rules/internal-links.js.map +1 -1
- package/dist/rules/islands.d.ts +54 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +577 -80
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/security.d.ts +1 -0
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +34 -7
- package/dist/rules/security.js.map +1 -1
- package/dist/runtime-differences.js +3 -3
- package/dist/runtime-differences.js.map +1 -1
- package/dist/scaffold.d.ts +1 -1
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +2 -2
- package/dist/scaffold.js.map +1 -1
- package/dist/source-rewrites.d.ts.map +1 -1
- package/dist/source-rewrites.js +2 -19
- package/dist/source-rewrites.js.map +1 -1
- package/docs/examples.json +4 -4
- package/docs/llms-full.txt +422 -42
- package/docs/types.json +466 -56
- package/package.json +12 -12
- package/src/check-diagnostics.ts +11 -0
- package/src/check-scan-sql.ts +1746 -0
- package/src/check-scan.ts +34 -1754
- package/src/check.ts +10 -1
- package/src/cli.ts +258 -98
- package/src/command-catalog.ts +21 -4
- package/src/frontend-guidance.ts +1 -1
- package/src/internal/a11y-element.ts +227 -0
- package/src/internal/a11y-jsx.ts +54 -0
- package/src/internal/a11y-template.ts +74 -0
- package/src/internal/aria-data.ts +242 -0
- package/src/internal/edit-distance.ts +16 -0
- package/src/internal/imported-as.ts +30 -0
- package/src/internal/source-facts.ts +4 -0
- package/src/internal/template-markup.ts +270 -0
- package/src/internal/typescript-import.ts +15 -0
- package/src/mcp-exec.ts +43 -22
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +5 -3
- package/src/mcp-root.ts +1 -1
- package/src/mcp-ws.ts +3 -2
- package/src/migrate-layout-config.ts +615 -0
- package/src/migrate-layout-split.ts +870 -0
- package/src/migrate-layout.ts +37 -1468
- package/src/output-schemas.ts +85 -17
- package/src/recipes/5.18.0.ts +63 -0
- package/src/recipes/index.ts +2 -0
- package/src/review.ts +6 -0
- package/src/rules/a11y.ts +119 -0
- package/src/rules/codes.ts +6 -0
- package/src/rules/comment-markers.ts +2 -2
- package/src/rules/explanations.ts +30 -0
- package/src/rules/host-exposure.ts +531 -0
- package/src/rules/internal-links.ts +21 -154
- package/src/rules/islands.ts +698 -86
- package/src/rules/security.ts +48 -7
- package/src/runtime-differences.ts +3 -3
- package/src/scaffold.ts +3 -3
- package/src/source-rewrites.ts +2 -15
package/dist/rules/islands.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { diagnostic } from "../diagnostics.js";
|
|
2
2
|
import { scriptKindOf } from "../internal/script-kind.js";
|
|
3
|
+
import { commentBlockExpectation, MISSING_EXPECTATION_REASON } from "./comment-markers.js";
|
|
3
4
|
import { loadRuleTypeScript } from "./typescript.js";
|
|
4
5
|
const parseCache = new WeakMap();
|
|
5
6
|
function parsedTree(ts, sources, file) {
|
|
@@ -225,7 +226,155 @@ export const islandListKeyRule = {
|
|
|
225
226
|
return findings;
|
|
226
227
|
},
|
|
227
228
|
};
|
|
229
|
+
const OMITTED = Symbol("omitted from JSON");
|
|
228
230
|
export const UNKNOWN = Symbol("unknown");
|
|
231
|
+
const memberName = (ts, name) => ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isNumericLiteral(name)
|
|
232
|
+
? name.text
|
|
233
|
+
: undefined;
|
|
234
|
+
function typeKind(ts, type) {
|
|
235
|
+
if (ts.isParenthesizedTypeNode(type))
|
|
236
|
+
return typeKind(ts, type.type);
|
|
237
|
+
if (type.kind === ts.SyntaxKind.StringKeyword)
|
|
238
|
+
return "string";
|
|
239
|
+
if (type.kind === ts.SyntaxKind.NumberKeyword)
|
|
240
|
+
return "number";
|
|
241
|
+
if (type.kind === ts.SyntaxKind.BooleanKeyword)
|
|
242
|
+
return "boolean";
|
|
243
|
+
if (ts.isLiteralTypeNode(type)) {
|
|
244
|
+
const literal = type.literal;
|
|
245
|
+
if (ts.isStringLiteralLike(literal))
|
|
246
|
+
return "string";
|
|
247
|
+
if (ts.isNumericLiteral(literal))
|
|
248
|
+
return "number";
|
|
249
|
+
if (literal.kind === ts.SyntaxKind.TrueKeyword || literal.kind === ts.SyntaxKind.FalseKeyword)
|
|
250
|
+
return "boolean";
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
if (ts.isUnionTypeNode(type)) {
|
|
254
|
+
const kinds = new Set(type.types.map((member) => typeKind(ts, member)));
|
|
255
|
+
const [only] = kinds;
|
|
256
|
+
return kinds.size === 1 ? only : undefined;
|
|
257
|
+
}
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
function admitsUndefined(ts, type) {
|
|
261
|
+
if (type.kind === ts.SyntaxKind.UndefinedKeyword)
|
|
262
|
+
return true;
|
|
263
|
+
if (ts.isParenthesizedTypeNode(type))
|
|
264
|
+
return admitsUndefined(ts, type.type);
|
|
265
|
+
return ts.isUnionTypeNode(type) && type.types.some((member) => admitsUndefined(ts, member));
|
|
266
|
+
}
|
|
267
|
+
function propsShape(ts, type) {
|
|
268
|
+
if (type === undefined || !ts.isTypeLiteralNode(type))
|
|
269
|
+
return undefined;
|
|
270
|
+
const shape = new Map();
|
|
271
|
+
for (const member of type.members) {
|
|
272
|
+
if (!ts.isPropertySignature(member) || member.type === undefined)
|
|
273
|
+
return undefined;
|
|
274
|
+
const name = memberName(ts, member.name);
|
|
275
|
+
if (name === undefined)
|
|
276
|
+
return undefined;
|
|
277
|
+
shape.set(name, {
|
|
278
|
+
optional: member.questionToken !== undefined || admitsUndefined(ts, member.type),
|
|
279
|
+
kind: typeKind(ts, member.type),
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
return shape;
|
|
283
|
+
}
|
|
284
|
+
function literalKind(ts, node) {
|
|
285
|
+
let inner = node;
|
|
286
|
+
while (ts.isParenthesizedExpression(inner) || ts.isAsExpression(inner))
|
|
287
|
+
inner = inner.expression;
|
|
288
|
+
if (ts.isVoidExpression(inner) || (ts.isIdentifier(inner) && inner.text === "undefined"))
|
|
289
|
+
return OMITTED;
|
|
290
|
+
if (ts.isStringLiteralLike(inner) || ts.isTemplateExpression(inner))
|
|
291
|
+
return "string";
|
|
292
|
+
if (ts.isNumericLiteral(inner))
|
|
293
|
+
return "number";
|
|
294
|
+
if (ts.isPrefixUnaryExpression(inner) &&
|
|
295
|
+
inner.operator === ts.SyntaxKind.MinusToken &&
|
|
296
|
+
ts.isNumericLiteral(inner.operand))
|
|
297
|
+
return "number";
|
|
298
|
+
if (inner.kind === ts.SyntaxKind.TrueKeyword || inner.kind === ts.SyntaxKind.FalseKeyword)
|
|
299
|
+
return "boolean";
|
|
300
|
+
if (inner.kind === ts.SyntaxKind.NullKeyword)
|
|
301
|
+
return "null";
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
function passedFromObject(ts, object) {
|
|
305
|
+
const entries = new Map();
|
|
306
|
+
const lastSpread = object.properties.findLastIndex(ts.isSpreadAssignment);
|
|
307
|
+
for (const [index, prop] of object.properties.entries()) {
|
|
308
|
+
if (ts.isSpreadAssignment(prop))
|
|
309
|
+
continue;
|
|
310
|
+
if (ts.isShorthandPropertyAssignment(prop))
|
|
311
|
+
entries.set(prop.name.text, undefined);
|
|
312
|
+
else if (ts.isPropertyAssignment(prop)) {
|
|
313
|
+
const name = memberName(ts, prop.name);
|
|
314
|
+
if (name === undefined)
|
|
315
|
+
return undefined;
|
|
316
|
+
entries.set(name, index < lastSpread ? undefined : literalKind(ts, prop.initializer));
|
|
317
|
+
}
|
|
318
|
+
else
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
return { entries, open: lastSpread !== -1 };
|
|
322
|
+
}
|
|
323
|
+
function passedFromExpression(ts, expr) {
|
|
324
|
+
let inner = expr;
|
|
325
|
+
while (inner !== undefined && (ts.isParenthesizedExpression(inner) || ts.isAsExpression(inner)))
|
|
326
|
+
inner = inner.expression;
|
|
327
|
+
if (inner === undefined)
|
|
328
|
+
return undefined;
|
|
329
|
+
if (ts.isObjectLiteralExpression(inner))
|
|
330
|
+
return passedFromObject(ts, inner);
|
|
331
|
+
if (!ts.isCallExpression(inner))
|
|
332
|
+
return undefined;
|
|
333
|
+
const callee = inner.expression;
|
|
334
|
+
const serializer = (ts.isIdentifier(callee) && callee.text === "islandProps") ||
|
|
335
|
+
(ts.isPropertyAccessExpression(callee) &&
|
|
336
|
+
ts.isIdentifier(callee.expression) &&
|
|
337
|
+
callee.expression.text === "JSON" &&
|
|
338
|
+
callee.name.text === "stringify");
|
|
339
|
+
const arg = inner.arguments[0];
|
|
340
|
+
return serializer &&
|
|
341
|
+
arg !== undefined &&
|
|
342
|
+
ts.isObjectLiteralExpression(arg) &&
|
|
343
|
+
inner.arguments.length === 1
|
|
344
|
+
? passedFromObject(ts, arg)
|
|
345
|
+
: undefined;
|
|
346
|
+
}
|
|
347
|
+
function passedFromJson(text) {
|
|
348
|
+
const decoded = text
|
|
349
|
+
.replaceAll(""", '"')
|
|
350
|
+
.replaceAll("'", "'")
|
|
351
|
+
.replaceAll("<", "<")
|
|
352
|
+
.replaceAll(">", ">")
|
|
353
|
+
.replaceAll("&", "&");
|
|
354
|
+
let value;
|
|
355
|
+
try {
|
|
356
|
+
value = JSON.parse(decoded);
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
return undefined;
|
|
360
|
+
}
|
|
361
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
362
|
+
return undefined;
|
|
363
|
+
const entries = new Map();
|
|
364
|
+
for (const [key, item] of Object.entries(value)) {
|
|
365
|
+
if (item === null)
|
|
366
|
+
entries.set(key, "null");
|
|
367
|
+
else if (typeof item === "string")
|
|
368
|
+
entries.set(key, "string");
|
|
369
|
+
else if (typeof item === "number")
|
|
370
|
+
entries.set(key, "number");
|
|
371
|
+
else if (typeof item === "boolean")
|
|
372
|
+
entries.set(key, "boolean");
|
|
373
|
+
else
|
|
374
|
+
entries.set(key, undefined);
|
|
375
|
+
}
|
|
376
|
+
return { entries, open: false };
|
|
377
|
+
}
|
|
229
378
|
function literalKeys(ts, node) {
|
|
230
379
|
const keys = new Set();
|
|
231
380
|
for (const prop of node.properties) {
|
|
@@ -341,6 +490,11 @@ function bindIslandNames(ts, setup) {
|
|
|
341
490
|
}
|
|
342
491
|
return { kind: "bind", state, handlers };
|
|
343
492
|
}
|
|
493
|
+
function parameterProps(ts, fn) {
|
|
494
|
+
return fn !== undefined && isEnhancerFn(ts, fn)
|
|
495
|
+
? propsShape(ts, fn.parameters[1]?.type)
|
|
496
|
+
: undefined;
|
|
497
|
+
}
|
|
344
498
|
function enhancerNames(ts, expr, locals) {
|
|
345
499
|
let target = expr;
|
|
346
500
|
if (ts.isIdentifier(expr))
|
|
@@ -348,31 +502,85 @@ function enhancerNames(ts, expr, locals) {
|
|
|
348
502
|
if (target === undefined)
|
|
349
503
|
return UNKNOWN;
|
|
350
504
|
if (isEnhancerFn(ts, target))
|
|
351
|
-
return { kind: "imperative" };
|
|
505
|
+
return { kind: "imperative", props: parameterProps(ts, target) };
|
|
352
506
|
if (ts.isCallExpression(target) && ts.isIdentifier(target.expression)) {
|
|
507
|
+
const declared = propsShape(ts, target.typeArguments?.[0]);
|
|
353
508
|
if (target.expression.text === "defineIsland")
|
|
354
|
-
return {
|
|
355
|
-
|
|
356
|
-
|
|
509
|
+
return {
|
|
510
|
+
kind: "imperative",
|
|
511
|
+
props: declared ?? parameterProps(ts, target.arguments[0]),
|
|
512
|
+
};
|
|
513
|
+
if (target.expression.text === "bindIsland") {
|
|
514
|
+
const names = bindIslandNames(ts, target.arguments[0]);
|
|
515
|
+
return names === UNKNOWN ? UNKNOWN : { ...names, props: declared };
|
|
516
|
+
}
|
|
357
517
|
}
|
|
358
518
|
return UNKNOWN;
|
|
359
519
|
}
|
|
520
|
+
function mountReference(ts, expr) {
|
|
521
|
+
let target = expr;
|
|
522
|
+
while (target !== undefined &&
|
|
523
|
+
(ts.isParenthesizedExpression(target) ||
|
|
524
|
+
ts.isAsExpression(target) ||
|
|
525
|
+
ts.isNonNullExpression(target) ||
|
|
526
|
+
ts.isSatisfiesExpression(target)))
|
|
527
|
+
target = target.expression;
|
|
528
|
+
if (target === undefined)
|
|
529
|
+
return false;
|
|
530
|
+
if (ts.isIdentifier(target))
|
|
531
|
+
return MOUNT_CALLS.has(target.text);
|
|
532
|
+
return ts.isPropertyAccessExpression(target) && MOUNT_CALLS.has(target.name.text);
|
|
533
|
+
}
|
|
360
534
|
function mountedIslands(ts, trees) {
|
|
361
535
|
const ids = new Map();
|
|
536
|
+
let open = false;
|
|
537
|
+
let calls = 0;
|
|
362
538
|
for (const tree of trees) {
|
|
363
539
|
const locals = new Map();
|
|
364
540
|
const mounts = [];
|
|
365
541
|
const visit = (node) => {
|
|
366
|
-
if (ts.isVariableDeclaration(node)
|
|
367
|
-
ts.isIdentifier(node.name)
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
ts
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
542
|
+
if (ts.isVariableDeclaration(node)) {
|
|
543
|
+
if (ts.isIdentifier(node.name)) {
|
|
544
|
+
const name = node.name.text;
|
|
545
|
+
locals.set(name, locals.has(name) ? undefined : node.initializer);
|
|
546
|
+
}
|
|
547
|
+
if (mountReference(ts, node.initializer))
|
|
548
|
+
open = true;
|
|
549
|
+
}
|
|
550
|
+
else if ((ts.isParameter(node) ||
|
|
551
|
+
ts.isBindingElement(node) ||
|
|
552
|
+
ts.isImportSpecifier(node) ||
|
|
553
|
+
ts.isImportClause(node) ||
|
|
554
|
+
ts.isNamespaceImport(node) ||
|
|
555
|
+
ts.isFunctionDeclaration(node) ||
|
|
556
|
+
ts.isFunctionExpression(node) ||
|
|
557
|
+
ts.isClassDeclaration(node) ||
|
|
558
|
+
ts.isClassExpression(node) ||
|
|
559
|
+
ts.isEnumDeclaration(node)) &&
|
|
560
|
+
node.name !== undefined &&
|
|
561
|
+
ts.isIdentifier(node.name))
|
|
562
|
+
locals.set(node.name.text, undefined);
|
|
563
|
+
if (ts.isBinaryExpression(node) &&
|
|
564
|
+
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
565
|
+
mountReference(ts, node.right))
|
|
566
|
+
open = true;
|
|
567
|
+
if (ts.isImportSpecifier(node) &&
|
|
568
|
+
node.propertyName !== undefined &&
|
|
569
|
+
MOUNT_CALLS.has(node.propertyName.text))
|
|
570
|
+
open = true;
|
|
571
|
+
if (ts.isCallExpression(node)) {
|
|
572
|
+
const callee = node.expression;
|
|
573
|
+
if (ts.isIdentifier(callee) && MOUNT_CALLS.has(callee.text)) {
|
|
574
|
+
const map = node.arguments[0];
|
|
575
|
+
if (map !== undefined && ts.isObjectLiteralExpression(map)) {
|
|
576
|
+
calls++;
|
|
577
|
+
mounts.push(map);
|
|
578
|
+
}
|
|
579
|
+
else
|
|
580
|
+
open = true;
|
|
581
|
+
}
|
|
582
|
+
else if (mountReference(ts, callee))
|
|
583
|
+
open = true;
|
|
376
584
|
}
|
|
377
585
|
ts.forEachChild(node, visit);
|
|
378
586
|
};
|
|
@@ -385,18 +593,22 @@ function mountedIslands(ts, trees) {
|
|
|
385
593
|
id = prop.name.text;
|
|
386
594
|
names = enhancerNames(ts, prop.name, locals);
|
|
387
595
|
}
|
|
388
|
-
else if (ts.isPropertyAssignment(prop)
|
|
389
|
-
(ts
|
|
390
|
-
id = prop.name.text;
|
|
596
|
+
else if (ts.isPropertyAssignment(prop)) {
|
|
597
|
+
id = memberName(ts, prop.name);
|
|
391
598
|
names = enhancerNames(ts, prop.initializer, locals);
|
|
392
599
|
}
|
|
600
|
+
else if (ts.isMethodDeclaration(prop)) {
|
|
601
|
+
id = memberName(ts, prop.name);
|
|
602
|
+
names = { kind: "imperative", props: propsShape(ts, prop.parameters[1]?.type) };
|
|
603
|
+
}
|
|
393
604
|
if (id === undefined)
|
|
394
|
-
|
|
395
|
-
|
|
605
|
+
open = true;
|
|
606
|
+
else
|
|
607
|
+
ids.set(id, ids.has(id) ? UNKNOWN : names);
|
|
396
608
|
}
|
|
397
609
|
}
|
|
398
610
|
}
|
|
399
|
-
return ids;
|
|
611
|
+
return { ids, open, calls };
|
|
400
612
|
}
|
|
401
613
|
export const HOLE = "\u0000";
|
|
402
614
|
function bindingNames(attribute, value) {
|
|
@@ -404,7 +616,7 @@ function bindingNames(attribute, value) {
|
|
|
404
616
|
return undefined;
|
|
405
617
|
const kind = attribute.slice("data-bind-".length);
|
|
406
618
|
if (kind === "text" || kind === "show" || kind === "value")
|
|
407
|
-
return value.trim() === "" ? [] : [{ name: value.trim(), handler: false }];
|
|
619
|
+
return value.trim() === "" ? [] : [{ name: value.trim(), handler: false, key: undefined }];
|
|
408
620
|
if (kind !== "class" && kind !== "attr" && kind !== "on")
|
|
409
621
|
return undefined;
|
|
410
622
|
const out = [];
|
|
@@ -413,8 +625,9 @@ function bindingNames(attribute, value) {
|
|
|
413
625
|
if (sep <= 0)
|
|
414
626
|
continue;
|
|
415
627
|
const name = part.slice(sep + 1).trim();
|
|
416
|
-
|
|
417
|
-
|
|
628
|
+
const key = part.slice(0, sep).trim();
|
|
629
|
+
if (key && name)
|
|
630
|
+
out.push({ name, handler: kind === "on", key });
|
|
418
631
|
}
|
|
419
632
|
return out;
|
|
420
633
|
}
|
|
@@ -433,6 +646,29 @@ const VOID_ELEMENTS = new Set([
|
|
|
433
646
|
"track",
|
|
434
647
|
"wbr",
|
|
435
648
|
]);
|
|
649
|
+
const TEXT_ELEMENTS = new Set([
|
|
650
|
+
"iframe",
|
|
651
|
+
"noembed",
|
|
652
|
+
"noframes",
|
|
653
|
+
"script",
|
|
654
|
+
"style",
|
|
655
|
+
"textarea",
|
|
656
|
+
"title",
|
|
657
|
+
"xmp",
|
|
658
|
+
]);
|
|
659
|
+
function htmlCommentAt(text, offset) {
|
|
660
|
+
const pattern = /<!--(?:-?>|(.*?)--!?>)/sy;
|
|
661
|
+
pattern.lastIndex = offset;
|
|
662
|
+
return pattern.exec(text);
|
|
663
|
+
}
|
|
664
|
+
function textElementClose(text, tag, start) {
|
|
665
|
+
const closer = /<\/([a-z][\w-]*)(?=[\t\n\f\r />])/gi;
|
|
666
|
+
closer.lastIndex = start;
|
|
667
|
+
for (let match = closer.exec(text); match !== null; match = closer.exec(text))
|
|
668
|
+
if (match[1]?.toLowerCase() === tag)
|
|
669
|
+
return match.index;
|
|
670
|
+
return -1;
|
|
671
|
+
}
|
|
436
672
|
function tagAttributes(inside) {
|
|
437
673
|
const out = [];
|
|
438
674
|
let i = 0;
|
|
@@ -448,6 +684,7 @@ function tagAttributes(inside) {
|
|
|
448
684
|
while (i < inside.length && /\s/.test(inside.charAt(i)))
|
|
449
685
|
i++;
|
|
450
686
|
let value = "";
|
|
687
|
+
let valueAt = -1;
|
|
451
688
|
if (inside.charAt(i) === "=") {
|
|
452
689
|
i++;
|
|
453
690
|
while (i < inside.length && /\s/.test(inside.charAt(i)))
|
|
@@ -456,17 +693,18 @@ function tagAttributes(inside) {
|
|
|
456
693
|
if (quote === '"' || quote === "'") {
|
|
457
694
|
const end = inside.indexOf(quote, i + 1);
|
|
458
695
|
const stop = end < 0 ? inside.length : end;
|
|
459
|
-
|
|
696
|
+
valueAt = i + 1;
|
|
697
|
+
value = inside.slice(valueAt, stop);
|
|
460
698
|
i = stop + 1;
|
|
461
699
|
}
|
|
462
700
|
else {
|
|
463
|
-
|
|
701
|
+
valueAt = i;
|
|
464
702
|
while (i < inside.length && !/\s/.test(inside.charAt(i)))
|
|
465
703
|
i++;
|
|
466
|
-
value = inside.slice(
|
|
704
|
+
value = inside.slice(valueAt, i);
|
|
467
705
|
}
|
|
468
706
|
}
|
|
469
|
-
out.push([name, value]);
|
|
707
|
+
out.push([name, value, valueAt]);
|
|
470
708
|
}
|
|
471
709
|
return out;
|
|
472
710
|
}
|
|
@@ -483,10 +721,24 @@ function ownerOf(stack) {
|
|
|
483
721
|
}
|
|
484
722
|
return { island: null, slot: null };
|
|
485
723
|
}
|
|
486
|
-
function
|
|
724
|
+
function hostProps(ts, attributes, inside, nameLength, holes, positions, tagAt) {
|
|
725
|
+
const found = attributes.find(([name]) => name === "data-island-props");
|
|
726
|
+
if (found === undefined)
|
|
727
|
+
return "absent";
|
|
728
|
+
const [, value, valueAt] = found;
|
|
729
|
+
if (!value.includes(HOLE))
|
|
730
|
+
return passedFromJson(value) ?? "unknown";
|
|
731
|
+
if (value.trim() !== HOLE)
|
|
732
|
+
return "unknown";
|
|
733
|
+
const before = positions.filter((position) => position < tagAt).length;
|
|
734
|
+
const inTag = inside.slice(0, nameLength + valueAt).split(HOLE).length - 1;
|
|
735
|
+
return passedFromExpression(ts, holes[before + inTag]) ?? "unknown";
|
|
736
|
+
}
|
|
737
|
+
function templateMarkup(ts, text, holes, node, placement, lineAt) {
|
|
487
738
|
const stack = [];
|
|
488
739
|
const bindings = [];
|
|
489
740
|
const swaps = [];
|
|
741
|
+
const hosts = [];
|
|
490
742
|
const positions = [];
|
|
491
743
|
for (let i = text.indexOf(HOLE); i >= 0; i = text.indexOf(HOLE, i + 1))
|
|
492
744
|
positions.push(i);
|
|
@@ -517,6 +769,13 @@ function templateMarkup(text, holes, node, placement, lineAt) {
|
|
|
517
769
|
let hasHost = false;
|
|
518
770
|
let at = text.indexOf("<");
|
|
519
771
|
while (at >= 0) {
|
|
772
|
+
if (text.startsWith("<!--", at)) {
|
|
773
|
+
const comment = htmlCommentAt(text, at);
|
|
774
|
+
if (comment === null)
|
|
775
|
+
break;
|
|
776
|
+
at = text.indexOf("<", at + comment[0].length);
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
520
779
|
let end = at + 1;
|
|
521
780
|
let quote = "";
|
|
522
781
|
while (end < text.length) {
|
|
@@ -539,7 +798,9 @@ function templateMarkup(text, holes, node, placement, lineAt) {
|
|
|
539
798
|
const tagAt = at;
|
|
540
799
|
at = text.indexOf("<", end + 1);
|
|
541
800
|
if (inside.startsWith("/")) {
|
|
542
|
-
const tag =
|
|
801
|
+
const tag = /^\/([a-z][\w-]*)/i.exec(inside)?.[1]?.toLowerCase();
|
|
802
|
+
if (tag === undefined)
|
|
803
|
+
continue;
|
|
543
804
|
const open = stack.map((entry) => entry.tag).lastIndexOf(tag);
|
|
544
805
|
if (open >= 0)
|
|
545
806
|
close(open, tagAt);
|
|
@@ -551,18 +812,27 @@ function templateMarkup(text, holes, node, placement, lineAt) {
|
|
|
551
812
|
const attributes = tagAttributes(inside.slice(tag.length));
|
|
552
813
|
const attribute = (name) => attributes.find(([key]) => key === name)?.[1];
|
|
553
814
|
const literal = (value) => value.includes(HOLE) ? UNKNOWN : value;
|
|
554
|
-
const
|
|
815
|
+
const ignored = stack.at(-1)?.ignored === true || attribute("data-island-ignore") !== undefined;
|
|
816
|
+
const hostId = ignored ? undefined : attribute("data-island");
|
|
555
817
|
const island = hostId === undefined ? null : literal(hostId);
|
|
556
818
|
if (island !== null)
|
|
557
819
|
hasHost = true;
|
|
558
820
|
const owner = ownerOf(stack);
|
|
559
821
|
const line = lineAt(tagAt);
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
822
|
+
if (island !== null) {
|
|
823
|
+
const props = attributes.some(([name]) => name.includes(HOLE))
|
|
824
|
+
? "unknown"
|
|
825
|
+
: hostProps(ts, attributes, inside, tag.length, holes, positions, tagAt);
|
|
826
|
+
hosts.push({ id: island, props, node, line });
|
|
827
|
+
}
|
|
828
|
+
if (!ignored) {
|
|
829
|
+
for (const [name, value] of attributes) {
|
|
830
|
+
if (!name.startsWith("data-bind-"))
|
|
831
|
+
continue;
|
|
832
|
+
const names = bindingNames(name, value);
|
|
833
|
+
if (names !== undefined)
|
|
834
|
+
bindings.push({ attribute: name, names, tag, ...owner, node, line });
|
|
835
|
+
}
|
|
566
836
|
}
|
|
567
837
|
const swap = attribute("data-swap");
|
|
568
838
|
const url = tag === "a" ? attribute("href") : tag === "form" ? attribute("action") : undefined;
|
|
@@ -574,10 +844,13 @@ function templateMarkup(text, holes, node, placement, lineAt) {
|
|
|
574
844
|
: { kind: "slot", tag, value: literal(slotName), url: undefined, jsx: false, node, line };
|
|
575
845
|
if (slot !== undefined)
|
|
576
846
|
swaps.push(slot);
|
|
577
|
-
|
|
578
|
-
|
|
847
|
+
const textElement = TEXT_ELEMENTS.has(tag);
|
|
848
|
+
if (!VOID_ELEMENTS.has(tag) && (textElement || !inside.trimEnd().endsWith("/")))
|
|
849
|
+
stack.push({ tag, island, slot, ignored, start: end + 1 });
|
|
850
|
+
if (textElement)
|
|
851
|
+
at = textElementClose(text, tag, end + 1);
|
|
579
852
|
}
|
|
580
|
-
return { hasHost, bindings, swaps };
|
|
853
|
+
return { hasHost, hosts, bindings, swaps };
|
|
581
854
|
}
|
|
582
855
|
function templateLines(tree, template, text, spans) {
|
|
583
856
|
const lineOfPosition = (position) => tree.getLineAndCharacterOfPosition(position).line + 1;
|
|
@@ -608,57 +881,98 @@ function templateText(ts, template) {
|
|
|
608
881
|
text += HOLE + span.literal.text;
|
|
609
882
|
return text;
|
|
610
883
|
}
|
|
611
|
-
function
|
|
612
|
-
|
|
613
|
-
|
|
884
|
+
function jsxAttributeNode(ts, element, name) {
|
|
885
|
+
let spread = false;
|
|
886
|
+
const { properties } = element.attributes;
|
|
887
|
+
for (let index = properties.length - 1; index >= 0; index--) {
|
|
888
|
+
const attr = properties[index];
|
|
889
|
+
if (attr === undefined)
|
|
614
890
|
continue;
|
|
615
|
-
if (
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
return "";
|
|
620
|
-
if (ts.isStringLiteral(init))
|
|
621
|
-
return init.text;
|
|
622
|
-
if (ts.isJsxExpression(init) &&
|
|
623
|
-
init.expression !== undefined &&
|
|
624
|
-
ts.isStringLiteralLike(init.expression))
|
|
625
|
-
return init.expression.text;
|
|
626
|
-
return UNKNOWN;
|
|
891
|
+
if (ts.isJsxSpreadAttribute(attr))
|
|
892
|
+
spread = true;
|
|
893
|
+
else if (attr.name.getText() === name)
|
|
894
|
+
return spread ? UNKNOWN : attr;
|
|
627
895
|
}
|
|
628
896
|
return undefined;
|
|
629
897
|
}
|
|
630
|
-
function
|
|
898
|
+
function jsxAttribute(ts, element, name) {
|
|
899
|
+
const attr = jsxAttributeNode(ts, element, name);
|
|
900
|
+
if (attr === undefined || attr === UNKNOWN)
|
|
901
|
+
return attr;
|
|
902
|
+
const init = attr.initializer;
|
|
903
|
+
if (init === undefined)
|
|
904
|
+
return "";
|
|
905
|
+
if (ts.isStringLiteral(init))
|
|
906
|
+
return init.text;
|
|
907
|
+
if (ts.isJsxExpression(init) &&
|
|
908
|
+
init.expression !== undefined &&
|
|
909
|
+
ts.isStringLiteralLike(init.expression))
|
|
910
|
+
return init.expression.text;
|
|
911
|
+
return UNKNOWN;
|
|
912
|
+
}
|
|
913
|
+
function jsxHostProps(ts, element, name) {
|
|
914
|
+
const attr = jsxAttributeNode(ts, element, name);
|
|
915
|
+
if (attr === UNKNOWN)
|
|
916
|
+
return "unknown";
|
|
917
|
+
if (attr === undefined)
|
|
918
|
+
return element.attributes.properties.some(ts.isJsxSpreadAttribute) ? "unknown" : "absent";
|
|
919
|
+
const init = attr.initializer;
|
|
920
|
+
if (init === undefined)
|
|
921
|
+
return "unknown";
|
|
922
|
+
if (ts.isStringLiteral(init))
|
|
923
|
+
return passedFromJson(init.text) ?? "unknown";
|
|
924
|
+
if (!ts.isJsxExpression(init) || init.expression === undefined)
|
|
925
|
+
return "unknown";
|
|
926
|
+
const expression = init.expression;
|
|
927
|
+
if (ts.isStringLiteralLike(expression))
|
|
928
|
+
return passedFromJson(expression.text) ?? "unknown";
|
|
929
|
+
return passedFromExpression(ts, expression) ?? "unknown";
|
|
930
|
+
}
|
|
931
|
+
function jsxMarkup(ts, root, islandNames) {
|
|
631
932
|
const bindings = [];
|
|
632
933
|
const swaps = [];
|
|
934
|
+
const hosts = [];
|
|
633
935
|
let hasHost = false;
|
|
634
|
-
const walk = (node, owner) => {
|
|
936
|
+
const walk = (node, owner, parentIgnored) => {
|
|
635
937
|
let opening;
|
|
636
938
|
if (ts.isJsxElement(node))
|
|
637
939
|
opening = node.openingElement;
|
|
638
940
|
else if (ts.isJsxSelfClosingElement(node))
|
|
639
941
|
opening = node;
|
|
942
|
+
const ignored = parentIgnored ||
|
|
943
|
+
(opening !== undefined && jsxAttribute(ts, opening, "data-island-ignore") !== undefined);
|
|
640
944
|
let inner = owner;
|
|
641
945
|
if (opening !== undefined) {
|
|
642
946
|
const tag = opening.tagName.getText();
|
|
643
|
-
const
|
|
644
|
-
|
|
645
|
-
|
|
947
|
+
const nifraIsland = islandNames.has(tag);
|
|
948
|
+
const host = ignored
|
|
949
|
+
? undefined
|
|
950
|
+
: jsxAttribute(ts, opening, nifraIsland || tag === "Island" ? "id" : "data-island");
|
|
646
951
|
if (host !== undefined) {
|
|
647
952
|
hasHost = true;
|
|
648
953
|
inner = { island: host, slot: null };
|
|
954
|
+
if (tag !== "Island" || nifraIsland)
|
|
955
|
+
hosts.push({
|
|
956
|
+
id: host,
|
|
957
|
+
props: jsxHostProps(ts, opening, nifraIsland ? "props" : "data-island-props"),
|
|
958
|
+
node: opening,
|
|
959
|
+
});
|
|
649
960
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
961
|
+
if (!ignored) {
|
|
962
|
+
const domTag = ts.isIdentifier(opening.tagName) && /^[a-z]/.test(tag) ? tag.toLowerCase() : undefined;
|
|
963
|
+
for (const attr of opening.attributes.properties) {
|
|
964
|
+
if (ts.isJsxSpreadAttribute(attr))
|
|
965
|
+
continue;
|
|
966
|
+
const name = attr.name.getText();
|
|
967
|
+
if (!name.startsWith("data-bind-"))
|
|
968
|
+
continue;
|
|
969
|
+
const value = jsxAttribute(ts, opening, name);
|
|
970
|
+
if (value === UNKNOWN || value === undefined)
|
|
971
|
+
continue;
|
|
972
|
+
const names = bindingNames(name, value);
|
|
973
|
+
if (names !== undefined)
|
|
974
|
+
bindings.push({ attribute: name, names, tag: domTag, ...owner, node: attr });
|
|
975
|
+
}
|
|
662
976
|
}
|
|
663
977
|
const swap = jsxAttribute(ts, opening, "data-swap");
|
|
664
978
|
if (swap !== undefined) {
|
|
@@ -675,10 +989,10 @@ function jsxMarkup(ts, root) {
|
|
|
675
989
|
inner = { island: inner.island, slot };
|
|
676
990
|
}
|
|
677
991
|
}
|
|
678
|
-
ts.forEachChild(node, (child) => walk(child, inner));
|
|
992
|
+
ts.forEachChild(node, (child) => walk(child, inner, ignored));
|
|
679
993
|
};
|
|
680
|
-
walk(root, { island: null, slot: null });
|
|
681
|
-
return { hasHost, bindings, swaps };
|
|
994
|
+
walk(root, { island: null, slot: null }, false);
|
|
995
|
+
return { hasHost, hosts, bindings, swaps };
|
|
682
996
|
}
|
|
683
997
|
function slotImports(ts, tree) {
|
|
684
998
|
const names = new Set();
|
|
@@ -696,15 +1010,34 @@ function slotImports(ts, tree) {
|
|
|
696
1010
|
}
|
|
697
1011
|
return names;
|
|
698
1012
|
}
|
|
1013
|
+
function islandImports(ts, tree) {
|
|
1014
|
+
const names = new Set();
|
|
1015
|
+
for (const statement of tree.statements) {
|
|
1016
|
+
if (!ts.isImportDeclaration(statement) ||
|
|
1017
|
+
!ts.isStringLiteral(statement.moduleSpecifier) ||
|
|
1018
|
+
!statement.moduleSpecifier.text.startsWith("@nifrajs/"))
|
|
1019
|
+
continue;
|
|
1020
|
+
const bound = statement.importClause?.namedBindings;
|
|
1021
|
+
if (statement.importClause?.isTypeOnly || bound === undefined || !ts.isNamedImports(bound))
|
|
1022
|
+
continue;
|
|
1023
|
+
for (const element of bound.elements)
|
|
1024
|
+
if (!element.isTypeOnly && (element.propertyName ?? element.name).text === "Island")
|
|
1025
|
+
names.add(element.name.text);
|
|
1026
|
+
}
|
|
1027
|
+
return names;
|
|
1028
|
+
}
|
|
699
1029
|
function fileMarkup(ts, tree) {
|
|
700
1030
|
const bindings = [];
|
|
1031
|
+
const hosts = [];
|
|
701
1032
|
const swaps = [];
|
|
702
1033
|
const slotCalls = [];
|
|
703
1034
|
const placement = new Map();
|
|
704
1035
|
const slotNames = slotImports(ts, tree);
|
|
1036
|
+
const islandNames = islandImports(ts, tree);
|
|
705
1037
|
const take = (found) => {
|
|
706
1038
|
if (found.hasHost)
|
|
707
1039
|
bindings.push(...found.bindings);
|
|
1040
|
+
hosts.push(...found.hosts);
|
|
708
1041
|
swaps.push(...found.swaps);
|
|
709
1042
|
};
|
|
710
1043
|
const visit = (node) => {
|
|
@@ -715,12 +1048,12 @@ function fileMarkup(ts, tree) {
|
|
|
715
1048
|
const text = templateText(ts, node.template);
|
|
716
1049
|
const holes = spans.map((span) => span.expression);
|
|
717
1050
|
const lineAt = templateLines(tree, node.template, text, spans);
|
|
718
|
-
take(templateMarkup(text, holes, node, placement, lineAt));
|
|
1051
|
+
take(templateMarkup(ts, text, holes, node, placement, lineAt));
|
|
719
1052
|
}
|
|
720
1053
|
else if (ts.isJsxElement(node) ||
|
|
721
1054
|
ts.isJsxSelfClosingElement(node) ||
|
|
722
1055
|
ts.isJsxFragment(node)) {
|
|
723
|
-
take(jsxMarkup(ts, node));
|
|
1056
|
+
take(jsxMarkup(ts, node, islandNames));
|
|
724
1057
|
return;
|
|
725
1058
|
}
|
|
726
1059
|
else if (ts.isCallExpression(node) &&
|
|
@@ -735,11 +1068,16 @@ function fileMarkup(ts, tree) {
|
|
|
735
1068
|
ts.forEachChild(node, visit);
|
|
736
1069
|
};
|
|
737
1070
|
visit(tree);
|
|
738
|
-
return { bindings, swaps, slotCalls, placement };
|
|
1071
|
+
return { bindings, hosts, swaps, slotCalls, placement };
|
|
739
1072
|
}
|
|
740
1073
|
const markupCache = new WeakMap();
|
|
741
1074
|
export async function projectMarkup(ctx, code, title) {
|
|
742
|
-
const empty = {
|
|
1075
|
+
const empty = {
|
|
1076
|
+
bindings: [],
|
|
1077
|
+
hosts: [],
|
|
1078
|
+
mounts: { open: true, calls: 0 },
|
|
1079
|
+
files: [],
|
|
1080
|
+
};
|
|
743
1081
|
const loaded = await loadRuleTypeScript(ctx);
|
|
744
1082
|
if (loaded.diagnostics !== undefined)
|
|
745
1083
|
return { diagnostics: loaded.diagnostics, markup: empty };
|
|
@@ -755,16 +1093,20 @@ export async function projectMarkup(ctx, code, title) {
|
|
|
755
1093
|
if (tree !== undefined)
|
|
756
1094
|
trees.push({ file, tree });
|
|
757
1095
|
}
|
|
758
|
-
const
|
|
1096
|
+
const surface = mountedIslands(ts, trees.map((entry) => entry.tree));
|
|
1097
|
+
const islands = surface.ids;
|
|
759
1098
|
const bindings = [];
|
|
1099
|
+
const hosts = [];
|
|
760
1100
|
const files = [];
|
|
761
1101
|
for (const { file, tree } of trees) {
|
|
762
1102
|
const found = fileMarkup(ts, tree);
|
|
763
1103
|
for (const binding of found.bindings)
|
|
764
1104
|
bindings.push({ file, line: binding.line ?? lineOf(tree, binding.node), binding, islands });
|
|
1105
|
+
for (const host of found.hosts)
|
|
1106
|
+
hosts.push({ file, line: host.line ?? lineOf(tree, host.node), host, islands });
|
|
765
1107
|
files.push({ file, tree, ...found });
|
|
766
1108
|
}
|
|
767
|
-
const markup = { bindings, files };
|
|
1109
|
+
const markup = { bindings, hosts, mounts: { open: surface.open, calls: surface.calls }, files };
|
|
768
1110
|
markupCache.set(ctx.project.source, markup);
|
|
769
1111
|
return { ts, markup };
|
|
770
1112
|
}
|
|
@@ -840,10 +1182,165 @@ export const islandBindingHostRule = {
|
|
|
840
1182
|
return findings;
|
|
841
1183
|
},
|
|
842
1184
|
};
|
|
1185
|
+
function expectationFor(ctx, cache, file, line, code) {
|
|
1186
|
+
let lines = cache.get(file);
|
|
1187
|
+
if (lines === undefined) {
|
|
1188
|
+
lines = (ctx.project.source.read(file) ?? "").split("\n").map((text) => {
|
|
1189
|
+
const start = text.search(/\S/);
|
|
1190
|
+
if (start < 0)
|
|
1191
|
+
return text;
|
|
1192
|
+
const comment = htmlCommentAt(text, start);
|
|
1193
|
+
if (comment === null || text.slice(start + comment[0].length).trim() !== "")
|
|
1194
|
+
return text;
|
|
1195
|
+
return `//${comment[1] ?? ""}`;
|
|
1196
|
+
});
|
|
1197
|
+
cache.set(file, lines);
|
|
1198
|
+
}
|
|
1199
|
+
const found = commentBlockExpectation(lines, line, [code]);
|
|
1200
|
+
if (found === undefined)
|
|
1201
|
+
return undefined;
|
|
1202
|
+
return found.reason === undefined ? "no-reason" : "silenced";
|
|
1203
|
+
}
|
|
1204
|
+
export const islandUnmountedRule = {
|
|
1205
|
+
code: "NF-C064",
|
|
1206
|
+
title: "Island host with no enhancer",
|
|
1207
|
+
async scan(ctx) {
|
|
1208
|
+
const { diagnostics, markup } = await projectMarkup(ctx, "NF-C064", "Island host with no enhancer");
|
|
1209
|
+
if (diagnostics !== undefined)
|
|
1210
|
+
return diagnostics;
|
|
1211
|
+
if (markup.mounts.open || markup.mounts.calls === 0)
|
|
1212
|
+
return [];
|
|
1213
|
+
const findings = [];
|
|
1214
|
+
const lines = new Map();
|
|
1215
|
+
for (const { file, line, host, islands } of markup.hosts) {
|
|
1216
|
+
if (typeof host.id !== "string" || islands.has(host.id))
|
|
1217
|
+
continue;
|
|
1218
|
+
const expected = expectationFor(ctx, lines, file, line, "NF-C064");
|
|
1219
|
+
if (expected === "silenced")
|
|
1220
|
+
continue;
|
|
1221
|
+
const mounted = [...islands.keys()];
|
|
1222
|
+
findings.push(diagnostic({
|
|
1223
|
+
code: "NF-C064",
|
|
1224
|
+
severity: "warn",
|
|
1225
|
+
file,
|
|
1226
|
+
line,
|
|
1227
|
+
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}`}`,
|
|
1228
|
+
evidence: [
|
|
1229
|
+
`data-island="${host.id}"`,
|
|
1230
|
+
mounted.length === 0 ? "mounted: none" : `mounted: ${mounted.slice(0, 8).join(", ")}`,
|
|
1231
|
+
],
|
|
1232
|
+
fix: { recipe: "islands.host-enhancer", command: "nifra_docs islands" },
|
|
1233
|
+
verify: "nifra check --lints-only",
|
|
1234
|
+
}));
|
|
1235
|
+
}
|
|
1236
|
+
return findings;
|
|
1237
|
+
},
|
|
1238
|
+
};
|
|
1239
|
+
function propMismatch(key, passed, declared) {
|
|
1240
|
+
if (declared === undefined)
|
|
1241
|
+
return `passes "${key}", which the enhancer's props type does not declare`;
|
|
1242
|
+
if (passed === undefined || declared.kind === undefined || passed === declared.kind)
|
|
1243
|
+
return undefined;
|
|
1244
|
+
return `passes "${key}" as ${passed === "null" ? "null" : `a ${passed}`}, which the enhancer declares as ${declared.kind}`;
|
|
1245
|
+
}
|
|
1246
|
+
export const islandPropsRule = {
|
|
1247
|
+
code: "NF-C065",
|
|
1248
|
+
title: "Island props differ from the enhancer's props type",
|
|
1249
|
+
async scan(ctx) {
|
|
1250
|
+
const { diagnostics, markup } = await projectMarkup(ctx, "NF-C065", "Island props differ from the enhancer's props type");
|
|
1251
|
+
if (diagnostics !== undefined)
|
|
1252
|
+
return diagnostics;
|
|
1253
|
+
const findings = [];
|
|
1254
|
+
const lines = new Map();
|
|
1255
|
+
for (const { file, line, host, islands } of markup.hosts) {
|
|
1256
|
+
if (typeof host.id !== "string" || host.props === "unknown")
|
|
1257
|
+
continue;
|
|
1258
|
+
const names = islands.get(host.id);
|
|
1259
|
+
const shape = names === undefined || names === UNKNOWN ? undefined : names.props;
|
|
1260
|
+
if (shape === undefined)
|
|
1261
|
+
continue;
|
|
1262
|
+
const passed = host.props === "absent" ? { entries: new Map(), open: false } : host.props;
|
|
1263
|
+
const problems = [];
|
|
1264
|
+
for (const [key, declared] of shape)
|
|
1265
|
+
if (!declared.optional &&
|
|
1266
|
+
(passed.entries.get(key) === OMITTED || (!passed.open && !passed.entries.has(key))))
|
|
1267
|
+
problems.push(`does not pass "${key}", which the enhancer's props type requires`);
|
|
1268
|
+
for (const [key, kind] of passed.entries) {
|
|
1269
|
+
if (kind === OMITTED)
|
|
1270
|
+
continue;
|
|
1271
|
+
const problem = propMismatch(key, kind, shape.get(key));
|
|
1272
|
+
if (problem !== undefined)
|
|
1273
|
+
problems.push(problem);
|
|
1274
|
+
}
|
|
1275
|
+
const expected = problems.length === 0 ? undefined : expectationFor(ctx, lines, file, line, "NF-C065");
|
|
1276
|
+
if (expected === "silenced")
|
|
1277
|
+
continue;
|
|
1278
|
+
for (const problem of problems)
|
|
1279
|
+
findings.push(diagnostic({
|
|
1280
|
+
code: "NF-C065",
|
|
1281
|
+
severity: "warn",
|
|
1282
|
+
file,
|
|
1283
|
+
line,
|
|
1284
|
+
message: `island "${host.id}" ${problem}${expected === undefined ? "" : `; ${MISSING_EXPECTATION_REASON}`}`,
|
|
1285
|
+
evidence: [`data-island="${host.id}"`],
|
|
1286
|
+
fix: { recipe: "islands.props-contract", command: "nifra_docs islands" },
|
|
1287
|
+
verify: "nifra check --lints-only",
|
|
1288
|
+
}));
|
|
1289
|
+
}
|
|
1290
|
+
return findings;
|
|
1291
|
+
},
|
|
1292
|
+
};
|
|
1293
|
+
export const REFUSED_CODE_ATTRIBUTE = /^(?:on|srcdoc$)/i;
|
|
1294
|
+
export const REFUSED_URL_ATTRIBUTE = /^(?:href|src|action|formaction|xlink:href|poster|data|background|cite|ping)$/i;
|
|
1295
|
+
export const REFUSED_URL_TAG = new Set(["script", "iframe", "object", "embed", "link", "base"]);
|
|
1296
|
+
export const REFUSED_SMIL_TAG = new Set(["set", "animate", "animatemotion", "animatetransform"]);
|
|
1297
|
+
export const islandRefusedBindingRule = {
|
|
1298
|
+
code: "NF-C066",
|
|
1299
|
+
title: "Island attribute binding the runtime refuses",
|
|
1300
|
+
async scan(ctx) {
|
|
1301
|
+
const { diagnostics, markup } = await projectMarkup(ctx, "NF-C066", "Island attribute binding the runtime refuses");
|
|
1302
|
+
if (diagnostics !== undefined)
|
|
1303
|
+
return diagnostics;
|
|
1304
|
+
const findings = [];
|
|
1305
|
+
for (const { file, line, binding } of markup.bindings) {
|
|
1306
|
+
if (binding.attribute !== "data-bind-attr")
|
|
1307
|
+
continue;
|
|
1308
|
+
for (const { key } of binding.names) {
|
|
1309
|
+
if (key === undefined)
|
|
1310
|
+
continue;
|
|
1311
|
+
let reason;
|
|
1312
|
+
if (REFUSED_CODE_ATTRIBUTE.test(key))
|
|
1313
|
+
reason = `"${key}" is an event-handler or srcdoc attribute, which a binding never sets`;
|
|
1314
|
+
else if (binding.tag !== undefined) {
|
|
1315
|
+
if (REFUSED_URL_ATTRIBUTE.test(key) && REFUSED_URL_TAG.has(binding.tag))
|
|
1316
|
+
reason = `"${key}" is a URL attribute on <${binding.tag}>, which loads code or rebases the page`;
|
|
1317
|
+
else if (REFUSED_SMIL_TAG.has(binding.tag))
|
|
1318
|
+
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`;
|
|
1319
|
+
}
|
|
1320
|
+
if (reason === undefined)
|
|
1321
|
+
continue;
|
|
1322
|
+
findings.push(diagnostic({
|
|
1323
|
+
code: "NF-C066",
|
|
1324
|
+
severity: "warn",
|
|
1325
|
+
file,
|
|
1326
|
+
line,
|
|
1327
|
+
message: `data-bind-attr never binds: ${reason}. The runtime skips it with a console warning`,
|
|
1328
|
+
evidence: [`data-bind-attr -> ${key}`],
|
|
1329
|
+
fix: { recipe: "islands.refused-binding", command: "nifra_docs islands" },
|
|
1330
|
+
verify: "nifra check --lints-only",
|
|
1331
|
+
}));
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
return findings;
|
|
1335
|
+
},
|
|
1336
|
+
};
|
|
843
1337
|
export const islandRules = Object.freeze([
|
|
844
1338
|
islandCleanupRule,
|
|
845
1339
|
islandListKeyRule,
|
|
846
1340
|
islandBindingNameRule,
|
|
847
1341
|
islandBindingHostRule,
|
|
1342
|
+
islandUnmountedRule,
|
|
1343
|
+
islandPropsRule,
|
|
1344
|
+
islandRefusedBindingRule,
|
|
848
1345
|
]);
|
|
849
1346
|
//# sourceMappingURL=islands.js.map
|