Skip to content

Commit 09372d9

Browse files
poradafengmk2
andauthored
fix(staged): Include additional supported config types from lint-staged (#1263)
This PR carries over additional supported config types from `lint-staged` to `vp staged`. ### The Reason `vite-plus@0.1.15` broke type checks in my projects ([example](https://github.com/standard-config/oxlint/actions/runs/23837790752/job/69485643872)). Turns out it was because I was using [`lint-staged` commands](https://github.com/standard-config/oxlint/blob/254b7997a49445f5feb3800f05d3a78c6a4b3b53/vite.config.ts#L20-L29) that didn’t match the original `staged` config type. For some reason, these weren’t flagged before the update (the commands worked and keep working fine at runtime). I can confirm this fixes the type issues for me locally, but I’d appreciate guidance on adding more thorough tests for these changes. --------- Co-authored-by: MK <fengmk2@gmail.com>
1 parent d4563bf commit 09372d9

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { assertType, describe, it } from 'vitest';
2+
3+
import type { StagedConfig } from '../../staged-config.js';
4+
5+
describe('StagedConfig type', () => {
6+
it('accepts string commands', () => {
7+
assertType<StagedConfig>({
8+
'*.ts': 'eslint --fix',
9+
});
10+
});
11+
12+
it('accepts arrays of string commands', () => {
13+
assertType<StagedConfig>({
14+
'*.ts': ['eslint --fix', 'prettier --write'],
15+
});
16+
});
17+
18+
it('accepts sync generate task functions', () => {
19+
assertType<StagedConfig>({
20+
'*.ts': (files: readonly string[]) => `eslint ${files.join(' ')}`,
21+
});
22+
});
23+
24+
it('accepts async generate task functions', () => {
25+
assertType<StagedConfig>({
26+
'*.ts': async (files: readonly string[]) => `eslint ${files.join(' ')}`,
27+
});
28+
});
29+
30+
it('accepts mixed arrays of strings and functions', () => {
31+
assertType<StagedConfig>({
32+
'*': [
33+
() => 'pnpm install --ignore-scripts',
34+
() => 'pnpm test',
35+
'oxlint --deny-warnings --fix',
36+
'prettier --ignore-unknown --write',
37+
],
38+
});
39+
});
40+
41+
it('accepts task function objects', () => {
42+
assertType<StagedConfig>({
43+
'*.ts': {
44+
title: 'Run eslint',
45+
task: (files: readonly string[]) => {
46+
void files;
47+
},
48+
},
49+
});
50+
});
51+
52+
it('accepts a top-level generate task function', () => {
53+
assertType<StagedConfig>((files: readonly string[]) => [`eslint ${files.join(' ')}`]);
54+
});
55+
});

packages/cli/src/staged-config.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
export type StagedConfig = Record<string, string | string[]>;
1+
// Copied from lint-staged@16.4.0 (node_modules/lint-staged/lib/index.d.ts)
2+
// TODO: Re-export directly from lint-staged once we can bundle .d.ts files (#744).
3+
4+
type SyncGenerateTask = (stagedFileNames: readonly string[]) => string | string[];
5+
6+
type AsyncGenerateTask = (stagedFileNames: readonly string[]) => Promise<string | string[]>;
7+
8+
type GenerateTask = SyncGenerateTask | AsyncGenerateTask;
9+
10+
type TaskFunction = {
11+
title: string;
12+
task: (stagedFileNames: readonly string[]) => void | Promise<void>;
13+
};
14+
15+
export type StagedConfig =
16+
| Record<string, string | TaskFunction | GenerateTask | (string | GenerateTask)[]>
17+
| GenerateTask;

0 commit comments

Comments
 (0)