refactor(lint): enforce the lib → components and client → server boundaries

Add the in-repo rule `healthlog/import-boundaries`:

- a file under src/lib may not import src/components or src/app (alias,
  relative, re-export and dynamic forms, type-only included);
- a "use client" file may not make a value import of the database client,
  the crypto module, the Prisma client entry, the job queue, pg, pg-boss or
  next/headers. Type-only imports and the Prisma enums entry stay legal.

The four lib files that still import upward are listed in the rule's allow
map with a reason each; a listed file that stops importing upward is
reported, so the map can only shrink.

Five of the nine upward imports are gone by moving pure modules to their
layer, without behaviour change:

- supply-runway estimate → src/lib/medications/supply-runway.ts
  (read by the low-stock job and the medication list read);
- weekday BYDAY tokens → src/lib/medications/scheduling/weekday-tokens.ts
  (read by the medication extraction prompt);
- settings section slugs → src/lib/settings/section-slugs.ts
  (read by the record-settings classification);
- the command palette index → src/components/command-palette/ (it is a
  client index built from component registries).

Proof the rule can fail: an import of @/components/ui/card appended to
src/lib/medications/supply-runway.ts reported libUpward, and a planted
"use client" file importing @/lib/db reported clientServerOnly; both were
removed again.
This commit is contained in:
Marc-André Bombeck committed 2026-10-22 04:15:11 +02:00
1 parent fdd0f50130
commit c86c7d1a79
40 files changed
+429 -49

No files matched your search

