@nac3/runtime 2.6.0 → 2.9.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.
- package/CHANGELOG.md +938 -0
- package/README.md +111 -3
- package/SPEC.md +1405 -30
- package/contract-head.json +4 -4
- package/dist/cli-harness.mjs +468 -0
- package/dist/cli-levels.json +56 -15
- package/dist/cli-lint.mjs +967 -24
- package/dist/cli-url.mjs +234 -80
- package/dist/cli.js +317 -12
- package/dist/dom-harness.mjs +578 -0
- package/dist/nac-chat-client.browser.js +47 -6
- package/dist/nac-chat-client.cjs +47 -6
- package/dist/nac-chat-client.mjs +47 -6
- package/dist/nac-mcp-interop.browser.js +556 -0
- package/dist/nac-mcp-interop.cjs +561 -0
- package/dist/nac-mcp-interop.d.ts +57 -0
- package/dist/nac-mcp-interop.mjs +561 -0
- package/dist/nac-v2-extensions.browser.js +359 -18
- package/dist/nac-v2-extensions.cjs +359 -18
- package/dist/nac-v2-extensions.d.ts +1 -1
- package/dist/nac-v2-extensions.mjs +359 -18
- package/dist/nac.browser.js +1970 -68
- package/dist/nac.cjs +1970 -68
- package/dist/nac.d.ts +78 -2
- package/dist/nac.mjs +1970 -68
- package/package.json +51 -9
package/dist/cli-lint.mjs
CHANGED
|
@@ -1,15 +1,122 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/* =====================================================================
|
|
2
|
+
cli-lint.mjs -- the static lint behind `nac validate <dir>`.
|
|
3
|
+
|
|
4
|
+
A REAL SOURCE FILE since 2026-11-24 (TF-B04). Until then this module
|
|
5
|
+
did not exist on disk: it was produced by buildCliLint() in
|
|
6
|
+
runtime/build.mjs out of a template string, with every regular
|
|
7
|
+
expression doubly escaped. That made it the single obstacle shared by
|
|
8
|
+
six separate findings -- the role set, the manifest extractor, the DOM
|
|
9
|
+
role rule, and the three rules that did not exist yet -- because
|
|
10
|
+
touching any rule meant editing JavaScript inside a string.
|
|
11
|
+
|
|
12
|
+
THE DECISION, AND ITS BOUND. Moving it here is NOT a refactor of the
|
|
13
|
+
build: runtime/src/cli.mjs and runtime/src/cli-url.mjs are already
|
|
14
|
+
real source files that the build reads and stamps with placeholders.
|
|
15
|
+
This file joins that pattern, and the invariant is written down and
|
|
16
|
+
enforced:
|
|
17
|
+
|
|
18
|
+
the build emits the SAME SIXTEEN artefacts, with the same names,
|
|
19
|
+
and the EXECUTABLE CONTENT of dist/cli-lint.mjs is byte for byte
|
|
20
|
+
what the literal produced.
|
|
21
|
+
|
|
22
|
+
Verified by diffing the artefact across the move: from the first
|
|
23
|
+
import statement onward the two are identical, 5154 bytes each. The
|
|
24
|
+
only difference is this header, which the literal could not carry.
|
|
25
|
+
Saying "byte for byte" without that qualification would have been
|
|
26
|
+
an overclaim, and this file is about not making those.
|
|
27
|
+
runtime/test/battery-coverage.mjs asserts the artefact set, and
|
|
28
|
+
runtime/test/lint-rules.mjs asserts the behaviour.
|
|
29
|
+
|
|
30
|
+
The CANONICAL_ROLES placeholder below is substituted by the build
|
|
31
|
+
with the roles DERIVED from the normative table in SPEC.md section
|
|
32
|
+
1. Never type that list here: a hand-typed copy of it is what made
|
|
33
|
+
the lint report [ERROR] against conforming pages for four minor
|
|
34
|
+
versions.
|
|
35
|
+
|
|
36
|
+
Plain regex-based; intentionally tolerant of malformed input.
|
|
37
|
+
===================================================================== */
|
|
3
38
|
|
|
4
39
|
import fs from 'node:fs';
|
|
5
40
|
import path from 'node:path';
|
|
6
41
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
42
|
+
// The canonical roles, DERIVED from the normative table in SPEC.md
|
|
43
|
+
// section 1 by scripts/spec-levels.mjs -- the same module that gives
|
|
44
|
+
// this build its levels and its locales. Until 2026-11-24 this was a
|
|
45
|
+
// hand-typed set of 18 of the 20, missing draggable and drop-target,
|
|
46
|
+
// so the lint reported manifest_role_unknown on pages that follow the
|
|
47
|
+
// specification exactly. Never type this list again.
|
|
48
|
+
const CANONICAL_ROLES = new Set(["plugin","section","region","action","field","option","tab","breadcrumb-item","accordion-toggle","step","pagination-item","confirm-button","sort-control","filter-control","data-table","navigation","confirm-dialog","draggable","drop-target","button","map","map-layer","map-marker","chart","chart-series","chart-point","calendar","calendar-event","tree","tree-node","tree-children","timeline","timeline-item","carousel","carousel-slide","status","log","toast","toast-region","banner","feedback","modal","drawer","bottom-sheet","combobox","listbox","slider","richtext","tablist","tabpanel","stepper","accordion-section","breadcrumb","pagination-control","avatar","presence-indicator","skeleton","empty-state","list","row","menu","label","value","paragraph","canvas","heading","list-item","code","code-inline","note"]);
|
|
49
|
+
|
|
50
|
+
// The declared aliases of SPEC.md section 1.5 (v2.8), DERIVED from
|
|
51
|
+
// that section by the same module. Seven spellings this tree had
|
|
52
|
+
// grown for roles that already had a name. They are legal -- the
|
|
53
|
+
// runtime resolves them -- and they are reported, so a tree gets
|
|
54
|
+
// migrated deliberately instead of all at once. Never type this one
|
|
55
|
+
// either: the whole v2.8 release exists because two hand-typed copies
|
|
56
|
+
// of a role list drifted for four minor versions.
|
|
57
|
+
const ROLE_ALIASES = {"treenode":"tree-node","timeline-entry":"timeline-item","input":"field","textbox":"field","panel":"region","fab":"action","dropzone":"drop-target"};
|
|
58
|
+
|
|
59
|
+
// The locales a conformance level requires, DERIVED from SPEC.md
|
|
60
|
+
// section 4.3 by the same module, and substituted by the build. Rule 7
|
|
61
|
+
// below needs them; the URL-mode half of the validator has read them
|
|
62
|
+
// from levels.locales_required since 2.5.0, and the directory mode had
|
|
63
|
+
// no way to ask. Never type this list either.
|
|
64
|
+
const REQUIRED_LOCALES = ["es","en","pt","fr","ja","zh","hi","ar","de","it"];
|
|
65
|
+
|
|
66
|
+
/* THE EXTENSIONS THIS LINT READS (PND-134, measured 2026-11-26 while
|
|
67
|
+
evaluating an ERP whose interface is 983 ASP.NET Razor views).
|
|
68
|
+
|
|
69
|
+
Until 2026-11-27 this list was the literal ['.html', '.htm', '.php']
|
|
70
|
+
typed into the two call sites of walk(). An adopter whose markup
|
|
71
|
+
lives anywhere else -- .cshtml, .jsx/.tsx (and this repository ships
|
|
72
|
+
a React demo), .vue -- ran `nac validate <dir>`, the walk found no
|
|
73
|
+
files, and the lint printed "Total findings: 0". Not "no files I can
|
|
74
|
+
read": zero findings, the same line a conforming tree prints. That
|
|
75
|
+
is a FALSE GREEN, the most expensive defect shape there is, because
|
|
76
|
+
the adopter stops looking.
|
|
77
|
+
|
|
78
|
+
Two things had to change, and the list is the lesser one. A list of
|
|
79
|
+
extensions is always one framework behind, so the lint now also
|
|
80
|
+
REPORTS how many files it read and REFUSES when it read none
|
|
81
|
+
(no_lintable_files, below) -- so the framework this list does not
|
|
82
|
+
know yet produces a refusal naming what the walk accepts and the
|
|
83
|
+
--ext escape hatch, instead of silence.
|
|
84
|
+
|
|
85
|
+
What is in here, and why: the server-rendered and component template
|
|
86
|
+
extensions that CARRY MARKUP. Not .js or .ts -- JSX inside a plain
|
|
87
|
+
.js file is real, but the same extension holds every build script
|
|
88
|
+
and bundle in a tree, and a validator that reads those reports
|
|
89
|
+
findings about code nobody serves. An adopter whose components live
|
|
90
|
+
in .js says so with --ext=.js, which is one word, and gets told
|
|
91
|
+
about that word by the refusal. */
|
|
92
|
+
export const LINT_EXTENSIONS = [
|
|
93
|
+
'.html', '.htm', '.xhtml', // plain markup
|
|
94
|
+
'.php', // including Blade (.blade.php)
|
|
95
|
+
'.cshtml', '.vbhtml', '.razor', // ASP.NET Razor and Blazor
|
|
96
|
+
'.aspx', '.ascx', '.master', // ASP.NET Web Forms
|
|
97
|
+
'.jsx', '.tsx', // React and anything JSX
|
|
98
|
+
'.vue', '.svelte', '.astro', // single-file components
|
|
99
|
+
'.hbs', '.handlebars', '.mustache', // Handlebars / Mustache
|
|
100
|
+
'.ejs', '.erb', '.twig', '.liquid', // the other server templates
|
|
101
|
+
'.njk', '.jinja', '.jinja2', '.j2',
|
|
102
|
+
'.jsp', '.jspx',
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
/* --ext ADDS, it never replaces. An adopter who names one extension
|
|
106
|
+
must not silently lose the defaults: that would be a second false
|
|
107
|
+
green wearing the shape of an option. A bare `cshtml` is accepted
|
|
108
|
+
alongside `.cshtml` because that is how it gets typed. */
|
|
109
|
+
function resolveExtensions(extra) {
|
|
110
|
+
const out = LINT_EXTENSIONS.slice();
|
|
111
|
+
for (const raw of (Array.isArray(extra) ? extra : [])) {
|
|
112
|
+
if (typeof raw !== 'string') continue;
|
|
113
|
+
const e = raw.trim().toLowerCase();
|
|
114
|
+
if (!e) continue;
|
|
115
|
+
const dotted = e.startsWith('.') ? e : '.' + e;
|
|
116
|
+
if (out.indexOf(dotted) < 0) out.push(dotted);
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
13
120
|
|
|
14
121
|
function walk(dir, exts) {
|
|
15
122
|
const out = [];
|
|
@@ -17,7 +124,7 @@ function walk(dir, exts) {
|
|
|
17
124
|
const st = fs.statSync(p);
|
|
18
125
|
if (st.isDirectory()) {
|
|
19
126
|
for (const e of fs.readdirSync(p)) rec(path.join(p, e));
|
|
20
|
-
} else if (exts.some(x => p.endsWith(x))) {
|
|
127
|
+
} else if (exts.some(x => p.toLowerCase().endsWith(x))) {
|
|
21
128
|
out.push(p);
|
|
22
129
|
}
|
|
23
130
|
}
|
|
@@ -25,6 +132,17 @@ function walk(dir, exts) {
|
|
|
25
132
|
return out;
|
|
26
133
|
}
|
|
27
134
|
|
|
135
|
+
/* The walk, exported: what this lint WOULD read under `dir`. The CLI
|
|
136
|
+
needs it to say how many files it read, and `nac plugins` needs the
|
|
137
|
+
same answer -- the two commands reading two different lists is the
|
|
138
|
+
producer/consumer asymmetry this release spent its length removing.
|
|
139
|
+
Paths come back relative to `dir`, which is how every finding
|
|
140
|
+
attributes a file. */
|
|
141
|
+
export function lintableFiles(dir, extraExtensions) {
|
|
142
|
+
const exts = resolveExtensions(extraExtensions);
|
|
143
|
+
return walk(dir, exts).map((f) => path.relative(dir, f));
|
|
144
|
+
}
|
|
145
|
+
|
|
28
146
|
function extractDomPairs(content) {
|
|
29
147
|
// Match every (data-nac-id="X", data-nac-role="Y") pair within an
|
|
30
148
|
// attribute-soup window. The naive approach: for each id match, look
|
|
@@ -53,33 +171,525 @@ function extractDomPairs(content) {
|
|
|
53
171
|
}
|
|
54
172
|
|
|
55
173
|
function extractManifestEntries(content) {
|
|
56
|
-
//
|
|
57
|
-
//
|
|
174
|
+
// Every NAC.register call in the file, in BOTH documented shapes,
|
|
175
|
+
// with the argument list delimited by brace matching rather than by
|
|
176
|
+
// a lazy regex.
|
|
177
|
+
//
|
|
178
|
+
// What this replaced, and why it mattered (TF-B02). The old pattern
|
|
179
|
+
// was /NAC\.register\s*\(\s*\{[\s\S]*?\}\s*\)/g, and it was wrong
|
|
180
|
+
// twice:
|
|
181
|
+
//
|
|
182
|
+
// 1. it required `(` to be followed by `{`, so
|
|
183
|
+
// NAC.register(slug, manifest) -- the two-argument form that
|
|
184
|
+
// runtime/build.mjs itself declares supported -- produced NO
|
|
185
|
+
// MATCH AT ALL. Not a wrong finding: zero findings. All four
|
|
186
|
+
// manifest rules inherited that blindness, so the lint
|
|
187
|
+
// APPROVED what it never looked at, which is the worst way for
|
|
188
|
+
// a validator to be wrong.
|
|
189
|
+
// 2. `[\s\S]*?\}\s*\)` stops at the FIRST `}` followed by `)`, so
|
|
190
|
+
// a manifest with a nested object closing that way was cut
|
|
191
|
+
// short and its remaining elements were never seen.
|
|
192
|
+
//
|
|
193
|
+
// Brace matching fixes both. Strings are tracked so a `}` or a `)`
|
|
194
|
+
// inside a quoted label -- a label_i18n value can contain anything
|
|
195
|
+
// -- does not end the argument list early.
|
|
58
196
|
const out = [];
|
|
59
|
-
const
|
|
60
|
-
let
|
|
61
|
-
while ((
|
|
62
|
-
const
|
|
197
|
+
const callRx = /NAC\.register\s*\(/g;
|
|
198
|
+
let call;
|
|
199
|
+
while ((call = callRx.exec(content)) !== null) {
|
|
200
|
+
const open = call.index + call[0].length - 1; // the '(' itself
|
|
201
|
+
const close = matchClose(content, open, '(', ')');
|
|
202
|
+
if (close < 0) continue; // unbalanced: skip, do not guess
|
|
203
|
+
const args = content.slice(open + 1, close);
|
|
204
|
+
|
|
205
|
+
// The manifest is the LAST object literal in the argument list:
|
|
206
|
+
// register({...}) has one, register('slug', {...}) has the slug
|
|
207
|
+
// first. Taking the last one covers both without having to parse
|
|
208
|
+
// the argument list.
|
|
209
|
+
const braceStart = findLastTopLevelBrace(args);
|
|
210
|
+
if (braceStart < 0) continue;
|
|
211
|
+
const braceEnd = matchClose(args, braceStart, '{', '}');
|
|
212
|
+
if (braceEnd < 0) continue;
|
|
213
|
+
const block = args.slice(braceStart, braceEnd + 1);
|
|
214
|
+
|
|
215
|
+
// The slug can come from plugin_slug inside the manifest or from
|
|
216
|
+
// the first argument of the two-argument form. The manifest wins
|
|
217
|
+
// when both are present, because that is what the runtime reads.
|
|
218
|
+
let plugin = '<unknown>';
|
|
219
|
+
const leading = args.slice(0, braceStart);
|
|
220
|
+
const argSlug = /^\s*['"]([^'"]+)['"]\s*,/.exec(leading);
|
|
221
|
+
if (argSlug) plugin = argSlug[1];
|
|
63
222
|
const slugM = /plugin_slug\s*:\s*['"]([^'"]+)['"]/.exec(block);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
223
|
+
if (slugM) plugin = slugM[1];
|
|
224
|
+
|
|
225
|
+
for (const e of elementsOf(block)) {
|
|
226
|
+
out.push({ plugin, id: e.id, role: e.role, labels: e.labels });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Walk from an opening delimiter to its match, skipping over quoted
|
|
233
|
+
// strings, template literals and line/block comments.
|
|
234
|
+
function matchClose(src, from, openCh, closeCh) {
|
|
235
|
+
let depth = 0;
|
|
236
|
+
let i = from;
|
|
237
|
+
while (i < src.length) {
|
|
238
|
+
const c = src[i];
|
|
239
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
240
|
+
const q = c;
|
|
241
|
+
i += 1;
|
|
242
|
+
while (i < src.length) {
|
|
243
|
+
if (src[i] === '\\') { i += 2; continue; }
|
|
244
|
+
if (src[i] === q) break;
|
|
245
|
+
i += 1;
|
|
246
|
+
}
|
|
247
|
+
i += 1;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (c === '/' && src[i + 1] === '/') {
|
|
251
|
+
const nl = src.indexOf('\n', i);
|
|
252
|
+
i = nl < 0 ? src.length : nl + 1;
|
|
253
|
+
continue;
|
|
70
254
|
}
|
|
255
|
+
if (c === '/' && src[i + 1] === '*') {
|
|
256
|
+
const end = src.indexOf('*/', i + 2);
|
|
257
|
+
i = end < 0 ? src.length : end + 2;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (c === openCh) depth += 1;
|
|
261
|
+
else if (c === closeCh) {
|
|
262
|
+
depth -= 1;
|
|
263
|
+
if (depth === 0) return i;
|
|
264
|
+
}
|
|
265
|
+
i += 1;
|
|
266
|
+
}
|
|
267
|
+
return -1;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// The last `{` in the argument list that is not inside another brace,
|
|
271
|
+
// bracket, paren or string: the manifest object of either call shape.
|
|
272
|
+
function findLastTopLevelBrace(args) {
|
|
273
|
+
let depth = 0;
|
|
274
|
+
let last = -1;
|
|
275
|
+
let i = 0;
|
|
276
|
+
while (i < args.length) {
|
|
277
|
+
const c = args[i];
|
|
278
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
279
|
+
const q = c;
|
|
280
|
+
i += 1;
|
|
281
|
+
while (i < args.length) {
|
|
282
|
+
if (args[i] === '\\') { i += 2; continue; }
|
|
283
|
+
if (args[i] === q) break;
|
|
284
|
+
i += 1;
|
|
285
|
+
}
|
|
286
|
+
i += 1;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (c === '{' || c === '[' || c === '(') {
|
|
290
|
+
if (c === '{' && depth === 0) last = i;
|
|
291
|
+
depth += 1;
|
|
292
|
+
} else if (c === '}' || c === ']' || c === ')') {
|
|
293
|
+
depth -= 1;
|
|
294
|
+
}
|
|
295
|
+
i += 1;
|
|
296
|
+
}
|
|
297
|
+
return last;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Each entry of elements[], with its id, its role and the locales each
|
|
301
|
+
// of its label_i18n blocks declares. Brace-matched one element at a
|
|
302
|
+
// time so a nested actions[] or label_i18n does not truncate it -- the
|
|
303
|
+
// old /\{[^{}]*?id.*?role.*?\}/ pattern could not see past one level,
|
|
304
|
+
// which is the same defect as the call pattern one scale down.
|
|
305
|
+
function elementsOf(block) {
|
|
306
|
+
const arrStart = block.search(/elements\s*:\s*\[/);
|
|
307
|
+
if (arrStart < 0) return [];
|
|
308
|
+
const bracket = block.indexOf('[', arrStart);
|
|
309
|
+
const arrEnd = matchClose(block, bracket, '[', ']');
|
|
310
|
+
if (arrEnd < 0) return [];
|
|
311
|
+
const body = block.slice(bracket + 1, arrEnd);
|
|
312
|
+
|
|
313
|
+
const out = [];
|
|
314
|
+
let i = 0;
|
|
315
|
+
while (i < body.length) {
|
|
316
|
+
if (body[i] !== '{') { i += 1; continue; }
|
|
317
|
+
const end = matchClose(body, i, '{', '}');
|
|
318
|
+
if (end < 0) break;
|
|
319
|
+
const entry = body.slice(i, end + 1);
|
|
320
|
+
const idM = /\bid\s*:\s*['"]([^'"]+)['"]/.exec(entry);
|
|
321
|
+
const roleM = /\brole\s*:\s*['"]([^'"]+)['"]/.exec(entry);
|
|
322
|
+
if (idM && roleM) {
|
|
323
|
+
const labels = [];
|
|
324
|
+
let j = 0;
|
|
325
|
+
while (j < entry.length) {
|
|
326
|
+
const at = entry.indexOf('label_i18n', j);
|
|
327
|
+
if (at < 0) break;
|
|
328
|
+
const ob = entry.indexOf('{', at);
|
|
329
|
+
if (ob < 0) break;
|
|
330
|
+
const oe = matchClose(entry, ob, '{', '}');
|
|
331
|
+
if (oe < 0) break;
|
|
332
|
+
const inner = entry.slice(ob + 1, oe);
|
|
333
|
+
labels.push([...inner.matchAll(/(?:^|[,{\s])([a-z]{2})\s*:/g)].map((m) => m[1]));
|
|
334
|
+
j = oe + 1;
|
|
335
|
+
}
|
|
336
|
+
out.push({ id: idM[1], role: roleM[1], labels });
|
|
337
|
+
}
|
|
338
|
+
i = end + 1;
|
|
71
339
|
}
|
|
72
340
|
return out;
|
|
73
341
|
}
|
|
74
342
|
|
|
75
|
-
|
|
343
|
+
/* THE JAVASCRIPT A FILE ACTUALLY RUNS (PND-145, measured 2026-11-27
|
|
344
|
+
while writing the demo card of SPEC 1.4).
|
|
345
|
+
|
|
346
|
+
THE DEFECT. Rule 5b read the file as BYTES: any occurrence of
|
|
347
|
+
register_role('name') counted as a call. So a page that DOCUMENTS
|
|
348
|
+
the mechanism with a literal example -- inside a <code>, which is
|
|
349
|
+
the only honest way to document it -- was reported as calling a
|
|
350
|
+
role it never declares, at severity `error`, which stops a
|
|
351
|
+
pipeline. Measured: demos/index.html, the demos showcase, which has
|
|
352
|
+
no scripts at all. The page was penalised for teaching the feature.
|
|
353
|
+
And it is not a corner: the landing, the manual, the generated
|
|
354
|
+
pages of the normative text and any adopter's own documentation all
|
|
355
|
+
want to quote that line.
|
|
356
|
+
|
|
357
|
+
WHAT COUNTS NOW. For markup, only the bodies of its inline
|
|
358
|
+
<script> elements -- which is what "this file calls" means, and it
|
|
359
|
+
makes prose, <code>, <pre> and HTML comments drop out for free
|
|
360
|
+
rather than by a list of tags to skip. For a file that IS
|
|
361
|
+
JavaScript (.jsx and .tsx by default; .js or .mjs when the adopter
|
|
362
|
+
adds them with --ext) the whole content, minus comments, because a
|
|
363
|
+
call commented out is not a call either.
|
|
364
|
+
|
|
365
|
+
THE LIMIT THAT WAS CLOSED (PND-153, 2026-11-27). Until today a
|
|
366
|
+
call that lived in a SEPARATE file named by <script src> stayed
|
|
367
|
+
invisible: the lint read one file at a time and did not follow the
|
|
368
|
+
reference. That was measured, written down as a passing assertion
|
|
369
|
+
(case 10d of the suite) rather than believed -- and it mattered,
|
|
370
|
+
because putting a plugin in its own file is the NORMAL thing to do,
|
|
371
|
+
so the adopter who does it had no lock at all, in both directions:
|
|
372
|
+
a real asymmetry went unreported, and a legitimate nac3.roles.json
|
|
373
|
+
was reported as an orphan because the only call that used it was in
|
|
374
|
+
a file nobody read. Rule 5c below follows the reference, under
|
|
375
|
+
three conditions that keep it from becoming "walk every .js in the
|
|
376
|
+
tree" (which this file rejects, with reason, two paragraphs down):
|
|
377
|
+
the reference is RELATIVE, it resolves INSIDE the linted root, and
|
|
378
|
+
the file exists. A remote URL, a root-absolute path (whose meaning
|
|
379
|
+
depends on a mount this lint does not know) and a path that climbs
|
|
380
|
+
out of the root are not read -- and when not reading one could
|
|
381
|
+
change the verdict on a page, the lint SAYS so instead of going
|
|
382
|
+
quiet (code script_not_read).
|
|
383
|
+
|
|
384
|
+
THE LIMIT THAT REMAINS: the scanner that drops comments understands
|
|
385
|
+
strings, template literals and both comment forms, but not a
|
|
386
|
+
regular-expression literal containing //. The whole cost of that
|
|
387
|
+
case is a call on that one line going unseen.
|
|
388
|
+
|
|
389
|
+
A <script> whose type is not JavaScript -- a JSON island, an
|
|
390
|
+
HTML template -- is not executed by the browser, so it is not
|
|
391
|
+
executed here either. */
|
|
392
|
+
const JS_LIKE_EXTENSIONS = [
|
|
393
|
+
'.js', '.mjs', '.cjs', '.jsx', '.ts', '.mts', '.cts', '.tsx',
|
|
394
|
+
];
|
|
395
|
+
|
|
396
|
+
function stripJsComments(src) {
|
|
397
|
+
let out = '';
|
|
398
|
+
let i = 0;
|
|
399
|
+
const n = src.length;
|
|
400
|
+
while (i < n) {
|
|
401
|
+
const c = src[i];
|
|
402
|
+
const d = src[i + 1];
|
|
403
|
+
if (c === '/' && d === '/') {
|
|
404
|
+
while (i < n && src[i] !== '\n') i++;
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (c === '/' && d === '*') {
|
|
408
|
+
i += 2;
|
|
409
|
+
while (i < n && !(src[i] === '*' && src[i + 1] === '/')) {
|
|
410
|
+
if (src[i] === '\n') out += '\n';
|
|
411
|
+
i++;
|
|
412
|
+
}
|
|
413
|
+
i += 2;
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
417
|
+
/* The role name of a real call lives INSIDE a string literal,
|
|
418
|
+
so string contents are kept. Only the quoting is tracked, to
|
|
419
|
+
stop a // or a /* inside a string from being read as the
|
|
420
|
+
start of a comment. */
|
|
421
|
+
const q = c;
|
|
422
|
+
out += c;
|
|
423
|
+
i++;
|
|
424
|
+
while (i < n) {
|
|
425
|
+
if (src[i] === '\\') { out += src.slice(i, i + 2); i += 2; continue; }
|
|
426
|
+
out += src[i];
|
|
427
|
+
if (src[i] === q) { i++; break; }
|
|
428
|
+
i++;
|
|
429
|
+
}
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
out += c;
|
|
433
|
+
i++;
|
|
434
|
+
}
|
|
435
|
+
return out;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const EXECUTABLE_SCRIPT_TYPE = /^(?:text|application)\/(?:java|ecma)script(?:;|$)|^module$/i;
|
|
439
|
+
|
|
440
|
+
/* THE JAVASCRIPT A PAGE LOADS FROM ANOTHER FILE (PND-153).
|
|
441
|
+
|
|
442
|
+
Returns one entry per <script src> of a markup file: either
|
|
443
|
+
{ rel, abs, body } for a reference this lint is allowed to read, or
|
|
444
|
+
{ ref, skipped } with the reason it is not. Nothing is thrown and
|
|
445
|
+
nothing is guessed: a reference that cannot be resolved honestly is
|
|
446
|
+
reported as unread, because the alternative -- reading a file the
|
|
447
|
+
page may not even be loading -- would make every finding about it
|
|
448
|
+
fiction.
|
|
449
|
+
|
|
450
|
+
The three conditions, and why each one:
|
|
451
|
+
RELATIVE a root-absolute src means whatever the deployment
|
|
452
|
+
mounts the tree at (this project serves under
|
|
453
|
+
/nac-spec/, decision D-02); the lint does not know
|
|
454
|
+
the mount of the tree it is given.
|
|
455
|
+
INSIDE a path that climbs out of the linted root is outside
|
|
456
|
+
the subject. Reading it would report findings about
|
|
457
|
+
a file the caller did not ask about.
|
|
458
|
+
EXISTS a src that does not resolve is already rule 6's and
|
|
459
|
+
check-tree-links' business, not this rule's. */
|
|
460
|
+
function referencedScripts(file, content, root) {
|
|
461
|
+
const out = [];
|
|
462
|
+
if (JS_LIKE_EXTENSIONS.indexOf(path.extname(file).toLowerCase()) >= 0) return out;
|
|
463
|
+
for (const m of content.matchAll(/<script\b([^>]*?)\ssrc\s*=\s*["']([^"']*)["']([^>]*)>/gi)) {
|
|
464
|
+
const attrs = (m[1] || '') + ' ' + (m[3] || '');
|
|
465
|
+
const type = (/\btype\s*=\s*["']?([^"'\s>]+)/i.exec(attrs) || [])[1];
|
|
466
|
+
if (type && !EXECUTABLE_SCRIPT_TYPE.test(type.trim())) continue;
|
|
467
|
+
const ref = (m[2] || '').trim();
|
|
468
|
+
if (!ref) continue;
|
|
469
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.slice(0, 2) === '//') {
|
|
470
|
+
out.push({ ref, skipped: 'it is a remote URL' });
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (ref[0] === '/') {
|
|
474
|
+
out.push({ ref, skipped: 'it is root-absolute, so where it points depends on the mount of the deployment and not on this tree' });
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
const clean = ref.split('#')[0].split('?')[0];
|
|
478
|
+
if (!clean) continue;
|
|
479
|
+
const abs = path.resolve(path.dirname(file), clean);
|
|
480
|
+
const rel = path.relative(root, abs);
|
|
481
|
+
if (!rel || rel.slice(0, 2) === '..' || path.isAbsolute(rel)) {
|
|
482
|
+
out.push({ ref, skipped: 'it resolves outside the linted root' });
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
let body;
|
|
486
|
+
try { body = fs.readFileSync(abs, 'utf8'); }
|
|
487
|
+
catch (_) { out.push({ ref, skipped: 'it does not resolve to a readable file in this tree' }); continue; }
|
|
488
|
+
out.push({ ref, abs, rel: rel.split(path.sep).join('/'), body: stripJsComments(body) });
|
|
489
|
+
}
|
|
490
|
+
return out;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function executableScript(file, content) {
|
|
494
|
+
const ext = path.extname(file).toLowerCase();
|
|
495
|
+
if (JS_LIKE_EXTENSIONS.indexOf(ext) >= 0) return stripJsComments(content);
|
|
496
|
+
let out = '';
|
|
497
|
+
for (const m of content.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi)) {
|
|
498
|
+
const type = (/\btype\s*=\s*["']?([^"'\s>]+)/i.exec(m[1] || '') || [])[1];
|
|
499
|
+
if (type && !EXECUTABLE_SCRIPT_TYPE.test(type.trim())) continue;
|
|
500
|
+
out += stripJsComments(m[2]) + '\n';
|
|
501
|
+
}
|
|
502
|
+
return out;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/* The manifest inventory of a tree: every element every NAC.register
|
|
506
|
+
call declares, with the file it came from.
|
|
507
|
+
|
|
508
|
+
Exported for `nac plugins <dir>` (TF-B09). The CLI had one
|
|
509
|
+
subcommand, so a protocol whose argument is that a UI becomes
|
|
510
|
+
DISCOVERABLE had no way to ask a tree what it declares -- while the
|
|
511
|
+
extraction it needed was already here, feeding the four manifest
|
|
512
|
+
rules. This adds the walk and the file attribution, nothing else:
|
|
513
|
+
same extractor, so `plugins` and `validate` can never disagree
|
|
514
|
+
about what a file declares. */
|
|
515
|
+
export function manifestInventory(dir, opts) {
|
|
516
|
+
const out = [];
|
|
517
|
+
/* The SAME extension list the lint walks with (PND-134). Two call
|
|
518
|
+
sites with the same literal is how `plugins` and `validate` would
|
|
519
|
+
come to disagree about what a tree contains. */
|
|
520
|
+
for (const file of walk(dir, resolveExtensions(opts && opts.extensions))) {
|
|
521
|
+
let content;
|
|
522
|
+
try { content = fs.readFileSync(file, 'utf8'); } catch (_) { continue; }
|
|
523
|
+
const rel = path.relative(dir, file);
|
|
524
|
+
for (const e of extractManifestEntries(content)) {
|
|
525
|
+
out.push({ plugin: e.plugin, id: e.id, role: e.role, file: rel });
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return out;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/* The whole answer of a directory run: the files that were READ, the
|
|
532
|
+
extensions the walk accepted, and the findings. staticLint() keeps
|
|
533
|
+
returning the findings alone, because that is what every caller
|
|
534
|
+
since 2.0 expects; this is the shape the CLI needs so it can print
|
|
535
|
+
the file count instead of only a finding count (PND-134). The count
|
|
536
|
+
comes from the same walk that produced the findings -- not from a
|
|
537
|
+
second one that could disagree with it. */
|
|
538
|
+
export function lintTree(dir, opts) {
|
|
539
|
+
const extensions = resolveExtensions(opts && opts.extensions);
|
|
540
|
+
const files = walk(dir, extensions);
|
|
541
|
+
return {
|
|
542
|
+
dir,
|
|
543
|
+
extensions,
|
|
544
|
+
files: files.map((f) => path.relative(dir, f)),
|
|
545
|
+
findings: lintFiles(dir, files, extensions),
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export function staticLint(dir, opts) {
|
|
550
|
+
const extensions = resolveExtensions(opts && opts.extensions);
|
|
551
|
+
return lintFiles(dir, walk(dir, extensions), extensions);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function lintFiles(dir, files, extensions) {
|
|
76
555
|
const findings = [];
|
|
77
|
-
|
|
556
|
+
|
|
557
|
+
/* THE REFUSAL (PND-134). Zero files read is not zero findings: it
|
|
558
|
+
is the lint saying nothing about a tree it could not open, and
|
|
559
|
+
until 2026-11-27 the two printed the same line. `error`, so the
|
|
560
|
+
exit code fails with it -- a false green that exits 0 is what
|
|
561
|
+
lets an adopter wire this into a pipeline and believe it. */
|
|
562
|
+
if (files.length === 0) {
|
|
563
|
+
findings.push({
|
|
564
|
+
severity: 'error',
|
|
565
|
+
code: 'no_lintable_files',
|
|
566
|
+
file: null,
|
|
567
|
+
message: 'read 0 files under this directory, so this reports nothing about it -- '
|
|
568
|
+
+ 'which is NOT the same as a conforming tree. The extensions this lint '
|
|
569
|
+
+ 'reads are: ' + extensions.join(' ') + '. If the markup of this tree '
|
|
570
|
+
+ 'lives in another extension, name it: nac validate <dir> --ext=.foo,.bar'
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/* THE PAGE-SCOPED ROLE REGISTRY of SPEC.md 1.4, read PER FILE
|
|
575
|
+
(2026-11-27, PND-132).
|
|
576
|
+
|
|
577
|
+
Until today this read exactly one path -- nac3.roles.json at the
|
|
578
|
+
lint ROOT -- and section 1.4 says the declaration goes "next to
|
|
579
|
+
the markup". The two agree while an adopter lints the directory
|
|
580
|
+
the page lives in, and disagree the moment anyone lints a PARENT
|
|
581
|
+
of it: every register_role call in the tree became
|
|
582
|
+
role_registration_undeclared, at `error`, over a registry that
|
|
583
|
+
was sitting right beside the page. Found by writing the demo that
|
|
584
|
+
exercises register_role end to end: `nac validate demos` -- which
|
|
585
|
+
runtime/test/smoke.mjs runs -- would have reported the demo as
|
|
586
|
+
wrong for doing what section 1.4 asks.
|
|
587
|
+
|
|
588
|
+
So a file's effective registry is every nac3.roles.json on the
|
|
589
|
+
path from the lint root down to that file's own directory, the
|
|
590
|
+
nearest one winning on a conflict. A root registry still applies
|
|
591
|
+
to everything under it, which is the behaviour the old code had
|
|
592
|
+
for the one case it handled.
|
|
593
|
+
|
|
594
|
+
A MISSING registry is the normal case and not a finding: almost
|
|
595
|
+
no page registers a role. A registry that is present and
|
|
596
|
+
unparseable IS one, because the alternative is silently linting
|
|
597
|
+
against an empty registry and reporting every registered role as
|
|
598
|
+
unknown -- the failure mode this release spent its length
|
|
599
|
+
removing. */
|
|
600
|
+
const registries = new Map(); /* dir (relative, '' = root) -> Map(role -> spec) */
|
|
601
|
+
const registryUsed = new Map(); /* same key -> Set(role actually registered below) */
|
|
602
|
+
|
|
603
|
+
function loadRegistry(absDir) {
|
|
604
|
+
const key = path.relative(dir, absDir).split(path.sep).join('/');
|
|
605
|
+
if (registries.has(key)) return key;
|
|
606
|
+
const file = path.join(absDir, 'nac3.roles.json');
|
|
607
|
+
if (!fs.existsSync(file)) { registries.set(key, null); return key; }
|
|
608
|
+
const relFile = (key ? key + '/' : '') + 'nac3.roles.json';
|
|
609
|
+
const map = new Map();
|
|
610
|
+
try {
|
|
611
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
612
|
+
const list = Array.isArray(parsed && parsed.roles) ? parsed.roles : [];
|
|
613
|
+
for (const r of list) {
|
|
614
|
+
if (r && typeof r.role === 'string') map.set(r.role, r);
|
|
615
|
+
}
|
|
616
|
+
if (!list.length) {
|
|
617
|
+
findings.push({
|
|
618
|
+
severity: 'warn',
|
|
619
|
+
code: 'role_registry_empty',
|
|
620
|
+
file: relFile,
|
|
621
|
+
message: relFile + ' is present and declares no roles. If this directory ' +
|
|
622
|
+
'registers none, delete the file; an empty registry and a missing one ' +
|
|
623
|
+
'mean the same thing to this lint and only one of them says so.'
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
} catch (e) {
|
|
627
|
+
findings.push({
|
|
628
|
+
severity: 'error',
|
|
629
|
+
code: 'role_registry_unreadable',
|
|
630
|
+
file: relFile,
|
|
631
|
+
message: relFile + ' could not be parsed (' + (e && e.message) + '). Linting ' +
|
|
632
|
+
'against an empty registry would report every registered role as unknown, ' +
|
|
633
|
+
'so this refuses instead (SPEC.md 1.4).'
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
registries.set(key, map);
|
|
637
|
+
registryUsed.set(key, new Set());
|
|
638
|
+
return key;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/* Every directory from the lint root down to `absDir`, nearest last,
|
|
642
|
+
so a registry closer to the file overrides one above it. */
|
|
643
|
+
function registryChain(absDir) {
|
|
644
|
+
const steps = [];
|
|
645
|
+
let cur = absDir;
|
|
646
|
+
for (;;) {
|
|
647
|
+
steps.unshift(cur);
|
|
648
|
+
const rel = path.relative(dir, cur);
|
|
649
|
+
if (!rel || rel === '.' || rel.startsWith('..')) break;
|
|
650
|
+
const up = path.dirname(cur);
|
|
651
|
+
if (up === cur) break;
|
|
652
|
+
cur = up;
|
|
653
|
+
}
|
|
654
|
+
return steps.map((d) => loadRegistry(d));
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function rolesFor(absDir) {
|
|
658
|
+
const out = new Map();
|
|
659
|
+
for (const key of registryChain(absDir)) {
|
|
660
|
+
const m = registries.get(key);
|
|
661
|
+
if (!m) continue;
|
|
662
|
+
for (const [role, spec] of m) out.set(role, spec);
|
|
663
|
+
}
|
|
664
|
+
return out;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/* Which registry a register_role call counts as using: the nearest
|
|
668
|
+
one on the chain that declares it. */
|
|
669
|
+
function markUsed(absDir, role) {
|
|
670
|
+
const chain = registryChain(absDir);
|
|
671
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
672
|
+
const m = registries.get(chain[i]);
|
|
673
|
+
if (m && m.has(role)) { registryUsed.get(chain[i]).add(role); return true; }
|
|
674
|
+
}
|
|
675
|
+
return false;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/* PND-153: one finding per (script, role), not one per page that
|
|
679
|
+
loads it. A shared plugin file loaded by nine pages is one
|
|
680
|
+
asymmetry, and reporting it nine times is how a reader stops
|
|
681
|
+
reading. */
|
|
682
|
+
const seenViaSrc = new Set();
|
|
683
|
+
|
|
78
684
|
for (const file of files) {
|
|
79
685
|
let content;
|
|
80
686
|
try { content = fs.readFileSync(file, 'utf8'); }
|
|
81
687
|
catch (_) { continue; }
|
|
82
688
|
const rel = path.relative(dir, file);
|
|
689
|
+
const findingsBefore = findings.length;
|
|
690
|
+
|
|
691
|
+
/* PND-132: the registry that applies to THIS file. */
|
|
692
|
+
const registeredRoles = rolesFor(path.dirname(file));
|
|
83
693
|
|
|
84
694
|
const domPairs = extractDomPairs(content);
|
|
85
695
|
const manifestEntries = extractManifestEntries(content);
|
|
@@ -99,7 +709,18 @@ export function staticLint(dir) {
|
|
|
99
709
|
|
|
100
710
|
// Rule 2: manifest_role_unknown -- manifest element role outside canonical set.
|
|
101
711
|
for (const e of manifestEntries) {
|
|
102
|
-
|
|
712
|
+
/* v2.8.0: same split as rule 5, on the manifest side. The
|
|
713
|
+
manifest is the one place where a spelling can be fixed
|
|
714
|
+
without touching markup, so it is worth naming separately. */
|
|
715
|
+
if (Object.prototype.hasOwnProperty.call(ROLE_ALIASES, e.role)) {
|
|
716
|
+
findings.push({
|
|
717
|
+
severity: 'warn',
|
|
718
|
+
code: 'manifest_role_legacy_spelling',
|
|
719
|
+
file: rel,
|
|
720
|
+
message: 'plugin "' + e.plugin + '" id="' + e.id + '" declares role="' + e.role +
|
|
721
|
+
'"; the canonical role is "' + ROLE_ALIASES[e.role] + '" (SPEC.md 1.5)'
|
|
722
|
+
});
|
|
723
|
+
} else if (!CANONICAL_ROLES.has(e.role) && !registeredRoles.has(e.role)) {
|
|
103
724
|
findings.push({
|
|
104
725
|
severity: 'error',
|
|
105
726
|
code: 'manifest_role_unknown',
|
|
@@ -137,6 +758,328 @@ export function staticLint(dir) {
|
|
|
137
758
|
});
|
|
138
759
|
}
|
|
139
760
|
}
|
|
761
|
+
|
|
762
|
+
/* ---------------------------------------------------------------
|
|
763
|
+
THE FOUR RULES ADDED 2026-11-24, AND THE SEVERITY EACH CHOSE.
|
|
764
|
+
|
|
765
|
+
Severity is not decoration here. runtime/test/smoke.mjs asserts
|
|
766
|
+
that `nac validate <demos>` reports no [ERROR], so a new rule at
|
|
767
|
+
`error` that fires over the repository's own demo pages would
|
|
768
|
+
break the battery -- and a rule that has to be switched off is
|
|
769
|
+
worse than no rule. Each severity below was picked after
|
|
770
|
+
MEASURING what the rule reports over demos/, and the number is
|
|
771
|
+
recorded with it.
|
|
772
|
+
--------------------------------------------------------------- */
|
|
773
|
+
|
|
774
|
+
/* Rule 5: dom_role_unknown -- a role written in the MARKUP that
|
|
775
|
+
the specification does not declare. severity: warn
|
|
776
|
+
|
|
777
|
+
The gap this closes: rules 2 and 4 iterate manifestEntries, so a
|
|
778
|
+
role that only ever appears as a data-nac-role attribute was
|
|
779
|
+
never compared against anything. Measured 2026-11-24, roles
|
|
780
|
+
invented straight in the markup -- map-marker,
|
|
781
|
+
totalmente-inventado-no-existe -- produced ZERO findings.
|
|
782
|
+
|
|
783
|
+
warn, and deliberately: measured 9 non-canonical DOM roles in
|
|
784
|
+
demos/ (status, input, menu, panel, canvas, list, log) and about
|
|
785
|
+
14,460 in the published tree, almost all of them code-inline
|
|
786
|
+
from one generator. SPEC.md section 1 says roles outside the
|
|
787
|
+
table are "reserved for future use" and that a NAC-permissive
|
|
788
|
+
runtime MAY treat an unknown role as action -- so this is a real
|
|
789
|
+
finding about vocabulary, not a broken contract. At `error` it
|
|
790
|
+
would turn every existing page red and would force a decision
|
|
791
|
+
about those 14,460 that belongs to the owner, not to a lint. */
|
|
792
|
+
for (const p of domPairs) {
|
|
793
|
+
if (!p.role) continue;
|
|
794
|
+
/* v2.8.0: a declared alias (SPEC.md 1.5) is NOT unknown. Before
|
|
795
|
+
this branch the seven legacy spellings of this tree were
|
|
796
|
+
reported as vocabulary the spec does not declare, which was
|
|
797
|
+
true of the spelling and false of the concept -- and it gave
|
|
798
|
+
the reader no way to tell "this word does not exist" from
|
|
799
|
+
"this word has a canonical form". They are now separate
|
|
800
|
+
codes, and the fix is in the message. */
|
|
801
|
+
if (Object.prototype.hasOwnProperty.call(ROLE_ALIASES, p.role)) {
|
|
802
|
+
findings.push({
|
|
803
|
+
severity: 'warn',
|
|
804
|
+
code: 'dom_role_legacy_spelling',
|
|
805
|
+
file: rel,
|
|
806
|
+
message: 'data-nac-id="' + p.id + '" carries data-nac-role="' + p.role +
|
|
807
|
+
'"; the canonical role is "' + ROLE_ALIASES[p.role] +
|
|
808
|
+
'" (SPEC.md 1.5). The alias resolves at runtime, so nothing is broken ' +
|
|
809
|
+
'today; migrating the spelling is what keeps one concept to one word.'
|
|
810
|
+
});
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
if (!CANONICAL_ROLES.has(p.role) && !registeredRoles.has(p.role)) {
|
|
814
|
+
findings.push({
|
|
815
|
+
severity: 'warn',
|
|
816
|
+
code: 'dom_role_unknown',
|
|
817
|
+
file: rel,
|
|
818
|
+
message: 'data-nac-id="' + p.id + '" carries data-nac-role="' + p.role +
|
|
819
|
+
'", which SPEC.md section 1 does not declare'
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/* Rule 5b: the static half of NAC.register_role (SPEC.md 1.4).
|
|
825
|
+
role_registration_undeclared severity: error
|
|
826
|
+
role_registration_orphan severity: warn
|
|
827
|
+
|
|
828
|
+
THE PROBLEM. A role registered with NAC.register_role() exists
|
|
829
|
+
only while the page is loaded. This lint runs with no page: it
|
|
830
|
+
reads files. So a page that declares its own vocabulary in
|
|
831
|
+
JavaScript is invisible here, and rule 5 would report it as
|
|
832
|
+
wrong for using a role the page itself declares.
|
|
833
|
+
|
|
834
|
+
Making the lint EXECUTE the page it is validating was never an
|
|
835
|
+
option -- a validator that runs its subject has stopped being
|
|
836
|
+
one. So section 1.4 asks for a second declaration this can
|
|
837
|
+
read, a nac3.roles.json next to the markup, and these two codes
|
|
838
|
+
are what stops two declarations of one fact from drifting,
|
|
839
|
+
which is the asymmetry this whole release exists to remove.
|
|
840
|
+
|
|
841
|
+
error on undeclared, and that asymmetry is deliberate: a role
|
|
842
|
+
the lint cannot see makes every OTHER finding about that
|
|
843
|
+
element unreliable, while a declaration nobody uses yet is
|
|
844
|
+
merely untidy. */
|
|
845
|
+
for (const m of executableScript(file, content)
|
|
846
|
+
.matchAll(/\bregister_role\s*\(\s*['"]([a-z][a-z0-9-]*)['"]/g)) {
|
|
847
|
+
if (!markUsed(path.dirname(file), m[1])) {
|
|
848
|
+
findings.push({
|
|
849
|
+
severity: 'error',
|
|
850
|
+
code: 'role_registration_undeclared',
|
|
851
|
+
file: rel,
|
|
852
|
+
message: 'the JavaScript this file RUNS calls NAC.register_role("' + m[1] +
|
|
853
|
+
'") and no nac3.roles.json beside it -- or in any directory between it and ' +
|
|
854
|
+
'the linted root -- declares it. A static validator cannot see a role declared ' +
|
|
855
|
+
'in JavaScript, so every finding about an element carrying that role would be ' +
|
|
856
|
+
'unreliable (SPEC.md 1.4). Prose that merely QUOTES the call -- inside a <code>, ' +
|
|
857
|
+
'a <pre> or a comment -- is not a call and is not reported (PND-145).'
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/* Rule 5c: the same asymmetry, when the call lives in the file the
|
|
863
|
+
page LOADS (PND-153). role_registration_undeclared error
|
|
864
|
+
script_not_read warn
|
|
865
|
+
|
|
866
|
+
THE DEFECT THIS CLOSES, measured on this tree: the only call of
|
|
867
|
+
demos/v28-register-role/ that a static reader could reach was
|
|
868
|
+
the inline one, and the plugin file beside it -- line 111,
|
|
869
|
+
register_role('gantt-bar') -- was never read. The receipt of
|
|
870
|
+
that demo did not depend on it only because the page declares
|
|
871
|
+
the role BOTH ways on purpose. An adopter who puts the plugin
|
|
872
|
+
in its own file, which is the normal thing to do, had no lock.
|
|
873
|
+
|
|
874
|
+
THE ATTRIBUTION, and it is a choice: the finding names the file
|
|
875
|
+
that CONTAINS the call, and the message names the page that
|
|
876
|
+
loads it. The call is the defect's location; the page is how
|
|
877
|
+
anyone finds it.
|
|
878
|
+
|
|
879
|
+
The registry chain is tried from the SCRIPT's directory first
|
|
880
|
+
and then from the PAGE's, because SPEC 1.4 puts the declaration
|
|
881
|
+
beside the markup and a shared plugin can sit in a directory of
|
|
882
|
+
its own. Only a role neither chain declares is reported. */
|
|
883
|
+
const loaded = referencedScripts(file, content, dir);
|
|
884
|
+
for (const s of loaded) {
|
|
885
|
+
if (!s.body) continue;
|
|
886
|
+
for (const m of s.body
|
|
887
|
+
.matchAll(/\bregister_role\s*\(\s*['"]([a-z][a-z0-9-]*)['"]/g)) {
|
|
888
|
+
const role = m[1];
|
|
889
|
+
const declared = markUsed(path.dirname(s.abs), role) || markUsed(path.dirname(file), role);
|
|
890
|
+
if (declared) continue;
|
|
891
|
+
const key = s.rel + '\u0000' + role;
|
|
892
|
+
if (seenViaSrc.has(key)) continue;
|
|
893
|
+
seenViaSrc.add(key);
|
|
894
|
+
findings.push({
|
|
895
|
+
severity: 'error',
|
|
896
|
+
code: 'role_registration_undeclared',
|
|
897
|
+
file: s.rel,
|
|
898
|
+
message: 'this file calls NAC.register_role("' + role + '") and is loaded by ' + rel +
|
|
899
|
+
' through <script src="' + s.ref + '">, and no nac3.roles.json beside either of ' +
|
|
900
|
+
'them -- or in any directory between them and the linted root -- declares it. A ' +
|
|
901
|
+
'static validator cannot see a role declared in JavaScript, so every finding ' +
|
|
902
|
+
'about an element carrying that role would be unreliable (SPEC.md 1.4).'
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/* PND-153, the other half of following a reference: SAYING when it
|
|
908
|
+
did not follow one. A page can load its vocabulary from a remote
|
|
909
|
+
URL, from a root-absolute path or from outside the linted root,
|
|
910
|
+
and this lint reads none of those -- so a role declared there is
|
|
911
|
+
invisible and rule 5 would report it as unknown.
|
|
912
|
+
|
|
913
|
+
Emitted ONLY when the page actually has an unknown-role finding,
|
|
914
|
+
which is the only case where not having read the file changes
|
|
915
|
+
the verdict. Unconditionally it would fire on the 526 generated
|
|
916
|
+
pages of this project alone, every one of them loading the
|
|
917
|
+
runtime by root-absolute path, and a warning that fires 526
|
|
918
|
+
times where it changes nothing is how a reader learns to ignore
|
|
919
|
+
the output. */
|
|
920
|
+
const unread = loaded.filter((x) => x.skipped);
|
|
921
|
+
if (unread.length) {
|
|
922
|
+
const unknownHere = findings.slice(findingsBefore)
|
|
923
|
+
.some((f) => f.code === 'dom_role_unknown' || f.code === 'manifest_role_unknown');
|
|
924
|
+
if (unknownHere) {
|
|
925
|
+
findings.push({
|
|
926
|
+
severity: 'warn',
|
|
927
|
+
code: 'script_not_read',
|
|
928
|
+
file: rel,
|
|
929
|
+
message: 'this page carries a role the specification does not declare, AND it loads ' +
|
|
930
|
+
unread.length + ' script(s) this validator did not read (' +
|
|
931
|
+
unread.map((x) => x.ref + ': ' + x.skipped).join('; ') + '). If one of them ' +
|
|
932
|
+
'calls NAC.register_role for that role, the finding above is wrong and the ' +
|
|
933
|
+
'fix is a nac3.roles.json beside the markup (SPEC.md 1.4), not a change to ' +
|
|
934
|
+
'the page.'
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/* Rule 6: opens_dangling -- data-nac-opens pointing at an id this
|
|
940
|
+
page does not contain. severity: error
|
|
941
|
+
|
|
942
|
+
Why error: measured 0 occurrences in demos/ (5 data-nac-opens,
|
|
943
|
+
all resolving), so it cannot break the battery -- and the defect
|
|
944
|
+
is concrete rather than stylistic. NAC.tree() reports the target
|
|
945
|
+
of data-nac-opens to the agent as if it were reachable, so a
|
|
946
|
+
dangling pointer makes the runtime LIE to the agent, which then
|
|
947
|
+
acts on something that was never there. validate_global() does
|
|
948
|
+
not look at this attribute either: its five buckets are other
|
|
949
|
+
things. Nothing in the project saw it. */
|
|
950
|
+
const paintedIds = new Set(domPairs.map((p) => p.id));
|
|
951
|
+
for (const m of content.matchAll(/data-nac-opens="([^"]+)"/g)) {
|
|
952
|
+
const target = m[1].trim();
|
|
953
|
+
if (!target) continue;
|
|
954
|
+
if (!paintedIds.has(target)) {
|
|
955
|
+
findings.push({
|
|
956
|
+
severity: 'error',
|
|
957
|
+
code: 'opens_dangling',
|
|
958
|
+
file: rel,
|
|
959
|
+
message: 'data-nac-opens="' + target + '" names an id this page does not declare' +
|
|
960
|
+
' (NAC.tree() reports it to the agent as a valid destination)'
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/* Rule 7: label_i18n_incomplete -- a label that does not cover the
|
|
966
|
+
locales the conformance levels require. severity: warn
|
|
967
|
+
|
|
968
|
+
The asymmetry this closes: the rule existed only on the side
|
|
969
|
+
that needs a browser. runtime/src/cli-url.mjs implements
|
|
970
|
+
label_i18n_covers_locales against levels.locales_required, and
|
|
971
|
+
the directory mode had four rules, all of them about roles. The
|
|
972
|
+
data was never the obstacle -- the build already derives the
|
|
973
|
+
required locales from SPEC.md section 4.3 and substitutes them
|
|
974
|
+
below, the same way it substitutes the canonical roles.
|
|
975
|
+
|
|
976
|
+
warn, because label coverage is what a conformance LEVEL
|
|
977
|
+
requires (SPEC.md section 8), not what level 1 requires. A page
|
|
978
|
+
that targets level 1 is not wrong for having one locale, and a
|
|
979
|
+
lint that calls it wrong is a lint that gets switched off.
|
|
980
|
+
Measured: 0 label_i18n blocks in demos/, so the severity is not
|
|
981
|
+
what keeps the battery green here -- the argument is. */
|
|
982
|
+
for (const e of manifestEntries) {
|
|
983
|
+
const labels = e.labels || [];
|
|
984
|
+
labels.forEach((locales, n) => {
|
|
985
|
+
const missing = REQUIRED_LOCALES.filter((loc) => locales.indexOf(loc) < 0);
|
|
986
|
+
if (!missing.length) return;
|
|
987
|
+
/* An element can carry more than one label_i18n -- its own,
|
|
988
|
+
plus one per entry of actions[] -- and they are genuinely
|
|
989
|
+
separate findings. Numbering them is what makes two lines
|
|
990
|
+
about the same id readable instead of looking like the same
|
|
991
|
+
finding printed twice. */
|
|
992
|
+
const which = labels.length > 1 ? ' (label ' + (n + 1) + ' of ' + labels.length + ')' : '';
|
|
993
|
+
findings.push({
|
|
994
|
+
severity: 'warn',
|
|
995
|
+
code: 'label_i18n_incomplete',
|
|
996
|
+
file: rel,
|
|
997
|
+
message: 'plugin "' + e.plugin + '" id="' + e.id + '"' + which +
|
|
998
|
+
' has a label_i18n missing ' + missing.length + ' of the ' +
|
|
999
|
+
REQUIRED_LOCALES.length + ' required locales: ' + missing.join(',')
|
|
1000
|
+
});
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/* Rule 8: drag_type_unsatisfiable -- a draggable whose type no
|
|
1005
|
+
drop target on the page accepts. severity: warn
|
|
1006
|
+
|
|
1007
|
+
The runtime half has been complete since 1.8.0:
|
|
1008
|
+
_dragTypesCompatible (js/nac.js:1271) decides it and drag_drop
|
|
1009
|
+
consults it, rejecting with reason 'drag_type_mismatch'. What
|
|
1010
|
+
was missing is the static half, so a page could declare a drag
|
|
1011
|
+
that can never have a destination and nothing would say so until
|
|
1012
|
+
an agent tried it.
|
|
1013
|
+
|
|
1014
|
+
THE THREE SHORTCUTS OF THAT FUNCTION DEFINE WHAT NOT TO REPORT,
|
|
1015
|
+
and getting them wrong is how this rule would cry wolf on
|
|
1016
|
+
correct pages:
|
|
1017
|
+
- an untyped source is compatible with everything, so a
|
|
1018
|
+
draggable with no data-nac-drag-type is never a finding;
|
|
1019
|
+
- a target whose data-nac-drag-accept is absent or empty, or
|
|
1020
|
+
is '*', accepts everything -- so ONE such target on the page
|
|
1021
|
+
satisfies every type;
|
|
1022
|
+
- '*' inside the comma-separated list does the same.
|
|
1023
|
+
Matching is case-insensitive and trimmed, as it is there.
|
|
1024
|
+
|
|
1025
|
+
warn, not error: a drop target can be rendered after load, so a
|
|
1026
|
+
static read cannot prove the destination will never exist. The
|
|
1027
|
+
finding is worth raising and not worth failing a build over.
|
|
1028
|
+
Measured: 0 drag attributes in demos/. */
|
|
1029
|
+
const dragTypes = [...content.matchAll(/data-nac-drag-type="([^"]*)"/g)]
|
|
1030
|
+
.map((m) => m[1].trim().toLowerCase()).filter(Boolean);
|
|
1031
|
+
if (dragTypes.length) {
|
|
1032
|
+
const acceptAttrs = [...content.matchAll(/data-nac-drag-accept="([^"]*)"/g)]
|
|
1033
|
+
.map((m) => m[1].trim().toLowerCase());
|
|
1034
|
+
/* A drop target with no accept attribute at all accepts
|
|
1035
|
+
everything, and it will not appear in acceptAttrs -- so look
|
|
1036
|
+
for one directly rather than inferring it from absence. */
|
|
1037
|
+
const openTarget = [...content.matchAll(/<[^>]*data-nac-role="drop-target"[^>]*>/g)]
|
|
1038
|
+
.some((t) => !/data-nac-drag-accept\s*=/.test(t[0]));
|
|
1039
|
+
const wildcard = acceptAttrs.some((a) => a === '' || a === '*'
|
|
1040
|
+
|| a.split(',').map((x) => x.trim()).indexOf('*') >= 0);
|
|
1041
|
+
if (!openTarget && !wildcard) {
|
|
1042
|
+
const accepted = new Set();
|
|
1043
|
+
for (const a of acceptAttrs) {
|
|
1044
|
+
for (const x of a.split(',')) if (x.trim()) accepted.add(x.trim());
|
|
1045
|
+
}
|
|
1046
|
+
for (const t of [...new Set(dragTypes)]) {
|
|
1047
|
+
if (!accepted.has(t)) {
|
|
1048
|
+
findings.push({
|
|
1049
|
+
severity: 'warn',
|
|
1050
|
+
code: 'drag_type_unsatisfiable',
|
|
1051
|
+
file: rel,
|
|
1052
|
+
message: 'data-nac-drag-type="' + t + '" has no drop target on this page that accepts it' +
|
|
1053
|
+
(accepted.size ? ' (targets accept: ' + [...accepted].sort().join(',') + ')'
|
|
1054
|
+
: ' (no drop target declares data-nac-drag-accept)')
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
/* The other direction, now per registry (PND-132): a role declared
|
|
1062
|
+
for the lint that no page under that registry's directory
|
|
1063
|
+
registers. warn, not error -- an unused declaration misleads a
|
|
1064
|
+
reader of nac3.roles.json and breaks nothing. */
|
|
1065
|
+
for (const [key, map] of registries) {
|
|
1066
|
+
if (!map) continue;
|
|
1067
|
+
const used = registryUsed.get(key) || new Set();
|
|
1068
|
+
const relFile = (key ? key + '/' : '') + 'nac3.roles.json';
|
|
1069
|
+
for (const role of map.keys()) {
|
|
1070
|
+
if (used.has(role)) continue;
|
|
1071
|
+
findings.push({
|
|
1072
|
+
severity: 'warn',
|
|
1073
|
+
code: 'role_registration_orphan',
|
|
1074
|
+
file: relFile,
|
|
1075
|
+
message: relFile + ' declares role "' + role + '" and no file under that ' +
|
|
1076
|
+
'directory calls NAC.register_role("' + role + '"). A declaration ' +
|
|
1077
|
+
'nobody uses is a reader believing a role exists on a page where it ' +
|
|
1078
|
+
'does not (SPEC.md 1.4).'
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
140
1081
|
}
|
|
1082
|
+
|
|
141
1083
|
return findings;
|
|
142
1084
|
}
|
|
1085
|
+
|