-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add documentation for using HTML ESLint with other HTML-processing plugins #435
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
Draft
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/update-html-eslint-documentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,8 @@ | |
| "nodownload", | ||
| "nofullscreen", | ||
| "controlslist", | ||
| "describedby" | ||
| "describedby", | ||
| "eslinthtml", | ||
| "eslintjs" | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| # Using HTML ESLint with Other Plugins | ||
|
|
||
| ## Overview | ||
|
|
||
| HTML ESLint focuses on linting HTML syntax and structure. However, you may also want to lint JavaScript code within HTML files (such as inline `<script>` tags) or use other ESLint plugins that process HTML files. This guide explains how to configure HTML ESLint alongside other plugins. | ||
|
|
||
| ## Linting JavaScript Inside HTML | ||
|
|
||
| HTML ESLint does not lint JavaScript code within HTML files. To lint JavaScript in HTML (such as inline scripts), you can use [eslint-plugin-html](https://github.com/BenoitZugmeyer/eslint-plugin-html). | ||
|
|
||
| ### Why Separate Configuration Files? | ||
|
|
||
| When using both HTML ESLint and eslint-plugin-html, **you need separate configuration files**. This is because: | ||
|
|
||
| 1. HTML ESLint uses a custom parser (`@html-eslint/parser`) that understands HTML syntax | ||
| 2. eslint-plugin-html uses a different approach to extract and lint JavaScript from HTML | ||
| 3. When both are configured for the same `*.html` files in a single config, ESLint may try to apply JavaScript rules to HTML syntax, causing errors | ||
|
|
||
| This limitation is due to how ESLint's parser system works and is not specific to HTML ESLint. For more context, see: | ||
|
|
||
| - [ESLint Discussion #18808](https://github.com/eslint/eslint/discussions/18808) | ||
| - [ESLint Issue #14286](https://github.com/eslint/eslint/issues/14286) | ||
| - [ESLint Issue #17655](https://github.com/eslint/eslint/issues/17655) | ||
|
|
||
| ### Configuration Example | ||
|
|
||
| #### Step 1: Create a Configuration for HTML ESLint | ||
|
|
||
| Create `eslinthtml.config.mjs` for HTML syntax linting: | ||
|
|
||
| ```js | ||
| import html from "@html-eslint/eslint-plugin"; | ||
|
|
||
| export default [ | ||
| { | ||
| files: ["**/*.html"], | ||
| plugins: { | ||
| html, | ||
| }, | ||
| extends: ["html/recommended"], | ||
| language: "html/html", | ||
| rules: { | ||
| // Customize HTML linting rules | ||
| "html/indent": ["error", 2], | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` | ||
|
|
||
| #### Step 2: Create a Configuration for JavaScript in HTML | ||
|
|
||
| Create `eslintjs.config.mjs` for JavaScript linting within HTML: | ||
|
|
||
| ```js | ||
| import js from "@eslint/js"; | ||
| import htmlPlugin from "eslint-plugin-html"; | ||
|
|
||
| export default [ | ||
| { | ||
| files: ["**/*.html"], | ||
| plugins: { | ||
| html: htmlPlugin, | ||
| }, | ||
| rules: { | ||
| ...js.configs.recommended.rules, | ||
| // Customize JavaScript rules for inline scripts | ||
| "no-unused-vars": "error", | ||
| "prefer-const": "error", | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` | ||
|
|
||
| #### Step 3: Run ESLint with Each Configuration | ||
|
|
||
| Run ESLint separately with each configuration file: | ||
|
|
||
| ```bash | ||
| # Lint HTML syntax | ||
| npx eslint --config eslinthtml.config.mjs "**/*.html" | ||
|
|
||
| # Lint JavaScript in HTML | ||
| npx eslint --config eslintjs.config.mjs "**/*.html" | ||
| ``` | ||
|
|
||
| ### Using npm Scripts | ||
|
|
||
| You can simplify this workflow by adding scripts to your `package.json`: | ||
|
|
||
| ```json | ||
| { | ||
| "scripts": { | ||
| "lint:html": "eslint --config eslinthtml.config.mjs \"**/*.html\"", | ||
| "lint:js-in-html": "eslint --config eslintjs.config.mjs \"**/*.html\"", | ||
| "lint": "npm run lint:html && npm run lint:js-in-html" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Then run: | ||
|
|
||
| ```bash | ||
| npm run lint | ||
| ``` | ||
|
|
||
| ## Complete Working Example | ||
|
|
||
| Here's a complete example with all necessary files: | ||
|
|
||
| ### Installation | ||
|
|
||
| ```bash | ||
| npm install --save-dev eslint @html-eslint/eslint-plugin @html-eslint/parser eslint-plugin-html @eslint/js | ||
| ``` | ||
|
|
||
| ### HTML File (`example.html`) | ||
|
|
||
| ```html | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Example</title> | ||
| <script> | ||
| let message = "Hello World"; | ||
| console.log(message); | ||
| </script> | ||
| </head> | ||
| <body> | ||
| <p>Welcome!</p> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
| ### HTML ESLint Configuration (`eslinthtml.config.mjs`) | ||
|
|
||
| ```js | ||
| import html from "@html-eslint/eslint-plugin"; | ||
|
|
||
| export default [ | ||
| { | ||
| files: ["**/*.html"], | ||
| plugins: { | ||
| html, | ||
| }, | ||
| extends: ["html/recommended"], | ||
| language: "html/html", | ||
| rules: { | ||
| "html/require-doctype": "error", | ||
| "html/no-duplicate-id": "error", | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` | ||
|
|
||
| ### JavaScript in HTML Configuration (`eslintjs.config.mjs`) | ||
|
|
||
| ```js | ||
| import js from "@eslint/js"; | ||
| import htmlPlugin from "eslint-plugin-html"; | ||
|
|
||
| export default [ | ||
| { | ||
| files: ["**/*.html"], | ||
| plugins: { | ||
| html: htmlPlugin, | ||
| }, | ||
| rules: { | ||
| ...js.configs.recommended.rules, | ||
| "no-unused-vars": "error", | ||
| "prefer-const": "error", | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` | ||
|
|
||
| ### Package.json Scripts | ||
|
|
||
| ```json | ||
| { | ||
| "scripts": { | ||
| "lint:html": "eslint --config eslinthtml.config.mjs \"**/*.html\"", | ||
| "lint:js": "eslint --config eslintjs.config.mjs \"**/*.html\"", | ||
| "lint": "npm run lint:html && npm run lint:js" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Alternative Approaches | ||
|
|
||
| ### Separate HTML and JS Files | ||
|
|
||
| If possible, consider separating your JavaScript into `.js` files and referencing them in HTML: | ||
|
|
||
| ```html | ||
| <script src="script.js"></script> | ||
| ``` | ||
|
|
||
| This allows you to: | ||
|
|
||
| - Use a single ESLint configuration | ||
| - Lint `.html` files with HTML ESLint | ||
| - Lint `.js` files with standard JavaScript rules | ||
|
|
||
| ### CI/CD Integration | ||
|
|
||
| When integrating with CI/CD, run both linting commands: | ||
|
|
||
| ```yaml | ||
| # Example GitHub Actions workflow | ||
| - name: Lint HTML syntax | ||
| run: npx eslint --config eslinthtml.config.mjs "**/*.html" | ||
|
|
||
| - name: Lint JavaScript in HTML | ||
| run: npx eslint --config eslintjs.config.mjs "**/*.html" | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Error: "Cannot read properties of undefined" | ||
|
|
||
| If you see errors like `Cannot read properties of undefined (reading 'ignorePatternRegExp')` when trying to use both plugins in one config, this confirms you need separate configuration files. | ||
|
|
||
| ### JavaScript Rules Applied to HTML | ||
|
|
||
| If you see JavaScript linting errors on HTML tags, ensure: | ||
|
|
||
| 1. You're using the correct config file for each purpose | ||
| 2. The `files` pattern matches appropriately | ||
| 3. You haven't mixed both parsers in the same configuration object | ||
|
|
||
| ## Related Documentation | ||
|
|
||
| - [Getting Started](./getting-started.md) | ||
| - [FAQ](./faq.md) | ||
| - [Integrating Template Engines](./integrating-template-engine.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Type: Suggestion ➕
1. Suggestion
I suggest using file names like
eslint-htmlandeslint-jsinstead ofeslinthtmlandeslintjs, respectively.2. The reason for the suggested changes
File names like
eslintjsandeslinthtmlare anti-patterns for spell checkers. Spell checkers don’t recognize two different words ineslintjsandeslinthtml, and users need to addeslintjsandeslinthtmlto the list of exceptions. On the other hand, good spell checkers like CSpell should recognize two different words ineslint-html,eslint_html, andeslintHtml.Thanks.