Skip to content
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

docs: show red underlines in examples in rules docs #18041

Merged
merged 28 commits into from Mar 7, 2024

Conversation

ota-meshi
Copy link
Member

@ota-meshi ota-meshi commented Jan 27, 2024

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 modifies docs/src/_plugins/md-syntax-highlighter.js to indicate where an error occurs with a red marker in the examples in the rules document.

Is there anything you'd like reviewers to focus on?

close #16227

  • This process is done using Prism's after-tokenize hook.
  • Content that starts with /* eslint and whose language is js is considered an example of an eslint rule.
    It is now consider it an example of an ESLint rule only if it is wrapped in a custom container (that is, if parser options are passed from the custom container's rendering process).
    I would like to hear your opinion if this will work.
  • I would like to hear your opinion on what style should be used.

@ota-meshi ota-meshi requested a review from a team as a code owner January 27, 2024 07:23
@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Jan 27, 2024
Copy link

netlify bot commented Jan 27, 2024

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit f90b1d2
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65e8fff0d610930008c0423c
😎 Deploy Preview https://deploy-preview-18041--docs-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@ota-meshi
Copy link
Member Author

It seems that CI does not install root dependencies, which causes the documentation to fail to build.
Can you temporarily change to install root dependencies to see how this PR changes the documentation?

@ota-meshi
Copy link
Member Author

It looks like this on my local.

image

@fasttime
Copy link
Member

Thanks for this PR! CI is failing because the job that builds the docs now requires ESLint dependencies to be installed, but only deps in the docs package (in the subfolder) are installed at that point:

- name: Build Docs Website
working-directory: docs
run: npm run build

We could run npm install in the parent folder before running the build script, or we could move this particular job to the main workflow in .github/workflows/ci.yml and have it running from there. But in this case, maybe we'll need to run npm install in the docs package as well. You should be able do all changes by yourself, I think.

@fasttime fasttime added the accepted There is consensus among the team that this change meets the criteria for inclusion label Jan 27, 2024
@@ -20,7 +20,8 @@
"lint:scss": "stylelint \"**/*.{scss,html}\"",
"lint:fix:scss": "npm run lint:scss -- --fix",
"build:minify-images": "imagemin '_site/assets/images' --out-dir='_site/assets/images'",
"start": "npm-run-all build:sass build:postcss --parallel *:*:watch"
"start": "npm-run-all build:sass build:postcss --parallel *:*:watch",
"postinstall": "cd .. && npm install -f"
Copy link
Member Author

@ota-meshi ota-meshi Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I temporarily added postinstall so that we can check the results of this PR with netlify. I think we should probably replace this PR with another method before merging.
https://deploy-preview-18041--docs-eslint.netlify.app/rules/array-callback-return

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add the top-level install to the workflow. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the postinstall and changed the workflow.
I think we probably need to modify the netlify script, but how should I do that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. There is no Netlify script. It just checks out docs and does npm install.

I wonder, could we just list "eslint": "file:../" in docs/package.json and that will work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, could we just list "eslint": "file:../" in docs/package.json and that will work?

I tried that locally but it doesn't seem to work. eslint dependencies were not installed 😓

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think putting the netlify.toml and adding the command config will work fine?
https://docs.netlify.com/configure-builds/file-based-configuration/#build-settings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try it. I'm out of other ideas.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried adding netlify.toml and it seems to be working fine 🎉

https://deploy-preview-18041--docs-eslint.netlify.app/rules/array-callback-return

image

@nzakas
Copy link
Member

nzakas commented Jan 29, 2024

I think it's okay to update the CI in this PR.

This looks really good, thanks! Can we change the red underline to look more like the one VS Code uses? (A little squiggly.) I'm pretty sure it's just a repeating image that we can find in the VS Code repo.

docs/tools/prism-eslint-hook.js Outdated Show resolved Hide resolved
docs/tools/prism-eslint-hook.js Outdated Show resolved Hide resolved
docs/tools/prism-eslint-hook.js Outdated Show resolved Hide resolved

/**
* Add content that needs to be marked.
* @param {string} content Source code content that marks eslint errors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param {string} content Source code content that marks eslint errors.
* @param {string} content Source code content that marks ESlint errors.

@nzakas
Copy link
Member

nzakas commented Jan 31, 2024

This is looking really nice. Is it possible to add a tooltip over the red-underlined code that includes the error message?

@ota-meshi
Copy link
Member Author

Is it possible to add a tooltip over the red-underlined code that includes the error message?

Hmm. It seems difficult to do that with Prism's hooks for now.
It doesn't seem possible to add anything other than the <span> class and content for each Token. (For example, we cannot add the popovertarget attribute only to the error token.)
If we request the Prism team to pass the Token instance to Prism's wrap hook, we might be able to get a tooltip, but I'm not sure yet 🤔

@ota-meshi
Copy link
Member Author

If we request the Prism team to pass the Token instance to Prism's wrap hook, we might be able to get a tooltip, but I'm not sure yet 🤔

I tried it but it didn't work well. During the after-tokenize hook and before, all Token instances created are replaced with new normalized Token instances before reaching the wrap hook.

@nzakas
Copy link
Member

nzakas commented Feb 1, 2024

Is it possible to add a title attribute to the <span>?

@ota-meshi
Copy link
Member Author

I don't know how to add title attribute (an attribute other than class) to a token using Prism's API 😓.
It might be possible to give a unique key to a class and later retrieve the message that maps to the unique key and set it to the title. But it may seem a bit like a hack.

@ota-meshi
Copy link
Member Author

It might be possible to give a unique key to a class and later retrieve the message that maps to the unique key and set it to the title.

I tried that. If someone doesn't like it that way, we can change it back.
I also made major changes to prism-eslint-hook.js because we needed to organize the token structure more to display the title properly.

image

https://deploy-preview-18041--docs-eslint.netlify.app/rules/array-callback-return

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks amazing. ❤️

We can update the docs CI to install the top-level packages and I think this is ready to go.

@@ -20,7 +20,8 @@
"lint:scss": "stylelint \"**/*.{scss,html}\"",
"lint:fix:scss": "npm run lint:scss -- --fix",
"build:minify-images": "imagemin '_site/assets/images' --out-dir='_site/assets/images'",
"start": "npm-run-all build:sass build:postcss --parallel *:*:watch"
"start": "npm-run-all build:sass build:postcss --parallel *:*:watch",
"postinstall": "cd .. && npm install -f"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add the top-level install to the workflow. 👍

@fasttime
Copy link
Member

fasttime commented Feb 4, 2024

Nice work! LGTM when the build is fixed.

Comment on lines 4 to 5
const { Linter } = require("../../lib/api");
const astUtils = require("../../lib/shared/ast-utils");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this work for https://github.com/eslint/zh-hans.docs.eslint.org, where we don't have these modules?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... I don't think it will work as it is now.
@kecrily Do you have any ideas how to solve it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about changing this PR as below?

  • astUtils is used only to get the regex, so it inlines the regex.
  • Try require("../../lib/api").Linter and require("eslint").Linter and use the one that does not cause an error as the Linter.
  • Change the install script of zh-hans.docs.eslint.org to install eslint as a dependency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to not do red underlines outside of the US site. We could check to see if these utilities exist, and if not, just don't do the linting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to not do red underlines outside of the US site. We could check to see if these utilities exist, and if not, just don't do the lining.

I'd prefer this option.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to not do red underlines outside of the US site. We could check to see if these utilities exist, and if not, just don't do the linting.

I've made changes to this PR to make it so 👍

@github-actions github-actions bot added github actions and removed documentation Relates to ESLint's documentation labels Feb 19, 2024
@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Feb 27, 2024
@github-actions github-actions bot removed the documentation Relates to ESLint's documentation label Feb 27, 2024
@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Feb 28, 2024
@github-actions github-actions bot removed the documentation Relates to ESLint's documentation label Feb 28, 2024
@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Feb 28, 2024
@github-actions github-actions bot removed the documentation Relates to ESLint's documentation label Feb 28, 2024
Copy link
Contributor

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great ⭐ , thanks!

Leaving it open for @mdjermanovic

@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Mar 4, 2024
@github-actions github-actions bot removed the documentation Relates to ESLint's documentation label Mar 4, 2024
Comment on lines 344 to 351
if (
env.content === "" ||
env.content === "\n" ||
env.content === "\r\n" ||
env.content === "\ufeff"
) {
env.classes.push(CLASS_ESLINT_MARKED_ON_ZERO_WIDTH);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, nothing is underlined in the fourth example as the reported text is \n\n:

image

https://deploy-preview-18041--docs-eslint.netlify.app/rules/padded-blocks#never

VS Code, for example, always underlines linebreaks at the start of the range, and linebreaks of empty lines within the range (so that the range always looks continuous), so this example looks like this:

image

Would it be possible to do something similar? If not, could we maybe also add CLASS_ESLINT_MARKED_ON_ZERO_WIDTH in case the entire content consists of only linebreaks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this PR to make it look like VSCode by extracting the newline code character by character into tokens 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to do this only for newline characters that appear in an error range and are either at the very start of the range or lone on the line?

image

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added CSS and changed it to look like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Mar 5, 2024
@github-actions github-actions bot removed the documentation Relates to ESLint's documentation label Mar 5, 2024
@amareshsm
Copy link
Member

nice 🎉

@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Mar 6, 2024
@github-actions github-actions bot removed the documentation Relates to ESLint's documentation label Mar 6, 2024
@mdjermanovic mdjermanovic added the documentation Relates to ESLint's documentation label Mar 7, 2024
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks!

@mdjermanovic mdjermanovic merged commit d961eeb into eslint:main Mar 7, 2024
19 checks passed
@ota-meshi ota-meshi deleted the red-underlines branch March 7, 2024 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion contributor pool documentation Relates to ESLint's documentation github actions
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Change Request: Show red underlines in examples in rules docs
8 participants