Skip to content

Commit 8acbeac

Browse files
committedNov 4, 2021
Require Node.js 12.20 and move to ESM
1 parent b778e2e commit 8acbeac

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed
 

‎.github/workflows/main.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
16-
- 8
1716
steps:
1817
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
2019
with:
2120
node-version: ${{ matrix.node-version }}
2221
- run: npm install

‎index.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
'use strict';
2-
const errorEx = require('error-ex');
3-
const fallback = require('json-parse-even-better-errors');
1+
import {createRequire} from 'node:module';
2+
import errorEx from 'error-ex';
3+
import fallback from 'json-parse-even-better-errors';
4+
import {codeFrameColumns} from '@babel/code-frame';
5+
6+
const require = createRequire(import.meta.url);
47
const {default: LinesAndColumns} = require('lines-and-columns');
5-
const {codeFrameColumns} = require('@babel/code-frame');
68

7-
const JSONError = errorEx('JSONError', {
9+
export const JSONError = errorEx('JSONError', {
810
fileName: errorEx.append('in %s'),
9-
codeFrame: errorEx.append('\n\n%s\n')
11+
codeFrame: errorEx.append('\n\n%s\n'),
1012
});
1113

12-
const parseJson = (string, reviver, filename) => {
14+
export default function parseJson(string, reviver, filename) {
1315
if (typeof reviver === 'string') {
1416
filename = reviver;
1517
reviver = null;
@@ -39,16 +41,12 @@ const parseJson = (string, reviver, filename) => {
3941
const codeFrame = codeFrameColumns(
4042
string,
4143
{start: {line: location.line + 1, column: location.column + 1}},
42-
{highlightCode: true}
44+
{highlightCode: true},
4345
);
4446

4547
jsonError.codeFrame = codeFrame;
4648
}
4749

4850
throw jsonError;
4951
}
50-
};
51-
52-
parseJson.JSONError = JSONError;
53-
54-
module.exports = parseJson;
52+
}

‎package.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && nyc ava"
@@ -32,14 +34,14 @@
3234
"string"
3335
],
3436
"dependencies": {
35-
"@babel/code-frame": "^7.0.0",
36-
"error-ex": "^1.3.1",
37-
"json-parse-even-better-errors": "^2.3.0",
37+
"@babel/code-frame": "^7.16.0",
38+
"error-ex": "^1.3.2",
39+
"json-parse-even-better-errors": "^2.3.1",
3840
"lines-and-columns": "^1.1.6"
3941
},
4042
"devDependencies": {
41-
"ava": "^1.4.1",
42-
"nyc": "^14.1.1",
43-
"xo": "^0.24.0"
43+
"ava": "^3.15.0",
44+
"nyc": "^15.1.0",
45+
"xo": "^0.46.4"
4446
}
4547
}

‎readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
55
## Install
66

7-
```
8-
$ npm install parse-json
7+
```sh
8+
npm install parse-json
99
```
1010

1111
## Usage
1212

1313
```js
14-
const parseJson = require('parse-json');
14+
import parseJson from 'parse-json';
1515

1616
const json = '{\n\t"foo": true,\n}';
1717

@@ -88,7 +88,7 @@ Prescribes how the value originally produced by parsing is transformed, before b
8888

8989
Type: `string`
9090

91-
Filename displayed in the error message.
91+
The filename displayed in the error message.
9292

9393
### parseJson.JSONError
9494

‎test.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import parseJson from '.';
2+
import parseJson, {JSONError} from './index.js';
33

44
const jsonErrorRegex = /Unexpected token "}".*in foo\.json/;
55

@@ -10,7 +10,7 @@ test('main', t => {
1010
parseJson('{\n\t"foo": true,\n}');
1111
}, {
1212
name: 'JSONError',
13-
message: /Unexpected token "}"/
13+
message: /Unexpected token "}"/,
1414
});
1515

1616
t.throws(() => {
@@ -20,11 +20,15 @@ test('main', t => {
2020
error.fileName = 'foo.json';
2121
throw error;
2222
}
23-
}, jsonErrorRegex);
23+
}, {
24+
message: jsonErrorRegex,
25+
});
2426

2527
t.throws(() => {
2628
parseJson('{\n\t"foo": true,\n}', 'foo.json');
27-
}, jsonErrorRegex);
29+
}, {
30+
message: jsonErrorRegex,
31+
});
2832

2933
t.throws(() => {
3034
try {
@@ -33,13 +37,15 @@ test('main', t => {
3337
error.fileName = 'foo.json';
3438
throw error;
3539
}
36-
}, jsonErrorRegex);
40+
}, {
41+
message: jsonErrorRegex,
42+
});
3743
});
3844

3945
test('throws exported error error', t => {
4046
t.throws(() => {
4147
parseJson('asdf');
4248
}, {
43-
instanceOf: parseJson.JSONError
49+
instanceOf: JSONError,
4450
});
4551
});

0 commit comments

Comments
 (0)
Please sign in to comment.