Improve glimmer parse errors (#5161)
Instead of showing 80 carets, point out just which line the error was
on, and rely on the original message to point out the "column".
I thought about matching the `----^` in the original message with a
regex and use the length of it as the column number, but unfortunately
the original message does not show the original code (in this example
some whitespace is missing), so it wouldn't work.
Before:
```
$ ./bin/prettier.js test.hbs --parser glimmer
[error] test.hbs: SyntaxError: Parse error on line 2:
[error] {{one}}{{
[error] ---------^
[error] Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EOF' (2:0)
[error] 1 | {{one}}
[error] > 2 | {{
[error] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error] 3 |
```
After:
```
$ ./bin/prettier.js test.hbs --parser glimmer
[error] test.hbs: SyntaxError: Parse error on line 2:
[error] {{one}}{{
[error] ---------^
[error] Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EOF' (2:0)
[error] 1 | {{one}}
[error] > 2 | {{
[error] 3 |
```
This commit is contained in:
1 parent
9910536b58
commit
f87c82cc4e
1 file changed
+1
-2
@@ -34,8 +34,7 @@ function parse(text) {
|
||||
const matches = error.message.match(/on line (\d+)/);
|
||||
if (matches) {
|
||||
throw createError(error.message, {
|
||||
start: { line: +matches[1], column: 0 },
|
||||
end: { line: +matches[1], column: 80 }
|
||||
start: { line: Number(matches[1]), column: 0 }
|
||||
});
|
||||
} else {
|
||||
throw error;
|
||||
|
||||
Reference in new issue
Block a user