Skip to content

Commit

Permalink
feat: strf-9303 Replaced jsonlint with parse-json
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc committed Sep 13, 2021
1 parent 7283549 commit b5f16db
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 83 deletions.
4 changes: 2 additions & 2 deletions lib/json-lint.js → lib/parse-json.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const jsonLint = require('jsonlint');
const parseJson = require('parse-json');

module.exports = {
parse(jsonString, file = '') {
try {
return jsonLint.parse(jsonString);
return parseJson(jsonString);
} catch (e) {
throw new Error(`${file} - ${e.message}`);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/json-lint.spec.js → lib/parse-json.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const jsonLint = require('./json-lint');
const { parse } = require('./parse-json');

describe('json-lint', () => {
const badJsonFilename = '/path/to/badfile.json';
Expand All @@ -7,15 +7,15 @@ describe('json-lint', () => {

it('should add file name to error', () => {
const throws = () => {
jsonLint.parse(badJson, badJsonFilename);
parse(badJson, badJsonFilename);
};

expect(throws).toThrow(Error, file);
});

it('should not need a file name', () => {
const throws = () => {
jsonLint.parse(badJson);
parse(badJson);
};

expect(throws).toThrow(Error, !file);
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/fsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

const fs = require('fs');
const recursiveReadDir = require('recursive-readdir');
const jsonLint = require('../json-lint');
const { parse } = require('../parse-json');

/**
* @param {string} filePath
* @returns {Promise<Object>} - parsed JSON content of the file
*/
async function parseJsonFile(filePath) {
const contentStr = await fs.promises.readFile(filePath, { encoding: 'utf-8' });
// We use jsonLint.parse instead of JSON.parse because jsonLint throws errors with better explanations what is wrong
return jsonLint.parse(contentStr, filePath);
// We use parse-json instead of JSON.parse because it throws errors with better explanations what is wrong
return parse(contentStr, filePath);
}

module.exports = {
Expand Down
107 changes: 33 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@
"graceful-fs": "^4.2.4",
"image-size": "^0.9.1",
"inquirer": "^7.3.3",
"jsonlint": "^1.6.3",
"lodash": "^4.17.20",
"memory-cache": "^0.2.0",
"npm-which": "^3.0.1",
"object-to-spawn-args": "^2.0.0",
"parse-json": "^5.2.0",
"progress": "^2.0.3",
"recursive-readdir": "^2.2.2",
"semver": "^7.3.2",
Expand Down

0 comments on commit b5f16db

Please sign in to comment.