11#!/usr/bin/env bun
22// Fails when a GUI package file this branch adds or edits has any oxlint problem, including warn-level rules (anti-slop):
33// a change that touches a file leaves the whole file clean. Untouched files and other packages are not checked.
4- // Usage: bun script/lint-changed.ts [base-ref] (default: merge base with upstream/dev, origin/dev, or dev)
4+ // Usage: bun script/lint-changed.ts [base-ref] [--committed]
5+ // (default base: merge base with upstream/dev, origin/dev, or dev)
6+ // --committed (the pre-push hook): only files changed in commits since the base, so uncommitted or untracked work in
7+ // progress doesn't block a push it isn't part of, and a clone without a dev ref skips the check instead of failing.
58import { $ } from "bun"
69
710type Diagnostic = {
@@ -11,10 +14,13 @@ type Diagnostic = {
1114 labels : { span : { line : number ; column : number } } [ ]
1215}
1316
14- const base = await resolveBase ( process . argv [ 2 ] ?? process . env . LINT_BASE )
17+ const committed = process . argv . includes ( "--committed" )
18+ const base = await resolveBase ( process . argv . slice ( 2 ) . find ( ( arg ) => ! arg . startsWith ( "--" ) ) ?? process . env . LINT_BASE )
1519// -M100% reports only exact moves as renames, so a moved file is linted only once its content changes.
16- const tracked = ( await $ `git diff --name-only -M100% --diff-filter=ACM ${ base } ` . text ( ) ) . split ( "\n" )
17- const untracked = ( await $ `git ls-files --others --exclude-standard` . text ( ) ) . split ( "\n" )
20+ const tracked = (
21+ await $ `git diff --name-only -M100% --diff-filter=ACM ${ base } ${ committed ? [ "HEAD" ] : [ ] } ` . text ( )
22+ ) . split ( "\n" )
23+ const untracked = committed ? [ ] : ( await $ `git ls-files --others --exclude-standard` . text ( ) ) . split ( "\n" )
1824// The packages .oxlintrc.json applies the anti-slop rules to.
1925const packages = [
2026 "packages/app/" ,
@@ -39,7 +45,9 @@ if (files.length === 0) {
3945}
4046
4147// In chunks: a renamed package lists hundreds of files, more than one Windows command line holds.
42- const chunks = Array . from ( { length : Math . ceil ( files . length / 100 ) } , ( _ , index ) => files . slice ( index * 100 , index * 100 + 100 ) )
48+ const chunks = Array . from ( { length : Math . ceil ( files . length / 100 ) } , ( _ , index ) =>
49+ files . slice ( index * 100 , index * 100 + 100 ) ,
50+ )
4351const report : { diagnostics : Diagnostic [ ] } = { diagnostics : [ ] }
4452
4553for ( const chunk of chunks ) {
@@ -66,6 +74,10 @@ async function resolveBase(explicit: string | undefined) {
6674 refs . map ( async ( ref ) => ( await $ `git rev-parse --verify --quiet ${ ref } ` . nothrow ( ) . quiet ( ) ) . exitCode === 0 ) ,
6775 )
6876 const ref = refs . find ( ( _ , index ) => found [ index ] )
77+ if ( ! ref && committed ) {
78+ console . warn ( "lint:changed: no upstream/dev, origin/dev or dev ref found; skipping" )
79+ process . exit ( 0 )
80+ }
6981 if ( ! ref ) throw new Error ( "lint:changed: no v2 ref found; pass a base ref" )
7082 return ( await $ `git merge-base HEAD ${ ref } ` . text ( ) ) . trim ( )
7183}
0 commit comments