Skip to content

Commit 9f3bd0b

Browse files
crondininisindresorhus
andauthoredSep 1, 2023
Add rawCodeFrame property to JSONError (#37)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 258f831 commit 9f3bd0b

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed
 

‎index.d.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {JsonObject} from 'type-fest';
33
/**
44
Exposed for `instanceof` checking.
55
*/
6-
export type JSONError = Error & { // eslint-disable-line @typescript-eslint/naming-convention
6+
export class JSONError extends Error { // eslint-disable-line @typescript-eslint/naming-convention
77
/**
88
The filename displayed in the error message, if any.
99
*/
@@ -13,7 +13,12 @@ export type JSONError = Error & { // eslint-disable-line @typescript-eslint/nami
1313
The printable section of the JSON which produces the error.
1414
*/
1515
readonly codeFrame: string;
16-
};
16+
17+
/**
18+
The raw version of `codeFrame` without colors.
19+
*/
20+
readonly rawCodeFrame: string;
21+
}
1722

1823
// Get 'reviver' parameter from JSON.parse()
1924
type ReviverFn = Parameters<typeof JSON['parse']>['1'];

‎index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ export default function parseJson(string, reviver, filename) {
3535
const index = Number(indexMatch[1]);
3636
const location = lines.locationForIndex(index);
3737

38-
const codeFrame = codeFrameColumns(
38+
const generateCodeFrame = ({highlightCode}) => codeFrameColumns(
3939
string,
4040
{start: {line: location.line + 1, column: location.column + 1}},
41-
{highlightCode: true},
41+
{highlightCode},
4242
);
4343

44-
jsonError.codeFrame = codeFrame;
44+
jsonError.codeFrame = generateCodeFrame({highlightCode: true});
45+
jsonError.rawCodeFrame = generateCodeFrame({highlightCode: false});
4546
}
4647

4748
throw jsonError;

‎index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ const jsonError: JSONError = {
3333
> 3 | }
3434
| ^
3535
`,
36+
rawCodeFrame: `
37+
1 | {
38+
2 | "foo": true,
39+
> 3 | }
40+
| ^
41+
`,
3642
};
3743

3844
expectError(jsonError.codeFrame = '');

‎readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,9 @@ The filename displayed in the error message.
105105
Type: `string`
106106

107107
The printable section of the JSON which produces the error.
108+
109+
#### rawCodeFrame
110+
111+
Type: `string`
112+
113+
The raw version of `codeFrame` without colors.

‎test.js

+9
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,12 @@ test('throws exported error error', t => {
4949
instanceOf: JSONError,
5050
});
5151
});
52+
53+
test('has error frame properties', t => {
54+
try {
55+
parseJson('{\n\t"foo": true,\n}', 'foo.json');
56+
} catch (error) {
57+
t.assert(error.codeFrame);
58+
t.is(error.rawCodeFrame, ' 1 | {\n 2 | \t"foo": true,\n> 3 | }\n | ^');
59+
}
60+
});

0 commit comments

Comments
 (0)