mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Vue kit: the view scan forbids any hand-written button, glyph, checkbox or range
viewControls.test.mjs now reads every SFC outside the kit for a raw <button>, a tag whose class (static or bound) names a Material Symbols style, a native checkbox, and a native range other than the radio tuner's and the world timeline's. It also reads every module under src outside the kit, tests and test support for a button, select, input or textarea built with createElement, or a Material Symbols class written in script. Every check passes on main as it stands; each was confirmed to fail when its violation is put back (a button, a glyph span, a checkbox, a range, a script-built button and a script-written glyph class). docs/VUE-MIGRATION.md says that no view hand-writes a control the kit has, names the native fields left and points at the scan. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
b57d50fe18
commit
591650d786
2 files changed
+187
-6
No files matched your search
@@ -1,10 +1,12 @@
|
||||
// The kit's every-control claims in docs/VUE-MIGRATION.md, read from every
|
||||
// SFC outside the kit: every select is a UiSelect, every pressed toggle a
|
||||
// UiButton with `pressed` (no template hand-writes `aria-pressed` on any
|
||||
// element, as an attribute or inside a `v-bind` object, apart from the
|
||||
// exceptions below), and every close, dismiss and clear button a
|
||||
// UiCloseButton.
|
||||
// Controls built in script are not read here.
|
||||
// SFC outside the kit: every button is a kit button, every select a
|
||||
// UiSelect, every checkbox a UiCheckbox, every range a UiSlider apart from
|
||||
// the two named below, and every Material Symbols glyph a UiIcon; every
|
||||
// pressed toggle is a UiButton with `pressed` (no template hand-writes
|
||||
// `aria-pressed` on any element, as an attribute or inside a `v-bind`
|
||||
// object, apart from the exceptions below), and every close, dismiss and
|
||||
// clear button a UiCloseButton. Outside the kit, no module builds a button,
|
||||
// select, field or Material glyph in script.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync, readdirSync } from 'node:fs';
|
||||
@@ -69,6 +71,43 @@ const HAND_WRITTEN_CLOSE = new RegExp(
|
||||
|
||||
const tidy = (markup) => markup.replace(/\s+/g, ' ');
|
||||
|
||||
// The two ranges that stay native, by file (docs/VUE-MIGRATION.md says why).
|
||||
const NATIVE_RANGES = {
|
||||
'src/ui/views/panels/RadioTuner.vue': 1,
|
||||
'src/ui/views/panels/WorldTimeline.vue': 1,
|
||||
};
|
||||
const RAW_BUTTON = /<button\b/gi;
|
||||
// A tag whose class, static or bound, names a Material Symbols style.
|
||||
const MATERIAL_CLASS =
|
||||
/(?:^|\s)(?::|v-bind:)?class=(?:"[^"]*material-symbols[^"]*"|'[^']*material-symbols[^']*')/i;
|
||||
const INPUT_TAG = new RegExp(String.raw`<input\b(${ATTRS})>`, 'gi');
|
||||
// A tag's static `type`, read from its attributes rather than their values.
|
||||
const ATTRIBUTE = /([^\s=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"']+)))?/g;
|
||||
function inputType(attrs) {
|
||||
for (const m of attrs.matchAll(ATTRIBUTE))
|
||||
if (m[1] === 'type') return m[2] ?? m[3] ?? m[4] ?? '';
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Hand-written buttons in an SFC's template. */
|
||||
const rawButtons = (source) =>
|
||||
[...template(source).matchAll(RAW_BUTTON)].map((m) => m[0]);
|
||||
|
||||
/** Tags in an SFC's template that draw a Material glyph by hand. */
|
||||
function materialGlyphs(source) {
|
||||
const found = [];
|
||||
for (const m of template(source).matchAll(ANY_TAG))
|
||||
if (!/^<UiIcon\b/.test(m[0]) && MATERIAL_CLASS.test(m[1]))
|
||||
found.push(tidy(m[0]));
|
||||
return found;
|
||||
}
|
||||
|
||||
/** Native inputs of a type in an SFC's template. */
|
||||
const nativeInputs = (source, type) =>
|
||||
[...template(source).matchAll(INPUT_TAG)]
|
||||
.filter((m) => inputType(m[1]) === type)
|
||||
.map((m) => tidy(m[0]));
|
||||
|
||||
/** Raw selects in an SFC's template. */
|
||||
const rawSelects = (source) =>
|
||||
[...template(source).matchAll(RAW_SELECT)].map((m) => m[0]);
|
||||
@@ -134,6 +173,63 @@ test('the scans find each hand-written control', () => {
|
||||
assert.equal(handWrittenCloses(markup).length, 0, markup);
|
||||
});
|
||||
|
||||
test('the scans find each hand-written button, glyph, checkbox and range', () => {
|
||||
for (const markup of [
|
||||
'<template><button type="button">A</button></template>',
|
||||
'<template><BUTTON\n class="x">A</BUTTON></template>',
|
||||
])
|
||||
assert.equal(rawButtons(markup).length, 1, markup);
|
||||
for (const markup of [
|
||||
'<template><UiButton>A</UiButton></template>',
|
||||
'<script>const html = "<button>";</script><template><p /></template>',
|
||||
'<template><!-- a <button> once sat here --><p /></template>',
|
||||
])
|
||||
assert.equal(rawButtons(markup).length, 0, markup);
|
||||
|
||||
for (const markup of [
|
||||
'<template><span class="material-symbols-outlined">search</span></template>',
|
||||
'<template><i class="pp-icon material-symbols-rounded" aria-hidden="true">add</i></template>',
|
||||
'<template><span :class="\'material-symbols-outlined \' + extra">add</span></template>',
|
||||
'<template><span\n class="x material-symbols-sharp"\n :title="a > b ? 1 : 2"\n>add</span></template>',
|
||||
])
|
||||
assert.equal(materialGlyphs(markup).length, 1, markup);
|
||||
for (const markup of [
|
||||
'<template><UiIcon name="search" class="material-symbols-outlined" :size="null" /></template>',
|
||||
'<template><span class="x">search</span></template>',
|
||||
'<template><p /></template><style>.x .material-symbols-outlined { color: red; }</style>',
|
||||
])
|
||||
assert.equal(materialGlyphs(markup).length, 0, markup);
|
||||
|
||||
assert.equal(
|
||||
nativeInputs(
|
||||
'<template><input type="checkbox" v-model="a" /></template>',
|
||||
'checkbox',
|
||||
).length,
|
||||
1,
|
||||
);
|
||||
assert.equal(
|
||||
nativeInputs(
|
||||
'<template><input\n class="x"\n type="range"\n :max="a > b ? 1 : 2"\n/></template>',
|
||||
'range',
|
||||
).length,
|
||||
1,
|
||||
);
|
||||
assert.equal(
|
||||
nativeInputs(
|
||||
'<template><input type="text" :title="\'type=checkbox\'" /></template>',
|
||||
'checkbox',
|
||||
).length,
|
||||
0,
|
||||
);
|
||||
assert.equal(
|
||||
nativeInputs(
|
||||
'<template><UiCheckbox v-model="a">A</UiCheckbox></template>',
|
||||
'checkbox',
|
||||
).length,
|
||||
0,
|
||||
);
|
||||
});
|
||||
|
||||
test('the views are read: the scan reaches the views and skips the kit', () => {
|
||||
const files = views.map((view) => view.file);
|
||||
assert.ok(files.includes('src/ui/views/panels/ChatPanel.vue'));
|
||||
@@ -169,3 +265,79 @@ test('no view hand-writes a close button; each is a UiCloseButton', () => {
|
||||
found.push(`${file}: ${markup}`);
|
||||
assert.deepEqual(found, []);
|
||||
});
|
||||
|
||||
test('every button in the views is a kit button', () => {
|
||||
const found = [];
|
||||
for (const { file, source } of views)
|
||||
for (const markup of rawButtons(source)) found.push(`${file}: ${markup}`);
|
||||
assert.deepEqual(found, []);
|
||||
});
|
||||
|
||||
test('every Material Symbols glyph in the views is a UiIcon', () => {
|
||||
const found = [];
|
||||
for (const { file, source } of views)
|
||||
for (const markup of materialGlyphs(source))
|
||||
found.push(`${file}: ${markup}`);
|
||||
assert.deepEqual(found, []);
|
||||
});
|
||||
|
||||
test('every checkbox in the views is a UiCheckbox', () => {
|
||||
const found = [];
|
||||
for (const { file, source } of views)
|
||||
for (const markup of nativeInputs(source, 'checkbox'))
|
||||
found.push(`${file}: ${markup}`);
|
||||
assert.deepEqual(found, []);
|
||||
});
|
||||
|
||||
test('every range in the views is a UiSlider apart from the two named', () => {
|
||||
const counts = {};
|
||||
for (const { file, source } of views) {
|
||||
const n = nativeInputs(source, 'range').length;
|
||||
if (n) counts[file] = n;
|
||||
}
|
||||
assert.deepEqual(counts, NATIVE_RANGES);
|
||||
});
|
||||
|
||||
/** Every module under src outside the kit, tests and test support. */
|
||||
function scriptFiles(directory = SRC_ROOT) {
|
||||
const files = [];
|
||||
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
||||
const absolute = path.join(directory, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
if (
|
||||
path.relative(KIT_ROOT, absolute) !== '' &&
|
||||
entry.name !== 'testSupport'
|
||||
)
|
||||
files.push(...scriptFiles(absolute));
|
||||
} else if (
|
||||
entry.isFile() &&
|
||||
/\.(?:js|mjs|ts)$/.test(entry.name) &&
|
||||
!/\.test\.(?:js|mjs|ts)$/.test(entry.name) &&
|
||||
!/Fixture\.mjs$/.test(entry.name)
|
||||
)
|
||||
files.push(absolute);
|
||||
}
|
||||
return files.sort();
|
||||
}
|
||||
|
||||
const SCRIPT_CONTROL =
|
||||
/\bcreateElement(?:NS)?\(\s*(?:[^,)]*,\s*)?['"`](?:button|select|input|textarea)['"`]/;
|
||||
const SCRIPT_GLYPH =
|
||||
/['"`][^'"`]*\bmaterial-symbols-(?:outlined|rounded|sharp)\b/;
|
||||
|
||||
test('no module outside the kit builds a control or Material glyph in script', () => {
|
||||
assert.ok(SCRIPT_CONTROL.test("const b = document.createElement('button');"));
|
||||
assert.ok(SCRIPT_CONTROL.test('doc.createElement("input")'));
|
||||
assert.ok(SCRIPT_GLYPH.test("span.className = 'material-symbols-outlined';"));
|
||||
assert.ok(!SCRIPT_CONTROL.test("document.createElement('div')"));
|
||||
const found = [];
|
||||
for (const file of scriptFiles()) {
|
||||
const relative = path.relative(REPO_ROOT, file).split(path.sep).join('/');
|
||||
const source = readFileSync(file, 'utf8');
|
||||
source.split('\n').forEach((line, index) => {
|
||||
if (SCRIPT_CONTROL.test(line) || SCRIPT_GLYPH.test(line))
|
||||
found.push(`${relative}:${index + 1}: ${line.trim()}`);
|
||||
});
|
||||
}
|
||||
assert.deepEqual(found, []);
|
||||
});
|
||||
Reference in new issue
Block a user