Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 4, 2021
1 parent b778e2e commit 8acbeac
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
24 changes: 11 additions & 13 deletions index.js
@@ -1,15 +1,17 @@
'use strict';
const errorEx = require('error-ex');
const fallback = require('json-parse-even-better-errors');
import {createRequire} from 'node:module';
import errorEx from 'error-ex';
import fallback from 'json-parse-even-better-errors';
import {codeFrameColumns} from '@babel/code-frame';

const require = createRequire(import.meta.url);
const {default: LinesAndColumns} = require('lines-and-columns');
const {codeFrameColumns} = require('@babel/code-frame');

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

const parseJson = (string, reviver, filename) => {
export default function parseJson(string, reviver, filename) {
if (typeof reviver === 'string') {
filename = reviver;
reviver = null;
Expand Down Expand Up @@ -39,16 +41,12 @@ const parseJson = (string, reviver, filename) => {
const codeFrame = codeFrameColumns(
string,
{start: {line: location.line + 1, column: location.column + 1}},
{highlightCode: true}
{highlightCode: true},
);

jsonError.codeFrame = codeFrame;
}

throw jsonError;
}
};

parseJson.JSONError = JSONError;

module.exports = parseJson;
}
16 changes: 9 additions & 7 deletions package.json
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && nyc ava"
Expand All @@ -32,14 +34,14 @@
"string"
],
"dependencies": {
"@babel/code-frame": "^7.0.0",
"error-ex": "^1.3.1",
"json-parse-even-better-errors": "^2.3.0",
"@babel/code-frame": "^7.16.0",
"error-ex": "^1.3.2",
"json-parse-even-better-errors": "^2.3.1",
"lines-and-columns": "^1.1.6"
},
"devDependencies": {
"ava": "^1.4.1",
"nyc": "^14.1.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"nyc": "^15.1.0",
"xo": "^0.46.4"
}
}
8 changes: 4 additions & 4 deletions readme.md
Expand Up @@ -4,14 +4,14 @@
## Install

```
$ npm install parse-json
```sh
npm install parse-json
```

## Usage

```js
const parseJson = require('parse-json');
import parseJson from 'parse-json';

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

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

Type: `string`

Filename displayed in the error message.
The filename displayed in the error message.

### parseJson.JSONError

Expand Down
18 changes: 12 additions & 6 deletions test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import parseJson from '.';
import parseJson, {JSONError} from './index.js';

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

Expand All @@ -10,7 +10,7 @@ test('main', t => {
parseJson('{\n\t"foo": true,\n}');
}, {
name: 'JSONError',
message: /Unexpected token "}"/
message: /Unexpected token "}"/,
});

t.throws(() => {
Expand All @@ -20,11 +20,15 @@ test('main', t => {
error.fileName = 'foo.json';
throw error;
}
}, jsonErrorRegex);
}, {
message: jsonErrorRegex,
});

t.throws(() => {
parseJson('{\n\t"foo": true,\n}', 'foo.json');
}, jsonErrorRegex);
}, {
message: jsonErrorRegex,
});

t.throws(() => {
try {
Expand All @@ -33,13 +37,15 @@ test('main', t => {
error.fileName = 'foo.json';
throw error;
}
}, jsonErrorRegex);
}, {
message: jsonErrorRegex,
});
});

test('throws exported error error', t => {
t.throws(() => {
parseJson('asdf');
}, {
instanceOf: parseJson.JSONError
instanceOf: JSONError,
});
});

0 comments on commit 8acbeac

Please sign in to comment.