From 36b9546dda5ca24174b519ce6d132f31077b093b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20Ja=CC=88ppinen?= Date: Wed, 19 Jan 2022 08:09:34 +0200 Subject: [PATCH] fix: only throw if no configurations were found Previously lint-staged would throw if any staged file was missing a config, but it should be enough that at least one file has a matching config. --- lib/getConfigGroups.js | 11 +++++++---- test/getConfigGroups.spec.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/getConfigGroups.js b/lib/getConfigGroups.js index be0da7668..c692b84b3 100644 --- a/lib/getConfigGroups.js +++ b/lib/getConfigGroups.js @@ -56,10 +56,7 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge Object.entries(filesByDir).map(([dir, files]) => { // Discover config from the base directory of the file return loadConfig({ cwd: dir }, logger).then(({ config, filepath }) => { - if (!config) { - logger.error(`${ConfigNotFoundError.message}.`) - throw ConfigNotFoundError - } + if (!config) return if (filepath in configGroups) { // Re-use cached config and skip validation @@ -73,5 +70,11 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge }) ) + // Throw if no configurations were found + if (Object.keys(configGroups).length === 0) { + logger.error(`${ConfigNotFoundError.message}.`) + throw ConfigNotFoundError + } + return configGroups } diff --git a/test/getConfigGroups.spec.js b/test/getConfigGroups.spec.js index 29af85aa7..3607659f0 100644 --- a/test/getConfigGroups.spec.js +++ b/test/getConfigGroups.spec.js @@ -53,4 +53,19 @@ describe('getConfigGroups', () => { '/deeper/.lintstagedrc.json': { config, files: ['/deeper/foo.js', '/even/deeper/foo.js'] }, }) }) + + it('should find config for one file, and not care about other', async () => { + // '/foo.js' + loadConfig.mockResolvedValueOnce({}) + // '/deeper/foo.js' + loadConfig.mockResolvedValueOnce({ config, filepath: '/deeper/.lintstagedrc.json' }) + + const configGroups = await getConfigGroups({ + files: ['/foo.js', '/deeper/foo.js'], + }) + + expect(configGroups).toEqual({ + '/deeper/.lintstagedrc.json': { config, files: ['/deeper/foo.js'] }, + }) + }) })