Skip to content

Commit 5894faa

Browse files
committed
fix: disallow passing object is argument when json content is null
1 parent 33fc969 commit 5894faa

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type LoadJsonFileOptions = Required<Pick<Options, 'encoding' | 'tabSize'>>
5959
const loadJsonFile = async (filePath: string, { encoding, tabSize }: LoadJsonFileOptions) => {
6060
const contents = stripBom(await fs.promises.readFile(filePath, encoding))
6161
return {
62-
json: parseJson(contents, filePath),
62+
json: parseJson(contents, filePath) as JsonRoot,
6363
indent: tabSize === 'preserve' ? detectIndent(contents).indent : tabSize === 'hard' ? '\t' : tabSize === null ? undefined : ' '.repeat(tabSize),
6464
}
6565
}
@@ -82,7 +82,7 @@ export const modifyJsonFile: ModifyJsonFileGenericFunction = async (path, modify
8282
if (typeof modifyFields === 'function') {
8383
json = await (modifyFields as any)(json)
8484
} else {
85-
if (typeof json !== 'object' || Array.isArray(json)) throw new TypeError(`${path}: Root type is not object. Only callback can be used`)
85+
if (typeof json !== 'object' || Array.isArray(json) || json === null) throw new TypeError(`${path}: Root type is not object. Only callback can be used`)
8686
for (const parts of Object.entries(modifyFields)) {
8787
// todo fix typescript types
8888
const name = parts[0] as string

0 commit comments

Comments
 (0)