Skip to content

fix: broken indentation of if condition in svelte/indent rule #802

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 2 commits into from
Jun 23, 2024
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
5 changes: 5 additions & 0 deletions .changeset/silent-pugs-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

fix: broken indentation of if condition in `svelte/indent` rule
26 changes: 19 additions & 7 deletions packages/eslint-plugin-svelte/src/rules/indent-helpers/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export function defineVisitor(context: IndentContext): NodeListener {
const { sourceCode, offsets, options } = context;

/**
* Find the root of left node.
* Find the head of chaining nodes.
*/
function getRootLeft(
function getChainHeadNode(
node:
| TSESTree.AssignmentExpression
| TSESTree.AssignmentPattern
Expand All @@ -49,13 +49,20 @@ export function defineVisitor(context: IndentContext): NodeListener {
parent.type === 'LogicalExpression')
) {
const prevToken = sourceCode.getTokenBefore(target);
if (prevToken && isOpeningParenToken(prevToken)) {
const nextToken = sourceCode.getTokenAfter(target);
if (
prevToken &&
isOpeningParenToken(prevToken) &&
nextToken &&
isClosingParenToken(nextToken)
) {
// The chain is broken because it is enclosed in parentheses.
break;
}
target = parent;
parent = getParent(target);
}
return target.left;
return target;
}

const visitor = {
Expand Down Expand Up @@ -116,17 +123,22 @@ export function defineVisitor(context: IndentContext): NodeListener {
| TSESTree.BinaryExpression
| TSESTree.LogicalExpression
) {
const leftNode = getRootLeft(node);
const baseNode = getChainHeadNode(node);
const opToken = sourceCode.getTokenAfter(node.left, {
filter: isNotClosingParenToken,
includeComments: false
})!;
const baseToken =
baseNode.type === 'AssignmentExpression' || baseNode.type === 'AssignmentPattern'
? sourceCode.getFirstToken(baseNode)
: getFirstAndLastTokens(sourceCode, baseNode).firstToken;
const leftToken = getFirstAndLastTokens(sourceCode, node.left).firstToken;
const rightToken = getFirstAndLastTokens(sourceCode, node.right).firstToken;

offsets.setOffsetToken(
[opToken, rightToken],
[leftToken === baseToken ? null : leftToken, opToken, rightToken],
1,
getFirstAndLastTokens(sourceCode, leftNode).firstToken
baseToken
);
},
AssignmentPattern(node: TSESTree.AssignmentPattern) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 3
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 4
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 5
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 6
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 7
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 8
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 9
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 10
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 11
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 12
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 13
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 14
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 15
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 16
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 17
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 18
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 19
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 20
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 21
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 22
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 23
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 24
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 25
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 26
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 27
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 28
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 29
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 30
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 31
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 32
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 33
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 34
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 35
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 36
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 37
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 38
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 39
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 40
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 41
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 42
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 43
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 44
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 45
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 46
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 47
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 48
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 49
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 50
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 51
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 52
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 53
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 54
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 55
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 56
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 57
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 58
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 59
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 60
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 61
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 62
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 63
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 64
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 65
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 66
column: 1
suggestions: null
- message: Expected indentation of 10 spaces but found 0 spaces.
line: 67
column: 1
suggestions: null
- message: Expected indentation of 10 spaces but found 0 spaces.
line: 68
column: 1
suggestions: null
- message: Expected indentation of 10 spaces but found 0 spaces.
line: 69
column: 1
suggestions: null
- message: Expected indentation of 10 spaces but found 0 spaces.
line: 70
column: 1
suggestions: null
- message: Expected indentation of 10 spaces but found 0 spaces.
line: 71
column: 1
suggestions: null
- message: Expected indentation of 10 spaces but found 0 spaces.
line: 72
column: 1
suggestions: null
- message: Expected indentation of 6 spaces but found 0 spaces.
line: 73
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 74
column: 1
suggestions: null
- message: Expected indentation of 8 spaces but found 0 spaces.
line: 75
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 76
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 77
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 78
column: 1
suggestions: null
Loading
Loading