Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/cli/src/__tests__/staged/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { assertType, describe, it } from 'vitest';

import type { StagedConfig } from '../../staged-config.js';

describe('StagedConfig type', () => {
it('accepts string commands', () => {
assertType<StagedConfig>({
'*.ts': 'eslint --fix',
});
});

it('accepts arrays of string commands', () => {
assertType<StagedConfig>({
'*.ts': ['eslint --fix', 'prettier --write'],
});
});

it('accepts sync generate task functions', () => {
assertType<StagedConfig>({
'*.ts': (files: readonly string[]) => `eslint ${files.join(' ')}`,
});
});

it('accepts async generate task functions', () => {
assertType<StagedConfig>({
'*.ts': async (files: readonly string[]) => `eslint ${files.join(' ')}`,
});
});

it('accepts mixed arrays of strings and functions', () => {
assertType<StagedConfig>({
'*': [
() => 'pnpm install --ignore-scripts',
() => 'pnpm test',
'oxlint --deny-warnings --fix',
'prettier --ignore-unknown --write',
],
});
});

it('accepts task function objects', () => {
assertType<StagedConfig>({
'*.ts': {
title: 'Run eslint',
task: (files: readonly string[]) => {
void files;
},
},
});
});

it('accepts a top-level generate task function', () => {
assertType<StagedConfig>((files: readonly string[]) => [`eslint ${files.join(' ')}`]);
});
});
18 changes: 17 additions & 1 deletion packages/cli/src/staged-config.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export type StagedConfig = Record<string, string | string[]>;
// Copied from lint-staged@16.4.0 (node_modules/lint-staged/lib/index.d.ts)
// TODO: Re-export directly from lint-staged once we can bundle .d.ts files (#744).

type SyncGenerateTask = (stagedFileNames: readonly string[]) => string | string[];

type AsyncGenerateTask = (stagedFileNames: readonly string[]) => Promise<string | string[]>;

type GenerateTask = SyncGenerateTask | AsyncGenerateTask;

type TaskFunction = {
title: string;
task: (stagedFileNames: readonly string[]) => void | Promise<void>;
};

export type StagedConfig =
| Record<string, string | TaskFunction | GenerateTask | (string | GenerateTask)[]>
| GenerateTask;
Loading