Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/json-handler.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
'use strict';

const fs = require('node:fs');
const { format } = require('node:util');

const jsonLint = require('json-lint');
const stripJsonComments = require('strip-json-comments');
const stripJsonCommentsRaw = require('strip-json-comments');

const stripJsonComments = stripJsonCommentsRaw?.default || stripJsonCommentsRaw;

// eslint-disable-next-line n/no-deprecated-api
require.extensions['.json'] = function(module, filename) {
let content = fs.readFileSync(filename, 'utf8');
content = stripJsonComments(content);
const lint = jsonLint(content);

// Only strip comments if file likely contains them
if (content.includes('//') || content.includes('/*')) {
content = stripJsonComments(content);
}

try {
module.exports = JSON.parse(content);
} catch {
let errorText;
if (lint.error) {
errorText = format('%s (%s:%s:%s)', lint.error, filename, lint.line, lint.character);
throw new SyntaxError(errorText, filename, lint.line);
}
throw new SyntaxError(errorText, filename);
} catch (err) {
// Add filename to error for better debugging
err.message = `${err.message} in ${filename}`;
throw err;
}
};
Loading
Loading