Fix unparsable JSON #105
Fix unparsable JSON #105
Conversation
const pkg = await loadPkg(pkgPath); | ||
|
||
if (pkg.xo === false && base !== null) { | ||
return findPackage(path.join(base, '..')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path.join(base, '..')
was never null
, so if a package.json
wasn't found, findPackage('/')
would be called repeatedly
const newBase = path.resolve(path.dirname(pkgPath), '..'); | ||
return getPackagePath(newBase); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calculates a new base
path from the directory above any package.json
file found.
This way, pkg-up
will handle most of the directory traversal. If it finds a package.json
file with pkg.xo === false
it continues traversing up.
When /
is reached, it returns null
.
mocks/unparsable/package.json
Outdated
"devDependencies": { | ||
"xo": "*" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like it's feasible to have a test for this situation.. Based on CI
> xo --ignore='mocks/**/*'
JSONError: JSON Error in /home/travis/build/xojs/atom-linter-xo/mocks/unparsable/package.json:
1 | {
> 2 | "name": "unparsable",,,,
| ^
3 | "devDependencies": {
4 | "xo": "*"
5 | }
Unexpected token "," (0x2C) in JSON at position 25 while parsing near "...name\": \"unparsable\",,,,\n \"devDependenci..."
1 | {
> 2 | "name": "unparsable",,,,
| ^
3 | "devDependencies": {
4 | "xo": "*"
5 | }
at module.exports (/home/travis/build/xojs/atom-linter-xo/node_modules/cosmiconfig/node_modules/parse-json/index.js:29:21)
at Object.loadJson (/home/travis/build/xojs/atom-linter-xo/node_modules/cosmiconfig/dist/loaders.js:28:20)
at Explorer.loadPackageProp (/home/travis/build/xojs/atom-linter-xo/node_modules/cosmiconfig/dist/ExplorerBase.js:82:44)
at Explorer.loadFileContent (/home/travis/build/xojs/atom-linter-xo/node_modules/cosmiconfig/dist/Explorer.js:84:32)
at Explorer.createCosmiconfigResult (/home/travis/build/xojs/atom-linter-xo/node_modules/cosmiconfig/dist/Explorer.js:89:36)
at runLoad (/home/travis/build/xojs/atom-linter-xo/node_modules/cosmiconfig/dist/Explorer.js:103:33)
at async cacheWrapper (/home/travis/build/xojs/atom-linter-xo/node_modules/cosmiconfig/dist/cacheWrapper.js:16:18)
at async Promise.all (index 4)
at async Object.lintFiles (/home/travis/build/xojs/atom-linter-xo/node_modules/xo/index.js:100:23)
at async /home/travis/build/xojs/atom-linter-xo/node_modules/xo/cli-main.js:185:18
When
package.json
contains a syntax error,findPackage()
is called repetitively, asbase
is nevernull
.This uses https://github.com/sindresorhus/pkg-up, which does return
null
if apackage.json
file isn't foundFixes #92
IssueHunt Summary
Referenced issues
This pull request has been submitted to: