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
Change Request: Ensure that examples in rule docs are parsable #17602
Comments
Wow, very interesting. Thanks for doing this. I agree, having a check for parseable code would be a good idea. I'm curious, when you say "at build time", are you thinking that would be a lint check on PRs? Run during lint-staged? Literally run as part of the Eleventy build? And how are you envisioning setting |
I was thinking of a new step in the CI workflow, in the
To pass parser options to the playground we are using JSON after the
I think we should use the same information to determine parser options for the syntax check. If a code example cannot be parsed in the Playground because it's missing some parser options, adding the options after the tag would fix both the Playground link and the syntax check. |
Sounds good to me. Let's do it. 👍 |
Thanks for accepting @nzakas! Any suggestion about how to handle examples in removed rules, e.g. |
I think it makes sense to just omit the removed rules from "Open in Playground", although it would still be nice to make sure they are parseable. I'd prefer not to have syntax errors anywhere in the docs. And another thing to consider: What if we made |
👍 I was going to suggest the same. Since the current default in the playground is |
Thanks guys! Great feedback, and I like all suggestions very much. To summarize the discussion, here is the list of todos:
If this is correct, I'm ready to work on it. I'm planning to split the changes into multiple PRs so they'll be easier to review. In case someone else would like to do part of the work, just let me know here so we can coordinate. |
ESLint version
v8.50.0
What problem do you want to solve?
Some of the correct/incorrect example code blocks in rule docs fail to parse because of various reasons.
Other code blocks expect certain parser options (e.g.
sourceType: "module"
) in order to work as intended, but these options are not always explicitly encoded along with thecorrect
/incorrect
tag.In both cases, the outcome is the same: when such a code block is opened in the Playground from the rule docs page, a visitor ends up seeing a syntax error instead of the expected rule validation.
I run a script to find these unparsable code blocks, and it found 186. The output also includes other problems like rules without a correct/incorrect code block:
Note that at most one syntax error per code block is reported.
For reference, this is what my script looks like:
check-rule-examples.js
What do you think is the correct solution?
I would suggest adding a check at build time to make sure that code blocks in rule docs contain no syntax errors.
Before doing so, unparsable code blocks should be fixed manually.
Most of the times, the appropriate fix is inferred directly from the error message. Here is a summary of error messages and their possible fixes without warranty:
Rename the identifier to make it unique. Other options are splitting the code blocks or inserting each individual example into a block statement
{
...}
.Add parser option
"sourceType": "module"
.Add a semicolon after each example. Other good solutions exist.
Move the call into a subclass constructor.
Move the call into a subclass method.
This typically happens when an unsyntactic escape sequence is inserted in place of a valid character for readability, for example
\t
for a tab.Move the return statement into a function or add parser option
"sourceType": "commonjs"
.Add parser option
"ecmaVersion": 6
This requires looking at the code. The fix could be adding a parser option, inserting a missing semicolon, fixing a typo, replacing unsyntactic pseudocode with real code, etc.
Participation
Additional comments
Some of the reported problems concern removed or deprecated rules, and the "Open in Playground" button works for those rules as well. I was thinking about excluding removed and deprecated rules from code block validation and disabling the Playground buttons, but that could be actually more effort than just fixing all code blocks, so not sure.
The text was updated successfully, but these errors were encountered: