diff --git a/client/scripts/lib/eslint-warnings.mjs b/client/scripts/lib/eslint-warnings.mjs index e240a39ce..0bb1c405b 100644 --- a/client/scripts/lib/eslint-warnings.mjs +++ b/client/scripts/lib/eslint-warnings.mjs @@ -155,74 +155,39 @@ function gitIn(root, args) { } } -/** The messages of one rule in one area of a single file's lint result. */ -function ofRule(results, rule, area) { - const result = results[0]; - if (!result) return []; - const pool = - area === 'suppressed' ? (result.suppressedMessages ?? []) : result.messages.filter((m) => m.severity === 1); - return pool.filter((m) => ruleOf(m) === rule); -} - /** - * Where a grown rule's new warnings sit: every file among `keys` (the files - * holding the rule now) that differs from `ref` and holds more of the rule - * than it did there, each of its warnings of the rule as `file:line message`. - * The count alone names no file, and the files holding the most are old ones; - * this names the file that tipped it. Null when `ref` is not at hand (a clone - * without it), and the report then keeps to the files holding the most. + * Locate warnings in changed files using the completed lint pass. Linting + * alternate file versions needs extra TypeScript programs and can exhaust + * the runner's heap before it reports the existing warnings. */ -export async function newWarningsOf({ root, rule, area, keys, lintText, current = null, ref = baseRef(), git = gitIn }) { - const changed = git(root, ['diff', '--name-only', '--relative', ref, '--', '.']); - if (changed === null) return null; - const differs = new Set(changed.split('\n').filter(Boolean)); +export function changedWarnings(results, root, changed, area, rule) { const found = []; - // Run typed linting for base versions in sequence. Linting many alternate - // versions in parallel can exhaust the CI runner's heap. - for (const key of keys.filter((key) => differs.has(key))) { - const path = join(root, key); - const lint = async (text) => { - try { - return ofRule(await lintText(text, path), rule, area); - } catch { - return null; - } - }; - const now = current ? ofRule([current.get(key)], rule, area) : await lint(readText(path)); - if (!now?.length) continue; - const before = git(root, ['show', `${ref}:./${key}`]); - const was = before === null ? [] : await lint(before); - if (was && now.length <= was.length) continue; - const note = was ? '' : ' (the base version could not be linted)'; - for (const message of now) found.push(`${key}:${message.line ?? 0} ${message.message}${note}`); + for (const result of results) { + const key = toKey(root, result.filePath); + if (!changed.has(key) || (area !== 'suppressed' && area !== areaOf(key))) continue; + const messages = + area === 'suppressed' ? (result.suppressedMessages ?? []) : result.messages.filter((m) => m.severity === 1); + for (const message of messages) { + if (ruleOf(message) === rule) found.push(`${key}:${message.line ?? 0} ${message.message}`); + } } return found; } -/** Prints, under the grown counts, the files and lines behind each one (see newWarningsOf). */ -async function reportNew(grown, { root, files, lintText, error, results }) { - if (!grown.length || !lintText) return; - const current = new Map(results.map((result) => [toKey(root, result.filePath), result])); - const previous = new Map(); - const lintOnce = (text, path) => { - if (!previous.has(path)) previous.set(path, lintText(text, path)); - return previous.get(path); - }; +function reportChanged(grown, { root, error, results }) { + if (!grown.length) return; + const ref = baseRef(); + const paths = gitIn(root, ['diff', '--name-only', '--relative', '-z', ref, '--', '.']); + if (paths === null) return; + const changed = new Set(paths.split('\0').filter(Boolean)); for (const { area, rule } of grown) { - const keys = Object.keys(files[area][rule]); - const lines = await newWarningsOf({ root, rule, area, keys, lintText: lintOnce, current }); - if (!lines?.length) continue; - error(`New ${rule} warning(s) in ${area}, against ${baseRef()}:`); + const lines = changedWarnings(results, root, changed, area, rule); + if (!lines.length) continue; + error(`${rule} warning(s) in changed ${area} files, against ${ref}:`); for (const line of lines) error(` ${line}`); } } -/** Lints one file's text with the client's own config, for newWarningsOf. */ -function lintTextClient(root) { - const eslint = new ESLint({ cwd: root }); - return (text, filePath) => eslint.lintText(text, { filePath }); -} - const sum = (map) => Object.values(map).reduce((a, b) => a + b, 0); /** @@ -234,7 +199,6 @@ export async function check({ baselinePath = join(root, 'scripts/eslint-baseline.json'), update = false, lint = lintClient, - lintText = lint === lintClient ? lintTextClient : null, log = console.log, error = console.error, }) { @@ -270,7 +234,7 @@ export async function check({ }); stale += fallen.length; } - await reportNew(grown, { root, files, lintText: lintText && lintText(root), error, results }); + if (lint === lintClient) reportChanged(grown, { root, error, results }); if (grown.length) { error( 'Fix the new warning rather than silencing it: an eslint-disable comment is counted under "suppressed". ' + diff --git a/client/src/mobile/screens/trip/roadtrip/MRtInfoSheet.tsx b/client/src/mobile/screens/trip/roadtrip/MRtInfoSheet.tsx index a98c74363..c78381235 100644 --- a/client/src/mobile/screens/trip/roadtrip/MRtInfoSheet.tsx +++ b/client/src/mobile/screens/trip/roadtrip/MRtInfoSheet.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react' +import { useState } from 'react' import { Car, ChevronLeft, Fuel, Route, Settings2, Zap } from 'lucide-react' import MSheet from '../../../components/MSheet' import MToggle from '../../../components/MToggle' @@ -52,7 +52,7 @@ export default function MRtInfoSheet({ planner, shell }: MTripSheetsProps) { const open = shell.sheet?.id === 'rtinfo' const tripId = planner.tripId const [editing, setEditing] = useState(false) - useEffect(() => { if (!open) setEditing(false) }, [open]) + if (!open && editing) setEditing(false) const unitSetting = useSettingsStore(s => s.settings.distance_unit) const unit: DistanceUnit = unitSetting === 'imperial' ? 'imperial' : 'metric' diff --git a/client/src/mobile/screens/trip/sheets/MExportSheet.tsx b/client/src/mobile/screens/trip/sheets/MExportSheet.tsx index 4f22d9a22..5669088a8 100644 --- a/client/src/mobile/screens/trip/sheets/MExportSheet.tsx +++ b/client/src/mobile/screens/trip/sheets/MExportSheet.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState, type ReactNode } from 'react' +import { useState, type ReactNode } from 'react' import { CalendarPlus, ChevronRight, FileDown, MapPinned, Share2, UserRound } from 'lucide-react' import MSheet from '../../../components/MSheet' import MIcsSubscribeSheet from './MIcsSubscribeSheet' @@ -22,12 +22,10 @@ export default function MExportSheet({ planner, shell }: MTripSheetsProps) { const dayNotes = useTripStore(s => s.dayNotes) const [subscribeOpen, setSubscribeOpen] = useState(false) const [formatsOpen, setFormatsOpen] = useState(false) - useEffect(() => { - if (!open) { - setSubscribeOpen(false) - setFormatsOpen(false) - } - }, [open]) + if (!open && (subscribeOpen || formatsOpen)) { + setSubscribeOpen(false) + setFormatsOpen(false) + } // The iOS Home Screen app hands the PDF to Safari, which lands here with ?export=pdf. useExportDeepLink(() => shell.openSheet('export')) // Fed the way the desktop dialog feeds it: the store's assignments, and the export diff --git a/client/src/mobile/screens/trip/tabs/MFileLinkSheet.tsx b/client/src/mobile/screens/trip/tabs/MFileLinkSheet.tsx index 7ef251d11..1dade0206 100644 --- a/client/src/mobile/screens/trip/tabs/MFileLinkSheet.tsx +++ b/client/src/mobile/screens/trip/tabs/MFileLinkSheet.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from 'react' +import { useRef, useState } from 'react' import { Check, ChevronLeft, Link2, Loader2, MapPin, Search, Ticket, TrainFront } from 'lucide-react' import type { LucideIcon } from 'lucide-react' import MIconBtn from '../../../components/MIconBtn' @@ -37,7 +37,12 @@ export default function MFileLinkSheet({ planner, file, onClose, onBack }: MFile const [busyKey, setBusyKey] = useState(null) const [query, setQuery] = useState('') - useEffect(() => { if (open) setQuery('') }, [open, file?.id]) + const fileId = file?.id ?? null + const [previousFileId, setPreviousFileId] = useState(fileId) + if (fileId !== previousFileId) { + setPreviousFileId(fileId) + if (fileId !== null) setQuery('') + } if (!shown) return diff --git a/client/tests/unit/scripts/eslint-ratchet.test.ts b/client/tests/unit/scripts/eslint-ratchet.test.ts index e8bce02dd..b2e45ce5a 100644 --- a/client/tests/unit/scripts/eslint-ratchet.test.ts +++ b/client/tests/unit/scripts/eslint-ratchet.test.ts @@ -1,13 +1,12 @@ -import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; -import { tmpdir } from 'node:os'; +import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { afterEach, describe, expect, it } from 'vitest'; import { areaCounts, + changedWarnings, check, inlineConfigsOf, lintClient, - newWarningsOf, tally, } from '../../../scripts/lib/eslint-warnings.mjs'; import { RatchetError } from '../../../scripts/lib/ratchet.mjs'; @@ -192,32 +191,15 @@ describe('lint:warnings', () => { expect(byKey['b.js'].inlineConfigs).toEqual([]); }); - it('names the file and line behind a grown count, measured against the base, and nothing without one', async () => { - const root = mkdtempSync(join(tmpdir(), 'lint-new-')); - try { - mkdirSync(join(root, 'src')); - writeFileSync(join(root, 'src/new.ts'), 'two'); - writeFileSync(join(root, 'src/old.ts'), 'one'); - // new.ts held none of the rule on the base, old.ts already held its one - const git = (_root: string, args: string[]) => - args[0] === 'diff' ? 'src/new.ts\nsrc/old.ts\n' : args[1]?.endsWith('old.ts') ? 'one' : 'none'; - const counts: Record = { two: 2, one: 1, none: 0 }; - const lintText = async (text: string) => [ - { - messages: Array.from({ length: counts[text] ?? 0 }, (_, i) => ({ - ruleId: 'no-empty', - severity: 1, - message: `w${i}`, - line: i + 1, - })), - }, - ]; - const keys = ['src/new.ts', 'src/old.ts', 'src/untouched.ts']; - const lines = await newWarningsOf({ root, rule: 'no-empty', area: 'src', keys, lintText, ref: 'base', git }); - expect(lines).toEqual(['src/new.ts:1 w0', 'src/new.ts:2 w1']); - expect(await newWarningsOf({ root, rule: 'no-empty', area: 'src', keys, lintText, git: () => null })).toBeNull(); - } finally { - rmSync(root, { recursive: true, force: true }); - } + it('locates the selected rule in changed files using the completed lint pass', () => { + const root = join('/r'); + const warning = { ...warn('no-empty', 'Empty block'), line: 5 }; + const results = [ + result(root, 'src/changed.ts', [warning, warn('no-console')]), + result(root, 'src/unchanged.ts', [warning]), + result(root, 'src/changed.test.ts', [warning]), + ]; + const changed = new Set(['src/changed.ts', 'src/changed.test.ts']); + expect(changedWarnings(results, root, changed, 'src', 'no-empty')).toEqual(['src/changed.ts:5 Empty block']); }); });