@behavioral-agentic-design-system/core 0.45.0 → 0.47.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 +17 -3
- package/scripts/a11y-lint.cjs +78 -15
- package/scripts/bads-sync.mjs +1 -1
- package/scripts/build-tokens.cjs +122 -7
- package/scripts/check-dark-contrast.mjs +204 -0
- package/scripts/check-flush-edges.mjs +245 -0
- package/scripts/design-system-advisory/README.md +239 -0
- package/scripts/design-system-advisory/check-copy-punctuation.mjs +44 -0
- package/scripts/design-system-advisory/check-even-padding.mjs +35 -0
- package/scripts/design-system-advisory/check-hand-built-button.mjs +46 -0
- package/scripts/design-system-advisory/check-html-nesting.mjs +51 -0
- package/scripts/design-system-advisory/check-icon-sizing.mjs +79 -0
- package/scripts/design-system-advisory/check-label-case.mjs +92 -0
- package/scripts/design-system-advisory/check-native-form-controls.mjs +53 -0
- package/scripts/design-system-advisory/check-radius.mjs +56 -0
- package/scripts/design-system-advisory/check-raw-values.mjs +61 -0
- package/scripts/design-system-advisory/check-small-text.mjs +72 -0
- package/scripts/design-system-advisory/check-tap-target.mjs +70 -0
- package/scripts/design-system-advisory/defaults.mjs +90 -0
- package/scripts/design-system-advisory/fixtures/clean.tsx.fixture +12 -0
- package/scripts/design-system-advisory/fixtures/violations.tsx.fixture +19 -0
- package/scripts/design-system-advisory/lib.mjs +255 -0
- package/scripts/design-system-advisory/run.mjs +189 -0
- package/scripts/design-system-advisory/workflow.yml +82 -0
- package/scripts/lib/copy-strings.mjs +146 -0
- package/scripts/lib/dark-contrast.mjs +120 -0
- package/scripts/lib/flush-edges.mjs +294 -0
- package/scripts/lib/lab-lint.mjs +62 -0
- package/scripts/lib/mount-page.mjs +25 -0
- package/scripts/lib/source-scan.mjs +173 -0
- package/scripts/lib/tells.mjs +240 -2
- package/scripts/lint-arbitrary-tokens.mjs +47 -0
- package/scripts/lint-component-token-refs.mjs +63 -0
- package/scripts/lint-content-hierarchy.mjs +114 -0
- package/scripts/lint-control-baseline.mjs +72 -0
- package/scripts/lint-copy-punctuation.mjs +60 -0
- package/scripts/lint-even-padding.mjs +58 -0
- package/scripts/lint-label-case.mjs +219 -0
- package/scripts/lint-microanimation.mjs +2 -2
- package/scripts/lint-proximity.mjs +181 -0
- package/scripts/lint-slop.mjs +1 -1
- package/scripts/lint-spacing-scale.mjs +78 -0
- package/scripts/lint-target-size.cjs +132 -0
- package/scripts/lint-token-primitives.mjs +307 -0
- package/scripts/size-audit.cjs +2 -2
- package/scripts/ux-tells.mjs +8 -6
- package/scripts/wcag-audit.cjs +25 -4
- package/src/api/layers/file.ts +2 -0
- package/src/api/site-auth.ts +14 -2
- package/src/app/preview/components/page.tsx +94 -14
- package/src/app/preview/sticker-sheet/page.tsx +329 -0
- package/src/app/preview/sticker-sheet/specimens.tsx +296 -0
- package/src/app/shell.tsx +4 -3
- package/src/components/Foundations/Colors.stories.tsx +119 -8
- package/src/components/home-tab.tsx +6 -1
- package/src/components/onboarding-wizard.tsx +1 -1
- package/src/components/ui/avatar.tsx +4 -0
- package/src/components/ui/chart.tsx +134 -42
- package/src/components/ui/checkbox-group.tsx +1 -1
- package/src/components/ui/checkbox.tsx +1 -1
- package/src/components/ui/combobox.tsx +1 -0
- package/src/components/ui/command.tsx +1 -1
- package/src/components/ui/date-picker.tsx +2 -0
- package/src/components/ui/date-range-picker.tsx +4 -0
- package/src/components/ui/file-upload.tsx +21 -2
- package/src/components/ui/heatmap.tsx +3 -1
- package/src/components/ui/input-group.tsx +5 -2
- package/src/components/ui/multi-select.tsx +1 -0
- package/src/components/ui/radio-group.tsx +2 -2
- package/src/components/ui/sparkline.tsx +33 -4
- package/src/components/ui/spinner.tsx +23 -4
- package/src/components/ui/table.tsx +9 -0
- package/src/components/ui/week-picker.tsx +0 -12
- package/src/data/component-added.json +8 -0
- package/src/data/docs-index.ts +22 -1
- package/src/data/history.json +161 -161
- package/src/data/sticker-sheet.json +9244 -0
- package/src/docs-trace-globs.json +1 -0
- package/src/lib/chart-motion.ts +41 -0
- package/src/lib/get-icon-color.ts +1 -1
- package/src/stories/Accordion.stories.tsx +7 -0
- package/src/stories/ActionBar.stories.tsx +5 -0
- package/src/stories/Affix.stories.tsx +4 -0
- package/src/stories/AiBadge.stories.tsx +9 -7
- package/src/stories/Alert.stories.tsx +13 -0
- package/src/stories/AlertDialog.stories.tsx +4 -0
- package/src/stories/Anchor.stories.tsx +5 -0
- package/src/stories/AngleSlider.stories.tsx +6 -3
- package/src/stories/AspectRatio.stories.tsx +4 -0
- package/src/stories/Autocomplete.stories.tsx +6 -0
- package/src/stories/Avatar.stories.tsx +10 -0
- package/src/stories/Badge.stories.tsx +14 -6
- package/src/stories/Blockquote.stories.tsx +6 -0
- package/src/stories/BottomNavigation.stories.tsx +4 -0
- package/src/stories/Breadcrumb.stories.tsx +3 -0
- package/src/stories/Burger.stories.tsx +1 -0
- package/src/stories/Button.stories.tsx +24 -0
- package/src/stories/ButtonGroup.stories.tsx +9 -0
- package/src/stories/Calendar.stories.tsx +3 -0
- package/src/stories/Card.stories.tsx +4 -0
- package/src/stories/Carousel.stories.tsx +6 -0
- package/src/stories/Cascader.stories.tsx +4 -0
- package/src/stories/Chart.stories.tsx +46 -1
- package/src/stories/ChatBubble.stories.tsx +8 -2
- package/src/stories/ChatMarker.stories.tsx +1 -0
- package/src/stories/Checkbox.stories.tsx +40 -0
- package/src/stories/CheckboxCard.stories.tsx +8 -0
- package/src/stories/CheckboxGroup.stories.tsx +19 -0
- package/src/stories/Chip.stories.tsx +10 -19
- package/src/stories/CircularProgress.stories.tsx +13 -27
- package/src/stories/Clipboard.stories.tsx +6 -0
- package/src/stories/CloseButton.stories.tsx +6 -4
- package/src/stories/Code.stories.tsx +8 -1
- package/src/stories/Collapsible.stories.tsx +2 -0
- package/src/stories/ColorPicker.stories.tsx +6 -0
- package/src/stories/ColorSwatch.stories.tsx +1 -0
- package/src/stories/Combobox.stories.tsx +9 -0
- package/src/stories/Command.stories.tsx +2 -0
- package/src/stories/Container.stories.tsx +8 -0
- package/src/stories/ContextMenu.stories.tsx +9 -0
- package/src/stories/CopyButton.stories.tsx +12 -36
- package/src/stories/DataList.stories.tsx +8 -0
- package/src/stories/DataTable.stories.tsx +9 -0
- package/src/stories/DateField.stories.tsx +7 -0
- package/src/stories/DatePicker.stories.tsx +34 -0
- package/src/stories/DateRangePicker.stories.tsx +32 -0
- package/src/stories/Dialog.stories.tsx +8 -0
- package/src/stories/Drawer.stories.tsx +6 -0
- package/src/stories/DropdownMenu.stories.tsx +2 -0
- package/src/stories/Editable.stories.tsx +10 -0
- package/src/stories/EmptyState.stories.tsx +9 -0
- package/src/stories/Field.stories.tsx +7 -0
- package/src/stories/Fieldset.stories.tsx +8 -0
- package/src/stories/FileButton.stories.tsx +4 -0
- package/src/stories/FileUpload.stories.tsx +16 -17
- package/src/stories/FloatLabel.stories.tsx +3 -8
- package/src/stories/FloatingActionButton.stories.tsx +10 -0
- package/src/stories/FloatingIndicator.stories.tsx +4 -0
- package/src/stories/FloatingPanel.stories.tsx +6 -0
- package/src/stories/FocusTrap.stories.tsx +5 -0
- package/src/stories/FormatByte.stories.tsx +6 -0
- package/src/stories/FormatDate.stories.tsx +8 -0
- package/src/stories/FormatNumber.stories.tsx +9 -0
- package/src/stories/Heatmap.stories.tsx +11 -0
- package/src/stories/Highlight.stories.tsx +6 -0
- package/src/stories/HoverCard.stories.tsx +4 -0
- package/src/stories/Image.stories.tsx +9 -0
- package/src/stories/Indicator.stories.tsx +19 -0
- package/src/stories/Input.stories.tsx +7 -0
- package/src/stories/InputClearButton.stories.tsx +3 -0
- package/src/stories/InputGroup.stories.tsx +40 -0
- package/src/stories/Kbd.stories.tsx +7 -1
- package/src/stories/KeyboardShortcut.stories.tsx +2 -0
- package/src/stories/Label.stories.tsx +2 -0
- package/src/stories/Link.stories.tsx +9 -0
- package/src/stories/List.stories.tsx +8 -0
- package/src/stories/Listbox.stories.tsx +1 -0
- package/src/stories/LoadingDots.stories.tsx +5 -3
- package/src/stories/LoadingOverlay.stories.tsx +5 -1
- package/src/stories/Mark.stories.tsx +5 -0
- package/src/stories/Marquee.stories.tsx +4 -0
- package/src/stories/Masonry.stories.tsx +5 -0
- package/src/stories/Menubar.stories.tsx +3 -0
- package/src/stories/Meter.stories.tsx +11 -17
- package/src/stories/MonthPicker.stories.tsx +5 -1
- package/src/stories/MotionToggle.stories.tsx +1 -0
- package/src/stories/MultiSelect.stories.tsx +9 -0
- package/src/stories/NativeSelect.stories.tsx +1 -0
- package/src/stories/NavLink.stories.tsx +7 -0
- package/src/stories/NavigationMenu.stories.tsx +5 -0
- package/src/stories/NavigationRail.stories.tsx +10 -0
- package/src/stories/Notification.stories.tsx +13 -34
- package/src/stories/NumberInput.stories.tsx +8 -1
- package/src/stories/Overlay.stories.tsx +5 -1
- package/src/stories/Pagination.stories.tsx +8 -0
- package/src/stories/PasswordInput.stories.tsx +2 -0
- package/src/stories/PinInput.stories.tsx +9 -2
- package/src/stories/Popover.stories.tsx +2 -0
- package/src/stories/Progress.stories.tsx +6 -1
- package/src/stories/Prose.stories.tsx +8 -0
- package/src/stories/QrCode.stories.tsx +2 -0
- package/src/stories/Questionnaire.stories.tsx +7 -0
- package/src/stories/RadioGroup.stories.tsx +40 -0
- package/src/stories/RatingGroup.stories.tsx +8 -4
- package/src/stories/Resizable.stories.tsx +4 -2
- package/src/stories/Result.stories.tsx +3 -0
- package/src/stories/Reveal.stories.tsx +2 -0
- package/src/stories/RollingNumber.stories.tsx +5 -0
- package/src/stories/ScrollArea.stories.tsx +3 -1
- package/src/stories/SearchInput.stories.tsx +8 -6
- package/src/stories/SegmentedControl.stories.tsx +11 -0
- package/src/stories/Select.stories.tsx +7 -0
- package/src/stories/Separator.stories.tsx +6 -0
- package/src/stories/Sheet.stories.tsx +5 -0
- package/src/stories/Sidebar.stories.tsx +40 -1
- package/src/stories/SimpleGrid.stories.tsx +5 -0
- package/src/stories/Skeleton.stories.tsx +8 -0
- package/src/stories/SkeletonTable.stories.tsx +6 -5
- package/src/stories/SkipLink.stories.tsx +3 -0
- package/src/stories/Slider.stories.tsx +2 -1
- package/src/stories/Sonner.stories.tsx +7 -0
- package/src/stories/Sparkline.stories.tsx +14 -1
- package/src/stories/Spinner.stories.tsx +20 -10
- package/src/stories/Spoiler.stories.tsx +5 -0
- package/src/stories/Squircle.stories.tsx +7 -0
- package/src/stories/Stack.stories.tsx +7 -0
- package/src/stories/StatusBadge.stories.tsx +12 -6
- package/src/stories/Stepper.stories.tsx +5 -0
- package/src/stories/Switch.stories.tsx +4 -0
- package/src/stories/Table.stories.tsx +10 -1
- package/src/stories/Tabs.stories.tsx +8 -0
- package/src/stories/Tag.stories.tsx +13 -5
- package/src/stories/TagsInput.stories.tsx +13 -0
- package/src/stories/Text.stories.tsx +12 -0
- package/src/stories/Textarea.stories.tsx +9 -0
- package/src/stories/ThemeIcon.stories.tsx +6 -3
- package/src/stories/TimePicker.stories.tsx +2 -0
- package/src/stories/Timeline.stories.tsx +12 -0
- package/src/stories/Timer.stories.tsx +4 -0
- package/src/stories/Toggle.stories.tsx +7 -5
- package/src/stories/ToggleGroup.stories.tsx +5 -0
- package/src/stories/ToggleTip.stories.tsx +4 -0
- package/src/stories/Toolbar.stories.tsx +6 -0
- package/src/stories/Tooltip.stories.tsx +3 -0
- package/src/stories/Tour.stories.tsx +3 -0
- package/src/stories/TransferList.stories.tsx +3 -0
- package/src/stories/TreeSelect.stories.tsx +4 -0
- package/src/stories/TreeView.stories.tsx +5 -0
- package/src/stories/TypingIndicator.stories.tsx +4 -0
- package/src/stories/VisuallyHidden.stories.tsx +3 -0
- package/src/stories/Watermark.stories.tsx +6 -0
- package/src/stories/WeekPicker.stories.tsx +4 -1
- package/src/stories/WordRotate.stories.tsx +5 -0
- package/src/stories/YearPicker.stories.tsx +5 -11
- package/src/styles.css +50 -2
- package/src/tokens.json +4 -4
- package/LICENSE +0 -21
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The in-page measure behind check:dark-contrast (bads-check-dark-contrast), shared with its browser test
|
|
3
|
+
* (dark-contrast.browser.test.mjs beside it).
|
|
4
|
+
*
|
|
5
|
+
* THE RULE. Text must be readable on what is actually painted behind it. A surface and the text on it are a pair, and
|
|
6
|
+
* nobody declares the pair: a shipped card that hard-codes `bg-white` stays white in dark mode while the theme-aware
|
|
7
|
+
* text token on it turns near-white. Each is fine alone; a token-pair audit passes both. Only the painted page shows
|
|
8
|
+
* the pair, so this measures the painted page. First found in a client's prototypes in dark mode: near-white text on a
|
|
9
|
+
* white card in every frame of a journey, and near-black names on blue bars.
|
|
10
|
+
*
|
|
11
|
+
* WHAT IT MEASURES. Every visible text node under the scope (each element once). It reads the computed text colour,
|
|
12
|
+
* lays the background fills from the element up to the first opaque one over white, composites the text over that,
|
|
13
|
+
* and takes the WCAG ratio. Colours arrive as lab() or oklch() as often as rgb(), so each one is painted to a canvas
|
|
14
|
+
* pixel to read it. Text is "large" at `largeSize` px, or `largeBoldSize` px at weight 700 or more.
|
|
15
|
+
*
|
|
16
|
+
* WHAT IT SKIPS, AND COUNTS. Text over a background image or gradient (it cannot be measured from styles), disabled
|
|
17
|
+
* controls (WCAG exempts them), hidden, aria-hidden, sr-only and near-transparent text, and anything in an iframe.
|
|
18
|
+
*
|
|
19
|
+
* SETTINGS, with their defaults in DEFAULTS: the floors (WCAG AA, 4.5:1 and 3:1 for large text) and the large-text
|
|
20
|
+
* sizes, and the attribute that names a design. A client changes them in its settings, never in this file.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const DEFAULTS = Object.freeze({
|
|
24
|
+
/** The least ratio for normal text. WCAG AA is 4.5; AAA is 7. */
|
|
25
|
+
normal: 4.5,
|
|
26
|
+
/** The least ratio for large text. WCAG AA is 3; AAA is 4.5. */
|
|
27
|
+
large: 3,
|
|
28
|
+
/** Text this size or larger, in CSS px, is large. */
|
|
29
|
+
largeSize: 24,
|
|
30
|
+
/** Bold text (700 or more) this size or larger is large. */
|
|
31
|
+
largeBoldSize: 18.66,
|
|
32
|
+
/** The attribute that names the design a text sits in, for the report. */
|
|
33
|
+
frameAttr: "data-frame-id",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Runs in the page (pass it as `(${measureTextContrast})(${JSON.stringify(options)})`). `scope` is a CSS selector of the
|
|
38
|
+
* roots to measure, or null for the whole body. Returns { measured, onImage, disabled, fails: [{ frame, text, ratio,
|
|
39
|
+
* floor, fg, bg }] }.
|
|
40
|
+
*/
|
|
41
|
+
export function measureTextContrast({ scope = null, normal = 4.5, large = 3, largeSize = 24, largeBoldSize = 18.66, frameAttr = "data-frame-id" } = {}) {
|
|
42
|
+
const px = document.createElement("canvas").getContext("2d", { willReadFrequently: true });
|
|
43
|
+
px.canvas.width = px.canvas.height = 1;
|
|
44
|
+
const rgba = (css) => {
|
|
45
|
+
px.clearRect(0, 0, 1, 1);
|
|
46
|
+
px.fillStyle = "#000";
|
|
47
|
+
px.fillStyle = css;
|
|
48
|
+
px.fillRect(0, 0, 1, 1);
|
|
49
|
+
const [r, g, b, a] = px.getImageData(0, 0, 1, 1).data;
|
|
50
|
+
return { r, g, b, a: a / 255 };
|
|
51
|
+
};
|
|
52
|
+
const lum = ({ r, g, b }) => {
|
|
53
|
+
const f = (v) => ((v /= 255) <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
|
|
54
|
+
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b);
|
|
55
|
+
};
|
|
56
|
+
const over = (t, u) => ({ r: t.r * t.a + u.r * (1 - t.a), g: t.g * t.a + u.g * (1 - t.a), b: t.b * t.a + u.b * (1 - t.a), a: 1 });
|
|
57
|
+
const roots = scope ? [...document.querySelectorAll(scope)] : [document.body];
|
|
58
|
+
const fails = [];
|
|
59
|
+
let measured = 0;
|
|
60
|
+
let onImage = 0;
|
|
61
|
+
let disabled = 0;
|
|
62
|
+
const seen = new Set();
|
|
63
|
+
for (const root of roots) {
|
|
64
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
65
|
+
while (walker.nextNode()) {
|
|
66
|
+
const el = walker.currentNode.parentElement;
|
|
67
|
+
if (!el || seen.has(el)) continue;
|
|
68
|
+
const text = walker.currentNode.textContent.replace(/\s+/g, " ").trim();
|
|
69
|
+
if (!text) continue;
|
|
70
|
+
seen.add(el);
|
|
71
|
+
if (el.closest("script,style,noscript,svg,template,[aria-hidden=true],[hidden],nextjs-portal")) continue;
|
|
72
|
+
const cs = getComputedStyle(el);
|
|
73
|
+
if (cs.visibility !== "visible") continue;
|
|
74
|
+
const box = el.getBoundingClientRect();
|
|
75
|
+
if (box.width < 1 || box.height < 1) continue;
|
|
76
|
+
let opacity = 1;
|
|
77
|
+
for (let e = el; e; e = e.parentElement) {
|
|
78
|
+
const s = getComputedStyle(e);
|
|
79
|
+
opacity *= parseFloat(s.opacity);
|
|
80
|
+
/* sr-only: clipped to nothing, read aloud, never painted. */
|
|
81
|
+
if (s.clip === "rect(0px, 0px, 0px, 0px)" || s.clipPath === "inset(50%)") opacity = 0;
|
|
82
|
+
}
|
|
83
|
+
if (opacity < 0.05) continue;
|
|
84
|
+
if (el.closest(":disabled,[aria-disabled=true],[data-disabled]")) {
|
|
85
|
+
disabled++;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
let image = false;
|
|
89
|
+
const layers = [];
|
|
90
|
+
for (let e = el; e; e = e.parentElement) {
|
|
91
|
+
const s = getComputedStyle(e);
|
|
92
|
+
if (s.backgroundImage !== "none") {
|
|
93
|
+
image = true;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
const c = rgba(s.backgroundColor);
|
|
97
|
+
if (c.a > 0) layers.push(c);
|
|
98
|
+
if (c.a >= 1) break;
|
|
99
|
+
}
|
|
100
|
+
if (image) {
|
|
101
|
+
onImage++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
let bg = { r: 255, g: 255, b: 255, a: 1 };
|
|
105
|
+
for (const l of layers.reverse()) bg = over(l, bg);
|
|
106
|
+
const fg = over(rgba(cs.color), bg);
|
|
107
|
+
const [hi, lo] = [lum(fg), lum(bg)].sort((a, b) => b - a);
|
|
108
|
+
const ratio = (hi + 0.05) / (lo + 0.05);
|
|
109
|
+
const size = parseFloat(cs.fontSize);
|
|
110
|
+
const isLarge = size >= largeSize || (parseInt(cs.fontWeight, 10) >= 700 && size >= largeBoldSize);
|
|
111
|
+
const floor = isLarge ? large : normal;
|
|
112
|
+
measured++;
|
|
113
|
+
if (ratio < floor) {
|
|
114
|
+
const rgb = (c) => `rgb(${[c.r, c.g, c.b].map(Math.round).join(",")})`;
|
|
115
|
+
fails.push({ frame: (frameAttr && el.closest(`[${frameAttr}]`)?.getAttribute(frameAttr)) || "(page)", text: text.slice(0, 50), ratio, floor, fg: rgb(fg), bg: rgb(bg) });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { measured, onImage, disabled, fails };
|
|
120
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The in-page measure behind check:flush-edges (bads-check-flush-edges), shared with its browser test
|
|
3
|
+
* (flush-edges.browser.test.mjs beside it).
|
|
4
|
+
*
|
|
5
|
+
* THE RULE. Text never sits right up against the edge of the shape that holds it. A label 6px from the start of a pill
|
|
6
|
+
* whose round end curves in by 22px touches the curve, and every source lint passes it: a class pair like `px-1.5` says
|
|
7
|
+
* nothing about where the letters land against the edge they are drawn in. Only the laid-out page shows that.
|
|
8
|
+
*
|
|
9
|
+
* WHAT IT MEASURES. Every visible text node inside the given roots, as the browser lays it out: one box per line, from
|
|
10
|
+
* a Range (the element's own box would include its padding and always pass). With `measure: "cap"` (the default) each
|
|
11
|
+
* line runs across its letters and from the top of its capitals to its baseline, which is the text a person sees: the
|
|
12
|
+
* font's line box adds empty space above the capitals, and descenders hang below the baseline in every pill. With
|
|
13
|
+
* `measure: "line"` the browser's line box is used as it is. Each line is clipped to any ancestor that clips (a
|
|
14
|
+
* `truncate` label ends where its ellipsis is drawn, not where its hidden text would run). It is then measured against
|
|
15
|
+
* the INNER edge (inside the border) of its nearest visually bounded ancestor:
|
|
16
|
+
* - an element whose background colour differs from what is behind it, that has a background image, or that is lifted
|
|
17
|
+
* by a visible shadow: all four sides;
|
|
18
|
+
* - an element with a visible border: only the sides that have one (a row with a rule under it is bounded below only);
|
|
19
|
+
* - a rounded corner is measured to the curve, so a label tucked into a pill's round end is caught.
|
|
20
|
+
* A radius alone, with nothing painted, draws no edge, so it does not bound anything.
|
|
21
|
+
*
|
|
22
|
+
* NOT MEASURED. A mark of three characters or fewer centred in a circle of its own (avatar initials, a count, a step
|
|
23
|
+
* number): its clearance is equal on every side by construction. Text that does not overlap its box at all (a label
|
|
24
|
+
* hung outside a field). Text inside svg, form controls, or anything hidden. Sides along which the box (or a scroller
|
|
25
|
+
* inside it) scrolls, because text sits wherever the scroll left it.
|
|
26
|
+
*
|
|
27
|
+
* Distances are in CSS pixels of the layout, not of the screen: a review canvas may scale every frame, so each distance
|
|
28
|
+
* is divided by the root's scale.
|
|
29
|
+
*
|
|
30
|
+
* EXCEPTIONS. The exception attribute (`data-flush-ok="<reason>"` by default) on the container or on any element between
|
|
31
|
+
* it and the text. The reason must be a real sentence (`minReason` characters or more); an empty or short one is itself
|
|
32
|
+
* reported.
|
|
33
|
+
*
|
|
34
|
+
* SETTINGS. Every number and name above is an option with a default in DEFAULTS. A client changes them in its own
|
|
35
|
+
* settings, never by editing this file.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/** The defaults. A client changes any of them with a setting (see check-flush-edges.mjs); none is hard-coded below. */
|
|
39
|
+
export const DEFAULTS = Object.freeze({
|
|
40
|
+
/** The least distance, in CSS px, between text and the edge that holds it. */
|
|
41
|
+
floor: 6,
|
|
42
|
+
/** "cap": each line runs from the top of its capitals to its baseline (what a person sees). "line": the browser's line box. */
|
|
43
|
+
measure: "cap",
|
|
44
|
+
/** The attribute that excuses a container, with a reason as its value. */
|
|
45
|
+
exceptAttr: "data-flush-ok",
|
|
46
|
+
/** The least length of that reason. A shorter one is itself reported. */
|
|
47
|
+
minReason: 12,
|
|
48
|
+
/** The attribute that names the design a root belongs to, for the report. */
|
|
49
|
+
frameAttr: "data-frame-id",
|
|
50
|
+
/** The name of an optional window function a host provides to say which shipped component a box is. */
|
|
51
|
+
probe: null,
|
|
52
|
+
});
|
|
53
|
+
export const FLOOR = DEFAULTS.floor;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Runs in the page (pass it as `(${measureFlushEdges})(${JSON.stringify(options)})`, so it carries nothing from outside).
|
|
57
|
+
* `rootSelector` picks the roots to measure; every other option falls back to DEFAULTS.
|
|
58
|
+
* Returns { measured, excused, dots, hits, badReasons }.
|
|
59
|
+
*/
|
|
60
|
+
export function measureFlushEdges({ rootSelector, floor = 6, measure = "cap", exceptAttr = "data-flush-ok", minReason = 12, frameAttr = "data-frame-id", probe = null }) {
|
|
61
|
+
if (measure !== "cap" && measure !== "line") throw new Error(`flush-edges: measure must be "cap" or "line", not ${JSON.stringify(measure)}`);
|
|
62
|
+
const cv = document.createElement("canvas");
|
|
63
|
+
cv.width = cv.height = 1;
|
|
64
|
+
const ctx = cv.getContext("2d", { willReadFrequently: true });
|
|
65
|
+
/* Browsers return lab()/oklab()/color-mix for some tokens, so a canvas turns any CSS colour into RGBA. */
|
|
66
|
+
const rgba = (css) => {
|
|
67
|
+
if (!css || css === "transparent" || css === "rgba(0, 0, 0, 0)") return [0, 0, 0, 0];
|
|
68
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
69
|
+
ctx.fillStyle = "#000";
|
|
70
|
+
ctx.fillStyle = css;
|
|
71
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
72
|
+
const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data;
|
|
73
|
+
return [r, g, b, a / 255];
|
|
74
|
+
};
|
|
75
|
+
const over = ([r, g, b, a], [br, bg, bb]) => [r * a + br * (1 - a), g * a + bg * (1 - a), b * a + bb * (1 - a)];
|
|
76
|
+
const effective = new Map();
|
|
77
|
+
/* What is painted behind an element's content: its own background flattened over its ancestors'. */
|
|
78
|
+
const backdrop = (el) => {
|
|
79
|
+
if (!el || el.nodeType !== 1) return [255, 255, 255];
|
|
80
|
+
if (effective.has(el)) return effective.get(el);
|
|
81
|
+
const own = rgba(getComputedStyle(el).backgroundColor);
|
|
82
|
+
const v = own[3] === 0 ? backdrop(el.parentElement) : over(own, backdrop(el.parentElement));
|
|
83
|
+
effective.set(el, v);
|
|
84
|
+
return v;
|
|
85
|
+
};
|
|
86
|
+
const differs = (a, b) => Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]) + Math.abs(a[2] - b[2]) > 6;
|
|
87
|
+
const SIDES = ["Top", "Right", "Bottom", "Left"];
|
|
88
|
+
/* Tailwind gives many elements a stack of transparent shadows (`0 0 #0000`, the ring variables); only a shadow with a
|
|
89
|
+
colour and some offset, blur or spread draws anything. */
|
|
90
|
+
const visibleShadow = (value) => {
|
|
91
|
+
if (!value || value === "none") return false;
|
|
92
|
+
for (const part of value.split(/,(?![^(]*\))/)) {
|
|
93
|
+
const colour = /(rgba?|oklch|oklab|lab|lch|hsla?|color)\([^)]*\)|#[0-9a-f]{3,8}/i.exec(part)?.[0] ?? "rgb(0, 0, 0)";
|
|
94
|
+
const sizes = (part.replace(colour, "").match(/-?[\d.]+px/g) ?? []).map(parseFloat);
|
|
95
|
+
if (rgba(colour)[3] > 0.04 && sizes.some((n) => n !== 0)) return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
};
|
|
99
|
+
/** Which sides of `el` are an edge a person can see, or null. */
|
|
100
|
+
const boundedSides = (el) => {
|
|
101
|
+
const cs = getComputedStyle(el);
|
|
102
|
+
const bg = rgba(cs.backgroundColor);
|
|
103
|
+
if (cs.backgroundImage !== "none" && !/^url\(.*\.svg/.test(cs.backgroundImage)) return new Set(SIDES);
|
|
104
|
+
if (bg[3] > 0.04 && differs(backdrop(el), backdrop(el.parentElement))) return new Set(SIDES);
|
|
105
|
+
/* A card lifted by its shadow has an edge even when it is the same colour as what is behind it. */
|
|
106
|
+
if (visibleShadow(cs.boxShadow) && el.offsetWidth > 0) return new Set(SIDES);
|
|
107
|
+
const sides = new Set();
|
|
108
|
+
for (const s of SIDES) {
|
|
109
|
+
if (parseFloat(cs[`border${s}Width`]) > 0 && cs[`border${s}Style`] !== "none" && rgba(cs[`border${s}Color`])[3] > 0.04) sides.add(s);
|
|
110
|
+
}
|
|
111
|
+
return sides.size ? sides : null;
|
|
112
|
+
};
|
|
113
|
+
const hiddenStyle = (el, stop) => {
|
|
114
|
+
for (let n = el; n && n !== stop.parentElement; n = n.parentElement) {
|
|
115
|
+
const cs = getComputedStyle(n);
|
|
116
|
+
if (cs.visibility === "hidden" || cs.visibility === "collapse" || parseFloat(cs.opacity) === 0 || cs.display === "none") return true;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
};
|
|
120
|
+
const describe = (el) => {
|
|
121
|
+
const cls = typeof el.className === "string" ? el.className : (el.getAttribute("class") ?? "");
|
|
122
|
+
return `<${el.tagName.toLowerCase()}${cls ? ` class="${cls.trim().replace(/\s+/g, " ").slice(0, 160)}"` : ""}>`;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const metrics = new Map();
|
|
126
|
+
/** Cap height, descent and content-area height of a font, in CSS px, measured with the canvas. */
|
|
127
|
+
const fontMetrics = (cs) => {
|
|
128
|
+
const font = `${cs.fontStyle} ${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`;
|
|
129
|
+
if (metrics.has(font)) return metrics.get(font);
|
|
130
|
+
ctx.font = font;
|
|
131
|
+
const t = ctx.measureText("H");
|
|
132
|
+
const size = parseFloat(cs.fontSize);
|
|
133
|
+
const ascent = t.fontBoundingBoxAscent || size * 0.97;
|
|
134
|
+
const descent = t.fontBoundingBoxDescent || size * 0.24;
|
|
135
|
+
const v = { cap: t.actualBoundingBoxAscent || size * 0.73, descent, box: ascent + descent };
|
|
136
|
+
metrics.set(font, v);
|
|
137
|
+
return v;
|
|
138
|
+
};
|
|
139
|
+
const out = { measured: 0, excused: 0, hits: [], badReasons: [] };
|
|
140
|
+
for (const root of document.querySelectorAll(rootSelector)) {
|
|
141
|
+
const rr = root.getBoundingClientRect();
|
|
142
|
+
const scale = root.offsetWidth ? rr.width / root.offsetWidth : 1;
|
|
143
|
+
if (!scale) continue;
|
|
144
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
145
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
146
|
+
const text = node.textContent.replace(/\s+/g, " ").trim();
|
|
147
|
+
if (!text) continue;
|
|
148
|
+
const parent = node.parentElement;
|
|
149
|
+
if (!parent || parent.closest("svg, script, style, noscript, textarea, select, option")) continue;
|
|
150
|
+
/* The nearest ancestor that draws an edge, up to and including the root. */
|
|
151
|
+
let box = null;
|
|
152
|
+
let sides = null;
|
|
153
|
+
for (let n = parent; n; n = n.parentElement) {
|
|
154
|
+
sides = boundedSides(n);
|
|
155
|
+
if (sides) {
|
|
156
|
+
box = n;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
if (n === root) break;
|
|
160
|
+
}
|
|
161
|
+
if (!box) continue;
|
|
162
|
+
if (hiddenStyle(parent, box)) continue;
|
|
163
|
+
const range = document.createRange();
|
|
164
|
+
range.selectNodeContents(node);
|
|
165
|
+
/* Each line, from the top of its capitals to its baseline: the text a person sees. The line box would add the font's
|
|
166
|
+
empty space above the capitals, and descenders hang below the baseline the way they do in every pill. */
|
|
167
|
+
const m = fontMetrics(getComputedStyle(parent));
|
|
168
|
+
let rects = [...range.getClientRects()]
|
|
169
|
+
.filter((r) => r.width > 0.5 && r.height > 0.5)
|
|
170
|
+
.map((r) => {
|
|
171
|
+
if (measure === "line") return { l: r.left, t: r.top, r: r.right, b: r.bottom };
|
|
172
|
+
const base = r.bottom - m.descent * (r.height / m.box);
|
|
173
|
+
return { l: r.left, t: base - m.cap * (r.height / m.box), r: r.right, b: base };
|
|
174
|
+
});
|
|
175
|
+
/* Clip each line to every ancestor that clips, from the text up to the box. A scroller does not count: what it
|
|
176
|
+
hides can be scrolled to, so it is not cut off. */
|
|
177
|
+
const clips = (v) => v === "hidden" || v === "clip";
|
|
178
|
+
for (let n = parent; n && rects.length; n = n.parentElement) {
|
|
179
|
+
const cs = getComputedStyle(n);
|
|
180
|
+
const cx = clips(cs.overflowX);
|
|
181
|
+
const cy = clips(cs.overflowY);
|
|
182
|
+
if (cx || cy) {
|
|
183
|
+
const c = n.getBoundingClientRect();
|
|
184
|
+
rects = rects
|
|
185
|
+
.map((r) => ({ l: cx ? Math.max(r.l, c.left) : r.l, r: cx ? Math.min(r.r, c.right) : r.r, t: cy ? Math.max(r.t, c.top) : r.t, b: cy ? Math.min(r.b, c.bottom) : r.b }))
|
|
186
|
+
.filter((r) => r.r - r.l > 0.5 && r.b - r.t > 0.5);
|
|
187
|
+
}
|
|
188
|
+
if (n === box) break;
|
|
189
|
+
}
|
|
190
|
+
/* sr-only text is clipped to a pixel. */
|
|
191
|
+
if (!rects.length || rects.every((r) => (r.r - r.l) / scale < 2 || (r.b - r.t) / scale < 2)) continue;
|
|
192
|
+
const cs = getComputedStyle(box);
|
|
193
|
+
const o = box.getBoundingClientRect();
|
|
194
|
+
const bw = Object.fromEntries(SIDES.map((s) => [s, parseFloat(cs[`border${s}Width`]) * scale]));
|
|
195
|
+
const inner = { l: o.left + bw.Left, t: o.top + bw.Top, r: o.right - bw.Right, b: o.bottom - bw.Bottom };
|
|
196
|
+
/* Text that does not overlap its box at all is placed outside it on purpose (a label hung above a field). */
|
|
197
|
+
const touches = rects.some((r) => r.r > inner.l && r.l < inner.r && r.b > inner.t && r.t < inner.b);
|
|
198
|
+
if (!touches) continue;
|
|
199
|
+
/* Along the way a box scrolls, or a scroller inside it, there is no fixed edge: text sits wherever the scroll left it. */
|
|
200
|
+
const scrolls = (v) => v === "auto" || v === "scroll";
|
|
201
|
+
for (let n = parent; n; n = n.parentElement) {
|
|
202
|
+
const ns = getComputedStyle(n);
|
|
203
|
+
if (scrolls(ns.overflowY) && n.scrollHeight > n.clientHeight + 1) {
|
|
204
|
+
sides.delete("Top");
|
|
205
|
+
sides.delete("Bottom");
|
|
206
|
+
}
|
|
207
|
+
if (scrolls(ns.overflowX) && n.scrollWidth > n.clientWidth + 1) {
|
|
208
|
+
sides.delete("Left");
|
|
209
|
+
sides.delete("Right");
|
|
210
|
+
}
|
|
211
|
+
if (n === box) break;
|
|
212
|
+
}
|
|
213
|
+
if (!sides.size) continue;
|
|
214
|
+
const d = { Top: Infinity, Right: Infinity, Bottom: Infinity, Left: Infinity };
|
|
215
|
+
for (const r of rects) {
|
|
216
|
+
if (sides.has("Left")) d.Left = Math.min(d.Left, (r.l - inner.l) / scale);
|
|
217
|
+
if (sides.has("Right")) d.Right = Math.min(d.Right, (inner.r - r.r) / scale);
|
|
218
|
+
if (sides.has("Top")) d.Top = Math.min(d.Top, (r.t - inner.t) / scale);
|
|
219
|
+
if (sides.has("Bottom")) d.Bottom = Math.min(d.Bottom, (inner.b - r.b) / scale);
|
|
220
|
+
}
|
|
221
|
+
const straight = { ...d };
|
|
222
|
+
/* Rounded corners: a line corner inside the corner's square is measured to the curve. */
|
|
223
|
+
if (sides.size === 4) {
|
|
224
|
+
const w = (inner.r - inner.l) / scale;
|
|
225
|
+
const h = (inner.b - inner.t) / scale;
|
|
226
|
+
const corners = [
|
|
227
|
+
["TopLeft", "Left", (r) => [r.l, r.t], [inner.l, inner.t], [1, 1]],
|
|
228
|
+
["TopRight", "Right", (r) => [r.r, r.t], [inner.r, inner.t], [-1, 1]],
|
|
229
|
+
["BottomRight", "Right", (r) => [r.r, r.b], [inner.r, inner.b], [-1, -1]],
|
|
230
|
+
["BottomLeft", "Left", (r) => [r.l, r.b], [inner.l, inner.b], [1, -1]],
|
|
231
|
+
];
|
|
232
|
+
for (const [name, side, pt, [cx0, cy0], [sx, sy]] of corners) {
|
|
233
|
+
const outer = parseFloat(cs[`border${name}Radius`]) || 0;
|
|
234
|
+
const rad = Math.min(Math.max(0, outer - (bw[side] / scale)), w / 2, h / 2);
|
|
235
|
+
if (rad < 1) continue;
|
|
236
|
+
for (const r of rects) {
|
|
237
|
+
const [px, py] = pt(r);
|
|
238
|
+
const x = ((px - cx0) * sx) / scale;
|
|
239
|
+
const y = ((py - cy0) * sy) / scale;
|
|
240
|
+
if (x >= rad || y >= rad) continue;
|
|
241
|
+
const dist = rad - Math.hypot(rad - x, rad - y);
|
|
242
|
+
d[side] = Math.min(d[side], dist);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/* A mark centred in a dot of its own (avatar initials, a count, a step number): a circle no wider than it is tall,
|
|
247
|
+
holding three characters or fewer, centred both ways. Its clearance is equal on every side by construction, and
|
|
248
|
+
the rule is about content running into an edge, not about a glyph sitting in the middle of a dot. */
|
|
249
|
+
{
|
|
250
|
+
const w = (o.right - o.left) / scale;
|
|
251
|
+
const h = (o.bottom - o.top) / scale;
|
|
252
|
+
const round = parseFloat(cs.borderTopLeftRadius) >= Math.min(w, h) / 2 - 1;
|
|
253
|
+
const centred = Math.abs(d.Left - d.Right) < 1.5 && Math.abs(d.Top - d.Bottom) < 2.5;
|
|
254
|
+
if (sides.size === 4 && round && Math.abs(w - h) <= 2 && text.length <= 3 && (centred || !Number.isFinite(d.Left))) {
|
|
255
|
+
out.dots = (out.dots ?? 0) + 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
out.measured++;
|
|
260
|
+
const worst = Object.entries(d).filter(([, v]) => v < floor - 0.5);
|
|
261
|
+
if (!worst.length) continue;
|
|
262
|
+
/* The written exception: on the box or on anything between the box and the text. */
|
|
263
|
+
let marker = null;
|
|
264
|
+
for (let n = parent; n; n = n.parentElement) {
|
|
265
|
+
if (n.hasAttribute(exceptAttr)) {
|
|
266
|
+
marker = n;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
if (n === box) break;
|
|
270
|
+
}
|
|
271
|
+
if (marker) {
|
|
272
|
+
const why = (marker.getAttribute(exceptAttr) ?? "").trim();
|
|
273
|
+
if (why.length >= minReason) {
|
|
274
|
+
out.excused++;
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
out.badReasons.push({ text: text.slice(0, 60), box: describe(marker) });
|
|
278
|
+
}
|
|
279
|
+
const probed = probe && typeof window[probe] === "function" ? window[probe](box) : null;
|
|
280
|
+
out.hits.push({
|
|
281
|
+
text: text.slice(0, 60),
|
|
282
|
+
box: describe(box),
|
|
283
|
+
frame: frameAttr ? (root.closest(`[${frameAttr}]`)?.getAttribute(frameAttr) ?? null) : null,
|
|
284
|
+
sides: worst.map(([s, v]) => `${s.toLowerCase()} ${Math.max(-99, v).toFixed(1)}px`),
|
|
285
|
+
/* The text runs past the box's own padding: the content is too long for the box, which is the caller's to fix
|
|
286
|
+
even when the box is a shipped component. */
|
|
287
|
+
overflows: worst.some(([s]) => straight[s] < parseFloat(cs[`padding${s}`]) - 0.5),
|
|
288
|
+
/* A shipped component marks its root with data-slot; a host's probe can name it more exactly. */
|
|
289
|
+
shipped: probed?.shipped ?? probed?.core ?? box.getAttribute("data-slot") ?? null,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return out;
|
|
294
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lab-lint: the shared runner for the source-level design lints in this folder
|
|
3
|
+
* (even-padding, spacing-scale, proximity, content-hierarchy, control-baseline,
|
|
4
|
+
* label-case, copy-punctuation, arbitrary-tokens).
|
|
5
|
+
*
|
|
6
|
+
* Each lint supplies one pure function, `detect(src, rel) => [{ line, text }]`, and this
|
|
7
|
+
* module does the rest: discovers files through scan-roots (so a fork whose tree matches no
|
|
8
|
+
* default root gets an error, never a false "clean"), reads each file, and hands the findings
|
|
9
|
+
* to counted-lint for the strict / --warn / --init / --ratchet modes.
|
|
10
|
+
*/
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { createRequire } from "node:module";
|
|
14
|
+
import { finishCountedLint } from "./counted-lint.mjs";
|
|
15
|
+
|
|
16
|
+
const require = createRequire(import.meta.url);
|
|
17
|
+
const { resolveScanFiles, guardEmptyScan } = require("./scan-roots.cjs");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* An exemption marker with a reason, on the finding's line or the line above.
|
|
21
|
+
* Read from the RAW lines: the marker lives in a comment and the scan blanks those.
|
|
22
|
+
* A bare marker does not count; the reason is the mechanism, because writing it is what
|
|
23
|
+
* forces the author to name the decision.
|
|
24
|
+
*/
|
|
25
|
+
export function markedNear(rawLines, line1, marker, { lines = 1 } = {}) {
|
|
26
|
+
const re = new RegExp(`${marker.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*[^\\s*/]`);
|
|
27
|
+
for (let k = 0; k <= lines; k++) {
|
|
28
|
+
if (re.test(rawLines[line1 - 1 - k] ?? "")) return true;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 1-based line of `index` in `src`. */
|
|
34
|
+
export const lineAt = (src, index) => src.slice(0, index).split("\n").length;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {object} o
|
|
38
|
+
* @param {string} o.ruleId key in .bads-ratchet.json
|
|
39
|
+
* @param {string} o.bin name for messages
|
|
40
|
+
* @param {string} o.fix how to fix a finding
|
|
41
|
+
* @param {(src: string, rel: string) => {line: number, text: string}[]} o.detect
|
|
42
|
+
* @param {boolean} [o.tsxOnly] only .tsx files (JSX rules); default is .ts and .tsx
|
|
43
|
+
*/
|
|
44
|
+
export function runLint({ ruleId, bin, fix, detect, tsxOnly = false, argv = process.argv.slice(2), root = process.cwd() }) {
|
|
45
|
+
const scan = resolveScanFiles({ root, argv, excludeStories: true, allowDirArgs: true });
|
|
46
|
+
guardEmptyScan(scan, bin);
|
|
47
|
+
const findings = [];
|
|
48
|
+
const scanned = [];
|
|
49
|
+
for (const abs of scan.files) {
|
|
50
|
+
if (tsxOnly ? !/\.tsx$/.test(abs) : !/\.tsx?$/.test(abs) || /\.d\.ts$/.test(abs)) continue;
|
|
51
|
+
let src;
|
|
52
|
+
try {
|
|
53
|
+
src = fs.readFileSync(abs, "utf8");
|
|
54
|
+
} catch {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const rel = path.relative(root, abs);
|
|
58
|
+
scanned.push(rel);
|
|
59
|
+
for (const f of detect(src, rel)) findings.push({ file: rel, ...f });
|
|
60
|
+
}
|
|
61
|
+
return finishCountedLint({ ruleId, bin, findings, scanned, fix, argv, root });
|
|
62
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mounts everything a page defers, before a rendered check measures it. Shared by the rendered checks @core ships.
|
|
3
|
+
*
|
|
4
|
+
* A showcase that defers its blocks until they near the viewport keeps first paint fast, and is a trap for a check: a
|
|
5
|
+
* measure taken on load sees only the opening screen and reports the rest as clean because it was never drawn. So the
|
|
6
|
+
* page is scrolled to the bottom until its height holds still for several reads in a row (lazy blocks mount in waves,
|
|
7
|
+
* and the page plateaus between them, so one repeat is not enough), then back to the top.
|
|
8
|
+
*
|
|
9
|
+
* Returns the number of `lazySelector` placeholders still on the page afterwards. A caller treats more than zero as a
|
|
10
|
+
* failure of the run: silence from a block that never mounted is not a pass.
|
|
11
|
+
*/
|
|
12
|
+
export async function mountEverything(page, { lazySelector = "[data-lazy-skeleton]", maxSteps = 120, stableReads = 4, stepMs = 250 } = {}) {
|
|
13
|
+
let lastHeight = -1;
|
|
14
|
+
let stable = 0;
|
|
15
|
+
for (let i = 0; i < maxSteps && stable < stableReads; i += 1) {
|
|
16
|
+
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
|
|
17
|
+
await page.waitForTimeout(stepMs);
|
|
18
|
+
const h = await page.evaluate(() => document.body.scrollHeight);
|
|
19
|
+
stable = h === lastHeight ? stable + 1 : 0;
|
|
20
|
+
lastHeight = h;
|
|
21
|
+
}
|
|
22
|
+
await page.evaluate(() => window.scrollTo(0, 0));
|
|
23
|
+
await page.waitForTimeout(600);
|
|
24
|
+
return lazySelector ? page.evaluate((sel) => document.querySelectorAll(sel).length, lazySelector) : 0;
|
|
25
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* source-scan: one source scanner, shared by every line-wise lint that reads class names or copy.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS. A regex over source text will eventually read something that is not code. Blanking
|
|
5
|
+
* comments is the easy half and not enough: the failure that actually recurs is deciding a match is code
|
|
6
|
+
* without knowing whether the cursor is inside a STRING. A lint hunting `px-4 py-6` must not fail over a
|
|
7
|
+
* sentence in a description that quotes `px-4 py-6` to explain a rule; a lint hunting import specifiers
|
|
8
|
+
* must not read the word "from" inside prose.
|
|
9
|
+
*
|
|
10
|
+
* It cannot simply blank every string, because the thing most lints look for lives inside one: Tailwind
|
|
11
|
+
* classes are string content (`className="px-4 py-6"`, `cn("flex", open && "p-4")`). So the useful
|
|
12
|
+
* question is "is this inside a string that holds CLASSES". `codeAndClasses` answers it: comments out,
|
|
13
|
+
* class-bearing strings kept, every other string blanked.
|
|
14
|
+
*
|
|
15
|
+
* Line numbers and column offsets are preserved exactly (everything blanked is replaced space-for-space),
|
|
16
|
+
* so a caller reports positions against the real file and can still read the RAW line to find an `*-ok:`
|
|
17
|
+
* exemption marker, which lives in a comment and would otherwise be blanked away.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Class-bearing contexts. A string inside one of these is kept. */
|
|
21
|
+
const CLASS_ATTR = /(?:^|[^\w$])(?:className|class)\s*=\s*$/;
|
|
22
|
+
const CLASS_CALL = /(?:^|[^\w$])(?:cn|clsx|cva|twMerge|classNames)\s*\($/;
|
|
23
|
+
|
|
24
|
+
/** Replace every character except newlines, so positions do not move. */
|
|
25
|
+
const blank = (s) => s.replace(/[^\n]/g, " ");
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Blank comments only. Exported because a few checks genuinely want string
|
|
29
|
+
* content regardless of context, and because it makes the difference between
|
|
30
|
+
* the two passes explicit at the call site rather than implied.
|
|
31
|
+
*/
|
|
32
|
+
export function withoutComments(src) {
|
|
33
|
+
let out = "";
|
|
34
|
+
let i = 0;
|
|
35
|
+
while (i < src.length) {
|
|
36
|
+
const c = src[i];
|
|
37
|
+
const n = src[i + 1];
|
|
38
|
+
if (c === "/" && n === "*") {
|
|
39
|
+
const end = src.indexOf("*/", i + 2);
|
|
40
|
+
const stop = end < 0 ? src.length : end + 2;
|
|
41
|
+
out += blank(src.slice(i, stop));
|
|
42
|
+
i = stop;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (c === "/" && n === "/") {
|
|
46
|
+
const end = src.indexOf("\n", i);
|
|
47
|
+
const stop = end < 0 ? src.length : end;
|
|
48
|
+
out += blank(src.slice(i, stop));
|
|
49
|
+
i = stop;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
53
|
+
const stop = endOfString(src, i);
|
|
54
|
+
out += src.slice(i, stop);
|
|
55
|
+
i = stop;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
out += c;
|
|
59
|
+
i++;
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Index just past the string literal that starts at `start`. */
|
|
65
|
+
function endOfString(src, start) {
|
|
66
|
+
const quote = src[start];
|
|
67
|
+
let i = start + 1;
|
|
68
|
+
while (i < src.length) {
|
|
69
|
+
const c = src[i];
|
|
70
|
+
if (c === "\\") {
|
|
71
|
+
i += 2;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (quote === "`" && c === "$" && src[i + 1] === "{") {
|
|
75
|
+
// Skip a ${ ... } interpolation, braces balanced.
|
|
76
|
+
let depth = 1;
|
|
77
|
+
i += 2;
|
|
78
|
+
while (i < src.length && depth > 0) {
|
|
79
|
+
if (src[i] === "{") depth++;
|
|
80
|
+
else if (src[i] === "}") depth--;
|
|
81
|
+
else if (src[i] === '"' || src[i] === "'" || src[i] === "`") {
|
|
82
|
+
i = endOfString(src, i);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
i++;
|
|
86
|
+
}
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (c === quote) return i + 1;
|
|
90
|
+
i++;
|
|
91
|
+
}
|
|
92
|
+
return src.length;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Comments blanked, and every string blanked EXCEPT those holding classes.
|
|
97
|
+
*
|
|
98
|
+
* A string counts as class-bearing when it is the value of a `className` /
|
|
99
|
+
* `class` attribute, or an argument to `cn` / `clsx` / `cva` / `twMerge` /
|
|
100
|
+
* `classNames`, or sits anywhere inside such an attribute's `{ ... }`
|
|
101
|
+
* expression — which covers the ternaries and `&&` chains codebases write
|
|
102
|
+
* constantly:
|
|
103
|
+
*
|
|
104
|
+
* className={cn("grid", wide && "px-4 py-6", open ? "p-2" : "p-4")}
|
|
105
|
+
*
|
|
106
|
+
* Everything else — a description, a blurb, a note, a piece of copy that quotes
|
|
107
|
+
* a class name in order to talk about it — is blanked.
|
|
108
|
+
*/
|
|
109
|
+
export function codeAndClasses(src) {
|
|
110
|
+
const stripped = withoutComments(src);
|
|
111
|
+
const out = [];
|
|
112
|
+
let i = 0;
|
|
113
|
+
/** Stack of open brackets; entries flagged true are class context. */
|
|
114
|
+
const stack = [];
|
|
115
|
+
let classDepth = 0;
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* A short trailing window of the code emitted so far, NOT the whole output.
|
|
119
|
+
*
|
|
120
|
+
* The first version of this tested `out.replace(/\s+$/, "")` at every bracket
|
|
121
|
+
* and every string. `out` grows to the length of the file, so that is a
|
|
122
|
+
* fresh full-length regex over the whole accumulated source per token —
|
|
123
|
+
* quadratic, and on a file thousands of
|
|
124
|
+
* lines long it ran for minutes and looked like a hang. The lookbehind patterns
|
|
125
|
+
* only ever need the last few dozen characters, so keep exactly that.
|
|
126
|
+
*/
|
|
127
|
+
let tail = "";
|
|
128
|
+
const TAIL = 64;
|
|
129
|
+
const pushTail = (text) => {
|
|
130
|
+
tail = (tail + text).slice(-TAIL);
|
|
131
|
+
};
|
|
132
|
+
const before = () => tail.replace(/\s+$/, "");
|
|
133
|
+
|
|
134
|
+
while (i < stripped.length) {
|
|
135
|
+
const c = stripped[i];
|
|
136
|
+
|
|
137
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
138
|
+
const stop = endOfString(stripped, i);
|
|
139
|
+
const raw = stripped.slice(i, stop);
|
|
140
|
+
const keep =
|
|
141
|
+
classDepth > 0 || CLASS_ATTR.test(before()) || CLASS_CALL.test(before());
|
|
142
|
+
out.push(keep ? raw : blank(raw));
|
|
143
|
+
pushTail(keep ? raw : " ");
|
|
144
|
+
i = stop;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (c === "{" || c === "(" || c === "[") {
|
|
149
|
+
const b = before();
|
|
150
|
+
const isClass =
|
|
151
|
+
c === "(" ? CLASS_CALL.test(b + "(") : CLASS_ATTR.test(b);
|
|
152
|
+
stack.push(isClass);
|
|
153
|
+
if (isClass) classDepth++;
|
|
154
|
+
out.push(c);
|
|
155
|
+
pushTail(c);
|
|
156
|
+
i++;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (c === "}" || c === ")" || c === "]") {
|
|
161
|
+
if (stack.pop()) classDepth--;
|
|
162
|
+
out.push(c);
|
|
163
|
+
pushTail(c);
|
|
164
|
+
i++;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
out.push(c);
|
|
169
|
+
pushTail(c);
|
|
170
|
+
i++;
|
|
171
|
+
}
|
|
172
|
+
return out.join("");
|
|
173
|
+
}
|