This adds a more helpful "file" location to JSON.parse error messages
that already have a position. Some error messages do not show a
position but instead show the full string input or the string input
with ellipses like
"Unexpected token '/', ..."8, 9, 10, /]" is not valid JSON".
These errors are left unchanged.
Given the example:
> JSON.parse(`{"ab `);
Previous error message:
"Unterminated string in JSON at position 5"
New error message:
"Unterminated string in JSON at position 5 (line 1 column 6)"
Spidermonkey for comparison:
"JSON.parse: unterminated string at line 1 column 6 of the JSON data"
It is a bit confusing with the position starting at 0 but I guess
removing the position completely might be risky as there might be
code extracting the position with a regex.
Bug: chromium:1459948
Change-Id: Ibd1a27e8f78bec9f926e19870fcce5126273a57b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4652014
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Matthias Liedtke <mliedtke@chromium.org>
Cr-Commit-Position: refs/heads/main@{#88612}
70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
function TryParse(s, message) {
|
|
try {
|
|
JSON.parse(s);
|
|
assertUnreachable();
|
|
} catch(e) {
|
|
assertEquals(message, e.message);
|
|
}
|
|
}
|
|
|
|
var s = `{"a\\\\b `;
|
|
TryParse(s, "Unterminated string in JSON at position 7 (line 1 column 8)");
|
|
|
|
var s = `{"a\\\\\u03A9 `;
|
|
TryParse(s, "Unterminated string in JSON at position 7 (line 1 column 8)");
|
|
|
|
var s = `{"ab `;
|
|
TryParse(s, "Unterminated string in JSON at position 5 (line 1 column 6)");
|
|
|
|
var s = `{"a\u03A9 `;
|
|
TryParse(s, "Unterminated string in JSON at position 5 (line 1 column 6)");
|
|
|
|
var s = `{"a\nb":"b"}`;
|
|
TryParse(s, "Bad control character in string literal in JSON " +
|
|
"at position 3 (line 1 column 4)");
|
|
|
|
var s = `{"a\nb":"b\u03A9"}`;
|
|
TryParse(s, "Bad control character in string literal in JSON " +
|
|
"at position 3 (line 1 column 4)");
|
|
|
|
// === Test line counting ===
|
|
// \r and \n are line terminators.
|
|
var s = `{\n7:1}`;
|
|
TryParse(s, "Expected property name or '}' in JSON " +
|
|
"at position 2 (line 2 column 1)");
|
|
|
|
var s = `{\r7:1}`;
|
|
TryParse(s, "Expected property name or '}' in JSON " +
|
|
"at position 2 (line 2 column 1)");
|
|
|
|
// The sequence \r\n counts as a single line terminator.
|
|
var s = `{\r\n7:1}`;
|
|
TryParse(s, "Expected property name or '}' in JSON " +
|
|
"at position 3 (line 2 column 1)");
|
|
|
|
// The sequences \r\r, \n\n and \n\r count as 2 line terminators.
|
|
var s = `{\r\r7:1}`;
|
|
TryParse(s, "Expected property name or '}' in JSON " +
|
|
"at position 3 (line 3 column 1)");
|
|
var s = `{\n\n7:1}`;
|
|
TryParse(s, "Expected property name or '}' in JSON " +
|
|
"at position 3 (line 3 column 1)");
|
|
var s = `{\n\r7:1}`;
|
|
TryParse(s, "Expected property name or '}' in JSON " +
|
|
"at position 3 (line 3 column 1)");
|
|
|
|
// \u2028 and \u2029 (line terminators according to ECMA-262 11.3) aren't
|
|
// allowed in JSON (and therefore don't need to be counted).
|
|
var s = `\u2028{}`;
|
|
TryParse(s, "Unexpected token '\u2028', \"\u2028{}\" is not valid JSON");
|
|
var s = `\u2029{}`;
|
|
TryParse(s, "Unexpected token '\u2029', \"\u2029{}\" is not valid JSON");
|
|
|
|
var s = `\n { \n\n"12345": 5,,}`;
|
|
TryParse(s, "Expected double-quoted property name in JSON " +
|
|
"at position 18 (line 4 column 12)");
|