Skip to content

Commit 38ab449

Browse files
committedJul 18, 2022
feat: disable ESLint with HTML comments
1 parent f7e7fcc commit 38ab449

File tree

5 files changed

+108
-6
lines changed

5 files changed

+108
-6
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
UNRELEASED v7.0.0
22

33
- **Breaking: drop Node 10 support**
4+
- Allow temporarily disabling ESLint wit HTML comments
45

56
2021-09-20 v6.2.0
67

‎README.md

+36-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</div>
88

99
- [Usage](#usage)
10+
- [Disable ESLint](#disable-eslint)
1011
- [Multiple scripts tags in a HTML file](#multiple-scripts-tags-in-a-html-file)
1112
- [History](#history)
1213
- [XML support](#xml-support)
@@ -42,6 +43,32 @@ Example:
4243
}
4344
```
4445

46+
## Disable ESLint
47+
48+
To temporarily disable ESLint, use the `<!-- eslint-disable -->` HTML comment. Re-enable it with
49+
`<!-- eslint enable -->`. Example:
50+
51+
```html
52+
<!-- eslint-disable -->
53+
<script>
54+
var foo = 1
55+
</script>
56+
<!-- eslint-enable -->
57+
```
58+
59+
To disable ESLint for the next script tag only, use the `<!-- eslint-disable-next-script -->` HTML
60+
comment. Example:
61+
62+
```html
63+
<!-- eslint-disable-next-script -->
64+
<script>
65+
var foo = 1
66+
</script>
67+
```
68+
69+
Disabled script tags are completely ignored: their content will not be parsed as JavaScript. You can
70+
use this to disable script tags containing template syntax.
71+
4572
## Multiple scripts tags in a HTML file
4673

4774
When linting a HTML with multiple script tags, this plugin tries to emulate the browser behavior by
@@ -209,14 +236,17 @@ documentation](http://eslint.org/docs/user-guide/command-line-interface#ext).
209236

210237
`eslint-plugin-html` won't evaluate or remove your template markup. If you have template markup in
211238
your script tags, the resulting script may not be valid JavaScript, so `ESLint` will fail to parse
212-
it.
239+
it. Here are some workarounds:
240+
241+
- You can use [HTML comments to disable ESLint](#disable-eslint) for specific script tags.
213242

214-
For PHP, you can use [`eslint-plugin-php-markup`](https://github.com/tengattack/eslint-plugin-php-markup) to
215-
lint php files, it use a same way to process php markup like `eslint-plugin-html`.
243+
- For PHP, you can use
244+
[`eslint-plugin-php-markup`](https://github.com/tengattack/eslint-plugin-php-markup) to lint php
245+
files, it use a same way to process php markup like `eslint-plugin-html`.
216246

217-
Or another possible hacky workaround to make sure the code is valid JavaScript is to put your template
218-
markup inside a comment. When the template is rendered, the generated JS code must start with a new
219-
line, so it will be written below the comment. PHP example:
247+
- Another possible hacky workaround to make sure the code is valid JavaScript is to put your
248+
template markup inside a comment. When the template is rendered, the generated JS code must start
249+
with a new line, so it will be written below the comment. PHP example:
220250

221251
```html
222252
<script>

‎src/__tests__/__snapshots__/extract.js.snap

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ a;
99
]
1010
`;
1111

12+
exports[`disable comments ignores next script 1`] = `
13+
Array [
14+
"var bar = 2;
15+
",
16+
]
17+
`;
18+
19+
exports[`disable comments ignores script 1`] = `
20+
Array [
21+
"var bar = 2;
22+
",
23+
]
24+
`;
25+
1226
exports[`extract empty script tag (#7) 1`] = `
1327
Array [
1428
"",

‎src/__tests__/extract.js

+32
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,35 @@ it("extract multiple tags types", () => {
334334
javaScriptTagNames: ["script", "customscript"],
335335
})
336336
})
337+
338+
describe("disable comments", () => {
339+
it("ignores next script", () => {
340+
test({
341+
input: `
342+
<!-- eslint-disable-next-script -->
343+
<script>
344+
var foo = 1;
345+
</script>
346+
347+
<script>
348+
var bar = 2;
349+
</script>
350+
`,
351+
})
352+
})
353+
354+
it("ignores script", () => {
355+
test({
356+
input: `
357+
<!-- eslint-disable -->
358+
<script>
359+
var foo = 1;
360+
</script>
361+
<!-- eslint-enable -->
362+
<script>
363+
var bar = 2;
364+
</script>
365+
`,
366+
})
367+
})
368+
})

‎src/extract.js

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
const htmlparser = require("htmlparser2")
44
const TransformableString = require("./TransformableString")
55

6+
const NO_IGNORE = 0
7+
const IGNORE_NEXT = 1
8+
const IGNORE_UNTIL_ENABLE = 2
9+
610
function iterateScripts(code, options, onChunk) {
711
if (!code) return
812

@@ -12,6 +16,7 @@ function iterateScripts(code, options, onChunk) {
1216
let index = 0
1317
let inScript = false
1418
let cdata = []
19+
let ignoreState = NO_IGNORE
1520

1621
const chunks = []
1722
function pushChunk(type, end) {
@@ -36,6 +41,15 @@ function iterateScripts(code, options, onChunk) {
3641
return
3742
}
3843

44+
if (ignoreState === IGNORE_NEXT) {
45+
ignoreState = NO_IGNORE
46+
return
47+
}
48+
49+
if (ignoreState === IGNORE_UNTIL_ENABLE) {
50+
return
51+
}
52+
3953
inScript = true
4054
pushChunk("html", parser.endIndex + 1)
4155
},
@@ -76,6 +90,17 @@ function iterateScripts(code, options, onChunk) {
7690

7791
pushChunk("script", parser.endIndex + 1)
7892
},
93+
94+
oncomment(comment) {
95+
comment = comment.trim()
96+
if (comment === "eslint-disable") {
97+
ignoreState = IGNORE_UNTIL_ENABLE
98+
} else if (comment === "eslint-enable") {
99+
ignoreState = NO_IGNORE
100+
} else if (comment === "eslint-disable-next-script") {
101+
ignoreState = IGNORE_NEXT
102+
}
103+
},
79104
},
80105
{
81106
xmlMode: xmlMode === true,

0 commit comments

Comments
 (0)
Please sign in to comment.