architecture-harness 0.9.0 → 0.11.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/package.json +3 -2
- package/src/cli/check.mjs +50 -1
- package/src/cli/registry-history.mjs +53 -0
- package/src/cli/registry-history.test.mjs +88 -0
- package/src/config.mjs +38 -0
- package/src/descriptions.mjs +4 -0
- package/src/eslint/index.mjs +6 -0
- package/src/eslint/preset.mjs +23 -8
- package/src/eslint/preset.test.mjs +62 -0
- package/src/eslint/rules/ime-safe-key.mjs +179 -0
- package/src/eslint/rules/no-sql-raw.mjs +30 -0
- package/src/eslint/rules/registry-literal.mjs +66 -0
- package/src/eslint/rules.test.mjs +151 -0
- package/src/gates/migration-numbers.mjs +33 -0
- package/src/gates/migration-numbers.test.mjs +35 -0
- package/src/gates/registry-literal.mjs +137 -0
- package/src/gates/registry-literal.test.mjs +68 -0
- package/src/gates/registry-readers.mjs +24 -0
- package/src/gates/registry-readers.test.mjs +35 -0
- package/src/gates/sql-identifiers.mjs +28 -9
- package/src/gates/sql-identifiers.test.mjs +27 -0
- package/src/gates/threshold-ratchet.mjs +46 -0
- package/src/gates/threshold-ratchet.test.mjs +60 -0
- package/templates/quality-thresholds.json +3 -2
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// Enter and Escape in text entry wait for the IME, or a half-typed conversion is submitted. docs/ui.md.
|
|
2
|
+
|
|
3
|
+
import { optionsOf, schemaOf } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
const HANDLERS = new Set(["onKeyDown", "onKeyUp"]);
|
|
6
|
+
const ACTION_KEYS = new Set(["Enter", "Escape"]);
|
|
7
|
+
const EQUALITY = new Set(["===", "==", "!==", "!="]);
|
|
8
|
+
const FUNCTIONS = new Set(["ArrowFunctionExpression", "FunctionExpression", "FunctionDeclaration"]);
|
|
9
|
+
// An input of these types takes no typed text, so no IME composes into it.
|
|
10
|
+
const NON_TEXT_TYPES = new Set(["button", "checkbox", "color", "file", "hidden", "image", "radio", "range", "reset", "submit"]);
|
|
11
|
+
const IME_KEY_CODE = 229;
|
|
12
|
+
|
|
13
|
+
function tagName(name) {
|
|
14
|
+
if (name.type === "JSXIdentifier") return name.name;
|
|
15
|
+
if (name.type === "JSXMemberExpression") return `${tagName(name.object)}.${name.property.name}`;
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** undefined when absent, the literal when static, null when computed at runtime. */
|
|
20
|
+
function attributeValue(element, name) {
|
|
21
|
+
const attr = element.attributes.find((a) => a.type === "JSXAttribute" && a.name.name === name);
|
|
22
|
+
if (!attr) return undefined;
|
|
23
|
+
if (attr.value === null) return true;
|
|
24
|
+
if (attr.value.type === "Literal") return attr.value.value;
|
|
25
|
+
const expression = attr.value.expression;
|
|
26
|
+
return expression?.type === "Literal" ? expression.value : null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isTextEntry(element, components) {
|
|
30
|
+
const editable = attributeValue(element, "contentEditable");
|
|
31
|
+
if (editable !== undefined && editable !== false && editable !== "false") return true;
|
|
32
|
+
const name = tagName(element.name);
|
|
33
|
+
if (name === "textarea" || components.has(name)) return true;
|
|
34
|
+
if (name !== "input") return false;
|
|
35
|
+
const type = attributeValue(element, "type");
|
|
36
|
+
return typeof type !== "string" || !NON_TEXT_TYPES.has(type);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** `useCallback(fn, deps)` and any wrapper like it: the handler is the first function argument. */
|
|
40
|
+
function unwrap(node) {
|
|
41
|
+
if (node?.type === "CallExpression") return node.arguments.find((arg) => FUNCTIONS.has(arg.type)) ?? null;
|
|
42
|
+
return node && FUNCTIONS.has(node.type) ? node : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function resolve(sourceCode, expression) {
|
|
46
|
+
if (expression.type !== "Identifier") return unwrap(expression);
|
|
47
|
+
for (let scope = sourceCode.getScope(expression); scope; scope = scope.upper) {
|
|
48
|
+
const variable = scope.set.get(expression.name);
|
|
49
|
+
if (!variable) continue;
|
|
50
|
+
// Branch on the definition kind: for a parameter, `def.node` is the enclosing function, not the handler.
|
|
51
|
+
const def = variable.defs[0];
|
|
52
|
+
if (def?.type === "FunctionName") return def.node;
|
|
53
|
+
return def?.type === "Variable" ? unwrap(def.node.init) : null;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function propertyName(node) {
|
|
59
|
+
return node.type === "MemberExpression" && !node.computed ? node.property.name : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isKey(node) {
|
|
63
|
+
return propertyName(node) === "key" || (node.type === "Identifier" && node.name === "key");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isActionKey(node) {
|
|
67
|
+
return node.type === "Literal" && ACTION_KEYS.has(node.value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isKeyCheck(node) {
|
|
71
|
+
if (node.type !== "BinaryExpression" || !EQUALITY.has(node.operator)) return false;
|
|
72
|
+
return (isKey(node.left) && isActionKey(node.right)) || (isKey(node.right) && isActionKey(node.left));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isInlineGuard(node) {
|
|
76
|
+
if (propertyName(node) === "isComposing") return true;
|
|
77
|
+
if (node.type !== "BinaryExpression" || !EQUALITY.has(node.operator)) return false;
|
|
78
|
+
const sides = [node.left, node.right];
|
|
79
|
+
return sides.some((side) => propertyName(side) === "keyCode") && sides.some((side) => side.type === "Literal" && side.value === IME_KEY_CODE);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function walk(node, keys, visit) {
|
|
83
|
+
visit(node);
|
|
84
|
+
for (const key of keys[node.type] ?? []) {
|
|
85
|
+
const child = node[key];
|
|
86
|
+
for (const item of Array.isArray(child) ? child : [child]) {
|
|
87
|
+
if (item && typeof item.type === "string" && !FUNCTIONS.has(item.type)) walk(item, keys, visit);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function contains(outer, inner) {
|
|
93
|
+
return outer.range[0] <= inner.range[0] && inner.range[1] <= outer.range[1];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function exitsEarly(statement) {
|
|
97
|
+
if (statement.type === "ReturnStatement") return true;
|
|
98
|
+
return statement.type === "BlockStatement" && statement.body.at(-1)?.type === "ReturnStatement";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** What one handler does with Enter and Escape, and what guards it. */
|
|
102
|
+
function survey(handler, keys, isGuard) {
|
|
103
|
+
const found = { checks: [], inline: [], guards: [] };
|
|
104
|
+
walk(handler.body, keys, (node) => {
|
|
105
|
+
if (isKeyCheck(node)) found.checks.push(node);
|
|
106
|
+
if (node.type === "SwitchStatement" && isKey(node.discriminant)) {
|
|
107
|
+
for (const branch of node.cases) if (branch.test && isActionKey(branch.test)) found.checks.push(branch.test);
|
|
108
|
+
}
|
|
109
|
+
if (isInlineGuard(node)) found.inline.push(node);
|
|
110
|
+
if (node.type === "IfStatement" && exitsEarly(node.consequent)) {
|
|
111
|
+
let guarded = false;
|
|
112
|
+
walk(node.test, keys, (inner) => (guarded ||= isGuard(inner)));
|
|
113
|
+
if (guarded) found.guards.push(node);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return found;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The first statement the branch runs when the check holds. */
|
|
120
|
+
function branchHead(check) {
|
|
121
|
+
let node = check;
|
|
122
|
+
while (node.parent && !FUNCTIONS.has(node.parent.type)) {
|
|
123
|
+
const parent = node.parent;
|
|
124
|
+
if (parent.type === "SwitchCase" && parent.test === node) return parent.consequent[0];
|
|
125
|
+
if (parent.type === "IfStatement" && parent.test === node) {
|
|
126
|
+
return parent.consequent.type === "BlockStatement" ? parent.consequent.body[0] : parent.consequent;
|
|
127
|
+
}
|
|
128
|
+
node = parent;
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function isGuarded(check, guards) {
|
|
134
|
+
const head = branchHead(check);
|
|
135
|
+
return guards.some((guard) => guard === head || (guard.range[1] <= check.range[0] && contains(guard.parent, check)));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export default {
|
|
139
|
+
meta: {
|
|
140
|
+
type: "problem",
|
|
141
|
+
docs: { description: "Enter and Escape in text entry act only after an early return on the IME guard" },
|
|
142
|
+
schema: schemaOf({
|
|
143
|
+
components: { type: "array", items: { type: "string" } },
|
|
144
|
+
guards: { type: "array", items: { type: "string" } },
|
|
145
|
+
}),
|
|
146
|
+
messages: {
|
|
147
|
+
unguarded: "Enter and Escape in text entry must wait for the IME: return early on {{guard}} before this branch.",
|
|
148
|
+
inline: "An inline IME check is a second mechanism, and isComposing alone misses keyCode 229: call {{guard}}.",
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
create(context) {
|
|
152
|
+
const options = optionsOf(context);
|
|
153
|
+
const components = new Set(options.components ?? []);
|
|
154
|
+
const guards = new Set(options.guards ?? []);
|
|
155
|
+
const guard = guards.size > 0 ? [...guards].join(" or ") : "isComposing or keyCode 229";
|
|
156
|
+
const isGuard =
|
|
157
|
+
guards.size > 0
|
|
158
|
+
? (node) => node.type === "CallExpression" && guards.has(node.callee.name ?? propertyName(node.callee))
|
|
159
|
+
: isInlineGuard;
|
|
160
|
+
const sourceCode = context.sourceCode;
|
|
161
|
+
const reported = new Set();
|
|
162
|
+
function report(node, messageId) {
|
|
163
|
+
if (reported.has(node)) return;
|
|
164
|
+
reported.add(node);
|
|
165
|
+
context.report({ node, messageId, data: { guard } });
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
JSXAttribute(node) {
|
|
169
|
+
if (!HANDLERS.has(node.name.name) || node.value?.type !== "JSXExpressionContainer") return;
|
|
170
|
+
if (!isTextEntry(node.parent, components)) return;
|
|
171
|
+
const handler = resolve(sourceCode, node.value.expression);
|
|
172
|
+
if (!handler) return;
|
|
173
|
+
const found = survey(handler, sourceCode.visitorKeys, isGuard);
|
|
174
|
+
for (const check of found.checks) if (!isGuarded(check, found.guards)) report(check, "unguarded");
|
|
175
|
+
if (guards.size > 0) for (const check of found.inline) report(check, "inline");
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// A query binds its values. sql.raw splices text into the statement unescaped. docs/architecture.md.
|
|
2
|
+
|
|
3
|
+
import { filenameOf, optionsOf, pathSuffix, schemaOf } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
function isRaw(node) {
|
|
6
|
+
if (node.object.type !== "Identifier" || node.object.name !== "sql") return false;
|
|
7
|
+
const property = node.property;
|
|
8
|
+
if (!node.computed) return property.type === "Identifier" && property.name === "raw";
|
|
9
|
+
return property.type === "Literal" && property.value === "raw";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default {
|
|
13
|
+
meta: {
|
|
14
|
+
type: "problem",
|
|
15
|
+
docs: { description: "a query binds its values; sql.raw is refused outside a reviewed file" },
|
|
16
|
+
schema: schemaOf({ allow: { type: "array", items: { type: "string" } } }),
|
|
17
|
+
messages: {
|
|
18
|
+
raw: "sql.raw splices text into the statement unescaped: bind the value, or list this file in sqlRaw.allow after a review.",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
create(context) {
|
|
22
|
+
const filename = filenameOf(context);
|
|
23
|
+
if ((optionsOf(context).allow ?? []).some((path) => pathSuffix(path).test(filename))) return {};
|
|
24
|
+
return {
|
|
25
|
+
MemberExpression(node) {
|
|
26
|
+
if (isRaw(node)) context.report({ node, messageId: "raw" });
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// A number the registry owns has one home. Only the readers an entry names may hold it in code.
|
|
2
|
+
|
|
3
|
+
import { filenameOf, optionsOf, pathSuffix, schemaOf } from "../options.mjs";
|
|
4
|
+
|
|
5
|
+
const strings = { type: "array", items: { type: "string" } };
|
|
6
|
+
|
|
7
|
+
function isNumber(node) {
|
|
8
|
+
if (!node) return false;
|
|
9
|
+
if (node.type === "Literal") return typeof node.value === "number";
|
|
10
|
+
if (node.type === "UnaryExpression") return node.operator === "-" && isNumber(node.argument);
|
|
11
|
+
if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression") return isNumber(node.expression);
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function keyName(key) {
|
|
16
|
+
if (key.type === "Identifier") return key.name;
|
|
17
|
+
return key.type === "Literal" && typeof key.value === "string" ? key.value : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default {
|
|
21
|
+
meta: {
|
|
22
|
+
type: "problem",
|
|
23
|
+
docs: { description: "a number the registry owns is held only by the readers its entry names" },
|
|
24
|
+
schema: schemaOf({
|
|
25
|
+
entries: {
|
|
26
|
+
type: "array",
|
|
27
|
+
items: {
|
|
28
|
+
type: "object",
|
|
29
|
+
properties: { key: { type: "string" }, names: strings, readers: strings },
|
|
30
|
+
required: ["key", "names"],
|
|
31
|
+
additionalProperties: false,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
messages: {
|
|
36
|
+
restated: "'{{name}}' restates the registry entry '{{key}}': import the number from {{readers}}.",
|
|
37
|
+
unread: "'{{name}}' restates the registry entry '{{key}}', which names no reader: read the number from the registry.",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
create(context) {
|
|
41
|
+
const filename = filenameOf(context);
|
|
42
|
+
const entries = (optionsOf(context).entries ?? [])
|
|
43
|
+
.map((entry) => ({ ...entry, readers: entry.readers ?? [] }))
|
|
44
|
+
.filter((entry) => !entry.readers.some((reader) => pathSuffix(reader).test(filename)))
|
|
45
|
+
.map((entry) => ({ ...entry, patterns: entry.names.map((name) => new RegExp(`^(?:${name})$`, "i")) }));
|
|
46
|
+
if (entries.length === 0) return {};
|
|
47
|
+
function check(node, name, value) {
|
|
48
|
+
if (!name || !isNumber(value)) return;
|
|
49
|
+
const entry = entries.find((candidate) => candidate.patterns.some((pattern) => pattern.test(name)));
|
|
50
|
+
if (!entry) return;
|
|
51
|
+
const data = { name, key: entry.key, readers: entry.readers.join(", ") };
|
|
52
|
+
context.report({ node, messageId: entry.readers.length > 0 ? "restated" : "unread", data });
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
VariableDeclarator(node) {
|
|
56
|
+
if (node.id.type === "Identifier") check(node, node.id.name, node.init);
|
|
57
|
+
},
|
|
58
|
+
Property(node) {
|
|
59
|
+
if (!node.computed) check(node, keyName(node.key), node.value);
|
|
60
|
+
},
|
|
61
|
+
PropertyDefinition(node) {
|
|
62
|
+
if (!node.computed) check(node, keyName(node.key), node.value);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
};
|
|
@@ -625,3 +625,154 @@ tester.run("name-the-pattern", rules["name-the-pattern"], {
|
|
|
625
625
|
},
|
|
626
626
|
],
|
|
627
627
|
});
|
|
628
|
+
|
|
629
|
+
const MIGRATE = "backend/src/infrastructure/db/migrate.ts";
|
|
630
|
+
|
|
631
|
+
console.log("→", "no-sql-raw");
|
|
632
|
+
tester.run("no-sql-raw", rules["no-sql-raw"], {
|
|
633
|
+
valid: [
|
|
634
|
+
{ code: "await db.execute(sql`select 1 where id = ${id}`);", filename: "backend/src/features/pins/resource.ts" },
|
|
635
|
+
{ code: "await db.execute(sql.raw(ddl));", filename: MIGRATE, options: [{ allow: [MIGRATE] }] },
|
|
636
|
+
{ code: "await db.execute(sql.raw(ddl));", filename: "C:\\repo\\backend\\src\\infrastructure\\db\\migrate.ts", options: [{ allow: [MIGRATE] }] },
|
|
637
|
+
{ code: "const text = other.raw(value);", filename: "backend/src/features/pins/resource.ts" },
|
|
638
|
+
],
|
|
639
|
+
invalid: [
|
|
640
|
+
{ code: "await db.execute(sql.raw(text));", filename: "backend/src/features/pins/resource.ts", errors: [{ messageId: "raw" }] },
|
|
641
|
+
{ code: "const splice = sql.raw;", filename: "backend/src/features/pins/resource.ts", errors: [{ messageId: "raw" }] },
|
|
642
|
+
{ code: 'sql["raw"](text);', filename: "backend/src/features/pins/resource.ts", errors: [{ messageId: "raw" }] },
|
|
643
|
+
{
|
|
644
|
+
code: "await db.execute(sql.raw(ddl));",
|
|
645
|
+
filename: "backend/src/features/pins/resource.ts",
|
|
646
|
+
options: [{ allow: [MIGRATE] }],
|
|
647
|
+
errors: [{ messageId: "raw" }],
|
|
648
|
+
},
|
|
649
|
+
],
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
const jsx = new RuleTester({
|
|
653
|
+
languageOptions: { ecmaVersion: 2023, sourceType: "module", parserOptions: { ecmaFeatures: { jsx: true } } },
|
|
654
|
+
});
|
|
655
|
+
const GUARDED = [{ guards: ["isImeComposing"] }];
|
|
656
|
+
const unguarded = { messageId: "unguarded" };
|
|
657
|
+
const inline = { messageId: "inline" };
|
|
658
|
+
|
|
659
|
+
console.log("→", "ime-safe-key");
|
|
660
|
+
jsx.run("ime-safe-key", rules["ime-safe-key"], {
|
|
661
|
+
valid: [
|
|
662
|
+
{
|
|
663
|
+
code: '<textarea onKeyDown={(e) => { if (isImeComposing(e.nativeEvent)) return; if (e.key === "Enter") submit(); }} />;',
|
|
664
|
+
options: GUARDED,
|
|
665
|
+
},
|
|
666
|
+
{ code: '<input onKeyDown={(e) => { if (e.nativeEvent.isComposing) return; if (e.key === "Enter") submit(); }} />;' },
|
|
667
|
+
{ code: '<input onKeyDown={(e) => { if (e.keyCode === 229) return; if (e.key === "Escape") close(); }} />;' },
|
|
668
|
+
{
|
|
669
|
+
// A handler passed in as a prop is not resolvable here, so the component's own body is never judged as the handler.
|
|
670
|
+
code:
|
|
671
|
+
"function Field({ onKey, key }) {\n" +
|
|
672
|
+
' if (key === "Enter") track();\n' +
|
|
673
|
+
" return <input onKeyDown={onKey} />;\n" +
|
|
674
|
+
"}",
|
|
675
|
+
options: GUARDED,
|
|
676
|
+
},
|
|
677
|
+
{
|
|
678
|
+
code: '<input onKeyDown={(e) => { if (e.key === "Enter") { if (isImeComposing(e.nativeEvent)) return; submit(); } }} />;',
|
|
679
|
+
options: GUARDED,
|
|
680
|
+
},
|
|
681
|
+
{
|
|
682
|
+
code:
|
|
683
|
+
'const onKey = useCallback((e) => { if (isImeComposing(e.nativeEvent)) return; if (e.key === "Enter") send(); }, []);\n' +
|
|
684
|
+
"const field = <input onKeyDown={onKey} />;",
|
|
685
|
+
options: GUARDED,
|
|
686
|
+
},
|
|
687
|
+
// A row, a button or a list item reacts to Enter or Space; it is not text entry.
|
|
688
|
+
{ code: '<div role="row" tabIndex={0} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") open(); }} />;' },
|
|
689
|
+
{ code: '<li onKeyDown={(e) => { if (e.key === "Enter") open(); }} />;' },
|
|
690
|
+
{ code: '<FolderRow onKeyDown={(e) => { if (e.key === "Enter") open(); }} />;', options: [{ components: ["Input"] }] },
|
|
691
|
+
{ code: 'const onKey = (e) => { if (e.key === "Enter") open(); };\nconst row = <div onKeyDown={onKey} />;' },
|
|
692
|
+
{ code: '<div contentEditable={false} onKeyDown={(e) => { if (e.key === "Enter") open(); }} />;' },
|
|
693
|
+
{ code: '<input type="checkbox" onKeyDown={(e) => { if (e.key === "Enter") toggle(); }} />;' },
|
|
694
|
+
{ code: '<input onKeyDown={(e) => { if (e.key === "Tab") next(); }} />;' },
|
|
695
|
+
],
|
|
696
|
+
invalid: [
|
|
697
|
+
{
|
|
698
|
+
// The drawing-comment-dialog case: Enter submits a half-typed kana conversion.
|
|
699
|
+
code: '<textarea onKeyDown={(e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); submit(); } }} />;',
|
|
700
|
+
errors: [unguarded],
|
|
701
|
+
},
|
|
702
|
+
{ code: '<input onKeyUp={(e) => { if (e.key === "Escape") cancel(); }} />;', errors: [unguarded] },
|
|
703
|
+
{ code: '<div contentEditable onKeyDown={(e) => { if (e.key === "Enter") commit(); }} />;', errors: [unguarded] },
|
|
704
|
+
{
|
|
705
|
+
code: '<Input onKeyDown={(e) => { if (e.key === "Enter") commit(); }} />;',
|
|
706
|
+
options: [{ components: ["Input"] }],
|
|
707
|
+
errors: [unguarded],
|
|
708
|
+
},
|
|
709
|
+
{
|
|
710
|
+
// The drawing-grid-calibration case: a handler defined outside the JSX, with an inline check.
|
|
711
|
+
code:
|
|
712
|
+
'const onKeyDown = (e) => { if (e.key === "Enter" && !e.nativeEvent.isComposing) confirm(); };\n' +
|
|
713
|
+
"const field = <Input onKeyDown={onKeyDown} />;",
|
|
714
|
+
options: [{ components: ["Input"], guards: ["isImeComposing"] }],
|
|
715
|
+
errors: [unguarded, inline],
|
|
716
|
+
},
|
|
717
|
+
{
|
|
718
|
+
code: 'function onKey(e) { if (e.key === "Escape") close(); }\nconst field = <textarea onKeyDown={onKey} />;',
|
|
719
|
+
errors: [unguarded],
|
|
720
|
+
},
|
|
721
|
+
{
|
|
722
|
+
code: '<input onKeyDown={(e) => { if (e.keyCode === 229) return; if (e.key === "Enter") send(); }} />;',
|
|
723
|
+
options: GUARDED,
|
|
724
|
+
errors: [inline, unguarded],
|
|
725
|
+
},
|
|
726
|
+
{
|
|
727
|
+
code: '<input onKeyDown={(e) => { if (e.nativeEvent.isComposing) return; if (e.key === "Enter") send(); }} />;',
|
|
728
|
+
options: GUARDED,
|
|
729
|
+
errors: [inline, unguarded],
|
|
730
|
+
},
|
|
731
|
+
{ code: '<input onKeyDown={(e) => { switch (e.key) { case "Enter": send(); break; } }} />;', errors: [unguarded] },
|
|
732
|
+
{
|
|
733
|
+
code: '<input onKeyDown={(e) => { if (e.key === "Enter") send(); if (isImeComposing(e)) return; }} />;',
|
|
734
|
+
options: GUARDED,
|
|
735
|
+
errors: [unguarded],
|
|
736
|
+
},
|
|
737
|
+
{
|
|
738
|
+
// The guard must return early; a conjoined check is a second shape for one job.
|
|
739
|
+
code: '<input onKeyDown={(e) => { if (e.key === "Escape" && !isImeComposing(e.nativeEvent)) close(); }} />;',
|
|
740
|
+
options: GUARDED,
|
|
741
|
+
errors: [unguarded],
|
|
742
|
+
},
|
|
743
|
+
{ code: '<input onKeyDown={({ key }) => { if (key === "Enter") send(); }} />;', errors: [unguarded] },
|
|
744
|
+
],
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
const READER = "frontend/src/platform/gesture-thresholds.ts";
|
|
748
|
+
const HOLD = [{ entries: [{ key: "holdpress", names: ["HOLD_\\w*MS", "\\w*_HOLD_MS"], readers: [READER] }] }];
|
|
749
|
+
|
|
750
|
+
console.log("→", "registry-literal");
|
|
751
|
+
tester.run("registry-literal", rules["registry-literal"], {
|
|
752
|
+
valid: [
|
|
753
|
+
{ code: "export const HOLD_DURATION_MS = 400;", filename: READER, options: HOLD },
|
|
754
|
+
{ code: "export const HOLD_DURATION_MS = 400;", filename: "C:\\repo\\frontend\\src\\platform\\gesture-thresholds.ts", options: HOLD },
|
|
755
|
+
{ code: "const SEARCH_SETTLE_MS = 250;", filename: "frontend/src/features/search/box.ts", options: HOLD },
|
|
756
|
+
{ code: "const HOLD_DURATION_MS = registry.gates.holdpress.value;", filename: "frontend/src/features/pins/drag.ts", options: HOLD },
|
|
757
|
+
// A pattern matches the whole identifier: THRESHOLD_MS holds "HOLD_MS" but is not a hold.
|
|
758
|
+
{ code: "const THRESHOLD_MS = 4;", filename: "frontend/src/features/pins/drag.ts", options: HOLD },
|
|
759
|
+
{ code: "const HOLD_DURATION_MS = 400;", filename: "frontend/src/features/pins/drag.ts" },
|
|
760
|
+
],
|
|
761
|
+
invalid: [
|
|
762
|
+
{
|
|
763
|
+
code: "const HOLD_DURATION_MS = 400;",
|
|
764
|
+
filename: "frontend/src/features/pins/drag.ts",
|
|
765
|
+
options: HOLD,
|
|
766
|
+
errors: [{ messageId: "restated", data: { name: "HOLD_DURATION_MS", key: "holdpress", readers: READER } }],
|
|
767
|
+
},
|
|
768
|
+
{ code: "const NAME_HOLD_MS = 900;", filename: "frontend/src/features/pins/drag.ts", options: HOLD, errors: [{ messageId: "restated" }] },
|
|
769
|
+
{ code: "const GESTURE = { HOLD_DURATION_MS: 400 };", filename: "frontend/src/features/pins/drag.ts", options: HOLD, errors: [{ messageId: "restated" }] },
|
|
770
|
+
{ code: "class Grip { static HOLD_DURATION_MS = 400; }", filename: "frontend/src/features/pins/drag.ts", options: HOLD, errors: [{ messageId: "restated" }] },
|
|
771
|
+
{
|
|
772
|
+
code: "const PIN_DRAG_THRESHOLD_PX = 4;",
|
|
773
|
+
filename: "frontend/src/features/pins/drag.ts",
|
|
774
|
+
options: [{ entries: [{ key: "dragslop", names: ["\\w*DRAG\\w*_PX"], readers: [] }] }],
|
|
775
|
+
errors: [{ messageId: "unread", data: { name: "PIN_DRAG_THRESHOLD_PX", key: "dragslop" } }],
|
|
776
|
+
},
|
|
777
|
+
],
|
|
778
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Migration numbers are handed out by hand, so two branches can take the same one, or a rebase can
|
|
2
|
+
// drop one. Each numeric prefix is unique, and the run from the lowest to the highest has no gap.
|
|
3
|
+
|
|
4
|
+
const MIGRATION = /^(\d+)_.*\.sql$/;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {string[]} names the names in the migrations folder
|
|
8
|
+
* @param {string} dir the folder, as a problem names it
|
|
9
|
+
*/
|
|
10
|
+
export function checkMigrationNumbers(names, dir) {
|
|
11
|
+
const byNumber = new Map();
|
|
12
|
+
for (const name of names) {
|
|
13
|
+
const found = MIGRATION.exec(name);
|
|
14
|
+
if (found === null) continue;
|
|
15
|
+
const number = Number(found[1]);
|
|
16
|
+
byNumber.set(number, [...(byNumber.get(number) ?? []), name]);
|
|
17
|
+
}
|
|
18
|
+
const problems = [];
|
|
19
|
+
for (const [number, files] of byNumber) {
|
|
20
|
+
if (files.length > 1) {
|
|
21
|
+
problems.push({ path: dir, rule: "duplicate-migration-number", detail: `${files.sort().join(", ")} share number ${number}; give each migration its own` });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const numbers = [...byNumber.keys()].sort((a, b) => a - b);
|
|
25
|
+
const missing = [];
|
|
26
|
+
for (let number = numbers[0]; number < numbers.at(-1); number += 1) {
|
|
27
|
+
if (!byNumber.has(number)) missing.push(number);
|
|
28
|
+
}
|
|
29
|
+
if (missing.length > 0) {
|
|
30
|
+
problems.push({ path: dir, rule: "migration-number-gap", detail: `no migration holds number ${missing.join(", ")}; the numbers run from ${numbers[0]} to ${numbers.at(-1)} with no gap` });
|
|
31
|
+
}
|
|
32
|
+
return problems;
|
|
33
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { checkMigrationNumbers } from "./migration-numbers.mjs";
|
|
4
|
+
|
|
5
|
+
const DIR = "backend/src/drizzle";
|
|
6
|
+
|
|
7
|
+
test("contiguous prefixes pass, and a name that is not a migration is ignored", () => {
|
|
8
|
+
const names = ["0001_identity.sql", "0002_projects.sql", "0003_sites.sql", "meta", "README.md"];
|
|
9
|
+
assert.deepEqual(checkMigrationNumbers(names, DIR), []);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("the numbers may start above one, as after a squash", () => {
|
|
13
|
+
assert.deepEqual(checkMigrationNumbers(["0040_base.sql", "0041_photos.sql"], DIR), []);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("a duplicate prefix fails and names both files", () => {
|
|
17
|
+
const problems = checkMigrationNumbers(["0001_identity.sql", "0002_projects.sql", "0002_sites.sql"], DIR);
|
|
18
|
+
assert.equal(problems.length, 1);
|
|
19
|
+
assert.equal(problems[0].rule, "duplicate-migration-number");
|
|
20
|
+
assert.equal(problems[0].path, DIR);
|
|
21
|
+
assert.match(problems[0].detail, /0002_projects\.sql/);
|
|
22
|
+
assert.match(problems[0].detail, /0002_sites\.sql/);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("a gap fails and names the missing numbers", () => {
|
|
26
|
+
const problems = checkMigrationNumbers(["0001_identity.sql", "0002_projects.sql", "0005_sites.sql"], DIR);
|
|
27
|
+
assert.equal(problems.length, 1);
|
|
28
|
+
assert.equal(problems[0].rule, "migration-number-gap");
|
|
29
|
+
assert.match(problems[0].detail, /\b3\b.*\b4\b/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a different width is the same number", () => {
|
|
33
|
+
const problems = checkMigrationNumbers(["0001_identity.sql", "01_projects.sql"], DIR);
|
|
34
|
+
assert.equal(problems[0].rule, "duplicate-migration-number");
|
|
35
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// A number a registry owns is written in the docs as a token, never as the figure. A figure goes
|
|
2
|
+
// stale the day the registry moves, and nothing else would notice.
|
|
3
|
+
|
|
4
|
+
import { escape } from "../eslint/options.mjs";
|
|
5
|
+
import { globMatcher } from "../glob.mjs";
|
|
6
|
+
|
|
7
|
+
const TOKEN = /\{\{(?:q|ver|v):[^}]*\}\}/g;
|
|
8
|
+
// Words between a phrase and a number, at most. Past that the number belongs to another sentence.
|
|
9
|
+
const WINDOW = 6;
|
|
10
|
+
const NUMBER = /^(\d{1,3}(?:,\d{3})+|\d+(?:\.\d+)?)(%|[a-z]+)?$/;
|
|
11
|
+
const UNIT_SPELLINGS = {
|
|
12
|
+
percent: ["%", "percent"],
|
|
13
|
+
ms: ["ms", "millisecond", "milliseconds"],
|
|
14
|
+
s: ["s", "second", "seconds"],
|
|
15
|
+
px: ["px", "pixel", "pixels"],
|
|
16
|
+
lines: ["line", "lines"],
|
|
17
|
+
days: ["day", "days"],
|
|
18
|
+
months: ["month", "months"],
|
|
19
|
+
count: [],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const spellings = (unit) => UNIT_SPELLINGS[unit] ?? (unit ? [unit, unit.replace(/s$/, "")] : []);
|
|
23
|
+
|
|
24
|
+
/** Lowercase, with the punctuation around a word gone. A trailing `%` stays: it is the unit. */
|
|
25
|
+
const normal = (word) => word.toLowerCase().replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}%]+$/gu, "");
|
|
26
|
+
|
|
27
|
+
/** Each entry that states its phrases, with the units that are its own. */
|
|
28
|
+
function thresholdEntries(thresholds) {
|
|
29
|
+
return Object.entries(thresholds)
|
|
30
|
+
.filter(([, entry]) => typeof entry.value === "number" && entry.match?.length > 0)
|
|
31
|
+
.map(([key, entry]) => ({
|
|
32
|
+
key,
|
|
33
|
+
value: entry.value,
|
|
34
|
+
unit: entry.unit,
|
|
35
|
+
own: new Set(spellings(entry.unit)),
|
|
36
|
+
phrases: entry.match.map((phrase) => phrase.split(/\s+/).map(normal).filter(Boolean)),
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The unit a number carries: attached as `70%`, or the next word as `70 percent`. */
|
|
41
|
+
function unitOf(words, index, attached, known) {
|
|
42
|
+
if (attached) return attached;
|
|
43
|
+
const next = words[index + 1]?.norm;
|
|
44
|
+
return next !== undefined && known.has(next) ? next : "";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The word ranges where a phrase stands. */
|
|
48
|
+
function phraseSpans(words, phrases) {
|
|
49
|
+
const spans = [];
|
|
50
|
+
for (const phrase of phrases) {
|
|
51
|
+
for (let start = 0; start + phrase.length <= words.length; start += 1) {
|
|
52
|
+
if (phrase.every((part, offset) => words[start + offset].norm === part)) spans.push([start, start + phrase.length - 1]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return spans;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const near = (index, [start, end]) => (index > end ? index - end - 1 : start - index - 1) <= WINDOW;
|
|
59
|
+
|
|
60
|
+
function thresholdProblems(file, words, entries, known) {
|
|
61
|
+
const problems = [];
|
|
62
|
+
const spansOf = new Map(entries.map((entry) => [entry.key, phraseSpans(words, entry.phrases)]));
|
|
63
|
+
words.forEach((word, index) => {
|
|
64
|
+
const found = NUMBER.exec(word.norm);
|
|
65
|
+
if (found === null) return;
|
|
66
|
+
const value = Number(found[1].replaceAll(",", ""));
|
|
67
|
+
const unit = unitOf(words, index, found[2], known);
|
|
68
|
+
for (const entry of entries) {
|
|
69
|
+
if (entry.value !== value) continue;
|
|
70
|
+
// An attached suffix that is no unit at all, as in `70th`, is not the entry's number either.
|
|
71
|
+
if (unit && !entry.own.has(unit)) continue;
|
|
72
|
+
if (!spansOf.get(entry.key).some((span) => near(index, span))) continue;
|
|
73
|
+
problems.push({
|
|
74
|
+
path: `${file}:${word.line}`,
|
|
75
|
+
rule: "threshold-literal",
|
|
76
|
+
detail: `'${word.norm}' restates the '${entry.key}' threshold (${entry.value}${entry.unit ? ` ${entry.unit}` : ""}); write {{q:${entry.key}}}`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return problems;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Paragraphs as word lists, each word with its line. A blank line ends a paragraph. */
|
|
84
|
+
function paragraphs(text) {
|
|
85
|
+
const out = [];
|
|
86
|
+
let words = [];
|
|
87
|
+
text.split("\n").forEach((line, index) => {
|
|
88
|
+
if (line.trim() === "") {
|
|
89
|
+
if (words.length > 0) out.push(words);
|
|
90
|
+
words = [];
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
for (const [raw] of line.matchAll(/\S+/g)) {
|
|
94
|
+
const norm = normal(raw);
|
|
95
|
+
if (norm) words.push({ raw, norm, line: index + 1 });
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
if (words.length > 0) out.push(words);
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function versionProblems(file, text, libraries) {
|
|
103
|
+
const problems = [];
|
|
104
|
+
for (const [id, library] of Object.entries(libraries)) {
|
|
105
|
+
if (typeof library?.label !== "string" || library.label.trim() === "") continue;
|
|
106
|
+
const label = escape(library.label.trim()).replace(/\s+/g, "\\s+");
|
|
107
|
+
for (const found of text.matchAll(new RegExp(`(?<![\\w.])${label}\\s+v?\\d+(?:\\.\\d+)*\\b`, "gi"))) {
|
|
108
|
+
const line = text.slice(0, found.index).split("\n").length;
|
|
109
|
+
problems.push({
|
|
110
|
+
path: `${file}:${line}`,
|
|
111
|
+
rule: "version-literal",
|
|
112
|
+
detail: `'${found[0].replace(/\s+/g, " ")}' restates the '${id}' version; write {{ver:${id}}} or {{v:${id}}}`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return problems;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param {{path: string, contents: string}[]} docs the markdown files, paths relative to the root
|
|
121
|
+
* @param {{thresholds: object, libraries: object, exempt: string[]}} registries the `gates` of the
|
|
122
|
+
* thresholds registry, the `libraries` of the versions registry, and the globs of files that may hold a figure
|
|
123
|
+
*/
|
|
124
|
+
export function checkRegistryLiteral(docs, { thresholds = {}, libraries = {}, exempt = [] }) {
|
|
125
|
+
const entries = thresholdEntries(thresholds);
|
|
126
|
+
const known = new Set([...Object.values(UNIT_SPELLINGS).flat(), ...entries.flatMap((entry) => [...entry.own])]);
|
|
127
|
+
const isExempt = globMatcher(exempt);
|
|
128
|
+
const problems = [];
|
|
129
|
+
for (const { path: file, contents } of docs) {
|
|
130
|
+
if (isExempt(file)) continue;
|
|
131
|
+
// A placeholder word, so a token still counts as one word of the window.
|
|
132
|
+
const text = contents.replace(/\r\n?/g, "\n").replace(TOKEN, "TOKEN");
|
|
133
|
+
for (const words of paragraphs(text)) problems.push(...thresholdProblems(file, words, entries, known));
|
|
134
|
+
problems.push(...versionProblems(file, text, libraries));
|
|
135
|
+
}
|
|
136
|
+
return problems;
|
|
137
|
+
}
|