Skip to content

Commit 9878994

Browse files
authored
Add comment-directive (#14)
1 parent ccd74f1 commit 9878994

File tree

14 files changed

+1190
-5
lines changed

14 files changed

+1190
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ The rules with the following star :star: are included in the configs.
141141
| Rule ID | Description | |
142142
|:--------|:------------|:---|
143143
| [@ota-meshi/svelte/button-has-type](https://ota-meshi.github.io/eslint-plugin-svelte/rules/button-has-type.html) | disallow usage of button without an explicit type attribute | |
144+
| [@ota-meshi/svelte/comment-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/comment-directive.html) | support comment-directives in HTML template | :star: |
144145
| [@ota-meshi/svelte/no-at-debug-tags](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-at-debug-tags.html) | disallow the use of `{@debug}` | :star: |
145146
| [@ota-meshi/svelte/no-at-html-tags](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-at-html-tags.html) | disallow use of `{@html}` to prevent XSS attack | :star: |
146147
| [@ota-meshi/svelte/no-dupe-else-if-blocks](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-dupe-else-if-blocks.html) | disallow duplicate conditions in `{#if}` / `{:else if}` chains | :star: |

docs/.vuepress/components/components/EslintPluginEditor.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
class="eslint-code-block"
88
:language="language"
99
:filename="fileName"
10+
:preprocess="preprocess"
11+
:postprocess="postprocess"
1012
:dark="dark"
1113
:format="format"
1214
:fix="fix"
@@ -64,6 +66,8 @@ export default {
6466
insertSpaces: true,
6567
tabSize: 2,
6668
},
69+
preprocess: plugin.processors[".svelte"].preprocess,
70+
postprocess: plugin.processors[".svelte"].postprocess,
6771
}
6872
},
6973

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The rules with the following star :star: are included in the `plugin:@ota-meshi/
1212
| Rule ID | Description | |
1313
|:--------|:------------|:---|
1414
| [@ota-meshi/svelte/button-has-type](./button-has-type.md) | disallow usage of button without an explicit type attribute | |
15+
| [@ota-meshi/svelte/comment-directive](./comment-directive.md) | support comment-directives in HTML template | :star: |
1516
| [@ota-meshi/svelte/no-at-debug-tags](./no-at-debug-tags.md) | disallow the use of `{@debug}` | :star: |
1617
| [@ota-meshi/svelte/no-at-html-tags](./no-at-html-tags.md) | disallow use of `{@html}` to prevent XSS attack | :star: |
1718
| [@ota-meshi/svelte/no-dupe-else-if-blocks](./no-dupe-else-if-blocks.md) | disallow duplicate conditions in `{#if}` / `{:else if}` chains | :star: |

docs/rules/comment-directive.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "@ota-meshi/svelte/comment-directive"
5+
description: "support comment-directives in HTML template"
6+
---
7+
8+
# @ota-meshi/svelte/comment-directive
9+
10+
> support comment-directives in HTML template
11+
12+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
13+
- :gear: This rule is included in `"plugin:@ota-meshi/svelte/base"` and `"plugin:@ota-meshi/svelte/recommended"`.
14+
15+
Sole purpose of this rule is to provide `eslint-disable` functionality in the template HTML.
16+
It supports usage of the following comments:
17+
18+
- `eslint-disable`
19+
- `eslint-enable`
20+
- `eslint-disable-line`
21+
- `eslint-disable-next-line`
22+
23+
::: warning Note
24+
We can't write HTML comments in tags.
25+
:::
26+
27+
## :book: Rule Details
28+
29+
ESLint doesn't provide any API to enhance `eslint-disable` functionality and ESLint rules cannot affect other rules. But ESLint provides [processors API](https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins).
30+
31+
This rule sends all `eslint-disable`-like comments to the post-process of the `.svelte` file processor, then the post-process removes the reported errors in disabled areas.
32+
33+
<eslint-code-block>
34+
35+
<!--eslint-skip-->
36+
37+
```html
38+
<script>
39+
/* eslint @ota-meshi/svelte/comment-directive: "error", no-undef: "error" */
40+
</script>
41+
42+
<!-- eslint-disable-next-line no-undef -->
43+
<UndefComponent />
44+
```
45+
46+
</eslint-code-block>
47+
48+
The `eslint-disable`-like comments can include descriptions to explain why the comment is necessary. The description must occur after the directive and is separated from the directive by two or more consecutive `-` characters. For example:
49+
50+
<eslint-code-block>
51+
52+
<!--eslint-skip-->
53+
54+
```html
55+
<script>
56+
/* eslint @ota-meshi/svelte/comment-directive: "error", no-undef: "error" */
57+
</script>
58+
59+
<!-- eslint-disable-next-line no-undef -- Here's a description about why this disabling is necessary. -->
60+
<UndefComponent />
61+
```
62+
63+
</eslint-code-block>
64+
65+
## :wrench: Options
66+
67+
Nothing.
68+
69+
## :books: Further Reading
70+
71+
- [Disabling rules with inline comments]
72+
73+
[disabling rules with inline comments]: https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments
74+
75+
## :mag: Implementation
76+
77+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/comment-directive.ts)
78+
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/comment-directive.ts)

src/configs/recommended.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export = {
66
extends: [baseExtend],
77
rules: {
88
// @ota-meshi/eslint-plugin-svelte rules
9+
"@ota-meshi/svelte/comment-directive": "error",
910
"@ota-meshi/svelte/no-at-debug-tags": "warn",
1011
"@ota-meshi/svelte/no-at-html-tags": "error",
1112
"@ota-meshi/svelte/no-dupe-else-if-blocks": "error",

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RuleModule } from "./types"
22
import { rules as ruleList } from "./utils/rules"
33
import base from "./configs/base"
44
import recommended from "./configs/recommended"
5+
import { processor } from "./processor"
56

67
const configs = {
78
base,
@@ -16,4 +17,8 @@ const rules = ruleList.reduce((obj, r) => {
1617
export = {
1718
configs,
1819
rules,
20+
processors: {
21+
".svelte": processor,
22+
svelte: processor,
23+
},
1924
}

src/processor/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Linter } from "eslint"
2+
import type { Shared } from "../shared"
3+
import { beginShared, terminateShared } from "../shared"
4+
export const processor = {
5+
preprocess(code: string, filename: string): string[] {
6+
if (filename) {
7+
beginShared(filename)
8+
}
9+
10+
return [code]
11+
},
12+
13+
postprocess(
14+
[messages]: Linter.LintMessage[][],
15+
filename: string,
16+
): Linter.LintMessage[] {
17+
const shared = terminateShared(filename)
18+
if (shared) {
19+
return filter(messages, shared)
20+
}
21+
22+
return messages
23+
},
24+
25+
supportsAutofix: true,
26+
}
27+
28+
/** Filter */
29+
function filter(
30+
messages: Linter.LintMessage[],
31+
shared: Shared,
32+
): Linter.LintMessage[] {
33+
if (shared.commentDirectives.length === 0) {
34+
return messages
35+
}
36+
let filteredMessages = messages
37+
for (const cd of shared.commentDirectives) {
38+
filteredMessages = cd.filterMessages(filteredMessages)
39+
}
40+
return filteredMessages
41+
}

0 commit comments

Comments
 (0)