包详细信息

eslint-plugin-html

BenoitZugmeyer2.6mISC8.1.2

A ESLint plugin to lint and fix inline scripts contained in HTML files.

eslint-plugin, eslintplugin, eslint, html

自述文件

eslint-plugin-html

NPM version Tests Status

A ESLint plugin to lint and fix inline scripts contained in HTML files.

Usage

Simply install via npm install --save-dev eslint-plugin-html and add the plugin to your ESLint configuration. See ESLint documentation.

<summary>Example with ESLint 9 and above (flat config)</summary> javascript import html from "eslint-plugin-html" export default [ { files: ["**/*.html"], plugins: { html }, }, ]
<summary>Example with ESLint 8 and below</summary> json { "plugins": ["html"] }

Disabling ESLint

To temporarily disable ESLint, use the <!-- eslint-disable --> HTML comment. Re-enable it with <!-- eslint enable -->. Example:

<!-- eslint-disable -->
<script>
  var foo = 1
</script>
<!-- eslint-enable -->

To disable ESLint for the next script tag only, use the <!-- eslint-disable-next-script --> HTML comment. Example:

<!-- eslint-disable-next-script -->
<script>
  var foo = 1
</script>

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.

Linting HTML

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.

Multiple scripts tags in a HTML file

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: <script type="module"> and most transpiled code), where each script tag gets its own top-level scope.

