Skip to content

Commit

Permalink
fix: only throw if no configurations were found
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iiroj committed Jan 19, 2022
1 parent 2604ac7 commit 36b9546
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/getConfigGroups.js
Expand Up @@ -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
Expand All @@ -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
}
15 changes: 15 additions & 0 deletions test/getConfigGroups.spec.js
Expand Up @@ -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'] },
})
})
})

0 comments on commit 36b9546

Please sign in to comment.