.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / parse-json / index.js
1 'use strict';
2 const errorEx = require('error-ex');
3 const fallback = require('./vendor/parse');
4
5 function appendPosition(message) {
6         const posRe = / at (\d+:\d+) in/;
7         const numbers = posRe.exec(message);
8         return message.replace(posRe, ' in') + ':' + numbers[1];
9 }
10
11 const JSONError = errorEx('JSONError', {
12         fileName: errorEx.append('in %s'),
13         appendPosition: {
14                 message: (shouldAppend, original) => {
15                         if (shouldAppend) {
16                                 original[0] = appendPosition(original[0]);
17                         }
18                         return original;
19                 }
20         }
21 });
22
23 module.exports = (input, reviver, filename) => {
24         if (typeof reviver === 'string') {
25                 filename = reviver;
26                 reviver = null;
27         }
28
29         try {
30                 try {
31                         return JSON.parse(input, reviver);
32                 } catch (err) {
33                         fallback.parse(input, {
34                                 mode: 'json',
35                                 reviver
36                         });
37
38                         throw err;
39                 }
40         } catch (err) {
41                 const jsonErr = new JSONError(err);
42
43                 if (filename) {
44                         jsonErr.fileName = filename;
45                         jsonErr.appendPosition = true;
46                 }
47
48                 throw jsonErr;
49         }
50 };