Skip to content

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

Merged
merged 6 commits into from
Jun 15, 2024
Merged

Conversation

hyoban
Copy link
Contributor

@hyoban hyoban commented Jun 2, 2024

Description

When writing some CSS demos in markdown files, the syntax may not always be valid. Then we can not lint anymore.

Oops! Something went wrong! :(

ESLint: 9.2.0

SyntaxError: CssSyntaxError: Unknown word (29:5)
  27 |     }
  28 |
> 29 |     ...
     |     ^
  30 |   }
  31 |   // #endregion
  32 |
    at Xf (file:///Users/hyoban/i/BewlyBewly/node_modules/.pnpm/[email protected]/node_modules/prettier/plugins/postcss.mjs:49:10786)
    at Ks (file:///Users/hyoban/i/BewlyBewly/node_modules/.pnpm/[email protected]/node_modules/prettier/plugins/postcss.mjs:49:17948)
    at Object.ol [as parse] (file:///Users/hyoban/i/BewlyBewly/node_modules/.pnpm/[email protected]/node_modules/prettier/plugins/postcss.mjs:49:18271)
    at parse4 (file:///Users/hyoban/i/BewlyBewly/node_modules/.pnpm/[email protected]/node_modules/prettier/index.mjs:20716:24)
    at async coreFormat (file:///Users/hyoban/i/BewlyBewly/node_modules/.pnpm/[email protected]/node_modules/prettier/index.mjs:21206:7)
    at async formatWithCursor (file:///Users/hyoban/i/BewlyBewly/node_modules/.pnpm/[email protected]/node_modules/prettier/index.mjs:21408:14)
    at async Module.format2 (file:///Users/hyoban/i/BewlyBewly/node_modules/.pnpm/[email protected]/node_modules/prettier/index.mjs:22793:25)

Linked Issues

Additional context

@antfu
Copy link
Owner

antfu commented Jun 2, 2024

I would usually put <!-- eslint-skip --> above the block - as you won't expect any linting anyway for invalid code. Or you can have /* ... */ instead.

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 Failed to parse the code instead of crashing

@hyoban hyoban changed the title fix: ignore syntax error fix: report syntax error Jun 2, 2024
@hyoban
Copy link
Contributor Author

hyoban commented Jun 2, 2024

Updated to report parse error

catch (error) {
context.report({
loc: {
start: { line: 1, column: 0 },
Copy link
Owner

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

Copy link
Contributor Author

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.

Comment on lines 48 to 67
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',
})
Copy link
Contributor

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 😂)

Copy link
Contributor Author

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!

@hyoban hyoban requested a review from antfu June 15, 2024 08:39
@antfu antfu merged commit 4e88d70 into antfu:main Jun 15, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants