@raintree-technology/hig-doctor-core 0.1.2 → 0.2.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/README.md +14 -18
- package/dist/audit.d.ts +1 -1
- package/dist/cache.d.ts +3 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.js +27 -494
- package/package.json +7 -8
- package/src/analyze.ts +1 -1
- package/src/audit-generator.ts +5 -130
- package/src/audit.ts +7 -13
- package/src/baseline.ts +0 -0
- package/src/benchmark.ts +1 -1
- package/src/cache.ts +12 -11
- package/src/categorizer.ts +2 -28
- package/src/config.ts +3 -3
- package/src/index.ts +1 -10
- package/src/patterns.ts +8 -9
- package/src/scanner.ts +3 -5
- package/CHANGELOG.md +0 -51
- package/src/audit-generator.test.ts +0 -166
- package/src/audit.test.ts +0 -121
- package/src/baseline.test.ts +0 -83
- package/src/benchmark.test.ts +0 -29
- package/src/cache.test.ts +0 -90
- package/src/categorizer.test.ts +0 -51
- package/src/config.test.ts +0 -156
- package/src/engines/ast-tsx.test.ts +0 -121
- package/src/engines/swift-structural.test.ts +0 -39
- package/src/fixes.test.ts +0 -152
- package/src/patterns.test.ts +0 -872
- package/src/sarif.test.ts +0 -39
- package/src/scanner.test.ts +0 -196
package/src/patterns.test.ts
DELETED
|
@@ -1,872 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from "bun:test";
|
|
2
|
-
import { detectPatterns, RULE_COUNT, ruleCatalog, getRuleById } from "./patterns";
|
|
3
|
-
import { existsSync, readdirSync } from "node:fs";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
|
|
6
|
-
// Exact-count guard. The rule count is quoted verbatim in user-facing docs, so
|
|
7
|
-
// it must not drift silently. When you intentionally add or remove rules, bump
|
|
8
|
-
// the number here AND in every doc spot below (search the repo for the old count):
|
|
9
|
-
// - README.md (root)
|
|
10
|
-
// - AGENTS.md (root)
|
|
11
|
-
// - .claude-plugin/marketplace.json (plugin description)
|
|
12
|
-
// - website/components/AuditDemo.tsx (the "same N rules" copy)
|
|
13
|
-
// - docs/remotion-showcase.md
|
|
14
|
-
// - demos/remotion-hig-doctor/src/data/report-data.json ("totalRules")
|
|
15
|
-
const EXPECTED_RULE_COUNT = 431;
|
|
16
|
-
test(`rule count is exactly ${EXPECTED_RULE_COUNT}`, () => {
|
|
17
|
-
expect(RULE_COUNT).toBe(EXPECTED_RULE_COUNT);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// ════════════════════════════════════════════════════════════════
|
|
21
|
-
// RULE CATALOG
|
|
22
|
-
// ════════════════════════════════════════════════════════════════
|
|
23
|
-
describe("rule catalog", () => {
|
|
24
|
-
const catalog = ruleCatalog();
|
|
25
|
-
|
|
26
|
-
test("has one meta entry per rule with unique stable IDs", () => {
|
|
27
|
-
expect(catalog.length).toBe(RULE_COUNT);
|
|
28
|
-
const ids = new Set(catalog.map(r => r.id));
|
|
29
|
-
expect(ids.size).toBe(RULE_COUNT);
|
|
30
|
-
for (const meta of catalog) {
|
|
31
|
-
expect(meta.id).toMatch(/^[a-z0-9-]+\/[a-z0-9-]+(-\d+)?$/);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("getRuleById round-trips", () => {
|
|
36
|
-
const first = catalog[0];
|
|
37
|
-
expect(getRuleById(first.id)).toEqual(first);
|
|
38
|
-
expect(getRuleById("nope/never")).toBeUndefined();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("every match carries the ruleId and engine of its rule", () => {
|
|
42
|
-
const matches = detectPatterns(`.foregroundColor(.red)`, "V.swift");
|
|
43
|
-
const m = matches.find(x => x.pattern === "hardcodedColor");
|
|
44
|
-
expect(m?.ruleId).toBe("swift/hardcoded-color");
|
|
45
|
-
expect(m?.engine).toBe("regex");
|
|
46
|
-
expect(getRuleById(m!.ruleId)?.label).toBe("hardcodedColor");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("every critical and serious concern has fix guidance", () => {
|
|
50
|
-
const gaps = catalog.filter(
|
|
51
|
-
r => r.type === "concern" && (r.severity === "critical" || r.severity === "serious") && !r.fix,
|
|
52
|
-
);
|
|
53
|
-
expect(gaps.map(g => g.id)).toEqual([]);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("every HIG citation resolves to a real reference topic", () => {
|
|
57
|
-
const skillsDir = join(import.meta.dir, "..", "..", "..", "skills");
|
|
58
|
-
const topics = new Set<string>();
|
|
59
|
-
for (const skill of readdirSync(skillsDir)) {
|
|
60
|
-
const refs = join(skillsDir, skill, "references");
|
|
61
|
-
if (!existsSync(refs)) continue;
|
|
62
|
-
for (const f of readdirSync(refs)) topics.add(f.replace(/\.md$/, ""));
|
|
63
|
-
}
|
|
64
|
-
const base = "https://developer.apple.com/design/human-interface-guidelines/";
|
|
65
|
-
for (const meta of catalog) {
|
|
66
|
-
expect(meta.hig.startsWith(base)).toBe(true);
|
|
67
|
-
const slug = meta.hig.slice(base.length);
|
|
68
|
-
if (slug !== "") {
|
|
69
|
-
expect(topics.has(slug)).toBe(true);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
// ════════════════════════════════════════════════════════════════
|
|
76
|
-
// SWIFT
|
|
77
|
-
// ════════════════════════════════════════════════════════════════
|
|
78
|
-
describe("detectPatterns — Swift", () => {
|
|
79
|
-
test("detects TabView navigation", () => {
|
|
80
|
-
const code = `struct ContentView: View {\n var body: some View {\n TabView {\n HomeView()\n }\n }\n}`;
|
|
81
|
-
const matches = detectPatterns(code, "ContentView.swift");
|
|
82
|
-
const nav = matches.filter(m => m.category === "components-layout");
|
|
83
|
-
expect(nav.length).toBeGreaterThan(0);
|
|
84
|
-
expect(nav.some(m => m.pattern === "TabView")).toBe(true);
|
|
85
|
-
});
|
|
86
|
-
test("flags hardcoded colors", () => {
|
|
87
|
-
const matches = detectPatterns(`.foregroundColor(.red)\n.foregroundColor(Color(red: 0.5, green: 0.2, blue: 0.1))`, "View.swift");
|
|
88
|
-
expect(matches.filter(m => m.category === "foundations" && m.type === "concern").length).toBeGreaterThan(0);
|
|
89
|
-
});
|
|
90
|
-
test("detects system color usage as positive", () => {
|
|
91
|
-
const matches = detectPatterns(`.foregroundStyle(.primary)\n.foregroundStyle(.secondary)`, "View.swift");
|
|
92
|
-
expect(matches.filter(m => m.category === "foundations" && m.type === "positive").length).toBeGreaterThan(0);
|
|
93
|
-
});
|
|
94
|
-
test("detects accessibility modifiers", () => {
|
|
95
|
-
const matches = detectPatterns(`.accessibilityLabel("Close")\n.accessibilityHint("Dismisses")`, "View.swift");
|
|
96
|
-
expect(matches.filter(m => m.category === "foundations" && m.subcategory === "accessibility").length).toBeGreaterThan(0);
|
|
97
|
-
});
|
|
98
|
-
test("flags hardcoded font sizes", () => {
|
|
99
|
-
const matches = detectPatterns(`.font(.system(size: 14))`, "View.swift");
|
|
100
|
-
expect(matches.filter(m => m.subcategory === "typography" && m.type === "concern").length).toBeGreaterThan(0);
|
|
101
|
-
});
|
|
102
|
-
test("detects Dynamic Type usage", () => {
|
|
103
|
-
const matches = detectPatterns(`.font(.title)\n.font(.body)`, "View.swift");
|
|
104
|
-
expect(matches.filter(m => m.subcategory === "typography" && m.type === "positive").length).toBeGreaterThan(0);
|
|
105
|
-
});
|
|
106
|
-
test("flags onTapGesture as concern", () => {
|
|
107
|
-
const matches = detectPatterns(`.onTapGesture {\n doSomething()\n}`, "View.swift");
|
|
108
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "onTapGesture without traits")).toBe(true);
|
|
109
|
-
});
|
|
110
|
-
test("detects WidgetKit import", () => {
|
|
111
|
-
const matches = detectPatterns(`import WidgetKit`, "Widget.swift");
|
|
112
|
-
expect(matches.some(m => m.pattern === "WidgetKit")).toBe(true);
|
|
113
|
-
});
|
|
114
|
-
test("flags hardcoded CGRect", () => {
|
|
115
|
-
const matches = detectPatterns(`let frame = CGRect(x: 10, y: 20, width: 100, height: 50)`, "View.swift");
|
|
116
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "hardcoded CGRect")).toBe(true);
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// ════════════════════════════════════════════════════════════════
|
|
121
|
-
// APPLE PLATFORMS (UIKit / AppKit / watchOS / visionOS)
|
|
122
|
-
// ════════════════════════════════════════════════════════════════
|
|
123
|
-
describe("detectPatterns — Apple platforms", () => {
|
|
124
|
-
test("UIKit: flags deprecated UIAlertView and a hardcoded UIColor constant", () => {
|
|
125
|
-
const m = detectPatterns(`let a = UIAlertView()\nview.backgroundColor = UIColor.white`, "VC.swift");
|
|
126
|
-
expect(m.some(x => x.ruleId === "uikit/uialert-view-deprecated" && x.type === "concern")).toBe(true);
|
|
127
|
-
expect(m.some(x => x.ruleId === "uikit/hardcoded-uicolor-constant" && x.type === "concern")).toBe(true);
|
|
128
|
-
});
|
|
129
|
-
test("UIKit: credits Dynamic Type and semantic color as positives", () => {
|
|
130
|
-
const m = detectPatterns(`label.font = .preferredFont(forTextStyle: .body)\nview.backgroundColor = UIColor.systemBackground`, "VC.swift");
|
|
131
|
-
expect(m.some(x => x.ruleId === "uikit/uikit-preferred-font" && x.type === "positive")).toBe(true);
|
|
132
|
-
expect(m.some(x => x.ruleId === "uikit/uikit-semantic-color" && x.type === "positive")).toBe(true);
|
|
133
|
-
});
|
|
134
|
-
test("AppKit: flags hardcoded NSFont size, credits NSColor semantic", () => {
|
|
135
|
-
const m = detectPatterns(`let f = NSFont.systemFont(ofSize: 13)\nlet c = NSColor.labelColor`, "Win.swift");
|
|
136
|
-
expect(m.some(x => x.ruleId === "appkit/hardcoded-nsfont-size" && x.type === "concern")).toBe(true);
|
|
137
|
-
expect(m.some(x => x.ruleId === "appkit/nscolor-semantic" && x.type === "positive")).toBe(true);
|
|
138
|
-
});
|
|
139
|
-
test("watchOS: detects Digital Crown usage", () => {
|
|
140
|
-
const m = detectPatterns(`ScrollView {}.digitalCrownRotation($value)`, "Watch.swift");
|
|
141
|
-
expect(m.some(x => x.ruleId === "watchos/digital-crown-rotation" && x.type === "positive")).toBe(true);
|
|
142
|
-
});
|
|
143
|
-
test("visionOS: detects ornaments and glass materials", () => {
|
|
144
|
-
const m = detectPatterns(`content.ornament(attachmentAnchor: .scene(.bottom)) {}\n .glassBackgroundEffect()`, "Space.swift");
|
|
145
|
-
expect(m.some(x => x.ruleId === "visionos/ornament" && x.type === "positive")).toBe(true);
|
|
146
|
-
expect(m.some(x => x.ruleId === "visionos/glass-background-effect" && x.type === "positive")).toBe(true);
|
|
147
|
-
});
|
|
148
|
-
test("UIKit rules do not fire on pure SwiftUI code", () => {
|
|
149
|
-
const m = detectPatterns(`Text("Hi").font(.body).foregroundStyle(.primary)`, "View.swift");
|
|
150
|
-
expect(m.some(x => x.ruleId.startsWith("uikit/"))).toBe(false);
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
// ════════════════════════════════════════════════════════════════
|
|
155
|
-
// WEB / REACT
|
|
156
|
-
// ════════════════════════════════════════════════════════════════
|
|
157
|
-
describe("detectPatterns — Web/React", () => {
|
|
158
|
-
test("detects aria attributes as positive", () => {
|
|
159
|
-
const code = `<button aria-label="Close" aria-expanded={open}>X</button>`;
|
|
160
|
-
const matches = detectPatterns(code, "Header.tsx");
|
|
161
|
-
const a11y = matches.filter(m => m.subcategory === "accessibility" && m.type === "positive");
|
|
162
|
-
expect(a11y.length).toBeGreaterThan(0);
|
|
163
|
-
expect(a11y.some(m => m.pattern === "aria-label")).toBe(true);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test("detects semantic color tokens as positive", () => {
|
|
167
|
-
const code = `<div className="text-foreground bg-background border-border">`;
|
|
168
|
-
const matches = detectPatterns(code, "Card.tsx");
|
|
169
|
-
const color = matches.filter(m => m.subcategory === "color" && m.type === "positive");
|
|
170
|
-
expect(color.length).toBeGreaterThan(0);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
test("flags inline hardcoded colors as concern", () => {
|
|
174
|
-
const code = `<div style={{color: "#ff0000"}}>Bad</div>`;
|
|
175
|
-
const matches = detectPatterns(code, "Bad.tsx");
|
|
176
|
-
const concerns = matches.filter(m => m.subcategory === "color" && m.type === "concern");
|
|
177
|
-
expect(concerns.length).toBeGreaterThan(0);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
test("detects Tailwind font size tokens as positive", () => {
|
|
181
|
-
const code = `<h1 className="text-4xl font-bold">Title</h1>`;
|
|
182
|
-
const matches = detectPatterns(code, "Hero.tsx");
|
|
183
|
-
const typo = matches.filter(m => m.subcategory === "typography" && m.type === "positive");
|
|
184
|
-
expect(typo.length).toBeGreaterThan(0);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
test("detects dark mode classes", () => {
|
|
188
|
-
const code = `<div className="dark:bg-gray-900 dark:text-white">`;
|
|
189
|
-
const matches = detectPatterns(code, "Layout.tsx");
|
|
190
|
-
const dark = matches.filter(m => m.subcategory === "darkMode" && m.type === "positive");
|
|
191
|
-
expect(dark.length).toBeGreaterThan(0);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
test("detects responsive breakpoints", () => {
|
|
195
|
-
const code = `<div className="flex flex-col sm:flex-row lg:grid-cols-3">`;
|
|
196
|
-
const matches = detectPatterns(code, "Grid.tsx");
|
|
197
|
-
const layout = matches.filter(m => m.subcategory === "layout" && m.type === "positive");
|
|
198
|
-
expect(layout.length).toBeGreaterThan(0);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
test("detects prefers-reduced-motion as positive", () => {
|
|
202
|
-
const code = `@media (prefers-reduced-motion: reduce) { * { animation: none; } }`;
|
|
203
|
-
const matches = detectPatterns(code, "globals.css");
|
|
204
|
-
const motion = matches.filter(m => m.subcategory === "motion" && m.type === "positive");
|
|
205
|
-
expect(motion.length).toBeGreaterThan(0);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
test("detects semantic HTML elements", () => {
|
|
209
|
-
const code = `<nav>\n<header>\n<main>\n<footer>`;
|
|
210
|
-
const matches = detectPatterns(code, "Layout.tsx");
|
|
211
|
-
const layout = matches.filter(m => m.category === "components-layout");
|
|
212
|
-
expect(layout.length).toBeGreaterThanOrEqual(4);
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
test("detects sr-only as accessibility positive", () => {
|
|
216
|
-
const code = `<span className="sr-only">Screen reader text</span>`;
|
|
217
|
-
const matches = detectPatterns(code, "Icon.tsx");
|
|
218
|
-
expect(matches.some(m => m.pattern === "sr-only" && m.type === "positive")).toBe(true);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
test("does not apply Swift rules to TSX files", () => {
|
|
222
|
-
const code = `const TabView = () => <div>tabs</div>`;
|
|
223
|
-
const matches = detectPatterns(code, "Tabs.tsx");
|
|
224
|
-
expect(matches.filter(m => m.pattern === "TabView").length).toBe(0);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test("does not apply web rules to Swift files", () => {
|
|
228
|
-
const code = `<nav className="flex">`;
|
|
229
|
-
const matches = detectPatterns(code, "View.swift");
|
|
230
|
-
expect(matches.filter(m => m.pattern === "nav element").length).toBe(0);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
// Accessibility anti-patterns
|
|
234
|
-
test("flags div with onClick but no role", () => {
|
|
235
|
-
const code = `<div onClick={handleClick}>Click me</div>`;
|
|
236
|
-
const matches = detectPatterns(code, "Bad.tsx");
|
|
237
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "div with onClick no role")).toBe(true);
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
test("flags ambiguous link text", () => {
|
|
241
|
-
const code = `<a href="/page">click here</a>`;
|
|
242
|
-
const matches = detectPatterns(code, "Links.tsx");
|
|
243
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "ambiguous link text")).toBe(true);
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
test("flags positive tabindex", () => {
|
|
247
|
-
const code = `<div tabIndex={5}>Bad</div>`;
|
|
248
|
-
const matches = detectPatterns(code, "Bad.tsx");
|
|
249
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "positive tabindex")).toBe(true);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
test("flags empty heading", () => {
|
|
253
|
-
const code = `<h2></h2>`;
|
|
254
|
-
const matches = detectPatterns(code, "Page.tsx");
|
|
255
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "empty heading")).toBe(true);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
test("flags empty button", () => {
|
|
259
|
-
const code = `<button></button>`;
|
|
260
|
-
const matches = detectPatterns(code, "Page.tsx");
|
|
261
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "empty button")).toBe(true);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
test("flags div used as button", () => {
|
|
265
|
-
const code = `<div role="button" onClick={click}>Do</div>`;
|
|
266
|
-
const matches = detectPatterns(code, "Bad.tsx");
|
|
267
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "div as button")).toBe(true);
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
test("flags autoplay media", () => {
|
|
271
|
-
const code = `<video autoplay src="video.mp4"></video>`;
|
|
272
|
-
const matches = detectPatterns(code, "Video.tsx");
|
|
273
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "autoplay media")).toBe(true);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
test("flags user-scalable=no", () => {
|
|
277
|
-
const code = `<meta name="viewport" content="width=device-width, user-scalable=no">`;
|
|
278
|
-
const matches = detectPatterns(code, "index.html");
|
|
279
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "user-scalable=no")).toBe(true);
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
test("detects form validation as positive", () => {
|
|
283
|
-
const code = `<input type="email" required />\n<input type="tel" autocomplete="tel" />`;
|
|
284
|
-
const matches = detectPatterns(code, "Form.tsx");
|
|
285
|
-
expect(matches.some(m => m.pattern === "input type email" && m.type === "positive")).toBe(true);
|
|
286
|
-
expect(matches.some(m => m.pattern === "input type tel" && m.type === "positive")).toBe(true);
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
test("detects fieldset/legend as positive", () => {
|
|
290
|
-
const code = `<fieldset><legend>Options</legend></fieldset>`;
|
|
291
|
-
const matches = detectPatterns(code, "Form.tsx");
|
|
292
|
-
expect(matches.some(m => m.pattern === "fieldset element")).toBe(true);
|
|
293
|
-
expect(matches.some(m => m.pattern === "legend element")).toBe(true);
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
test("detects i18n usage as positive", () => {
|
|
297
|
-
const code = `const { t } = useTranslation();`;
|
|
298
|
-
const matches = detectPatterns(code, "Page.tsx");
|
|
299
|
-
expect(matches.some(m => m.pattern === "i18n/l10n" && m.type === "positive")).toBe(true);
|
|
300
|
-
});
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
// ════════════════════════════════════════════════════════════════
|
|
304
|
-
// CSS
|
|
305
|
-
// ════════════════════════════════════════════════════════════════
|
|
306
|
-
describe("detectPatterns — CSS", () => {
|
|
307
|
-
test("detects CSS custom properties as positive", () => {
|
|
308
|
-
const code = `--background: 225 10% 6%;`;
|
|
309
|
-
const matches = detectPatterns(code, "globals.css");
|
|
310
|
-
expect(matches.some(m => m.pattern === "CSS custom property def" && m.type === "positive")).toBe(true);
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
test("detects focus-visible styles", () => {
|
|
314
|
-
const code = `button:focus-visible { outline: 2px solid var(--ring); }`;
|
|
315
|
-
const matches = detectPatterns(code, "globals.css");
|
|
316
|
-
expect(matches.some(m => m.pattern === "focus-visible" && m.type === "positive")).toBe(true);
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
test("detects high contrast support", () => {
|
|
320
|
-
const code = `@media (forced-colors: active) { button { border: 1px solid; } }`;
|
|
321
|
-
const matches = detectPatterns(code, "globals.css");
|
|
322
|
-
expect(matches.some(m => m.pattern === "high contrast")).toBe(true);
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
test("flags outline: none as concern", () => {
|
|
326
|
-
const code = `button { outline: none; }`;
|
|
327
|
-
const matches = detectPatterns(code, "globals.css");
|
|
328
|
-
expect(matches.some(m => m.pattern === "outline none" && m.type === "concern")).toBe(true);
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
test("flags text-align justify as concern", () => {
|
|
332
|
-
const code = `p { text-align: justify; }`;
|
|
333
|
-
const matches = detectPatterns(code, "globals.css");
|
|
334
|
-
expect(matches.some(m => m.pattern === "text-align justify" && m.type === "concern")).toBe(true);
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
test("flags !important as concern", () => {
|
|
338
|
-
const code = `color: red !important;`;
|
|
339
|
-
const matches = detectPatterns(code, "globals.css");
|
|
340
|
-
expect(matches.some(m => m.pattern === "!important usage" && m.type === "concern")).toBe(true);
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
test("flags extreme z-index as concern", () => {
|
|
344
|
-
const code = `z-index: 9999;`;
|
|
345
|
-
const matches = detectPatterns(code, "globals.css");
|
|
346
|
-
expect(matches.some(m => m.pattern === "extreme z-index" && m.type === "concern")).toBe(true);
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
test("detects logical properties as positive", () => {
|
|
350
|
-
const code = `margin-inline-start: 1rem;`;
|
|
351
|
-
const matches = detectPatterns(code, "globals.css");
|
|
352
|
-
expect(matches.some(m => m.pattern === "logical margin/padding" && m.type === "positive")).toBe(true);
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
test("flags physical text-align as concern", () => {
|
|
356
|
-
const code = `text-align: left;`;
|
|
357
|
-
const matches = detectPatterns(code, "globals.css");
|
|
358
|
-
expect(matches.some(m => m.pattern === "physical text-align" && m.type === "concern")).toBe(true);
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
test("detects clamp font size as positive", () => {
|
|
362
|
-
const code = `font-size: clamp(1rem, 2vw, 2rem);`;
|
|
363
|
-
const matches = detectPatterns(code, "globals.css");
|
|
364
|
-
expect(matches.some(m => m.pattern === "clamp font size" && m.type === "positive")).toBe(true);
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
test("detects max-width with ch as positive", () => {
|
|
368
|
-
const code = `max-width: 65ch;`;
|
|
369
|
-
const matches = detectPatterns(code, "globals.css");
|
|
370
|
-
expect(matches.some(m => m.pattern === "max-width on text" && m.type === "positive")).toBe(true);
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
test("flags small font size as concern", () => {
|
|
374
|
-
const code = `font-size: 10px;`;
|
|
375
|
-
const matches = detectPatterns(code, "globals.css");
|
|
376
|
-
expect(matches.some(m => m.pattern === "font-size below 12px" && m.type === "concern")).toBe(true);
|
|
377
|
-
});
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
// ════════════════════════════════════════════════════════════════
|
|
381
|
-
// VUE
|
|
382
|
-
// ════════════════════════════════════════════════════════════════
|
|
383
|
-
describe("detectPatterns — Vue", () => {
|
|
384
|
-
test("detects vue aria-label as positive", () => {
|
|
385
|
-
const code = `<button :aria-label="label">X</button>`;
|
|
386
|
-
const matches = detectPatterns(code, "Button.vue");
|
|
387
|
-
expect(matches.some(m => m.type === "positive" && m.subcategory === "accessibility")).toBe(true);
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
test("detects router-link as navigation", () => {
|
|
391
|
-
const code = `<router-link to="/about">About</router-link>`;
|
|
392
|
-
const matches = detectPatterns(code, "Nav.vue");
|
|
393
|
-
expect(matches.some(m => m.pattern === "router-link")).toBe(true);
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
test("detects v-model as control pattern", () => {
|
|
397
|
-
const code = `<input v-model="name" />`;
|
|
398
|
-
const matches = detectPatterns(code, "Form.vue");
|
|
399
|
-
expect(matches.some(m => m.pattern === "v-model")).toBe(true);
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
test("detects Vue i18n as positive", () => {
|
|
403
|
-
const code = `{{ $t('hello') }}`;
|
|
404
|
-
const matches = detectPatterns(code, "Page.vue");
|
|
405
|
-
expect(matches.some(m => m.pattern === "Vue i18n" && m.type === "positive")).toBe(true);
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
test("detects Vue transition as animation pattern", () => {
|
|
409
|
-
const code = `<transition name="fade"><div>Content</div></transition>`;
|
|
410
|
-
const matches = detectPatterns(code, "Page.vue");
|
|
411
|
-
expect(matches.some(m => m.pattern === "Vue transition")).toBe(true);
|
|
412
|
-
});
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
// ════════════════════════════════════════════════════════════════
|
|
416
|
-
// SVELTE
|
|
417
|
-
// ════════════════════════════════════════════════════════════════
|
|
418
|
-
describe("detectPatterns — Svelte", () => {
|
|
419
|
-
test("detects svelte aria-label as positive", () => {
|
|
420
|
-
const code = `<button aria-label="Close">X</button>`;
|
|
421
|
-
const matches = detectPatterns(code, "Button.svelte");
|
|
422
|
-
expect(matches.some(m => m.type === "positive" && m.subcategory === "accessibility")).toBe(true);
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
test("flags on:click without on:keydown as concern", () => {
|
|
426
|
-
const code = `<div on:click={handle}>Click</div>`;
|
|
427
|
-
const matches = detectPatterns(code, "Bad.svelte");
|
|
428
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "on:click without on:keydown")).toBe(true);
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
test("detects svelte bind:value as positive", () => {
|
|
432
|
-
const code = `<input bind:value={name} />`;
|
|
433
|
-
const matches = detectPatterns(code, "Form.svelte");
|
|
434
|
-
expect(matches.some(m => m.pattern === "Svelte bind:value")).toBe(true);
|
|
435
|
-
});
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
// ════════════════════════════════════════════════════════════════
|
|
439
|
-
// ANGULAR
|
|
440
|
-
// ════════════════════════════════════════════════════════════════
|
|
441
|
-
describe("detectPatterns — Angular", () => {
|
|
442
|
-
test("detects [attr.aria-label] as positive", () => {
|
|
443
|
-
const code = `<button [attr.aria-label]="label">X</button>`;
|
|
444
|
-
const matches = detectPatterns(code, "button.component.html");
|
|
445
|
-
expect(matches.some(m => m.type === "positive" && m.pattern === "[attr.aria-label]")).toBe(true);
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
test("detects routerLink as navigation", () => {
|
|
449
|
-
const code = `<a routerLink="/about">About</a>`;
|
|
450
|
-
const matches = detectPatterns(code, "nav.component.html");
|
|
451
|
-
expect(matches.some(m => m.pattern === "routerLink")).toBe(true);
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
test("detects Angular CDK a11y as positive", () => {
|
|
455
|
-
const code = `<div cdkTrapFocus><input cdkFocusInitial /></div>`;
|
|
456
|
-
const matches = detectPatterns(code, "dialog.component.html");
|
|
457
|
-
expect(matches.some(m => m.pattern === "Angular CDK a11y" && m.type === "positive")).toBe(true);
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
test("flags (click) without (keydown) as concern", () => {
|
|
461
|
-
const code = `<div (click)="handleClick()">Click</div>`;
|
|
462
|
-
const matches = detectPatterns(code, "bad.component.html");
|
|
463
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "(click) without (keydown)")).toBe(true);
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
test("detects mat-form-field as control pattern", () => {
|
|
467
|
-
const code = `<mat-form-field><input matInput /></mat-form-field>`;
|
|
468
|
-
const matches = detectPatterns(code, "form.component.html");
|
|
469
|
-
expect(matches.some(m => m.pattern === "mat-form-field")).toBe(true);
|
|
470
|
-
expect(matches.some(m => m.pattern === "mat-input")).toBe(true);
|
|
471
|
-
});
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
// ════════════════════════════════════════════════════════════════
|
|
475
|
-
// KOTLIN / JETPACK COMPOSE
|
|
476
|
-
// ════════════════════════════════════════════════════════════════
|
|
477
|
-
describe("detectPatterns — Kotlin/Compose", () => {
|
|
478
|
-
test("detects contentDescription as positive", () => {
|
|
479
|
-
const code = `Image(contentDescription = "Profile photo")`;
|
|
480
|
-
const matches = detectPatterns(code, "Profile.kt");
|
|
481
|
-
expect(matches.some(m => m.pattern === "contentDescription" && m.type === "positive")).toBe(true);
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
test("flags hardcoded Color as concern", () => {
|
|
485
|
-
const code = `Color(0xFF0000FF)`;
|
|
486
|
-
const matches = detectPatterns(code, "Theme.kt");
|
|
487
|
-
expect(matches.some(m => m.pattern === "hardcoded Color(0x)" && m.type === "concern")).toBe(true);
|
|
488
|
-
});
|
|
489
|
-
|
|
490
|
-
test("flags hardcoded Color.Red as concern", () => {
|
|
491
|
-
const code = `Text(color = Color.Red)`;
|
|
492
|
-
const matches = detectPatterns(code, "View.kt");
|
|
493
|
-
expect(matches.some(m => m.pattern === "hardcoded Color.Red/Blue" && m.type === "concern")).toBe(true);
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
test("detects MaterialTheme colorScheme as positive", () => {
|
|
497
|
-
const code = `val color = MaterialTheme.colorScheme.primary`;
|
|
498
|
-
const matches = detectPatterns(code, "Theme.kt");
|
|
499
|
-
expect(matches.some(m => m.pattern === "MaterialTheme colorScheme" && m.type === "positive")).toBe(true);
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
test("detects Scaffold layout pattern", () => {
|
|
503
|
-
const code = `Scaffold(\n topBar = { TopAppBar() }\n) { }`;
|
|
504
|
-
const matches = detectPatterns(code, "Main.kt");
|
|
505
|
-
expect(matches.some(m => m.pattern === "Scaffold")).toBe(true);
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
test("detects isSystemInDarkTheme as positive", () => {
|
|
509
|
-
const code = `val isDark = isSystemInDarkTheme()`;
|
|
510
|
-
const matches = detectPatterns(code, "Theme.kt");
|
|
511
|
-
expect(matches.some(m => m.pattern === "isSystemInDarkTheme" && m.type === "positive")).toBe(true);
|
|
512
|
-
});
|
|
513
|
-
|
|
514
|
-
test("flags clickable without Role as concern (paren and trailing-lambda forms)", () => {
|
|
515
|
-
for (const code of [`Modifier.clickable(onClick = { })`, `Box(Modifier.clickable { open() })`]) {
|
|
516
|
-
const matches = detectPatterns(code, "Item.kt");
|
|
517
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "clickable without Role")).toBe(true);
|
|
518
|
-
}
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
test("does not flag clickable when a role is supplied", () => {
|
|
522
|
-
const code = `Modifier.clickable(role = Role.Button, onClick = { open() })`;
|
|
523
|
-
const matches = detectPatterns(code, "Item.kt");
|
|
524
|
-
expect(matches.some(m => m.pattern === "clickable without Role")).toBe(false);
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
test("flags hardcoded fontSize as concern", () => {
|
|
528
|
-
const code = `fontSize = 14.sp`;
|
|
529
|
-
const matches = detectPatterns(code, "Text.kt");
|
|
530
|
-
expect(matches.some(m => m.type === "concern" && m.pattern === "hardcoded fontSize")).toBe(true);
|
|
531
|
-
});
|
|
532
|
-
});
|
|
533
|
-
|
|
534
|
-
// ════════════════════════════════════════════════════════════════
|
|
535
|
-
// ANDROID XML
|
|
536
|
-
// ════════════════════════════════════════════════════════════════
|
|
537
|
-
describe("detectPatterns — Android XML", () => {
|
|
538
|
-
test("detects contentDescription as positive", () => {
|
|
539
|
-
const code = `<ImageView android:contentDescription="@string/photo" />`;
|
|
540
|
-
const matches = detectPatterns(code, "layout.xml");
|
|
541
|
-
expect(matches.some(m => m.pattern === "android:contentDescription" && m.type === "positive")).toBe(true);
|
|
542
|
-
});
|
|
543
|
-
|
|
544
|
-
test("flags ImageView without contentDescription", () => {
|
|
545
|
-
const code = `<ImageView android:src="@drawable/icon" />`;
|
|
546
|
-
const matches = detectPatterns(code, "layout.xml");
|
|
547
|
-
expect(matches.some(m => m.pattern === "ImageView without contentDescription" && m.type === "concern")).toBe(true);
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
test("flags hardcoded color in XML", () => {
|
|
551
|
-
const code = `<TextView android:textColor="#FF0000" />`;
|
|
552
|
-
const matches = detectPatterns(code, "layout.xml");
|
|
553
|
-
expect(matches.some(m => m.pattern === "hardcoded color in XML" && m.type === "concern")).toBe(true);
|
|
554
|
-
});
|
|
555
|
-
|
|
556
|
-
test("detects @color/ resource as positive", () => {
|
|
557
|
-
const code = `android:textColor="@color/primary"`;
|
|
558
|
-
const matches = detectPatterns(code, "layout.xml");
|
|
559
|
-
expect(matches.some(m => m.pattern === "@color/ resource" && m.type === "positive")).toBe(true);
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
test("detects sp text size as positive", () => {
|
|
563
|
-
const code = `android:textSize="16sp"`;
|
|
564
|
-
const matches = detectPatterns(code, "layout.xml");
|
|
565
|
-
expect(matches.some(m => m.pattern === "sp text size" && m.type === "positive")).toBe(true);
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
test("flags dp text size as concern", () => {
|
|
569
|
-
const code = `android:textSize="16dp"`;
|
|
570
|
-
const matches = detectPatterns(code, "layout.xml");
|
|
571
|
-
expect(matches.some(m => m.pattern === "dp text size" && m.type === "concern")).toBe(true);
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
test("flags px units as concern", () => {
|
|
575
|
-
const code = `android:layout_width="100px"`;
|
|
576
|
-
const matches = detectPatterns(code, "layout.xml");
|
|
577
|
-
expect(matches.some(m => m.pattern === "px units in XML" && m.type === "concern")).toBe(true);
|
|
578
|
-
});
|
|
579
|
-
});
|
|
580
|
-
|
|
581
|
-
// ════════════════════════════════════════════════════════════════
|
|
582
|
-
// FLUTTER
|
|
583
|
-
// ════════════════════════════════════════════════════════════════
|
|
584
|
-
describe("detectPatterns — Flutter", () => {
|
|
585
|
-
test("detects Semantics widget as positive", () => {
|
|
586
|
-
const matches = detectPatterns(`Semantics(label: "Close")`, "widget.dart");
|
|
587
|
-
expect(matches.some(m => m.pattern === "Semantics widget" && m.type === "positive")).toBe(true);
|
|
588
|
-
});
|
|
589
|
-
|
|
590
|
-
test("flags Colors.red as concern", () => {
|
|
591
|
-
const matches = detectPatterns(`color: Colors.red`, "widget.dart");
|
|
592
|
-
expect(matches.some(m => m.pattern === "Colors.red/blue" && m.type === "concern")).toBe(true);
|
|
593
|
-
});
|
|
594
|
-
|
|
595
|
-
test("detects Theme.of as positive", () => {
|
|
596
|
-
const matches = detectPatterns(`Theme.of(context).colorScheme.primary`, "widget.dart");
|
|
597
|
-
expect(matches.some(m => m.pattern === "Theme color" && m.type === "positive")).toBe(true);
|
|
598
|
-
});
|
|
599
|
-
|
|
600
|
-
test("detects darkTheme as positive", () => {
|
|
601
|
-
const matches = detectPatterns(`darkTheme: ThemeData.dark()`, "app.dart");
|
|
602
|
-
expect(matches.some(m => m.pattern === "darkTheme" && m.type === "positive")).toBe(true);
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
test("detects Flutter l10n as positive", () => {
|
|
606
|
-
const matches = detectPatterns(`import 'package:flutter_localizations/flutter_localizations.dart';`, "app.dart");
|
|
607
|
-
expect(matches.some(m => m.pattern === "Flutter l10n" && m.type === "positive")).toBe(true);
|
|
608
|
-
});
|
|
609
|
-
});
|
|
610
|
-
|
|
611
|
-
// ════════════════════════════════════════════════════════════════
|
|
612
|
-
// FILE FILTER ISOLATION
|
|
613
|
-
// ════════════════════════════════════════════════════════════════
|
|
614
|
-
describe("file filter isolation", () => {
|
|
615
|
-
test("Kotlin rules don't match Swift files", () => {
|
|
616
|
-
const code = `MaterialTheme.colorScheme.primary`;
|
|
617
|
-
const matches = detectPatterns(code, "View.swift");
|
|
618
|
-
expect(matches.filter(m => m.pattern === "MaterialTheme colorScheme").length).toBe(0);
|
|
619
|
-
});
|
|
620
|
-
|
|
621
|
-
test("Android XML rules don't match HTML files", () => {
|
|
622
|
-
const code = `<ImageView android:src="@drawable/icon" />`;
|
|
623
|
-
const matches = detectPatterns(code, "page.html");
|
|
624
|
-
expect(matches.filter(m => m.pattern === "ImageView without contentDescription").length).toBe(0);
|
|
625
|
-
});
|
|
626
|
-
|
|
627
|
-
test("Vue rules don't match TSX files", () => {
|
|
628
|
-
const code = `<router-link to="/about">About</router-link>`;
|
|
629
|
-
const matches = detectPatterns(code, "Nav.tsx");
|
|
630
|
-
expect(matches.filter(m => m.pattern === "router-link").length).toBe(0);
|
|
631
|
-
});
|
|
632
|
-
|
|
633
|
-
test("Svelte rules don't match Vue files", () => {
|
|
634
|
-
const code = `<input bind:value={name} />`;
|
|
635
|
-
const matches = detectPatterns(code, "Form.vue");
|
|
636
|
-
expect(matches.filter(m => m.pattern === "Svelte bind:value").length).toBe(0);
|
|
637
|
-
});
|
|
638
|
-
});
|
|
639
|
-
|
|
640
|
-
// ════════════════════════════════════════════════════════════════
|
|
641
|
-
// NO FALSE POSITIVES — correct code must stay quiet.
|
|
642
|
-
// These guard the negative-attribute rules whose original lookbehind/lookahead
|
|
643
|
-
// form matched tags that DID have the attribute (e.g. <img alt> flagged as
|
|
644
|
-
// "missing alt"). Without these, that class of bug is invisible to the suite.
|
|
645
|
-
// ════════════════════════════════════════════════════════════════
|
|
646
|
-
describe("no false positives — negative-attribute rules", () => {
|
|
647
|
-
test("does NOT flag an <img> that has alt", () => {
|
|
648
|
-
const matches = detectPatterns(`<img src="logo.png" alt="Company logo" />`, "Hero.tsx");
|
|
649
|
-
expect(matches.some(m => m.pattern === "missing alt")).toBe(false);
|
|
650
|
-
});
|
|
651
|
-
test("flags an <img> with no alt", () => {
|
|
652
|
-
const matches = detectPatterns(`<img src="logo.png" />`, "Hero.tsx");
|
|
653
|
-
expect(matches.some(m => m.pattern === "missing alt" && m.type === "concern")).toBe(true);
|
|
654
|
-
});
|
|
655
|
-
test("does NOT flag a <div onClick> that has role (role after onClick)", () => {
|
|
656
|
-
const matches = detectPatterns(`<div onClick={go} role="button" tabIndex={0}>Go</div>`, "Btn.tsx");
|
|
657
|
-
expect(matches.some(m => m.pattern === "div with onClick no role")).toBe(false);
|
|
658
|
-
});
|
|
659
|
-
test("does NOT flag a <span onClick> that has role (role before onClick)", () => {
|
|
660
|
-
const matches = detectPatterns(`<span role="button" onClick={go}>Go</span>`, "Btn.tsx");
|
|
661
|
-
expect(matches.some(m => m.pattern === "span with onClick no role")).toBe(false);
|
|
662
|
-
});
|
|
663
|
-
test("does NOT flag an <svg> with role/aria", () => {
|
|
664
|
-
const matches = detectPatterns(`<svg role="img" aria-label="Menu" viewBox="0 0 24 24"></svg>`, "Icon.tsx");
|
|
665
|
-
expect(matches.some(m => m.pattern === "svg without a11y")).toBe(false);
|
|
666
|
-
});
|
|
667
|
-
test("flags a bare <svg> with no a11y", () => {
|
|
668
|
-
const matches = detectPatterns(`<svg viewBox="0 0 24 24"></svg>`, "Icon.tsx");
|
|
669
|
-
expect(matches.some(m => m.pattern === "svg without a11y" && m.type === "concern")).toBe(true);
|
|
670
|
-
});
|
|
671
|
-
test("does NOT flag onMouseOver when onFocus appears earlier in a multi-line tag", () => {
|
|
672
|
-
const code = `<button
|
|
673
|
-
onFocus={preview}
|
|
674
|
-
onMouseOver={preview}
|
|
675
|
-
aria-label="Preview"
|
|
676
|
-
>`;
|
|
677
|
-
expect(detectPatterns(code, "Hero.tsx").some(m => m.pattern === "onMouseOver without onFocus")).toBe(false);
|
|
678
|
-
});
|
|
679
|
-
test("does NOT flag onMouseOver when onFocus appears later in the tag", () => {
|
|
680
|
-
const code = `<button onMouseOver={preview} onFocus={preview}>Preview</button>`;
|
|
681
|
-
expect(detectPatterns(code, "Hero.tsx").some(m => m.pattern === "onMouseOver without onFocus")).toBe(false);
|
|
682
|
-
});
|
|
683
|
-
test("flags onMouseOver when the same tag has no onFocus", () => {
|
|
684
|
-
const code = `<button
|
|
685
|
-
onMouseOver={preview}
|
|
686
|
-
aria-label="Preview"
|
|
687
|
-
>`;
|
|
688
|
-
const matches = detectPatterns(code, "Hero.tsx").filter(m => m.pattern === "onMouseOver without onFocus");
|
|
689
|
-
expect(matches).toHaveLength(1);
|
|
690
|
-
expect(matches[0]?.line).toBe(1);
|
|
691
|
-
});
|
|
692
|
-
test("does NOT flag onMouseOut when onBlur appears in the same multi-line tag", () => {
|
|
693
|
-
const code = `<button
|
|
694
|
-
onMouseOut={hidePreview}
|
|
695
|
-
onBlur={hidePreview}
|
|
696
|
-
>`;
|
|
697
|
-
expect(detectPatterns(code, "Hero.tsx").some(m => m.pattern === "onMouseOut without onBlur")).toBe(false);
|
|
698
|
-
});
|
|
699
|
-
});
|
|
700
|
-
|
|
701
|
-
// ════════════════════════════════════════════════════════════════
|
|
702
|
-
// COMMENT & STRING HANDLING — rules must not fire on commented-out code
|
|
703
|
-
// or prose, but must still fire on real code (and not truncate at // inside
|
|
704
|
-
// string literals such as URLs).
|
|
705
|
-
// ════════════════════════════════════════════════════════════════
|
|
706
|
-
describe("comment & string handling", () => {
|
|
707
|
-
test("ignores a pattern inside a line comment", () => {
|
|
708
|
-
const matches = detectPatterns(`// example: <img src="x" /> has no alt`, "notes.tsx");
|
|
709
|
-
expect(matches.some(m => m.pattern === "missing alt")).toBe(false);
|
|
710
|
-
});
|
|
711
|
-
test("ignores autoplay mentioned in a block comment", () => {
|
|
712
|
-
const matches = detectPatterns(`/* never set autoplay on <video> */\nconst x = 1;`, "Player.tsx");
|
|
713
|
-
expect(matches.some(m => m.pattern === "autoplay media")).toBe(false);
|
|
714
|
-
});
|
|
715
|
-
test("ignores markup inside an HTML comment", () => {
|
|
716
|
-
const matches = detectPatterns(`<!-- <img src="x"> <video autoplay></video> -->`, "page.html");
|
|
717
|
-
expect(matches.some(m => m.pattern === "missing alt")).toBe(false);
|
|
718
|
-
expect(matches.some(m => m.pattern === "autoplay media")).toBe(false);
|
|
719
|
-
});
|
|
720
|
-
test("still flags autoplay in real markup", () => {
|
|
721
|
-
const matches = detectPatterns(`<video autoplay src="v.mp4"></video>`, "Player.tsx");
|
|
722
|
-
expect(matches.some(m => m.pattern === "autoplay media" && m.type === "concern")).toBe(true);
|
|
723
|
-
});
|
|
724
|
-
test("preserves https:// inside a string (does not truncate the line at //)", () => {
|
|
725
|
-
const matches = detectPatterns(`<a href="https://example.com/x">read more</a>`, "Links.tsx");
|
|
726
|
-
expect(matches.some(m => m.pattern === "ambiguous link text")).toBe(true);
|
|
727
|
-
});
|
|
728
|
-
});
|
|
729
|
-
|
|
730
|
-
// ════════════════════════════════════════════════════════════════
|
|
731
|
-
// REGRESSION — audit hardening: multi-line tags, child-aware rules,
|
|
732
|
-
// file-level signals, and the severity downgrades. Each test below maps to a
|
|
733
|
-
// confirmed false positive / false negative the per-line engine produced.
|
|
734
|
-
// ════════════════════════════════════════════════════════════════
|
|
735
|
-
describe("regression — multi-line tags & child-aware rules", () => {
|
|
736
|
-
// "video without track" used to fire on EVERY <video> — even captioned ones —
|
|
737
|
-
// because its lookahead was bounded to the opening tag and never saw <track>.
|
|
738
|
-
test("does NOT flag a <video> with a <track> child (single line)", () => {
|
|
739
|
-
const matches = detectPatterns(`<video controls src="v.mp4"><track kind="captions" src="c.vtt" /></video>`, "Player.tsx");
|
|
740
|
-
expect(matches.some(m => m.pattern === "video without track")).toBe(false);
|
|
741
|
-
});
|
|
742
|
-
test("does NOT flag a multi-line <video> with a <track> child", () => {
|
|
743
|
-
const code = `<video controls src="v.mp4">\n <track kind="captions" src="c.vtt" srcLang="en" />\n</video>`;
|
|
744
|
-
expect(detectPatterns(code, "Player.tsx").some(m => m.pattern === "video without track")).toBe(false);
|
|
745
|
-
});
|
|
746
|
-
test("flags a <video> with no <track> child", () => {
|
|
747
|
-
expect(detectPatterns(`<video controls src="v.mp4"></video>`, "Player.tsx")
|
|
748
|
-
.some(m => m.pattern === "video without track" && m.type === "concern")).toBe(true);
|
|
749
|
-
});
|
|
750
|
-
|
|
751
|
-
// Per-line matching structurally missed tags whose attributes wrap across lines.
|
|
752
|
-
test("flags a multi-line <img> with no alt", () => {
|
|
753
|
-
const code = `<img\n src="hero.png"\n width={640}\n/>`;
|
|
754
|
-
expect(detectPatterns(code, "Hero.tsx").some(m => m.pattern === "missing alt" && m.type === "concern")).toBe(true);
|
|
755
|
-
});
|
|
756
|
-
test("does NOT flag a multi-line <img> that has alt", () => {
|
|
757
|
-
const code = `<img\n src="hero.png"\n alt="A hero"\n/>`;
|
|
758
|
-
expect(detectPatterns(code, "Hero.tsx").some(m => m.pattern === "missing alt")).toBe(false);
|
|
759
|
-
});
|
|
760
|
-
test("flags a multi-line bare <svg>", () => {
|
|
761
|
-
const code = `<svg\n viewBox="0 0 24 24"\n fill="none"\n>`;
|
|
762
|
-
expect(detectPatterns(code, "Icon.tsx").some(m => m.pattern === "svg without a11y" && m.type === "concern")).toBe(true);
|
|
763
|
-
});
|
|
764
|
-
test("reports the line where a multi-line tag starts", () => {
|
|
765
|
-
const code = `const x = 1;\n<img\n src="hero.png"\n/>`;
|
|
766
|
-
const matches = detectPatterns(code, "Hero.tsx").filter(m => m.pattern === "missing alt");
|
|
767
|
-
expect(matches[0]?.line).toBe(2);
|
|
768
|
-
});
|
|
769
|
-
|
|
770
|
-
// autoPlay={false} is the accessible default and must not be flagged.
|
|
771
|
-
test("does NOT flag autoPlay={false}", () => {
|
|
772
|
-
expect(detectPatterns(`<video autoPlay={false} src="v.mp4" />`, "Player.tsx")
|
|
773
|
-
.some(m => m.pattern === "autoplay media")).toBe(false);
|
|
774
|
-
});
|
|
775
|
-
test("flags a bare autoPlay (JSX boolean true)", () => {
|
|
776
|
-
expect(detectPatterns(`<video autoPlay src="v.mp4" />`, "Player.tsx")
|
|
777
|
-
.some(m => m.pattern === "autoplay media" && m.type === "concern")).toBe(true);
|
|
778
|
-
});
|
|
779
|
-
test("flags lowercase autoplay (HTML)", () => {
|
|
780
|
-
expect(detectPatterns(`<video autoplay src="v.mp4"></video>`, "page.html")
|
|
781
|
-
.some(m => m.pattern === "autoplay media" && m.type === "concern")).toBe(true);
|
|
782
|
-
});
|
|
783
|
-
});
|
|
784
|
-
|
|
785
|
-
describe("regression — file-level & comment handling", () => {
|
|
786
|
-
// hover-without-focus is now a file-level signal: it fires only when a
|
|
787
|
-
// stylesheet has :hover styles but no :focus anywhere — not on every block.
|
|
788
|
-
test("does NOT flag :hover when the file also defines :focus", () => {
|
|
789
|
-
const code = `.btn:hover {\n background: blue;\n}\n.btn:focus {\n outline: 2px solid;\n}`;
|
|
790
|
-
expect(detectPatterns(code, "buttons.css").some(m => m.pattern === "hover without focus")).toBe(false);
|
|
791
|
-
});
|
|
792
|
-
test("flags a stylesheet with :hover but no :focus, exactly once", () => {
|
|
793
|
-
const code = `.a:hover { color: red; }\n.b:hover { color: blue; }`;
|
|
794
|
-
expect(detectPatterns(code, "buttons.css").filter(m => m.pattern === "hover without focus").length).toBe(1);
|
|
795
|
-
});
|
|
796
|
-
test("treats :focus-visible as satisfying the focus requirement", () => {
|
|
797
|
-
const code = `.btn:hover { color: red; }\n.btn:focus-visible { outline: 2px solid; }`;
|
|
798
|
-
expect(detectPatterns(code, "buttons.css").some(m => m.pattern === "hover without focus")).toBe(false);
|
|
799
|
-
});
|
|
800
|
-
|
|
801
|
-
// `//` is not a comment in plain CSS; a protocol-relative url(//…) must not
|
|
802
|
-
// swallow the rest of the line (which previously hid concerns after it).
|
|
803
|
-
test("does not treat // as a comment in plain CSS (protocol-relative url)", () => {
|
|
804
|
-
const code = `.logo {\n background: url(//cdn.example.com/x.png);\n color: #ffffff;\n}`;
|
|
805
|
-
expect(detectPatterns(code, "logo.css").some(m => m.pattern === "hardcoded hex in CSS")).toBe(true);
|
|
806
|
-
});
|
|
807
|
-
test("still treats // as a comment in SCSS", () => {
|
|
808
|
-
const code = `// color: #ffffff;\n.x { color: red; }`;
|
|
809
|
-
expect(detectPatterns(code, "a.scss").some(m => m.pattern === "hardcoded hex in CSS")).toBe(false);
|
|
810
|
-
});
|
|
811
|
-
});
|
|
812
|
-
|
|
813
|
-
describe("regression — Swift @State matcher & severity downgrades", () => {
|
|
814
|
-
// The matcher was written backwards (var … @State) so it never fired; it now
|
|
815
|
-
// matches the real `@State var` order and skips private / @StateObject.
|
|
816
|
-
test("flags a non-private @State var", () => {
|
|
817
|
-
expect(detectPatterns(`@State var counter = 0`, "V.swift")
|
|
818
|
-
.some(m => m.pattern === "non-private @State" && m.type === "concern" && m.category === "patterns")).toBe(true);
|
|
819
|
-
});
|
|
820
|
-
test("does NOT flag @State private var", () => {
|
|
821
|
-
expect(detectPatterns(`@State private var name = ""`, "V.swift")
|
|
822
|
-
.some(m => m.pattern === "non-private @State")).toBe(false);
|
|
823
|
-
});
|
|
824
|
-
test("does NOT flag @StateObject", () => {
|
|
825
|
-
expect(detectPatterns(`@StateObject var vm = VM()`, "V.swift")
|
|
826
|
-
.some(m => m.pattern === "non-private @State")).toBe(false);
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
// ════════════════════════════════════════════════════════════════
|
|
830
|
-
// INLINE SUPPRESSIONS
|
|
831
|
-
// ════════════════════════════════════════════════════════════════
|
|
832
|
-
test("hig-disable-next-line suppresses a specific rule on the next line only", () => {
|
|
833
|
-
const code = `// hig-disable-next-line swift/hardcoded-color -- brand splash\n.foregroundColor(.red)\n.foregroundColor(.blue)`;
|
|
834
|
-
const matches = detectPatterns(code, "V.swift");
|
|
835
|
-
const colors = matches.filter(m => m.ruleId === "swift/hardcoded-color");
|
|
836
|
-
expect(colors.length).toBe(1);
|
|
837
|
-
expect(colors[0].line).toBe(3);
|
|
838
|
-
});
|
|
839
|
-
|
|
840
|
-
test("hig-disable-next-line with no IDs suppresses everything on the next line", () => {
|
|
841
|
-
const code = `// hig-disable-next-line\n.foregroundColor(.red)`;
|
|
842
|
-
expect(detectPatterns(code, "V.swift").filter(m => m.type === "concern")).toEqual([]);
|
|
843
|
-
});
|
|
844
|
-
|
|
845
|
-
test("hig-disable-next-line leaves other rules on the line alone", () => {
|
|
846
|
-
const code = `// hig-disable-next-line swift/navigation-view-deprecated\n.foregroundColor(.red)`;
|
|
847
|
-
const matches = detectPatterns(code, "V.swift");
|
|
848
|
-
expect(matches.some(m => m.ruleId === "swift/hardcoded-color")).toBe(true);
|
|
849
|
-
});
|
|
850
|
-
|
|
851
|
-
test("hig-disable-file suppresses matching rules across the whole file", () => {
|
|
852
|
-
const code = `// hig-disable-file swift/*\n.foregroundColor(.red)\nNavigationView {`;
|
|
853
|
-
const matches = detectPatterns(code, "V.swift");
|
|
854
|
-
expect(matches).toEqual([]);
|
|
855
|
-
});
|
|
856
|
-
|
|
857
|
-
test("suppression markers work in HTML comments", () => {
|
|
858
|
-
const code = `<!-- hig-disable-next-line web/missing-alt -->\n<img src="x.png">`;
|
|
859
|
-
const matches = detectPatterns(code, "index.html");
|
|
860
|
-
expect(matches.some(m => m.ruleId === "web/missing-alt")).toBe(false);
|
|
861
|
-
});
|
|
862
|
-
|
|
863
|
-
// The three heuristics that fire on common, often-fine code stay concerns but
|
|
864
|
-
// at moderate severity, so they don't trip a `--fail-on serious` gate.
|
|
865
|
-
test("Image / onTapGesture / hover concerns are moderate, not serious", () => {
|
|
866
|
-
const swift = detectPatterns(`Image(systemName: "star")\nText("Hi").onTapGesture { go() }`, "V.swift");
|
|
867
|
-
expect(swift.find(m => m.pattern === "Image without a11y")?.severity).toBe("moderate");
|
|
868
|
-
expect(swift.find(m => m.pattern === "onTapGesture without traits")?.severity).toBe("moderate");
|
|
869
|
-
const css = detectPatterns(`.a:hover { color: red; }`, "x.css");
|
|
870
|
-
expect(css.find(m => m.pattern === "hover without focus")?.severity).toBe("moderate");
|
|
871
|
-
});
|
|
872
|
-
});
|