ESLint has already an option to tell the parser if the script are modules. eslint-plugin-html will use this option as well to know if the scopes should be shared (the default) or not. To change this, just set it in your ESLint configuration:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { // ... languageOptions: { sourceType: "module", }, }, ]
<summary>ESLint 8 and below</summary> json { "parserOptions": { "sourceType": "module" } }

To illustrate this behavior, consider this HTML extract:

<script>
  var foo = 1
</script>

<script>
  alert(foo)
</script>

This is perfectly valid by default, and the ESLint rules no-unused-vars and no-undef shouldn't complain. But if those scripts are considerated as ES modules, no-unused-vars should report an error in the first script, and no-undef should report an error in the second script.

History

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.

XML support

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 <script> tags: the CDATA delimiter is considered as normal text and thus, part of the script.

Settings

Note: all settings can be written either as "html/key": value or in a nested object "html": { "key": value }

html/html-extensions

By default, this plugin will only consider files ending with those extensions as HTML: .erb, .handlebars, .hbs, .htm, .html, .mustache, .nunjucks, .php, .tag, .twig, .we. You can set your own list of HTML extensions by using this setting. Example:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { files: ["**/*.html", "**/*.we"], plugins: { html }, settings: { "html/html-extensions": [".html", ".we"], // consider .html and .we files as HTML }, }, ] Note: you need to specify extensions twice, which is not ideal. This should be imporved in the future.
<summary>ESLint 8 and below</summary> json { "plugins": ["html"], "settings": { "html/html-extensions": [".html", ".we"] // consider .html and .we files as HTML } }

html/xml-extensions

By default, this plugin will only consider files ending with those extensions as XML: .xhtml, .xml. You can set your own list of XML extensions by using this setting. Example:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { files: ["**/*.html"], plugins: { html }, settings: { "html/xtml-extensions": [".html"], // consider .html files as XML }, }, ] Note: you need to specify extensions twice, which is not ideal. This should be imporved in the future.
<summary>ESLint 8 and below</summary> json { "plugins": ["html"], "settings": { "html/xml-extensions": [".html"] // consider .html files as XML } }

html/indent

By default, the code between <script> tags is dedented according to the first non-empty line. The setting html/indent allows to ensure that every script tags follow an uniform indentation. Like the indent rule, you can pass a number of spaces, or "tab" to indent with one tab. Prefix this value with a + to be relative to the <script> tag indentation. Example:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { files: ["**/*.html"], plugins: { html }, settings: { "html/indent": "0", // code should start at the beginning of the line (no initial indentation). "html/indent": "+2", // indentation is the <script> indentation plus two spaces. "html/indent": "tab", // indentation is one tab at the beginning of the line. }, }, ]
<summary>ESLint 8 and below</summary> json { "plugins": ["html"], "settings": { "html/indent": "0", // code should start at the beginning of the line (no initial indentation). "html/indent": "+2", // indentation is the <script> indentation plus two spaces. "html/indent": "tab" // indentation is one tab at the beginning of the line. } }

html/report-bad-indent

By default, this plugin won't warn if it encounters a problematic indentation (ex: a line is under indented). If you want to make sure the indentation is correct, use the html/report-bad-indent in conjunction with the indent rule. Pass "warn" or 1 to display warnings, "error" or 2 to display errors. Example:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { files: ["**/*.html"], plugins: { html }, settings: { "html/report-bad-indent": "error", }, }, ]
<summary>ESLint 8 and below</summary> json { "plugins": ["html"], "settings": { "html/report-bad-indent": "error" } }

html/javascript-tag-names

By default, the code between <script> tags is considered as JavaScript. You can customize which tags should be considered JavaScript by providing one or multiple tag names.

Example:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { files: ["**/*.html"], plugins: { html }, settings: { "html/javascript-tag-names": ["script", "customscript"], }, }, ]
<summary>ESLint 8 and below</summary> json { "plugins": ["html"], "settings": { "html/javascript-tag-names": ["script", "customscript"] } }

html/javascript-mime-types

By default, the code between <script> tags is considered as JavaScript code only if there is no type attribute or if its value matches the pattern (application|text)/(x-)?(javascript|babel|ecmascript-6) or module (case insensitive). You can customize the types that should be considered as JavaScript by providing one or multiple MIME types. If a MIME type starts with a /, it will be considered as a regular expression. Example:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { files: ["**/*.html"], plugins: { html }, settings: { "html/javascript-mime-types": ["text/javascript", "text/jsx"], // also use script tags with a "text/jsx" type attribute "html/javascript-mime-types": "/^text\\/(javascript|jsx)$/", // same thing }, }, ]
<summary>ESLint 8 and below</summary> json { "plugins": ["html"], "settings": { "html/javascript-mime-types": ["text/javascript", "text/jsx"], // also use script tags with a "text/jsx" type attribute "html/javascript-mime-types": "/^text\\/(javascript|jsx)$/" // same thing } }

html/ignore-tags-without-type

By default, the code between <script> tags is considered JavaScript if there is no type attribute. You can set this setting to true to ignore script tags without a type attribute. Example:

<summary>ESLint 9 and above (flat config)</summary> javascript export default [ { files: ["**/*.html"], plugins: { html }, settings: { "html/ignore-tags-without-type": true, }, }, ]
<summary>ESLint 8 and below</summary> json { "plugins": ["html"], "settings": { "html/ignore-tags-without-type": true } }

Troubleshooting

No file linted when running eslint on a directory

By default, when executing the eslint command on a directory, only .js files will be linted. You will have to specify extra extensions with the --ext option. Example: eslint --ext .html,.js src will lint both .html and .js files in the src directory. See ESLint documentation.

Linting templates (or PHP)

eslint-plugin-html won't evaluate or remove your template markup. If you have template markup in your script tags, the resulting script may not be valid JavaScript, so ESLint will fail to parse it. Here are some workarounds:

  • You can use HTML comments to disable ESLint for specific script tags.

  • For PHP, you can use eslint-plugin-php-markup to lint php files, it use a same way to process php markup like eslint-plugin-html.

  • Another possible hacky workaround to make sure the code is valid JavaScript is to put your template markup inside a comment. When the template is rendered, the generated JS code must start with a new line, so it will be written below the comment. PHP example:

<script>
  var mydata
  // <?= "\n mydata = " . json_encode($var) . ";" ?>
  console.log(mydata)
</script>

Linting VUE files

Initially, eslint-plugin-vue was using eslint-plugin-html to lint code inside script tags. Since v3, eslint-plugin-vue is using its own parser, so it is incompatible with eslint-plugin-html. You should use eslint-plugin-vue exclusively and remove eslint-plugin-html from your dependencies if you still have it.

Migration from older versions

To v4

eslint-plugin-html v4 requires at least ESLint v4.7. This is because a lot of internal changes 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 eslint-plugin-html 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.

To v3

If you are considering upgrading to v3, please read this guide.

Credits

A big thank you to @kuceb for the logo image!

更新日志

2024-09-21 v8.1.2

  • Fix sourceType config for ESLint 9
  • Don't require espree directly #271

2024-04-22 v8.1.1

  • Fix compatibility with @eslint/config-inspector #267
  • Drop Jest #266
  • Update dependencies

2024-04-10 v8.1.0

  • Introduce the html/ignore-tags-without-type setting #249
  • Fix files with unknown extensions being considered as XML #257
  • Fix plugin applied even if it's not in the configuration #263
  • Update dependencies

2024-02-09 v8.0.0

  • Breaking: drop Node 12 and 14 support
  • ESLint 9 support (flat config)

2022-07-25 v7.1.0

  • Add support for @html-eslint and @angular-eslint/template plugins #176

2022-07-18 v7.0.0

  • Breaking: drop Node 10 support
  • Allow temporarily disabling ESLint with HTML comments
  • Allow configuring which script tag names should be considered as JavaScript #137

2021-09-20 v6.2.0

  • Update dependencies
  • Fix support for ESLint v8.0.0-beta.2
  • Add .riot extension #146

2021-03-08 v6.1.2

  • Update htmlparser2 #141
  • Update dependencies

2020-11-11 v6.1.1

  • Update dependencies
  • Move from travis to github actions

2020-09-06 v6.1.0

  • Allow dots in extensions #127

2020-08-08 v6.0.3

  • Update dependencies

2020-04-15 v6.0.2

  • Remove npm-shrinkwrap.json from the npm package #125

2020-04-05 v6.0.1 (the 5th anniversary release)

  • Update dependencies
  • Run CI against eslint@next
  • Add eslint-plugin-php-markup reference to the README

2019-06-26 v6.0.0 (the 666 release)

  • Breaking: drop Node 6 support
  • Fix support for ESLint v6

2019-05-13 v5.0.5

  • Fix support for ESLint v6.0.0-alpha.1 (again)
  • Improved integration tests

2019-05-12 v5.0.4

  • Fix support for ESLint v6.0.0-alpha.1 #117

2019-02-02 v5.0.3

  • Fix support for parserOptions.ecmaFeatures.globalReturn: true while sharing scope between multiple script tags

2019-02-02 v5.0.2

  • Fix support for the --report-unused-disabled-directives option #111

2019-02-02 v5.0.1

  • Fix compatibility with ESLint 5.13.0
  • Update dependencies

2018-11-11 v5.0.0

  • Breaking: drop Node 4 support
  • Breaking: don't lint .vue files by default
  • Update dependencies

2018-09-22 v4.0.6

  • Ignore script tags with a src attribute #102
  • More detailed error reporting

2018-06-20 v4.0.5

  • Fix typo regression from v4.0.4

2018-06-20 v4.0.4

  • Request detailed informations when ESLint is not found

2018-04-11 v4.0.3

  • Prevent patching ESLint multiple times #89

2018-01-24 v4.0.2

  • Fix compatibility with tslint #85

2017-11-22 v4.0.1

  • Fix processing files after processing a HTML file without script tag #82

2017-11-12 v4.0.0

  • Breaking: drop ESLint < 4.7 support
  • Breaking: Non-module script tags are sharing the global scope #66
  • Lint script tags with type="module" by default

2017-09-16 v3.2.2

  • Fix ESLint 4.7 support

2017-09-03 v3.2.1

  • Fix ESLint 4.6 support #77

2017-08-12 v3.2.0

  • improve compatibility with the prettier plugin by ignoring the first empty script line #76
  • fix compatibility with the eslint-comments plugin #70
  • add some Troubleshooting documentation about linting template markup and Vue files

2017-07-18 v3.1.1

  • Fix node 4 support #71 #72

2017-07-11 v3.1.0

  • Remap messages "source" property to fix custom formatters support #69

2017-06-12 v3.0.0 migration guide

  • Breaking: lint script tags separately #49 #55 #56
  • ESLint 4 support #57
  • Support nested settings in a "html" object #58

2017-05-06 v2.0.3

  • No change, new version to work around a publish issue

2017-05-06 v2.0.2

  • Support self-closing script tags in XHTML #52
  • Fix tests for ESLint 4.0.0-alpha.{0-2}

2017-02-16 v2.0.1

  • Support for empty filenames #48
  • Support for empty files #46

2017-01-25 v2.0.0

  • Breaking: drop html/xml-mode setting
  • eslint --fix support #23
  • Allow configuring HTML and XML extensions via ESLint config
  • Allow configuring JS MIME types via ESLint config
  • Report correct end locations of error messages
  • Report correct fix ranges of error messages

2016-11-18 v1.7.0

  • Ignore all warnings for non-script lines #37

2016-11-05 v1.6.0

  • Patch all loaded eslint packages #28

2016-10-23 v1.5.5

  • Fix typo in handlebars extension #36

2016-10-22 v1.5.4

  • Support .nunjucks files #35

2016-09-24 v1.5.3

  • Fix tests for ESLint 3.6.0 #32
  • Support .we files #30

2016-08-06 v1.5.2

  • Laxer way to retrieve the eslint package #27
  • Support .erb files #26
  • Support .tag files #25

2016-05-22 v1.5.1

  • Republish v1.5.0 on NPM, files were missing because of npm/npm#5082

2016-05-22 v1.5.0

  • Support .handlebars files
  • Introduce the html/xml-mode setting and parse XML files as XML #20
  • Support .twig files #21
  • Support the text/ecmascript-6 mime type #17

2016-02-16 v1.4.0

  • Support .php files

2016-01-27 v1.3.0

  • Fix crlf newlines support #16
  • Introduce the html/report-bad-indent setting
  • Introduce the html/indent setting
  • Support more standard JS MIME types #15

2016-01-15 v1.2.0

  • Support the text/babel mime type
  • Support .mustache files #11

2015-11-02 v1.1.0

  • Stop non-JS lines breaking no-multiple-empty-lines #8

2015-08-22 v1.0.4

  • Check if there is some lines before striping the last line spaces #7

2015-08-17 v1.0.3

  • Support .hbs files

2015-06-30 v1.0.2

  • Support .vue files #4

2015-04-18 v1.0.1

  • Fix space eating issue
  • Introduce unit tests

2015-04-05 v1.0.0

  • Add README
  • Initial commit