File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Expand file tree Collapse file tree 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'
99
1010const debugLog = debug ( 'lint-staged:loadConfig' )
1111
12+ const PACKAGE_JSON = 'package.json'
13+
1214/**
1315 * The list of files `lint-staged` will read configuration
1416 * from, in the declared order.
1517 */
1618export const searchPlaces = [
17- 'package.json' ,
19+ PACKAGE_JSON ,
1820 '.lintstagedrc' ,
1921 '.lintstagedrc.json' ,
2022 '.lintstagedrc.yaml' ,
@@ -27,7 +29,18 @@ export const searchPlaces = [
2729 'lint-staged.config.cjs' ,
2830]
2931
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+ }
3144
3245const yamlParse = ( path , content ) => YAML . parse ( content )
3346
Original file line number Diff line number Diff line change 1+ import fs from 'node:fs/promises'
12import path from 'node:path'
23
34import makeConsoleMock from 'consolemock'
@@ -177,4 +178,32 @@ describe('loadConfig', () => {
177178
178179 expect ( result ) . toMatchInlineSnapshot ( `{}` )
179180 } )
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+ } )
180209} )
You can’t perform that action at this time.
0 commit comments