A ESLint plugin to lint and fix inline scripts contained in HTML files.
npm install eslint-plugin-html- Usage
- Disabling ESLint
- Linting HTML
- Multiple scripts tags in a HTML file
- History
- XML support
- Settings
- html/html-extensions
- html/xml-extensions
- html/indent
- html/report-bad-indent
- html/javascript-tag-names
- html/javascript-mime-types
- html/ignore-tags-without-type
- Troubleshooting
- No file linted when running eslint on a directory
- Linting templates (or PHP)
- Linting VUE files
- Migration from older versions
- To v4
- To v3
- Credits
Simply install via npm install --save-dev eslint-plugin-html and add the plugin to your ESLint
configuration. See
ESLint documentation.
Example with ESLint 9 and above (flat config)
``javascript
import html from "eslint-plugin-html"
export default [
{
files: ["*/.html"],
plugins: { html },
},
]
`
Example with ESLint 8 and below
`json`
{
"plugins": ["html"]
}
To temporarily disable ESLint, use the HTML comment. Re-enable it with. Example:
`html`
To disable ESLint for the next script tag only, use the HTML
comment. Example:
`html`
Disabled script tags are completely ignored: their content will not be parsed as JavaScript. You can
use this to disable script tags containing template syntax.
This plugin focuses on applying ESLint rules on inline scripts contained in HTML. It does not
provide any rule related to HTML. For that, you can use other plugins like
@eslint-html or
@angular-eslint. eslint-plugin-html is
compatible with those plugins and can be used along them.
When linting a HTML with multiple script tags, this plugin tries to emulate the browser behavior by
sharing the global scope between scripts by default. This behavior doesn't apply to "module"
scripts (ie:
`
This is perfectly valid by default, and the ESLint rules no-unused-vars and no-undef shouldn'tno-unused-vars
complain. But if those scripts are considerated as ES modules, should report anno-undef
error in the first script, and should report an error in the second script.
In eslint-plugin-html v1 and v2, script code were concatenated and linted in a single pass, so
the scope were always shared. This caused some issues, so in v3 all scripts
were linted separately, and scopes were never shared. In v4, the plugin still lint scripts
separately, but makes sure global variables are declared and used correctly in the non-module case.
This plugin parses HTML and XML markup slightly differently, mainly when considering CDATA
sections:
- in XML, any data inside a CDATA section will be considered as raw text (not XML) and the CDATA
delimiter will be droped ;
- in HTML, there is no such thing for
`
Initially, eslint-plugin-vue was using
eslint-plugin-html to lint code inside script tags. Since v3, eslint-plugin-vue is using itseslint-plugin-html
own parser, so it is _incompatible_ with . You should use eslint-plugin-vueeslint-plugin-html
exclusively and remove from your dependencies if you still have it.
eslint-plugin-html v4 requires at least ESLint v4.7. This is because a lot of internal changeseslint-plugin-html
occured in ESLint v4.7, including a new API to support autofixing in
preprocessors.
If you are still using an older version of ESLint, please consider upgrading, or keep using v3.
The big feature (and breaking change) in eslint-plugin-html` v4 is the ability to choose how scopes
are shared between script tags in the same HTML file.
If you are considering upgrading to v3, please read this guide.
A big thank you to @kuceb for the logo image!