-
-
Notifications
You must be signed in to change notification settings - Fork 5
fix: report syntax error #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I would usually put I am not sure if silently bail out is a good idea or not. But maybe a try...catch is still useful that we can emit an ESLint error to say |
Updated to report parse error |
src/rules/prettier.ts
Outdated
catch (error) { | ||
context.report({ | ||
loc: { | ||
start: { line: 1, column: 0 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to get the position information from Prettier or the error project and report it to the place? And then we could fallback to the start of the file if failed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message looks like this:
CssSyntaxError: Unknown word (3:3)
1 | .foo {
2 | color: green;
> 3 | ...
| ^
4 | }
5 |
I've updated it to get a more accurate location.
src/rules/prettier.ts
Outdated
let loc = { | ||
start: { line: 1, column: 0 }, | ||
end: { line: 1, column: 0 }, | ||
} | ||
|
||
if (error instanceof Error) { | ||
const locationMatch = error.message.match(/\((?<line>\d):(?<column>\d)\)/) | ||
if (locationMatch?.groups) { | ||
const { line, column } = locationMatch.groups | ||
loc = { | ||
start: { line: +line, column: +column }, | ||
end: { line: +line, column: +column }, | ||
} | ||
} | ||
} | ||
|
||
context.report({ | ||
loc, | ||
message: 'Failed to format the code', | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend you to look into this: eslint-plugin-prettier handling errors thrown by Prettier
(Actually I was going to submit a PR with these changes and just found this PR 😂)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use the same logic with eslint-plugin-prettier
. Thank you!
Co-authored-by: XiNiHa <[email protected]>
Description
When writing some CSS demos in markdown files, the syntax may not always be valid. Then we can not lint anymore.
Linked Issues
Additional context