Repository: okonet/lint-staged
Pull request #1088 (2022-01-19 06:10:11)

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.

Fixes https://github.com/okonet/lint-staged/issues/1087

codecov[bot]: # [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/1088?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov) Report
> Merging [#1088](https://codecov.io/gh/okonet/lint-staged/pull/1088?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov) (bd17fb2) into [master](https://codecov.io/gh/okonet/lint-staged/commit/2604ac7dbca20194b6b8a417335166ccf311a769?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov) (2604ac7) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/okonet/lint-staged/pull/1088/graphs/tree.svg?width=650&height=150&src=pr&token=LSoEgsjk4O&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov)](https://codecov.io/gh/okonet/lint-staged/pull/1088?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov)

```diff
@@            Coverage Diff            @@
##            master     #1088   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           23        23           
  Lines          623       624    +1     
  Branches       166       167    +1     
=========================================
+ Hits           623       624    +1     
```


| [Impacted Files](https://codecov.io/gh/okonet/lint-staged/pull/1088?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov) | Coverage Δ | |
|---|---|---|
| [lib/getConfigGroups.js](https://codecov.io/gh/okonet/lint-staged/pull/1088/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov#diff-bGliL2dldENvbmZpZ0dyb3Vwcy5qcw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/okonet/lint-staged/pull/1088?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/1088?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov). Last update [2604ac7...bd17fb2](https://codecov.io/gh/okonet/lint-staged/pull/1088?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrey+Okonetchnikov).

iiroj: Ping @okonet 

Patch:
diff --git a/lib/getConfigGroups.js b/lib/getConfigGroups.js
index be0da76..c692b84 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
 }


Test patch:
diff --git a/test/getConfigGroups.spec.js b/test/getConfigGroups.spec.js
index 29af85a..3607659 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'] },
+    })
+  })
 })