@behavioral-agentic-design-system/core 0.45.0 → 0.48.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 +183 -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/number-field.tsx +390 -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 +12 -0
- package/src/data/docs-index.ts +22 -1
- package/src/data/history.json +162 -162
- package/src/data/sticker-sheet.json +9297 -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 +20 -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/NumberField.stories.tsx +214 -0
- 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/stories/story-groups.json +2 -1
- package/src/styles.css +50 -2
- package/src/tokens.json +4 -4
- package/LICENSE +0 -21
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* lint-even-padding: padding is the same on all sides unless somebody chose otherwise.
|
|
4
|
+
*
|
|
5
|
+
* Enforces layer-6-patterns-and-rules/even-padding.md. `px-3 py-1` is 12px beside and 4px above, and
|
|
6
|
+
* nobody decided it: it is what comes of reaching for a horizontal value and a vertical value
|
|
7
|
+
* independently, and it reads as a squashed shape rather than a considered one.
|
|
8
|
+
*
|
|
9
|
+
* UNEVEN PADDING IS NOT BANNED, and a lint that banned it would be wrong. A full-width band whose
|
|
10
|
+
* horizontal inset is the page gutter and whose vertical inset is rhythm, a table row, a pill whose
|
|
11
|
+
* label needs room beside it that would look absurd above it: all honest. The requirement is that it
|
|
12
|
+
* was a DECISION, so mark it and say why, on the same line or the line above:
|
|
13
|
+
*
|
|
14
|
+
* {/* uneven-padding-ok: page gutter beside, rhythm above *\/}
|
|
15
|
+
*
|
|
16
|
+
* WHAT IT SEES. `px-*` paired with `py-*` on the same line, resolved through the spacing scale so
|
|
17
|
+
* `p-px` and `p-0.5` compare correctly. It does NOT see padding assembled by `cn()` across several
|
|
18
|
+
* lines, or `pt`/`pb`/`pl`/`pr` written out separately. Those are real gaps and this says so rather
|
|
19
|
+
* than implying full coverage.
|
|
20
|
+
*
|
|
21
|
+
* ADOPTION. Existing pairs are recorded with `--init` and ratcheted with `--ratchet`: a listed file may
|
|
22
|
+
* not get worse and no file may join the list.
|
|
23
|
+
*
|
|
24
|
+
* node packages/core/scripts/lint-even-padding.mjs [--warn | --init | --ratchet] [paths]
|
|
25
|
+
*/
|
|
26
|
+
import { codeAndClasses } from "./lib/source-scan.mjs";
|
|
27
|
+
import { runLint, markedNear } from "./lib/lab-lint.mjs";
|
|
28
|
+
import { isMain } from "./lib/counted-lint.mjs";
|
|
29
|
+
|
|
30
|
+
/** Tailwind's spacing scale in CSS pixels. `--spacing` is 0.25rem, so step N is N * 4px. */
|
|
31
|
+
const px = (step) => (step === "px" ? 1 : parseFloat(step) * 4);
|
|
32
|
+
|
|
33
|
+
export function findUnevenPadding(src) {
|
|
34
|
+
const rawLines = src.split("\n");
|
|
35
|
+
const findings = [];
|
|
36
|
+
codeAndClasses(src)
|
|
37
|
+
.split("\n")
|
|
38
|
+
.forEach((line, i) => {
|
|
39
|
+
const x = line.match(/(?<![\w-])px-(\d+(?:\.\d+)?|px)(?![\w.-])/);
|
|
40
|
+
const y = line.match(/(?<![\w-])py-(\d+(?:\.\d+)?|px)(?![\w.-])/);
|
|
41
|
+
if (!x || !y || px(x[1]) === px(y[1])) return;
|
|
42
|
+
if (markedNear(rawLines, i + 1, "uneven-padding-ok:")) return;
|
|
43
|
+
findings.push({ line: i + 1, text: `px-${x[1]} py-${y[1]} (${px(x[1])}px beside, ${px(y[1])}px above)` });
|
|
44
|
+
});
|
|
45
|
+
return findings;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (isMain(import.meta.url)) {
|
|
49
|
+
process.exit(
|
|
50
|
+
runLint({
|
|
51
|
+
ruleId: "even-padding",
|
|
52
|
+
bin: "bads-lint-even-padding",
|
|
53
|
+
tsxOnly: true,
|
|
54
|
+
detect: (src) => findUnevenPadding(src),
|
|
55
|
+
fix: "Use `p-N`, or keep the uneven pair and say why with `{/* uneven-padding-ok: <reason> */}` on the line above. See layer-6-patterns-and-rules/even-padding.md.",
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* lint-label-case: UI labels are sentence case, not Title Case.
|
|
4
|
+
*
|
|
5
|
+
* WHY. The convention "Book this tenant", not "Book This Tenant" is the common one for product UI
|
|
6
|
+
* (layer-1-foundation/language.md carries it as "Sentence case unless brand explicitly requires Title
|
|
7
|
+
* Case"), and language-lint does not check it. A rule that lives only in a doc is broken within the
|
|
8
|
+
* hour, and Title Case is the default an agent reaches for. This makes it a check.
|
|
9
|
+
*
|
|
10
|
+
* THE RULE. Capitalize the first word and proper nouns. Nothing else.
|
|
11
|
+
*
|
|
12
|
+
* Book this tenant not Book This Tenant
|
|
13
|
+
* Save for later not Save For Later
|
|
14
|
+
* Move-in date not Move-In Date
|
|
15
|
+
*
|
|
16
|
+
* The one exception is the eyebrow (the small label above a heading), which is uppercase with
|
|
17
|
+
* letter-spacing. That is a text-transform, not typed capitals, so it is not seen here.
|
|
18
|
+
*
|
|
19
|
+
* WHAT IT SCANS, AND WHY SO NARROWLY. Sentence case applies to every label, but a heading or a line of
|
|
20
|
+
* prose is full of names a script cannot know, so scanning all copy would cost more in false positives
|
|
21
|
+
* than the rule is worth. A casing check earns its place only if it is right nearly every time. So:
|
|
22
|
+
* what sits inside a <Button>, and what is passed as a label or CTA prop or key.
|
|
23
|
+
*
|
|
24
|
+
* PROPER NOUNS. A name that is genuinely capitalized belongs in the list, not in a suppression. The
|
|
25
|
+
* built-in list is deliberately generic (months, days, widely used platform names, the "I'm" family).
|
|
26
|
+
* A project adds its own product, partner and place names in `label-case-nouns.json` at the repo root:
|
|
27
|
+
*
|
|
28
|
+
* { "properNouns": ["Acme Cloud", "Riverbend Loft"] }
|
|
29
|
+
*
|
|
30
|
+
* ESCAPE HATCH. `label-case-ok: <reason>` in a comment on the label's line or the line above, for a
|
|
31
|
+
* label that is Title Case because it is QUOTED (a third party's name for its own product, copy taken
|
|
32
|
+
* verbatim from a source of truth). A bare marker with no reason does not count: "the source ships
|
|
33
|
+
* this capitalisation" is a very different claim from "I liked the capitals".
|
|
34
|
+
*
|
|
35
|
+
* node packages/core/scripts/lint-label-case.mjs [--warn | --init | --ratchet] [paths]
|
|
36
|
+
*/
|
|
37
|
+
import fs from "node:fs";
|
|
38
|
+
import path from "node:path";
|
|
39
|
+
import { withoutComments } from "./lib/source-scan.mjs";
|
|
40
|
+
import { runLint, markedNear, lineAt } from "./lib/lab-lint.mjs";
|
|
41
|
+
import { isMain } from "./lib/counted-lint.mjs";
|
|
42
|
+
|
|
43
|
+
const BUILT_IN_NOUNS = [
|
|
44
|
+
"Claude", "Claude Code", "Storybook", "Figma", "GitHub", "GitLab", "Vercel", "Next.js", "Tailwind", "Slack", "Notion",
|
|
45
|
+
"Google", "Google Pay", "Apple", "Apple Pay", "Facebook", "Instagram", "LinkedIn", "Stripe", "PayPal", "Venmo", "Plaid", "Wi-Fi",
|
|
46
|
+
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",
|
|
47
|
+
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday",
|
|
48
|
+
// The pronoun is capitalised in sentence case. Listed as whole contractions, in all three spellings a
|
|
49
|
+
// label can carry (straight, curly, and the JSX entity), because the masking below has no word
|
|
50
|
+
// boundary and a bare "I" would mask every capital I in the file.
|
|
51
|
+
"I'm", "I\u2019m", "I’m", "I'll", "I\u2019ll", "I’ll", "I've", "I\u2019ve", "I’ve", "I'd", "I\u2019d", "I’d",
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
/** The project's own proper nouns, from label-case-nouns.json (repo root or scripts/). */
|
|
55
|
+
export function projectNouns(root = process.cwd()) {
|
|
56
|
+
for (const p of ["label-case-nouns.json", "scripts/label-case-nouns.json"]) {
|
|
57
|
+
try {
|
|
58
|
+
const j = JSON.parse(fs.readFileSync(path.join(root, p), "utf8"));
|
|
59
|
+
if (Array.isArray(j.properNouns)) return j.properNouns.filter((n) => typeof n === "string");
|
|
60
|
+
} catch {
|
|
61
|
+
/* absent or unreadable: fall through to the built-in list */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// An acronym or a code, not a Title Cased word: ID, FAQ, US, W-9, 2BR.
|
|
68
|
+
const ACRONYM = /^[A-Z0-9][A-Z0-9&/.-]*$/;
|
|
69
|
+
|
|
70
|
+
const LABEL_KEYS =
|
|
71
|
+
// `title` is in the list because menus and trays commonly hold their row labels as `title:`.
|
|
72
|
+
"title|label|cta|ctaLabel|ctaText|buttonText|buttonLabel|actionLabel|submitLabel|confirmLabel|cancelLabel|primaryLabel|secondaryLabel|primaryCta|secondaryCta";
|
|
73
|
+
|
|
74
|
+
// Two forms. A JSX prop (`label="…"`) is the obvious one. But most copy lives in object literals,
|
|
75
|
+
// `{ id: "screen", label: "Screen the Tenant" }`, and a scanner that only knows the prop form passes
|
|
76
|
+
// a page whose every label is Title Case.
|
|
77
|
+
const LABEL_PROP = new RegExp(`(?<![\\w-])(?:${LABEL_KEYS})\\s*=\\s*(?:"([^"]*)"|'([^']*)')`, "g");
|
|
78
|
+
const LABEL_KEY = new RegExp(`(?<![\\w-])(?:${LABEL_KEYS})\\s*:\\s*(?:"([^"]*)"|'([^']*)'|\`([^\`]*)\`)`, "g");
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Blank Next.js `export const metadata` blocks. Its `title` is the browser tab and the name the site is
|
|
82
|
+
* bookmarked under: a document title, not a control a person reads and presses.
|
|
83
|
+
*/
|
|
84
|
+
function blankMetadata(src) {
|
|
85
|
+
const at = src.indexOf("export const metadata");
|
|
86
|
+
if (at === -1) return src;
|
|
87
|
+
const open = src.indexOf("{", at);
|
|
88
|
+
if (open === -1) return src;
|
|
89
|
+
let depth = 0;
|
|
90
|
+
for (let i = open; i < src.length; i++) {
|
|
91
|
+
if (src[i] === "{") depth++;
|
|
92
|
+
else if (src[i] === "}") {
|
|
93
|
+
depth--;
|
|
94
|
+
if (depth === 0) {
|
|
95
|
+
const block = src.slice(at, i + 1);
|
|
96
|
+
return src.slice(0, at) + block.replace(/[^\n]/g, " ") + src.slice(i + 1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return src;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Children of any <…Button…> / <…Cta…> element.
|
|
105
|
+
*
|
|
106
|
+
* A scan rather than one regex, because an opening tag routinely contains `>`
|
|
107
|
+
* inside a handler (`onClick={() => …}`) and a regex that stops at the first
|
|
108
|
+
* `>` silently skips every button that has one — which is most of them here.
|
|
109
|
+
*/
|
|
110
|
+
function buttonChildren(src) {
|
|
111
|
+
const out = [];
|
|
112
|
+
const tag = /<([A-Za-z][A-Za-z0-9.]*)\b/g;
|
|
113
|
+
let m;
|
|
114
|
+
while ((m = tag.exec(src))) {
|
|
115
|
+
if (!/button|cta/i.test(m[1])) continue;
|
|
116
|
+
let depth = 0;
|
|
117
|
+
let quote = null;
|
|
118
|
+
let end = -1;
|
|
119
|
+
for (let i = tag.lastIndex; i < src.length; i++) {
|
|
120
|
+
const c = src[i];
|
|
121
|
+
if (quote) { if (c === quote) quote = null; continue; }
|
|
122
|
+
if (c === '"' || c === "'" || c === "`") { quote = c; continue; }
|
|
123
|
+
if (c === "{") { depth++; continue; }
|
|
124
|
+
if (c === "}") { depth--; continue; }
|
|
125
|
+
if (depth > 0) continue;
|
|
126
|
+
if (c === ">") { end = i; break; }
|
|
127
|
+
}
|
|
128
|
+
if (end === -1 || src[end - 1] === "/") continue; // unterminated or self-closing
|
|
129
|
+
const close = src.indexOf("<", end + 1);
|
|
130
|
+
if (close === -1) continue;
|
|
131
|
+
const raw = src.slice(end + 1, close);
|
|
132
|
+
if (/[{}]/.test(raw)) continue; // dynamic child — nothing static to judge
|
|
133
|
+
const text = raw.trim();
|
|
134
|
+
if (text) out.push({ text, index: end + 1 });
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function labels(src) {
|
|
140
|
+
const out = buttonChildren(src);
|
|
141
|
+
for (const re of [LABEL_PROP, LABEL_KEY]) {
|
|
142
|
+
re.lastIndex = 0;
|
|
143
|
+
let m;
|
|
144
|
+
while ((m = re.exec(src))) {
|
|
145
|
+
let val = m[1] ?? m[2] ?? m[3];
|
|
146
|
+
if (!val) continue;
|
|
147
|
+
// A template literal carries its static head: `Add Damage Protection ·
|
|
148
|
+
// ${price}` is still a label whose casing can be judged, up to the
|
|
149
|
+
// first hole.
|
|
150
|
+
const hole = val.indexOf("${");
|
|
151
|
+
if (hole !== -1) val = val.slice(0, hole);
|
|
152
|
+
val = val.trim();
|
|
153
|
+
if (val) out.push({ text: val, index: m.index });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The offending word, or null. Masks known proper nouns first. */
|
|
160
|
+
function titleCasedWord(text, nounRes) {
|
|
161
|
+
if (/[{}<>]/.test(text)) return null;
|
|
162
|
+
const trimmed = text.trim();
|
|
163
|
+
if (!/^[A-Za-z]/.test(trimmed)) return null;
|
|
164
|
+
const count = trimmed.split(/\s+/).length;
|
|
165
|
+
if (count < 2 || count > 8) return null; // one word cannot be Title Case; 9+ is prose
|
|
166
|
+
if (/[.!?]\s+\S/.test(trimmed)) return null; // more than one sentence — not a label
|
|
167
|
+
|
|
168
|
+
let masked = trimmed;
|
|
169
|
+
for (const re of nounRes) {
|
|
170
|
+
re.lastIndex = 0;
|
|
171
|
+
masked = masked.replace(re, (hit) => "_".repeat(hit.length));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const tokens = [...masked.matchAll(/\S+/g)].map((t) => t[0]);
|
|
175
|
+
for (let i = 1; i < tokens.length; i++) {
|
|
176
|
+
if (/[:.!?…—–|·]$/.test(tokens[i - 1])) continue; // a new clause opens; a capital is correct
|
|
177
|
+
const core = tokens[i].replace(/^[^A-Za-z]+/, "").replace(/[^A-Za-z-]+$/, "");
|
|
178
|
+
if (core.length < 2) continue;
|
|
179
|
+
if (!/^[A-Z]/.test(core)) continue;
|
|
180
|
+
if (ACRONYM.test(core)) continue; // ID, FAQ, US
|
|
181
|
+
if (/[A-Z]/.test(core.slice(1))) continue; // internal capital — a name like PayPal
|
|
182
|
+
return core;
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
const nounResFor = (extra) =>
|
|
189
|
+
[...BUILT_IN_NOUNS, ...extra]
|
|
190
|
+
// Longest first, so "Claude Code" is masked whole before the single word "Claude" can match half of it.
|
|
191
|
+
.sort((a, b) => b.length - a.length)
|
|
192
|
+
.map((n) => new RegExp(n.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"));
|
|
193
|
+
|
|
194
|
+
export function findTitleCaseLabels(raw, extraNouns = []) {
|
|
195
|
+
const rawLines = raw.split("\n");
|
|
196
|
+
const src = blankMetadata(withoutComments(raw));
|
|
197
|
+
const nounRes = nounResFor(extraNouns);
|
|
198
|
+
const findings = [];
|
|
199
|
+
for (const { text, index } of labels(src)) {
|
|
200
|
+
const word = titleCasedWord(text, nounRes);
|
|
201
|
+
if (!word) continue;
|
|
202
|
+
const line = lineAt(src, index);
|
|
203
|
+
if (markedNear(rawLines, line, "label-case-ok:")) continue;
|
|
204
|
+
findings.push({ line, text: `"${text}": "${word}" should be lower case` });
|
|
205
|
+
}
|
|
206
|
+
return findings;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (isMain(import.meta.url)) {
|
|
210
|
+
const nouns = projectNouns();
|
|
211
|
+
process.exit(
|
|
212
|
+
runLint({
|
|
213
|
+
ruleId: "label-case",
|
|
214
|
+
bin: "bads-lint-label-case",
|
|
215
|
+
detect: (src) => findTitleCaseLabels(src, nouns),
|
|
216
|
+
fix: "Labels are sentence case: the first word and proper nouns only. If the flagged word is a name, add it to `label-case-nouns.json`. If the label is quoted verbatim from a source of truth, say so on the line above: `// label-case-ok: <why>`.",
|
|
217
|
+
}),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// pnpm lint:microanimation — flags components that use hardcoded Tailwind
|
|
3
3
|
// motion utilities instead of BADS motion tokens.
|
|
4
4
|
//
|
|
5
|
-
// New components (per
|
|
5
|
+
// New components (per docs/agent-rules/components-registry.md → Components & Registry → microanimation rule)
|
|
6
6
|
// must drive transitions from BADS motion tokens:
|
|
7
7
|
// duration-[var(--duration-fast|base|slow)]
|
|
8
8
|
// ease-[var(--ease-out|in|in-out|spring)]
|
|
@@ -257,7 +257,7 @@ function main() {
|
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
-
console.log('See
|
|
260
|
+
console.log('See docs/agent-rules/components-registry.md → Components & Registry → microanimation rule for the full contract.');
|
|
261
261
|
|
|
262
262
|
// --strict fails ONLY on errors. Warnings are advisory (e.g. duration-200
|
|
263
263
|
// works but bypasses tokens — don't block a build on it).
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* lint-proximity: a vertical stack must not state one relationship for children that sit at more than one level.
|
|
4
|
+
*
|
|
5
|
+
* Enforces layer-6-patterns-and-rules/proximity-and-grouping.md, which reduces to:
|
|
6
|
+
*
|
|
7
|
+
* spacing within a group < spacing between groups
|
|
8
|
+
*
|
|
9
|
+
* WHY. A section heading, the line explaining it, then four switch rows, all on one `gap-4`. Every gap
|
|
10
|
+
* is on the spacing scale, the type levels are right, the copy and colour and padding are right, and
|
|
11
|
+
* every other lint passes. The only thing broken is the one signal that says the paragraph explains the
|
|
12
|
+
* heading, and that signal is distance. A `flex-col` with one `gap` applies that gap between EVERY pair
|
|
13
|
+
* of children, so a container holding items at two levels can only state one distance. The bug is not
|
|
14
|
+
* a wrong number, it is a structure that cannot express two.
|
|
15
|
+
*
|
|
16
|
+
* WHAT IT ASSERTS (P1). A uniform-gap column must not hold a heading, that heading's supporting line,
|
|
17
|
+
* AND unrelated siblings, all at the same gap. A column of fewer than three children is never flagged:
|
|
18
|
+
* one gap between one pair states one relationship, which is all it has to state.
|
|
19
|
+
*
|
|
20
|
+
* WHAT IT DOES NOT DO. It cannot tell a supporting line from a peer, and does not try. A card that
|
|
21
|
+
* means to hold four peer statements at one gap is a real design, and `proximity-ok: <reason>` is how
|
|
22
|
+
* that is declared; demanding the reason forces the author to name the grouping. It does not compare
|
|
23
|
+
* the two distances either: it flags the structure that makes two distances impossible. A container
|
|
24
|
+
* using per-child margins can express both and is invisible here.
|
|
25
|
+
*
|
|
26
|
+
* node packages/core/scripts/lint-proximity.mjs [--warn | --init | --ratchet] [paths]
|
|
27
|
+
*/
|
|
28
|
+
import { withoutComments } from "./lib/source-scan.mjs";
|
|
29
|
+
import { runLint, lineAt } from "./lib/lab-lint.mjs";
|
|
30
|
+
import { isMain } from "./lib/counted-lint.mjs";
|
|
31
|
+
|
|
32
|
+
const MARKER = "proximity-ok:";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A real heading, or a component that names itself one. The uppercase start is a LOOKAHEAD, not a
|
|
36
|
+
* consumed character: written as `[A-Z][\w.]*Title[\w.]*` it silently fails on the component named
|
|
37
|
+
* exactly `Title`, because the `[A-Z]` eats the T and "itle" can never contain "Title" again.
|
|
38
|
+
*/
|
|
39
|
+
const HEADING = /^(?:h[1-6]|(?=[A-Z])[\w.]*(?:Title|Heading)[\w.]*)$/;
|
|
40
|
+
|
|
41
|
+
/** Anything that acts rather than describes. A control after a heading is not that heading's supporting line. */
|
|
42
|
+
const INTERACTIVE =
|
|
43
|
+
/^(?:button|input|select|textarea|a|form|label|fieldset|(?=[A-Z])[\w.]*(?:Button|Switch|Checkbox|Radio|Input|Select|Toggle|Link|Action|Field|Composer|Menu|Tabs?)[\w.]*)$/;
|
|
44
|
+
|
|
45
|
+
const VOID = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]);
|
|
46
|
+
|
|
47
|
+
/** `flex-col` with exactly one unprefixed, non-zero gap, in px. */
|
|
48
|
+
function columnGap(cls) {
|
|
49
|
+
if (!/(?:^|\s)flex(?=\s|$)/.test(cls) || !/(?:^|\s)flex-col(?=\s|$)/.test(cls)) return null;
|
|
50
|
+
const gaps = [...cls.matchAll(/(?<![\w:-])gap(?:-y)?-(\d+(?:\.\d+)?)\b/g)].map((m) => Number(m[1]));
|
|
51
|
+
if (gaps.length !== 1 || gaps[0] === 0) return null;
|
|
52
|
+
return gaps[0] * 4;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Read one JSX tag starting at `<`, brace-aware. A regex cannot: an opening tag routinely contains `>`
|
|
57
|
+
* inside `onChange={(v) => set({ ...cfg, x: v })}`, and the braces nest. Null when `<` is not a tag.
|
|
58
|
+
*/
|
|
59
|
+
function readTag(src, i) {
|
|
60
|
+
let j = i + 1;
|
|
61
|
+
const closing = src[j] === "/";
|
|
62
|
+
if (closing) j += 1;
|
|
63
|
+
const nameStart = j;
|
|
64
|
+
while (j < src.length && /[A-Za-z0-9_.$-]/.test(src[j])) j += 1;
|
|
65
|
+
const name = src.slice(nameStart, j);
|
|
66
|
+
if (!name || !/^[A-Za-z]/.test(name)) return null;
|
|
67
|
+
let depth = 0;
|
|
68
|
+
let quote = null;
|
|
69
|
+
for (; j < src.length; j += 1) {
|
|
70
|
+
const ch = src[j];
|
|
71
|
+
if (quote) {
|
|
72
|
+
if (ch === quote) quote = null;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (ch === '"' || ch === "'") { quote = ch; continue; }
|
|
76
|
+
if (ch === "{") { depth += 1; continue; }
|
|
77
|
+
if (ch === "}") { depth -= 1; continue; }
|
|
78
|
+
if (ch === ">" && depth === 0) {
|
|
79
|
+
return { name, closing, selfClosing: src[j - 1] === "/", raw: src.slice(i, j + 1), end: j + 1 };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `className="…"` on the tag, including the strings of a simple `cn("a b", …)` head. */
|
|
86
|
+
function classOf(raw) {
|
|
87
|
+
const plain = /\bclassName="([^"]*)"/.exec(raw);
|
|
88
|
+
if (plain) return plain[1];
|
|
89
|
+
const expr = /\bclassName=\{([\s\S]*)\}/.exec(raw);
|
|
90
|
+
return expr ? [...expr[1].matchAll(/"([^"]*)"/g)].map((m) => m[1]).join(" ") : "";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Comments and template literals blanked (prose can name a class without using it); quoted strings stay. */
|
|
94
|
+
function blankNonCode(src) {
|
|
95
|
+
return withoutComments(src).replace(/`(?:[^`\\]|\\.)*`/g, (m) => m.replace(/[^\n]/g, " "));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The marker, read off the container's own line or the WHOLE comment block directly above it, so a
|
|
100
|
+
* reason worth demanding can be written in full across several lines. A wrapped marker that fails
|
|
101
|
+
* the lint it is explaining is a trap.
|
|
102
|
+
*/
|
|
103
|
+
function hasReason(frameLine, rawLines, isComment) {
|
|
104
|
+
const withReason = (l) => {
|
|
105
|
+
const at = l.indexOf(MARKER);
|
|
106
|
+
return at !== -1 && l.slice(at + MARKER.length).trim().length > 0;
|
|
107
|
+
};
|
|
108
|
+
if (withReason(rawLines[frameLine - 1] ?? "")) return true;
|
|
109
|
+
for (let i = frameLine - 2; i >= 0; i -= 1) {
|
|
110
|
+
const line = rawLines[i] ?? "";
|
|
111
|
+
if (!isComment[i] && line.trim() !== "") break;
|
|
112
|
+
if (withReason(line)) return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function findFlatStacks(raw) {
|
|
118
|
+
const src = blankNonCode(raw);
|
|
119
|
+
const rawLines = raw.split("\n");
|
|
120
|
+
// A line held a comment when blanking changed it. Derived, not pattern-matched: a wrapped JSX block
|
|
121
|
+
// comment's continuation lines are plain indented prose and match no leading-`*` test.
|
|
122
|
+
const blanked = src.split("\n");
|
|
123
|
+
const isComment = rawLines.map((l, i) => l !== blanked[i]);
|
|
124
|
+
const hits = [];
|
|
125
|
+
const stack = [];
|
|
126
|
+
|
|
127
|
+
const verdict = (frame) => {
|
|
128
|
+
const kids = frame.children;
|
|
129
|
+
if (kids.length < 3) return;
|
|
130
|
+
for (let i = 0; i < kids.length - 2; i += 1) {
|
|
131
|
+
const head = kids[i];
|
|
132
|
+
const next = kids[i + 1];
|
|
133
|
+
if (!head.isHeading || next.isHeading || next.isInteractive) continue;
|
|
134
|
+
if (hasReason(frame.line, rawLines, isComment)) return;
|
|
135
|
+
hits.push({
|
|
136
|
+
line: frame.line,
|
|
137
|
+
text: `gap-${frame.gapPx / 4} (${frame.gapPx}px) across ${kids.length} children, <${head.name}> then <${next.name}> at the same distance as everything else`,
|
|
138
|
+
});
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
for (let i = 0; i < src.length; i += 1) {
|
|
144
|
+
if (src[i] !== "<") continue;
|
|
145
|
+
const tag = readTag(src, i);
|
|
146
|
+
if (!tag) continue;
|
|
147
|
+
const start = i;
|
|
148
|
+
i = tag.end - 1;
|
|
149
|
+
if (tag.closing) {
|
|
150
|
+
for (let k = stack.length - 1; k >= 0; k -= 1) {
|
|
151
|
+
if (stack[k].name === tag.name) {
|
|
152
|
+
const frame = stack[k];
|
|
153
|
+
stack.length = k;
|
|
154
|
+
if (frame.gapPx) verdict(frame);
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
// Register with the parent BEFORE pushing, so the element is its parent's child rather than its own.
|
|
161
|
+
const parent = stack[stack.length - 1];
|
|
162
|
+
if (parent?.gapPx) {
|
|
163
|
+
parent.children.push({ name: tag.name, isHeading: HEADING.test(tag.name), isInteractive: INTERACTIVE.test(tag.name) });
|
|
164
|
+
}
|
|
165
|
+
if (tag.selfClosing || VOID.has(tag.name.toLowerCase())) continue;
|
|
166
|
+
stack.push({ name: tag.name, gapPx: columnGap(classOf(tag.raw)), line: lineAt(src, start), children: [] });
|
|
167
|
+
}
|
|
168
|
+
return hits;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (isMain(import.meta.url)) {
|
|
172
|
+
process.exit(
|
|
173
|
+
runLint({
|
|
174
|
+
ruleId: "proximity",
|
|
175
|
+
bin: "bads-lint-proximity",
|
|
176
|
+
tsxOnly: true,
|
|
177
|
+
detect: (src) => findFlatStacks(src),
|
|
178
|
+
fix: "Wrap the heading and its supporting line in their own column with a smaller gap, so spacing within the group is less than spacing between groups. If they are genuinely peers: `{/* proximity-ok: <why> */}` (a bare marker does not count). See layer-6-patterns-and-rules/proximity-and-grouping.md.",
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
}
|
package/scripts/lint-slop.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* machine-default moves (indigo→purple gradients, gradient text, springy
|
|
5
5
|
* hover pops, huge glows) that make a vibe-coded UI read as generated.
|
|
6
6
|
*
|
|
7
|
-
* Why this exists: BADS already lists anti-defaults in
|
|
7
|
+
* Why this exists: BADS already lists anti-defaults in docs/agent-rules/design-exploration-discipline.md, but they
|
|
8
8
|
* were *prose* enforced only by the (subjective, expensive) independent-
|
|
9
9
|
* critique agent. Per docs/intent-driven-context.md, a constraint — a thing
|
|
10
10
|
* that must NOT happen — should be delivered by a lint. This is that lint.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* lint-spacing-scale: every padding, margin and gap a codebase CHOOSES is a step of its own spacing scale.
|
|
4
|
+
*
|
|
5
|
+
* WHY. Tailwind offers every multiple of 4px plus half-steps, so `p-2.5` (10px) and `gap-7` (28px) are
|
|
6
|
+
* as easy to type as `p-2` and `gap-8`. Nothing stops them, and they are not decisions: they are what
|
|
7
|
+
* you get when a value is picked by eye against a scale nobody enforces. The scale a project actually
|
|
8
|
+
* means is in its tokens (`spacing.*` in tokens.json), so this lint READS it rather than hard-coding
|
|
9
|
+
* one. A client with a 2/4/8/12/16/24/32/40 ramp and a client with BADS's 4/8/12/16/20/24/32/48/64 get
|
|
10
|
+
* the right answer from the same script.
|
|
11
|
+
*
|
|
12
|
+
* PADDING, MARGIN AND GAP ONLY (p*, m*, gap*, space-x/y). Widths and heights are not spacing: a 44px
|
|
13
|
+
* tap target and a 56px thumbnail are sizes with their own reasons.
|
|
14
|
+
*
|
|
15
|
+
* ESCAPE HATCH. `off-scale-ok: <reason>` on the line or the line above. It is for DERIVED values: an
|
|
16
|
+
* indent that mirrors a 20px checkbox plus its 8px gap, or a negative margin that cancels measured
|
|
17
|
+
* half-leading. Both are computed from on-scale inputs, and rounding either one breaks the alignment
|
|
18
|
+
* it exists to hold. A value that was merely picked by eye is not derived.
|
|
19
|
+
*
|
|
20
|
+
* Tailwind's `--spacing` is 0.25rem, so step N is N * 4px. A project that overrides `--spacing` should
|
|
21
|
+
* set BADS_SPACING_UNIT=<px>.
|
|
22
|
+
*
|
|
23
|
+
* node packages/core/scripts/lint-spacing-scale.mjs [--warn | --init | --ratchet] [paths]
|
|
24
|
+
*/
|
|
25
|
+
import { codeAndClasses } from "./lib/source-scan.mjs";
|
|
26
|
+
import { runLint, markedNear } from "./lib/lab-lint.mjs";
|
|
27
|
+
import { isMain } from "./lib/counted-lint.mjs";
|
|
28
|
+
import { loadTokens } from "./token-nearest.mjs";
|
|
29
|
+
|
|
30
|
+
// Spacing only: padding, margin, gap. NOT width/height/size (those are dimensions).
|
|
31
|
+
const RE = /(?<![\w-])(-?)(px|py|pt|pr|pb|pl|ps|pe|p|mx|my|mt|mr|mb|ml|ms|me|m|gap-x|gap-y|gap|space-x|space-y)-(\d+(?:\.\d+)?)(?![\w.\-/\[])/g;
|
|
32
|
+
|
|
33
|
+
/** The px values of `spacing.*` in the project's tokens.json, plus 0. */
|
|
34
|
+
export function spacingScalePx(tokens) {
|
|
35
|
+
const out = new Set([0]);
|
|
36
|
+
for (const t of Object.values(tokens?.spacing ?? {})) {
|
|
37
|
+
const v = typeof t === "object" ? t.$value : t;
|
|
38
|
+
const m = /^(-?[\d.]+)(px|rem)$/.exec(String(v));
|
|
39
|
+
if (m) out.add(m[2] === "rem" ? parseFloat(m[1]) * 16 : parseFloat(m[1]));
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function findOffScale(src, allowed, unit = 4) {
|
|
45
|
+
const rawLines = src.split("\n");
|
|
46
|
+
const findings = [];
|
|
47
|
+
codeAndClasses(src)
|
|
48
|
+
.split("\n")
|
|
49
|
+
.forEach((line, i) => {
|
|
50
|
+
for (const m of line.matchAll(RE)) {
|
|
51
|
+
const px = parseFloat(m[3]) * unit;
|
|
52
|
+
if (allowed.has(px)) continue;
|
|
53
|
+
if (markedNear(rawLines, i + 1, "off-scale-ok:")) continue;
|
|
54
|
+
findings.push({ line: i + 1, text: `${m[1]}${m[2]}-${m[3]} is ${m[1]}${px}px, not a step of the spacing scale` });
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return findings;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (isMain(import.meta.url)) {
|
|
61
|
+
const { tokens, source } = loadTokens();
|
|
62
|
+
const allowed = spacingScalePx(tokens);
|
|
63
|
+
if (!tokens || allowed.size < 2) {
|
|
64
|
+
console.error("bads-lint-spacing-scale: no `spacing` tokens found in tokens.json, so there is no scale to check against. Run from a BADS repo root or a fork of one.");
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
const unit = Number(process.env.BADS_SPACING_UNIT ?? 4);
|
|
68
|
+
const steps = [...allowed].sort((a, b) => a - b).join(" / ");
|
|
69
|
+
process.exit(
|
|
70
|
+
runLint({
|
|
71
|
+
ruleId: "spacing-scale",
|
|
72
|
+
bin: "bads-lint-spacing-scale",
|
|
73
|
+
tsxOnly: true,
|
|
74
|
+
detect: (src) => findOffScale(src, allowed, unit),
|
|
75
|
+
fix: `The scale (${source ? "from " + source.replace(process.cwd() + "/", "") : "tokens.json"}) is ${steps} px. Pick the nearest step, or mark a DERIVED value \`off-scale-ok: <why>\`.`,
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
}
|