All Projects → BenoitZugmeyer → Eslint Plugin Html

BenoitZugmeyer / Eslint Plugin Html

Licence: isc
An ESLint plugin to extract and lint scripts from HTML files.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Eslint Plugin Html

eslint-plugin-decorator-position
ESLint plugin for enforcing decorator position
Stars: ✭ 32 (-90.39%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-sql
SQL linting rules for ESLint.
Stars: ✭ 56 (-83.18%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-lodash-template
ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
Stars: ✭ 15 (-95.5%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Import
ESLint plugin with rules that help validate proper imports.
Stars: ✭ 3,722 (+1017.72%)
Mutual labels:  eslint-plugin, eslint
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.
Stars: ✭ 19 (-94.29%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-total-functions
An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript.
Stars: ✭ 72 (-78.38%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin
😎 基于 @lint-md,提供 eslint-plugin,让 lint-md 玩家在 IDE 中得到愉悦的文档编写体验。
Stars: ✭ 22 (-93.39%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-react-hook-form
ESLint plugin for react-hook-form
Stars: ✭ 27 (-91.89%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-ember-best-practices
Static analysis tools for enforcing best practices in Ember
Stars: ✭ 77 (-76.88%)
Mutual labels:  eslint, eslint-plugin
eslint-config-get-off-my-lawn
A highly opinionated, sharable config of ESLint rules to produce beautiful, readable JavaScript.
Stars: ✭ 44 (-86.79%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Functional
ESLint rules to disable mutation and promote fp in JavaScript and TypeScript.
Stars: ✭ 282 (-15.32%)
Mutual labels:  eslint, eslint-plugin
Eslint Config Auto
Automatically configure ESLint based on project dependencies
Stars: ✭ 302 (-9.31%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-layout-shift
ESLint plugin to force responsive media elements to set the width/height attributes
Stars: ✭ 15 (-95.5%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin
autofix some errors reported by eslint rules.
Stars: ✭ 74 (-77.78%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-ban
Ban some methods and functions
Stars: ✭ 23 (-93.09%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-expect-type
ESLint plugin with $ExpectType, $ExpectError, and $ExpectTypeSnapshot type assertions
Stars: ✭ 27 (-91.89%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-disable
Disable ESLint plugins using file path patterns and inline comments
Stars: ✭ 51 (-84.68%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-svelte
ESLint plugin for Svelte using AST
Stars: ✭ 22 (-93.39%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin
Enforcing best practices for Effector
Stars: ✭ 69 (-79.28%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Proper Arrows
ESLint rules to ensure proper arrow function definitions
Stars: ✭ 271 (-18.62%)
Mutual labels:  eslint, eslint-plugin

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.

Example:

{
    "plugins": [
        "html"
    ]
}

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:

{
  "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:

{
    "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:

{
    "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:

{
    "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:

{
    "plugins": [ "html" ],
    "settings": {
        "html/report-bad-indent": "error",
    }
}

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:

{
    "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
    }
}

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.

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.

Or 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 @Bkucera for the logo image!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].