File tree 2 files changed +44
-2
lines changed
2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -9,12 +9,14 @@ import { resolveConfig } from './resolveConfig.js'
9
9
10
10
const debugLog = debug ( 'lint-staged:loadConfig' )
11
11
12
+ const PACKAGE_JSON = 'package.json'
13
+
12
14
/**
13
15
* The list of files `lint-staged` will read configuration
14
16
* from, in the declared order.
15
17
*/
16
18
export const searchPlaces = [
17
- 'package.json' ,
19
+ PACKAGE_JSON ,
18
20
'.lintstagedrc' ,
19
21
'.lintstagedrc.json' ,
20
22
'.lintstagedrc.yaml' ,
@@ -27,7 +29,18 @@ export const searchPlaces = [
27
29
'lint-staged.config.cjs' ,
28
30
]
29
31
30
- const jsonParse = ( path , content ) => JSON . parse ( content )
32
+ const jsonParse = ( path , content ) => {
33
+ try {
34
+ return JSON . parse ( content )
35
+ } catch ( error ) {
36
+ if ( path . endsWith ( PACKAGE_JSON ) ) {
37
+ debugLog ( 'Ignoring invalid package file `%s` with content:\n%s' , path , content )
38
+ return undefined
39
+ }
40
+
41
+ throw error
42
+ }
43
+ }
31
44
32
45
const yamlParse = ( path , content ) => YAML . parse ( content )
33
46
Original file line number Diff line number Diff line change
1
+ import fs from 'node:fs/promises'
1
2
import path from 'node:path'
2
3
3
4
import makeConsoleMock from 'consolemock'
@@ -177,4 +178,32 @@ describe('loadConfig', () => {
177
178
178
179
expect ( result ) . toMatchInlineSnapshot ( `{}` )
179
180
} )
181
+
182
+ it ( 'should return empty object ".lintstagedrc.json" file is invalid' , async ( ) => {
183
+ expect . assertions ( 1 )
184
+
185
+ const configFile = path . join ( __dirname , '__mocks__' , '.lintstagedrc.json' )
186
+
187
+ await fs . writeFile ( configFile , '{' )
188
+
189
+ const result = await loadConfig ( { configPath : configFile } , logger )
190
+
191
+ expect ( result ) . toMatchInlineSnapshot ( `{}` )
192
+
193
+ await fs . rm ( configFile )
194
+ } )
195
+
196
+ it ( 'should return null config when package.json file is invalid' , async ( ) => {
197
+ expect . assertions ( 1 )
198
+
199
+ const configFile = path . join ( __dirname , '__mocks__' , 'package.json' )
200
+
201
+ await fs . writeFile ( configFile , '{' )
202
+
203
+ const { config } = await loadConfig ( { configPath : configFile } , logger )
204
+
205
+ expect ( config ) . toBeNull ( )
206
+
207
+ await fs . rm ( configFile )
208
+ } )
180
209
} )
You can’t perform that action at this time.
0 commit comments