+1 -1
View File
@@ -19,7 +19,7 @@ import { dirname, join } from "node:path";
import { describe, expect, it } from "vitest";
import nextConfig from "../../next.config";
import { SETTINGS_SECTION_SLUGS } from "@/components/settings/section-slugs";
import { SETTINGS_SECTION_SLUGS } from "@/lib/settings/section-slugs";
import { LAYOUT_GROUP_IDS } from "@/components/settings/layout-groups";
import { MODULE_SETTINGS_PAGE_IDS } from "@/components/settings/module-settings-page-ids";
import {
+1 -1
View File
@@ -34,7 +34,7 @@ import {
estimateRunwayDays,
lowStockTriggerDays,
type RunwaySchedule,
} from "@/components/medications/detail/supply-runway";
} from "@/lib/medications/supply-runway";
import {
resolveLowStockRunwayDays,
resolveReorderLeadDays,
+1 -1
View File
@@ -54,7 +54,7 @@ import { assertMedicationOwnership } from "@/lib/medications/route-guards";
import {
effectiveUnitsPerDose,
estimateUnitsRunwayDays,
} from "@/components/medications/detail/supply-runway";
} from "@/lib/medications/supply-runway";
import { serializeScheduleUnitsPerDose } from "@/lib/medications/schedule-units-dto";
import { hhmmToMinutesOrNull } from "@/lib/medications/scheduling/hhmm";
import { getUserTodayBounds } from "@/lib/tz/local-day";
@@ -10,7 +10,7 @@ import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { isSettingsSectionSlug } from "@/components/settings/section-slugs";
import { isSettingsSectionSlug } from "@/lib/settings/section-slugs";
describe("sleep empty-state CTA href", () => {
it("links to /settings/integrations (a real settings slug)", () => {
+1 -1
View File
@@ -3,7 +3,7 @@ import { notFound } from "next/navigation";
import {
SETTINGS_SECTION_SLUGS,
isSettingsSectionSlug,
} from "@/components/settings/section-slugs";
} from "@/lib/settings/section-slugs";
import { SettingsShell } from "@/components/settings/settings-shell";
import {
SettingsSectionLoader,
+1 -1
View File
@@ -1,6 +1,6 @@
/**
* Server-safe constants for the v1.5 admin split. Mirrors the pattern in
* `src/components/settings/section-slugs.ts` so server-rendered code paths
* `src/lib/settings/section-slugs.ts` so server-rendered code paths
* (`generateStaticParams()`, the page guard, route-existence tests) can
* import the slug list without dragging the `<AdminShell>` client bundle
* into the server graph.
@@ -3,7 +3,7 @@ import { describe, expect, it } from "vitest";
import {
buildPaletteIndex,
type PaletteIndexInput,
} from "@/lib/command-palette/build-index";
} from "@/components/command-palette/build-index";
const t = (key: string) => key;
@@ -3,7 +3,7 @@ import { renderToStaticMarkup } from "react-dom/server";
import { Activity, Moon, Plus, Smile } from "lucide-react";
import { I18nProvider } from "@/lib/i18n/context";
import type { PaletteEntry } from "@/lib/command-palette/build-index";
import type { PaletteEntry } from "@/components/command-palette/build-index";
import type { SearchResponse } from "@/lib/search/contract";
vi.mock("next/navigation", () => ({
@@ -24,8 +24,8 @@ import {
SETTINGS_CARDS,
SETTINGS_LAYOUT_PAGES,
SETTINGS_MODULE_PAGES,
} from "./settings-cards";
import { PALETTE_SYNONYMS } from "./synonyms";
} from "@/lib/command-palette/settings-cards";
import { PALETTE_SYNONYMS } from "@/lib/command-palette/synonyms";
/**
* The command palette's index: every place and action this session may
@@ -30,7 +30,7 @@ import {
EMPTY_STATE_ACTIONS,
type PaletteEntry,
type PaletteGroup,
} from "@/lib/command-palette/build-index";
} from "@/components/command-palette/build-index";
import { rankEntries } from "@/lib/command-palette/rank";
import { pushRecent, readRecent } from "@/lib/command-palette/recent";
import { scrollToAnchorWhenReady } from "@/lib/dom/scroll-to-anchor";
@@ -14,7 +14,7 @@ import { useRecordCapabilities } from "@/hooks/use-record-capabilities";
import {
buildPaletteIndex,
type PaletteEntry,
} from "@/lib/command-palette/build-index";
} from "@/components/command-palette/build-index";
import { useTranslations } from "@/lib/i18n/context";
import {
MODULE_REGISTRY,
@@ -11,7 +11,7 @@ import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { isSettingsSectionSlug } from "@/components/settings/section-slugs";
import { isSettingsSectionSlug } from "@/lib/settings/section-slugs";
describe("sleep-overview empty-state CTA href", () => {
it("links to /settings/integrations (a real settings slug)", () => {
@@ -24,10 +24,8 @@ import {
weekdayIndexesToTokens,
weekdayIndexToToken,
} from "@/components/medications/scheduling/legacy-bridge";
import {
DEFAULT_SUB_CONTROLS,
type WeekdayToken,
} from "@/components/medications/scheduling/types";
import { DEFAULT_SUB_CONTROLS } from "@/components/medications/scheduling/types";
import { type WeekdayToken } from "@/lib/medications/scheduling/weekday-tokens";
describe("weekdayIndexToToken", () => {
it("maps Sunday-anchored 0..6 to SU/MO/TU/WE/TH/FR/SA", () => {
@@ -88,7 +88,7 @@ import { PhaseConfigSheet } from "@/components/medications/sections/phase-config
import { SideEffectsSection } from "@/components/medications/side-effects-section";
import { ChartSkeleton } from "@/components/charts/chart-skeleton";
import { TitrationTimeline } from "@/components/medications/titration-timeline";
import { estimateRunwayDays } from "@/components/medications/detail/supply-runway";
import { estimateRunwayDays } from "@/lib/medications/supply-runway";
import type { SupplySummary } from "@/lib/medications/inventory/summary";
import { MedicationWizardDialog } from "@/components/medications/wizard/medication-wizard-dialog";
import { parseScheduleRecurrence } from "@/lib/medication-schedule";
@@ -38,7 +38,7 @@ import {
estimateDailyDoseCount,
estimateRunwayDays,
lowStockTriggerDays,
} from "@/components/medications/detail/supply-runway";
} from "@/lib/medications/supply-runway";
import { useAuth } from "@/hooks/use-auth";
import { useMedicationComplianceSummary } from "@/lib/queries/use-medication-compliance-summary";
import { DEFAULT_TIMEZONE } from "@/lib/tz/format";
@@ -24,7 +24,7 @@ import {
estimateDailyDoseCount,
estimateRunwayDays,
lowStockTriggerDays,
} from "@/components/medications/detail/supply-runway";
} from "@/lib/medications/supply-runway";
import { useTranslations, useFormatters } from "@/lib/i18n/context";
import {
invalidateKeys,
@@ -28,7 +28,7 @@ import { Skeleton } from "@/components/ui/skeleton";
import { MedicationStatusPill } from "@/components/medications/card-parts/medication-status-pill";
import { useWeekdayLabel } from "@/components/medications/card-parts/medication-next-last-slot";
import { resolveDisplayedSlotInstant } from "@/components/medications/card-parts/displayed-slot-instant";
import { estimateRunwayDays } from "@/components/medications/detail/supply-runway";
import { estimateRunwayDays } from "@/lib/medications/supply-runway";
import { useMedicationIntake } from "@/components/medications/use-medication-intake";
import { reduceCurrentWindowStatus } from "@/lib/medications/window-status";
import { resolveNextDueDayLabel } from "@/lib/medications/next-due-day-label";
@@ -59,9 +59,11 @@ import {
type CadenceSubControls,
type CadenceValue,
DEFAULT_SUB_CONTROLS,
} from "./types";
import {
type WeekdayToken,
WEEKDAY_TOKENS,
} from "./types";
} from "@/lib/medications/scheduling/weekday-tokens";
export interface CadencePickerProps {
value: CadenceValue;
@@ -17,9 +17,11 @@ import {
type CadenceSubControls,
type CadenceValue,
DEFAULT_SUB_CONTROLS,
} from "./types";
import {
type WeekdayToken,
WEEKDAY_TOKENS,
} from "./types";
} from "@/lib/medications/scheduling/weekday-tokens";
/**
* Legacy schedule shape the page passes to the form. Sunday-anchored
+2 -12
View File
@@ -17,6 +17,8 @@
* structured values; the wizard / form maps them to the wire format.
*/
import type { WeekdayToken } from "@/lib/medications/scheduling/weekday-tokens";
/**
* The eight cadence kinds the wizard exposes. Maps to the engine's
* dispatch tiers in `src/lib/medications/scheduling/recurrence.ts`:
@@ -71,18 +73,6 @@ export interface CadenceSubControls {
rollingDays: number;
}
/** RFC 5545 BYDAY tokens — the order the chip row renders. */
export const WEEKDAY_TOKENS = [
"MO",
"TU",
"WE",
"TH",
"FR",
"SA",
"SU",
] as const;
export type WeekdayToken = (typeof WEEKDAY_TOKENS)[number];
/** Sensible defaults so a brand-new picker has valid sub-controls. */
export const DEFAULT_SUB_CONTROLS: CadenceSubControls = {
weekdays: ["MO"],
@@ -16,8 +16,8 @@ import { isoStringToDate } from "@/components/medications/scheduling/course-wind
import {
DEFAULT_SUB_CONTROLS,
type CadenceSubControls,
type WeekdayToken,
} from "@/components/medications/scheduling/types";
import { type WeekdayToken } from "@/lib/medications/scheduling/weekday-tokens";
import type { WizardPayload as ExtractorPayload } from "@/components/medications/scheduling/natural-language-extractor";
import type { WizardPayload } from "./wizard-payload";
@@ -5,11 +5,11 @@ import { useMemo } from "react";
import { encodeCadence } from "@/components/medications/scheduling/cadence-picker";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { type CadenceSubControls } from "@/components/medications/scheduling/types";
import {
type CadenceSubControls,
type WeekdayToken,
WEEKDAY_TOKENS,
} from "@/components/medications/scheduling/types";
} from "@/lib/medications/scheduling/weekday-tokens";
import { useTranslations } from "@/lib/i18n/context";
import type { StepProps } from "./step1-name";
@@ -12,7 +12,7 @@
import { describe, expect, it } from "vitest";
import en from "../../../../messages/en.json" with { type: "json" };
import de from "../../../../messages/de.json" with { type: "json" };
import { SETTINGS_SECTION_SLUGS } from "../section-slugs";
import { SETTINGS_SECTION_SLUGS } from "@/lib/settings/section-slugs";
type Bag = Record<string, unknown>;
@@ -112,7 +112,7 @@ vi.mock("@/components/settings/thresholds-editor-section", () => ({
}));
import { I18nProvider } from "@/lib/i18n/context";
import { SettingsSectionFrame } from "./section-frame-harness";
import type { SettingsSectionSlug } from "../section-slugs";
import type { SettingsSectionSlug } from "@/lib/settings/section-slugs";
import { AboutSection } from "../about-section";
import { AccountSection } from "../account-section";
import { AdvancedSection } from "../advanced-section";
@@ -11,7 +11,7 @@ import { classifySettingsDestination } from "@/lib/record-settings/classificatio
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { ManagedIntegrationStatus } from "@/components/settings/managed-integration-status";
import { SettingsCard } from "@/components/settings/settings-card";
import type { SettingsSectionSlug } from "@/components/settings/section-slugs";
import type { SettingsSectionSlug } from "@/lib/settings/section-slugs";
// The two record-scoped surfaces load on demand. Both are forms that validate
// with Zod (the managed settings patch, the allergy and family-history
@@ -5,7 +5,7 @@ import { useEffect, type ComponentType } from "react";
import { Skeleton } from "@/components/ui/skeleton";
import { SettingsCard } from "@/components/settings/settings-card";
import type { SettingsSectionSlug } from "@/components/settings/section-slugs";
import type { SettingsSectionSlug } from "@/lib/settings/section-slugs";
import type { IntegrationCallbackUrls } from "@/lib/integrations/callback-urls";
/**
+1 -1
View File
@@ -55,7 +55,7 @@ import {
SETTINGS_SECTION_SLUGS,
isSettingsSectionSlug,
type SettingsSectionSlug,
} from "./section-slugs";
} from "@/lib/settings/section-slugs";
import { SettingsHubBackLink } from "./settings-hub-back-link";
import { SettingsMobileNav } from "./settings-mobile-nav";
import { SettingsAnchorForward } from "./settings-anchor-forward";
@@ -29,7 +29,7 @@
import { z } from "zod/v4";
import { WEEKDAY_TOKENS } from "@/components/medications/scheduling/types";
import { WEEKDAY_TOKENS } from "@/lib/medications/scheduling/weekday-tokens";
/** Closed list — matches the design-synthesis dose-unit dropdown. */
export const DOSE_UNITS = [
+1 -1
View File
@@ -69,7 +69,7 @@ import {
lowStockTriggerDays,
supplyRunwayDates,
type RunwaySchedule,
} from "@/components/medications/detail/supply-runway";
} from "@/lib/medications/supply-runway";
import {
detectSupplyUnderflow,
summariseSupply,
@@ -16,7 +16,7 @@ import {
lowStockTriggerDays,
supplyRunwayDates,
type RunwaySchedule,
} from "@/components/medications/detail/supply-runway";
} from "@/lib/medications/supply-runway";
function schedule(partial: Partial<RunwaySchedule>): RunwaySchedule {
return {
+1 -1
View File
@@ -24,7 +24,7 @@ import {
import {
effectiveUnitsPerDose,
estimateUnitsRunwayDays,
} from "@/components/medications/detail/supply-runway";
} from "@/lib/medications/supply-runway";
import { serializeScheduleUnitsPerDose } from "@/lib/medications/schedule-units-dto";
import {
computeDisplayDue,
@@ -0,0 +1,17 @@
/**
* RFC 5545 BYDAY tokens in the order the weekday chip row renders them.
*
* Shared by the scheduling wizard and the server-side medication extraction
* prompt, so it lives beside the recurrence engine rather than in a
* component folder.
*/
export const WEEKDAY_TOKENS = [
"MO",
"TU",
"WE",
"TH",
"FR",
"SA",
"SU",
] as const;
export type WeekdayToken = (typeof WEEKDAY_TOKENS)[number];
+1 -1
View File
@@ -1,7 +1,7 @@
import {
SETTINGS_SECTION_SLUGS,
type SettingsSectionSlug,
} from "@/components/settings/section-slugs";
} from "@/lib/settings/section-slugs";
import type {
AccountAccessLevel,
AccountRecordKind,
File renamed without changes.