Skip to content

Commit 9baef95

Browse files
committed
Add indent rule
1 parent 6b3ae52 commit 9baef95

File tree

105 files changed

+10945
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+10945
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ The rules with the following star :star: are included in the configs.
243243
|:--------|:------------|:---|
244244
| [@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 | |
245245
| [@ota-meshi/svelte/comment-directive](https://ota-meshi.github.io/eslint-plugin-svelte/rules/comment-directive.html) | support comment-directives in HTML template | :star: |
246+
| [@ota-meshi/svelte/indent](https://ota-meshi.github.io/eslint-plugin-svelte/rules/indent.html) | enforce consistent indentation | :wrench: |
246247
| [@ota-meshi/svelte/max-attributes-per-line](https://ota-meshi.github.io/eslint-plugin-svelte/rules/max-attributes-per-line.html) | enforce the maximum number of attributes per line | :wrench: |
247248
| [@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: |
248249
| [@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: |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The rules with the following star :star: are included in the `plugin:@ota-meshi/
1313
|:--------|:------------|:---|
1414
| [@ota-meshi/svelte/button-has-type](./button-has-type.md) | disallow usage of button without an explicit type attribute | |
1515
| [@ota-meshi/svelte/comment-directive](./comment-directive.md) | support comment-directives in HTML template | :star: |
16+
| [@ota-meshi/svelte/indent](./indent.md) | enforce consistent indentation | :wrench: |
1617
| [@ota-meshi/svelte/max-attributes-per-line](./max-attributes-per-line.md) | enforce the maximum number of attributes per line | :wrench: |
1718
| [@ota-meshi/svelte/no-at-debug-tags](./no-at-debug-tags.md) | disallow the use of `{@debug}` | :star: |
1819
| [@ota-meshi/svelte/no-at-html-tags](./no-at-html-tags.md) | disallow use of `{@html}` to prevent XSS attack | :star: |

docs/rules/indent.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "@ota-meshi/svelte/indent"
5+
description: "enforce consistent indentation"
6+
---
7+
8+
# @ota-meshi/svelte/indent
9+
10+
> enforce consistent indentation
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+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
14+
15+
## :book: Rule Details
16+
17+
This rule enforces a consistent indentation style in `.svelte`. The default style is 2 spaces.
18+
19+
- This rule checks all tags, also all expressions in directives and mustaches.
20+
- In the expressions, this rule supports ECMAScript 2020 syntaxes. It ignores unknown AST nodes, but it might be confused by non-standard syntaxes.
21+
22+
<eslint-code-block fix>
23+
24+
<!--eslint-skip-->
25+
<!-- prettier-ignore -->
26+
```html
27+
<script>
28+
/* eslint @ota-meshi/svelte/indent: "error" */
29+
function click() {}
30+
</script>
31+
32+
<!-- ✓ GOOD -->
33+
<button
34+
type="button"
35+
on:click="{click}"
36+
class="my-button primally"
37+
>
38+
CLICK ME!
39+
</button>
40+
41+
<!-- ✗ BAD -->
42+
<button
43+
type="button"
44+
on:click="{click}"
45+
class="my-button primally"
46+
>
47+
CLICK ME!
48+
</button>
49+
```
50+
51+
</eslint-code-block>
52+
53+
## :wrench: Options
54+
55+
```json
56+
{
57+
"@ota-meshi/svelte/indent": [
58+
"error",
59+
{
60+
"indent": 2,
61+
"ignoredNodes": [],
62+
"switchCase": 1
63+
}
64+
]
65+
}
66+
```
67+
68+
- `indent` (`number | "tab"`) ... The type of indentation. Default is `2`. If this is a number, it's the number of spaces for one indent. If this is `"tab"`, it uses one tab for one indent.
69+
- `ignoredNodes` ... Can be used to disable indentation checking for any AST node. This accepts an array of [selectors](https://eslint.org/docs/developer-guide/selectors). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern.
70+
- `switchCase` ... Enforces indentation level for case clauses in switch statements. Default is `1`.
71+
72+
## :mag: Implementation
73+
74+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/indent.ts)
75+
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/indent.ts)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"homepage": "https://github.com/ota-meshi/eslint-plugin-svelte#readme",
4848
"dependencies": {
4949
"debug": "^4.3.1",
50+
"eslint-utils": "^3.0.0",
5051
"svelte-eslint-parser": "^0.3.0"
5152
},
5253
"peerDependencies": {

src/rules/indent-helpers/ast.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { AST } from "svelte-eslint-parser"
2+
import type * as ESTree from "estree"
3+
type AnyToken = AST.Token | AST.Comment
4+
/**
5+
* Check whether the given token is a whitespace.
6+
*/
7+
export function isWhitespace(
8+
token: AnyToken | ESTree.Comment | null | undefined,
9+
): boolean {
10+
return token != null && token.type === "HTMLText" && !token.value.trim()
11+
}
12+
13+
/**
14+
* Check whether the given token is a not whitespace.
15+
*/
16+
export function isNotWhitespace(
17+
token: AnyToken | ESTree.Comment | null | undefined,
18+
): boolean {
19+
return (
20+
token != null && (token.type !== "HTMLText" || Boolean(token.value.trim()))
21+
)
22+
}

src/rules/indent-helpers/commons.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import type { ASTNode, SourceCode } from "../../types"
2+
import type { AST } from "svelte-eslint-parser"
3+
import { isOpeningParenToken, isClosingParenToken } from "eslint-utils"
4+
import { isNotWhitespace, isWhitespace } from "./ast"
5+
6+
type AnyToken = AST.Token | AST.Comment
7+
8+
export type IndentOptions = {
9+
indentChar: " " | "\t"
10+
indentSize: number
11+
switchCase: number
12+
ignoredNodes: string[]
13+
}
14+
export type IndentContext = {
15+
sourceCode: SourceCode
16+
options: IndentOptions
17+
/**
18+
* Set offset to the given tokens.
19+
*/
20+
setOffset: (
21+
token: AnyToken | null | undefined | (AnyToken | null | undefined)[],
22+
offset: number,
23+
baseToken: AnyToken,
24+
) => void
25+
/**
26+
* Copy offset to the given tokens from srcToken.
27+
*/
28+
copyOffset: (
29+
token: AnyToken | null | undefined | (AnyToken | null | undefined)[],
30+
srcToken: AnyToken,
31+
) => void
32+
33+
/**
34+
* Set baseline offset to the given token.
35+
*/
36+
setOffsetBaseLine: (
37+
token: AnyToken | null | undefined | (AnyToken | null | undefined)[],
38+
offset: number,
39+
) => void
40+
/**
41+
* Ignore all tokens of the given node.
42+
*/
43+
ignore: (node: ASTNode) => void
44+
}
45+
46+
/**
47+
* Set offset to the given nodes.
48+
* The first node is offsetted from the given base token.
49+
*/
50+
export function setOffsetNodes(
51+
{ sourceCode, setOffset }: IndentContext,
52+
nodes: (ASTNode | AnyToken | null | undefined)[],
53+
baseNodeOrToken: ASTNode | AnyToken,
54+
lastNodeOrToken: ASTNode | AnyToken | null,
55+
offset: number,
56+
): void {
57+
const baseToken = sourceCode.getFirstToken(baseNodeOrToken)
58+
59+
let prevToken = sourceCode.getLastToken(baseNodeOrToken)
60+
for (const node of nodes) {
61+
if (node == null) {
62+
continue
63+
}
64+
const elementTokens = getFirstAndLastTokens(
65+
sourceCode,
66+
node,
67+
prevToken.range[1],
68+
)
69+
70+
let t: AnyToken | null = prevToken
71+
while (
72+
(t = sourceCode.getTokenAfter(t, {
73+
includeComments: true,
74+
filter: isNotWhitespace,
75+
})) != null &&
76+
t.range[1] <= elementTokens.firstToken.range[0]
77+
) {
78+
setOffset(t, offset, baseToken)
79+
}
80+
setOffset(elementTokens.firstToken, offset, baseToken)
81+
82+
prevToken = elementTokens.lastToken
83+
}
84+
85+
if (lastNodeOrToken) {
86+
const lastToken = sourceCode.getFirstToken(lastNodeOrToken)
87+
let t: AnyToken | null = prevToken
88+
while (
89+
(t = sourceCode.getTokenAfter(t, {
90+
includeComments: true,
91+
filter: isNotWhitespace,
92+
})) != null &&
93+
t.range[1] <= lastToken.range[0]
94+
) {
95+
setOffset(t, offset, baseToken)
96+
}
97+
setOffset(lastToken, 0, baseToken)
98+
}
99+
}
100+
101+
/**
102+
* Get the first and last tokens of the given node.
103+
* If the node is parenthesized, this gets the outermost parentheses.
104+
* If the node have whitespace at the start and the end, they will be skipped.
105+
*/
106+
export function getFirstAndLastTokens(
107+
sourceCode: SourceCode,
108+
node: ASTNode | AnyToken,
109+
borderOffset = 0,
110+
): { firstToken: AST.Token; lastToken: AST.Token } {
111+
let firstToken = sourceCode.getFirstToken(node)
112+
let lastToken = sourceCode.getLastToken(node)
113+
114+
// Get the outermost left parenthesis if it's parenthesized.
115+
let left: AST.Token | null, right: AST.Token | null
116+
while (
117+
(left = sourceCode.getTokenBefore(firstToken)) != null &&
118+
(right = sourceCode.getTokenAfter(lastToken)) != null &&
119+
isOpeningParenToken(left) &&
120+
isClosingParenToken(right) &&
121+
borderOffset <= left.range[0]
122+
) {
123+
firstToken = left
124+
lastToken = right
125+
}
126+
127+
while (isWhitespace(firstToken) && firstToken.range[0] < lastToken.range[0]) {
128+
firstToken = sourceCode.getTokenAfter(firstToken) as AST.Token
129+
}
130+
while (isWhitespace(lastToken) && firstToken.range[0] < lastToken.range[0]) {
131+
lastToken = sourceCode.getTokenBefore(lastToken) as AST.Token
132+
}
133+
134+
return { firstToken, lastToken }
135+
}

0 commit comments

Comments
 (0)