Sha256: d4b850bd6931b55ac55e48936efc5e3ce75e601456857ec6657c40a78cbddbe7

Contents?: true

Size: 1.24 KB

Versions: 24

Compression:

Stored size: 1.24 KB

Contents

'use strict';
const errorEx = require('error-ex');
const fallback = require('json-parse-better-errors');
const {default: LinesAndColumns} = require('lines-and-columns');
const {codeFrameColumns} = require('@babel/code-frame');

const JSONError = errorEx('JSONError', {
	fileName: errorEx.append('in %s'),
	codeFrame: errorEx.append('\n\n%s\n')
});

module.exports = (string, reviver, filename) => {
	if (typeof reviver === 'string') {
		filename = reviver;
		reviver = null;
	}

	try {
		try {
			return JSON.parse(string, reviver);
		} catch (error) {
			fallback(string, reviver);
			throw error;
		}
	} catch (error) {
		error.message = error.message.replace(/\n/g, '');
		const indexMatch = error.message.match(/in JSON at position (\d+) while parsing near/);

		const jsonError = new JSONError(error);
		if (filename) {
			jsonError.fileName = filename;
		}

		if (indexMatch && indexMatch.length > 0) {
			const lines = new LinesAndColumns(string);
			const index = Number(indexMatch[1]);
			const location = lines.locationForIndex(index);

			const codeFrame = codeFrameColumns(
				string,
				{start: {line: location.line + 1, column: location.column + 1}},
				{highlightCode: true}
			);

			jsonError.codeFrame = codeFrame;
		}

		throw jsonError;
	}
};

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
trusty-cms-5.0.7 node_modules/parse-json/index.js
trusty-cms-5.0.6 node_modules/parse-json/index.js
trusty-cms-5.0.5 node_modules/parse-json/index.js
trusty-cms-5.0.4 node_modules/parse-json/index.js
trusty-cms-5.0.3 node_modules/parse-json/index.js
trusty-cms-5.0.2 node_modules/parse-json/index.js
trusty-cms-5.0.1 node_modules/parse-json/index.js
trusty-cms-4.3.5 node_modules/parse-json/index.js
trusty-cms-5.0.0 node_modules/parse-json/index.js
trusty-cms-4.3.4 node_modules/parse-json/index.js
trusty-cms-4.3.3 node_modules/parse-json/index.js
trusty-cms-4.3.2 node_modules/parse-json/index.js
trusty-cms-4.3.1 node_modules/parse-json/index.js
trusty-cms-4.3 node_modules/parse-json/index.js
trusty-cms-4.2.3 node_modules/parse-json/index.js
trusty-cms-4.2.2 node_modules/parse-json/index.js
trusty-cms-4.2.1 node_modules/parse-json/index.js
trusty-cms-4.2 node_modules/parse-json/index.js
trusty-cms-4.1.9 node_modules/parse-json/index.js
trusty-cms-4.1.8 node_modules/parse-json/index.js