Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Fix unparsable JSON #105

Merged
merged 2 commits into from Nov 3, 2020
Merged

Fix unparsable JSON #105

merged 2 commits into from Nov 3, 2020

Conversation

BarryThePenguin
Copy link
Contributor

@BarryThePenguin BarryThePenguin commented Nov 2, 2020

When package.json contains a syntax error, findPackage() is called repetitively, as base is never null.

This uses https://github.com/sindresorhus/pkg-up, which does return null if a package.json file isn't found

Fixes #92


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


const pkg = await loadPkg(pkgPath);

if (pkg.xo === false && base !== null) {
return findPackage(path.join(base, '..'));
Copy link
Contributor Author

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

Comment on lines +13 to +14
const newBase = path.resolve(path.dirname(pkgPath), '..');
return getPackagePath(newBase);
Copy link
Contributor Author

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.

"devDependencies": {
"xo": "*"
}
}
Copy link
Contributor Author

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

@sindresorhus sindresorhus changed the title Fix unparsable json Fix unparsable JSON Nov 3, 2020
@sindresorhus sindresorhus merged commit 2ba541d into sindresorhus:master Nov 3, 2020
@BarryThePenguin BarryThePenguin deleted the fix-unparsable-json branch November 3, 2020 19:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Atom Incredibly slowed down atom if the package.json is unparsable when linter-xo is enabled
2 participants