1 | eslint-plugin-html
|
2 | ==================
|
3 |
|
4 | [![Build Status](https://travis-ci.org/BenoitZugmeyer/eslint-plugin-html.svg?branch=master)](https://travis-ci.org/BenoitZugmeyer/eslint-plugin-html)
|
5 |
|
6 | This [`ESLint`](http://eslint.org) plugin allows linting and fixing inline scripts contained in HTML
|
7 | files.
|
8 |
|
9 | Migration to v3
|
10 | ---------------
|
11 |
|
12 | If you are considering upgrading to v3, please read [this guide](MIGRATION_TO_V3.md).
|
13 |
|
14 | Usage
|
15 | -----
|
16 |
|
17 | Simply install via `npm install --save-dev eslint-plugin-html` and add the plugin to your ESLint
|
18 | configuration. See
|
19 | [ESLint documentation](http://eslint.org/docs/user-guide/configuring#configuring-plugins).
|
20 |
|
21 | Example:
|
22 |
|
23 | ```javascript
|
24 | {
|
25 | "plugins": [
|
26 | "html"
|
27 | ]
|
28 | }
|
29 | ```
|
30 |
|
31 | Note: by default, when executing the `eslint` command on a directory, only `.js` files will be
|
32 | linted. You will have to specify extra extensions with the `--ext` option. Example: `eslint --ext
|
33 | .html,.js src` will lint both `.html` and `.js` files in the `src` directory. See [ESLint
|
34 | documentation](http://eslint.org/docs/user-guide/command-line-interface#ext).
|
35 |
|
36 | XML support
|
37 | -----------
|
38 |
|
39 | This plugin parses HTML and XML markup slightly differently, mainly when considering `CDATA`
|
40 | sections:
|
41 | * in XML, any data inside a `CDATA` section will be considered as raw text (not XML) and the `CDATA`
|
42 | delimiter will be droped ;
|
43 | * in HTML, there is no such thing for `<script>` tags: the `CDATA` delimiter is considered as normal
|
44 | text and thus, part of the script.
|
45 |
|
46 |
|
47 | Settings
|
48 | --------
|
49 |
|
50 | > Note: all settings can be written either as `"html/key": value` or in a nested object `"html": {
|
51 | > "key": value }`
|
52 |
|
53 | ### `html/html-extensions`
|
54 |
|
55 | By default, this plugin will only consider files ending with those extensions as HTML: `.erb`,
|
56 | `.handlebars`, `.hbs`, `.htm`, `.html`, `.mustache`, `.nunjucks`, `.php`, `.tag`, `.twig`, `.vue`,
|
57 | `.we`. You can set your own list of HTML extensions by using this setting. Example:
|
58 |
|
59 | ```javascript
|
60 | {
|
61 | "plugins": [ "html" ],
|
62 | "settings": {
|
63 | "html/html-extensions": [".html", ".we"], // consider .html and .we files as HTML
|
64 | }
|
65 | }
|
66 | ```
|
67 |
|
68 |
|
69 | ### `html/xml-extensions`
|
70 |
|
71 | By default, this plugin will only consider files ending with those extensions as XML: `.xhtml`,
|
72 | `.xml`. You can set your own list of XML extensions by using this setting. Example:
|
73 |
|
74 | ```javascript
|
75 | {
|
76 | "plugins": [ "html" ],
|
77 | "settings": {
|
78 | "html/xml-extensions": [".html"], // consider .html files as XML
|
79 | }
|
80 | }
|
81 | ```
|
82 |
|
83 |
|
84 | ### `html/indent`
|
85 |
|
86 | By default, the code between `<script>` tags is dedented according to the first non-empty line. The
|
87 | setting `html/indent` allows to ensure that every script tags follow an uniform indentation. Like
|
88 | the `indent` rule, you can pass a number of spaces, or `"tab"` to indent with one tab. Prefix this
|
89 | value with a `+` to be relative to the `<script>` tag indentation. Example:
|
90 |
|
91 | ```javascript
|
92 | {
|
93 | "plugins": [ "html" ],
|
94 | "settings": {
|
95 | "html/indent": "0", // code should start at the beginning of the line (no initial indentation).
|
96 | "html/indent": "+2", // indentation is the <script> indentation plus two spaces.
|
97 | "html/indent": "tab", // indentation is one tab at the beginning of the line.
|
98 | }
|
99 | }
|
100 | ```
|
101 |
|
102 |
|
103 | ### `html/report-bad-indent`
|
104 |
|
105 | By default, this plugin won't warn if it encounters a problematic indentation (ex: a line is under
|
106 | indented). If you want to make sure the indentation is correct, use the `html/report-bad-indent` in
|
107 | conjunction with the `indent` rule. Pass `"warn"` or `1` to display warnings, `"error"` or `2` to
|
108 | display errors. Example:
|
109 |
|
110 | ```javascript
|
111 | {
|
112 | "plugins": [ "html" ],
|
113 | "settings": {
|
114 | "html/report-bad-indent": "error",
|
115 | }
|
116 | }
|
117 | ```
|
118 |
|
119 |
|
120 | ### `html/javascript-mime-types`
|
121 |
|
122 | By default, the code between `<script>` tags is considered as JavaScript code only if there is no
|
123 | `type` attribute or if its value matches the pattern
|
124 | `/^(application|text)\/(x-)?(javascript|babel|ecmascript-6)$/i`. You can customize the types that
|
125 | should be considered as JavaScript by providing one or multiple MIME types. If a MIME type starts
|
126 | with a `/`, it will be considered as a regular expression. Example:
|
127 |
|
128 | ```javascript
|
129 | {
|
130 | "plugins": [ "html" ],
|
131 | "settings": {
|
132 | "html/javascript-mime-types": ["text/javascript", "text/jsx"], // also use script tags with a "text/jsx" type attribute
|
133 | "html/javascript-mime-types": "/^text\\/(javascript|jsx)$/", // same thing
|
134 | }
|
135 | }
|
136 | ```
|
137 |
|
138 | Troubleshooting
|
139 | ---------------
|
140 |
|
141 | ### Linting templates (or PHP)
|
142 |
|
143 | `eslint-plugin-html` won't evaluate or remove your template markup. If you have template markup in
|
144 | your script tags, the resulting script may not be valid JavaScript, so `ESLint` will fail to parse
|
145 | it.
|
146 |
|
147 | One possible hacky workaround to make sure the code is valid JavaScript is to put your template
|
148 | markup inside a comment. When the template is rendered, the generated JS code must start with a new
|
149 | line, so it will be written below the comment. PHP example:
|
150 |
|
151 | ```html
|
152 | <script>
|
153 | var mydata;
|
154 | // <?= "\n mydata = " . json_encode($var) . ";" ?>
|
155 | console.log(mydata);
|
156 | </script>
|
157 | ```
|
158 |
|
159 |
|
160 | ### Linting VUE files
|
161 |
|
162 | Initially, [`eslint-plugin-vue`](https://github.com/vuejs/eslint-plugin-vue) was using
|
163 | `eslint-plugin-html` to lint code inside script tags. Since v3, `eslint-plugin-vue` is using its
|
164 | own parser, so it is *incompatible* with `eslint-plugin-html`. You should remove
|
165 | `eslint-plugin-html` from your dependencies if you still have this.
|