Skip to content

Commit

Permalink
[FIX] disable showing line and col in error message due to performanc…
Browse files Browse the repository at this point in the history
…e issue
  • Loading branch information
shellyln committed Feb 17, 2020
1 parent beb27c4 commit d657bf7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/_spec/foo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ describe("foo", function() {
.replace(/0555/, '0o555')
.replace(/\\256/, '\\xae') + ')'));
});
it("json-5", function() {
const src =
`
]
`;
expect(() => parseJson(src)).toThrowError('parse faild at position:13 line:2 col:14 operator "charSequence([)"');
});
// it("json-5", function() {
// const src =
// `
// //
// //
// ]
// `;
// expect(() => parseJson(src)).toThrowError('parse faild at position:43 line:4 col:14 operator "charSequence([)"');
// });
});
24 changes: 18 additions & 6 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ParserInputWithCtx,



/*
function getLineAndCol(src: string, pos: number) {
let line = 1;
let col = 1;
Expand All @@ -37,6 +38,7 @@ function getLineAndCol(src: string, pos: number) {
col,
});
}
*/


export function makeErrorMessage<T extends ArrayLike<T[number]>, C>(
Expand All @@ -52,10 +54,16 @@ export function makeErrorMessage<T extends ArrayLike<T[number]>, C>(
.concat(...ar.slice(1));
src = ar.join('\n') + '\n\n';

const lineAndCol = getLineAndCol(input.src, input.start);
return (`parse error occured at position:${
input.start} line:${lineAndCol.line} col:${lineAndCol.col} ${
input.start} ${
message ? ` ${message}` : ''}\n ${src}`);

// TODO: Disabled due to performance issue
//
// const lineAndCol = getLineAndCol(input.src, input.start);
// return (`parse error occured at position:${
// input.start} line:${lineAndCol.line} col:${lineAndCol.col} ${
// message ? ` ${message}` : ''}\n ${src}`);
} else {
src = ' (object)\n ^~~~~~~~';
try {
Expand All @@ -82,10 +90,14 @@ export function makeMessage<T extends ArrayLike<T[number]>, C>(
input: ParserInputWithCtx<T, C>, message?: string) {

if (typeof input.src === 'string') {
const lineAndCol = getLineAndCol(input.src, input.start);
return (`parse faild at position:${
input.start} line:${lineAndCol.line} col:${lineAndCol.col} ${
message ? ` ${message}` : ''}`);
return (`parse faild at position:${input.start} ${message ? ` ${message}` : ''}`);

// TODO: Disabled due to performance issue
//
// const lineAndCol = getLineAndCol(input.src, input.start);
// return (`parse faild at position:${
// input.start} line:${lineAndCol.line} col:${lineAndCol.col} ${
// message ? ` ${message}` : ''}`);
} else {
return (`parse faild at position:${input.start} ${message ? ` ${message}` : ''}`);
}
Expand Down

0 comments on commit d657bf7

Please sign in to comment.