From 6f2d08acfba2a0a993f88182d920d930cffa082f Mon Sep 17 00:00:00 2001 From: luojiyin Date: Sat, 22 Aug 2026 15:55:05 +0800 Subject: [PATCH] fix(cli): avoid executing lint setup when no input is provided --- __tests__/cli.spec.ts | 14 ++++++++++++++ src/lint-md.ts | 12 +++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/__tests__/cli.spec.ts b/__tests__/cli.spec.ts index 35b2e84..f7f84ac 100644 --- a/__tests__/cli.spec.ts +++ b/__tests__/cli.spec.ts @@ -120,6 +120,20 @@ describe("cli tests", () => { expect(mockExit).toHaveBeenCalledWith(0); }); + test("skips lint setup when no input is provided", async () => { + const getLintConfig = jest.fn(); + jest.doMock("../src/utils/configure", () => ({ getLintConfig })); + const mockExit = jest.spyOn(process, "exit").mockImplementation((() => { + throw new Error("process.exit"); + }) as never); + const { main } = require("../src/lint-md"); + + await expect(main(["node", "lint-md"])).rejects.toThrow("process.exit"); + + expect(getLintConfig).not.toHaveBeenCalled(); + expect(mockExit).toHaveBeenCalledWith(0); + }); + test("rejects file arguments combined with --stdin", async () => { const runFileLint = jest.fn().mockResolvedValue({ exitCode: 0 }); const runStdinLint = jest.fn().mockReturnValue({ exitCode: 0 }); diff --git a/src/lint-md.ts b/src/lint-md.ts index 18ca197..ce7d896 100644 --- a/src/lint-md.ts +++ b/src/lint-md.ts @@ -76,6 +76,13 @@ export const createProgram = (): Command => { const isFixMode = Boolean(fix); const isDev = Boolean(dev); + // No input at all: show help before touching config, threads, or + // stdin. A broken .lintmdrc must not turn bare "lint-md" into a + // config error. + if (!files.length && !stdin) { + program.help(); + } + // Fail before touching stdin or the file list so neither input mode // starts on a command that mixes both. if (stdin && files.length > 0) { @@ -130,11 +137,6 @@ export const createProgram = (): Command => { export const main = async (argv: string[] = process.argv): Promise => { const program = createProgram(); await program.parseAsync(argv); - - const isStdin = argv.includes("--stdin") || argv.includes("-i"); - if (!program.args.length && !isStdin) { - program.help(); - } }; export const runCli = (argv: string[] = process.argv): void => {