jju
jju copied to clipboard
The parse exception clues are off sometimes
I'm using jju
to provide more helpful errors when parsing invalid JSON files. I noticed that sometimes the clues about the location of the error (line:column) are off and it seems it mostly adds 1 to either the line or the column.
Here's an example of the column index being off:
var jju = require('jju');
jju.parse('{"a":1,\n"b":{"ba":"foo"\n}', {mode: 'json'});
When I run it I get:
Unexpected end of input at 3:3
}
^
The end of input is at 3:2 and not 3:3. Am I missing something?
When the error is occured, current caret position is at 3:3. So parser expects something at 3:3, but can't find anything.
I don't think this one is misleading, an end of input is an end of input. Are there any other similar cases?
Why is the caret at 3:3? The string ends with '}' which is located at 3:1 so I expect the end of input to be at 3:2. Also if you look at the output the ^
is pointing to the next character after '}' so that also seems to suggest the location should be 3:2.
Here's another case that I find a bit confusing, when you parse this:
{
"a": "b",
"c": 5,
}
You get:
Trailing comma in object at 4:1
}
^
The trailing comma is at the end of the third line and not at the beginning of the fourth line. In this case I understand the index, since 4:1 is the point where the parse actually failed. That said, I find it confusing that we say the issue is a "Trailing comma in object at", but instead of telling the user where that comma is located, we give the index of the closing curly brace. If you add some extra new lines before the closing brace the index would change accordingly, even due the actual problem, the trailing comma, is at the same location.