Skip to content

Add alignAttributesVertically option to indent rule #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions docs/rules/indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ This rule enforces a consistent indentation style in `.svelte`. The default styl

<!--eslint-skip-->
<!-- prettier-ignore -->

```html
<script>
/* eslint @ota-meshi/svelte/indent: "error" */
Expand Down Expand Up @@ -51,6 +50,26 @@ CLICK ME!

</eslint-code-block>

::: warning Note
This rule only checks `.svelte` files and does not interfere with other `.js` files. Unfortunately the default `indent` rule when turned on will try to lint both, so in order to make them complementary you can use `overrides` setting and disable `indent` rule on `.svelte` files:
:::

```json
{
"rules": {
"@ota-meshi/svelte/indent": "error"
},
"overrides": [
{
"files": ["*.svelte"],
"rules": {
"indent": "off"
}
}
]
}
```

## :wrench: Options

```json
Expand All @@ -60,7 +79,8 @@ CLICK ME!
{
"indent": 2,
"ignoredNodes": [],
"switchCase": 1
"switchCase": 1,
"alignAttributesVertically": false
}
]
}
Expand All @@ -69,6 +89,7 @@ CLICK ME!
- `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.
- `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.
- `switchCase` ... Enforces indentation level for case clauses in switch statements. Default is `1`.
- `alignAttributesVertically` ... Condition for whether attributes should be vertically aligned to the first attribute in multiline case or not. Default is `false`

## :rocket: Version

Expand Down
91 changes: 8 additions & 83 deletions src/rules/indent-helpers/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ASTNode, SourceCode } from "../../types"
import type { AST } from "svelte-eslint-parser"
import { isOpeningParenToken, isClosingParenToken } from "eslint-utils"
import { isNotWhitespace, isWhitespace } from "./ast"
import type { OffsetContext } from "./offset-context"

export type AnyToken = AST.Token | AST.Comment
export type MaybeNode = {
Expand All @@ -14,93 +15,14 @@ export type IndentOptions = {
indentChar: " " | "\t"
indentSize: number
switchCase: number
alignAttributesVertically: boolean
ignoredNodes: string[]
}

export type IndentContext = {
sourceCode: SourceCode
options: IndentOptions
/**
* Set offset to the given tokens.
*/
setOffset: (
token: AnyToken | null | undefined | (AnyToken | null | undefined)[],
offset: number,
baseToken: AnyToken,
) => void
/**
* Copy offset to the given tokens from srcToken.
*/
copyOffset: (
token: AnyToken | null | undefined | (AnyToken | null | undefined)[],
srcToken: AnyToken,
) => void

/**
* Set baseline offset to the given token.
*/
setOffsetBaseLine: (
token: AnyToken | null | undefined | (AnyToken | null | undefined)[],
offset: number,
) => void
/**
* Ignore all tokens of the given node.
*/
ignore: (node: ASTNode) => void
}

/**
* Set offset to the given nodes.
* The first node is offsetted from the given base token.
*/
export function setOffsetNodes(
{ sourceCode, setOffset }: IndentContext,
nodes: (ASTNode | AnyToken | MaybeNode | null | undefined)[],
baseNodeOrToken: ASTNode | AnyToken | MaybeNode,
lastNodeOrToken: ASTNode | AnyToken | MaybeNode | null,
offset: number,
): void {
const baseToken = sourceCode.getFirstToken(baseNodeOrToken)

let prevToken = sourceCode.getLastToken(baseNodeOrToken)
for (const node of nodes) {
if (node == null) {
continue
}
const elementTokens = getFirstAndLastTokens(
sourceCode,
node,
prevToken.range[1],
)

let t: AnyToken | null = prevToken
while (
(t = sourceCode.getTokenAfter(t, {
includeComments: true,
filter: isNotWhitespace,
})) != null &&
t.range[1] <= elementTokens.firstToken.range[0]
) {
setOffset(t, offset, baseToken)
}
setOffset(elementTokens.firstToken, offset, baseToken)

prevToken = elementTokens.lastToken
}

if (lastNodeOrToken) {
const lastToken = sourceCode.getFirstToken(lastNodeOrToken)
let t: AnyToken | null = prevToken
while (
(t = sourceCode.getTokenAfter(t, {
includeComments: true,
filter: isNotWhitespace,
})) != null &&
t.range[1] <= lastToken.range[0]
) {
setOffset(t, offset, baseToken)
}
setOffset(lastToken, 0, baseToken)
}
offsets: OffsetContext
}

/**
Expand Down Expand Up @@ -146,7 +68,10 @@ export function isBeginningOfLine(
sourceCode: SourceCode,
node: ASTNode | AnyToken | MaybeNode,
): boolean {
const prevToken = sourceCode.getTokenBefore(node, { includeComments: false })
const prevToken = sourceCode.getTokenBefore(node, {
includeComments: false,
filter: isNotWhitespace,
})

return !prevToken || prevToken.loc.end.line < node.loc!.start.line
}
Loading