1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Add better parser errors (#1553)

Old SVGO errors were not very helpful. Packages like cssnano
(postcss-svgo) had to deal with a lot of issues which are hard to debug
with old errors.

```
Error: Error in parsing SVG: Unquoted attribute value
Line: 1
Column: 29
Char: 6
File: input.svg
```

New errors are more informative and may solve many struggles

```
Error: SvgoParserError: input.svg:2:29: Unquoted attribute value

  1 | <svg viewBox="0 0 120 120">
> 2 |   <circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
    |                             ^
  3 | </svg>
  4 |
```
This commit is contained in:
Bogdan Chadkin
2021-09-12 01:09:10 +03:00
committed by GitHub
parent e8321f0c27
commit 77102ed096
12 changed files with 327 additions and 139 deletions

33
test/cli/cli.test.js Normal file
View File

@ -0,0 +1,33 @@
'use strict';
const { spawn } = require('child_process');
const stripAnsi = require('strip-ansi');
test('should exit with 1 code on syntax error', async () => {
const proc = spawn('node', ['../../bin/svgo', 'invalid.svg'], {
cwd: __dirname,
});
const [code, stderr] = await Promise.all([
new Promise((resolve) => {
proc.on('close', (code) => {
resolve(code);
});
}),
new Promise((resolve) => {
proc.stderr.on('data', (error) => {
resolve(error.toString());
});
}),
]);
expect(code).toEqual(1);
expect(stripAnsi(stderr))
.toEqual(`SvgoParserError: invalid.svg:2:27: Unquoted attribute value
1 | <svg>
> 2 | <rect x="0" y="0" width=10" height="20" />
| ^
3 | </svg>
4 |
`);
});