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
build: fix TypeError in prism-eslint-hooks.js #18209
Conversation
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
docs/tools/prism-eslint-hook.js
Outdated
@@ -317,7 +317,7 @@ function installPrismESLintMarkerHook() { | |||
continue; | |||
} | |||
|
|||
if (Array.isArray(token.content) || typeof token.content !== "string") { | |||
if (Array.isArray(token.content)) { |
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.
According to Prism typings, token.content
can be a string, Token, or array of strings/Tokens, so it looks like this was targeting the Token case, but missed the fact that token
itself can be a string, and that's causing the crash?
Would it make sense to keep this condition and fix the problem this way:
for (const token of tokens) {
+ if (typeof token === "string") {
+ yield token;
+ continue;
+ }
const content = getTokenContent(token);
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 condition is flawed because Array.isArray(token.content)
implies typeof token.content !== "string"
, but I think it's a good idea to check if token
is a string early in the loop nonetheless, because it makes the logic easier to follow. In the below condition it's enough to check that token.content
is not a string to verify that it's an array. I wasn't able to reproduce the case where token.content
is a Token, but in theory, it should work as well.
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.
LGTM! Thank you for fixing the problem!
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.
LGTM, thanks!
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[X] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
This PR fixes an issue in the docs build process that is causing some incorrect rule examples in the
indent
andindent-legacy
rules to be displayed without red markers for errors, for example here.This is due to a
TypeError
that occurs because of an incorrect condition in another part of the code. This incorrect condition allows the creation of an array with an undefined element where only strings or plain objects are expected as elements:The error is visibile in the Netlify logs under Deploy log > Building > Lines 219-306 (last production deploy: https://app.netlify.com/sites/docs-eslint/deploys/65f345a38ee0750008f1a05c#L219-L306) or when manually running
npm run build
in thedocs
folder.Is there anything you'd like reviewers to focus on?
There are no unit tests for prism-eslint-hooks.js. Shall we add one?