Skip to content

Commit

Permalink
fix(svelte/indent): ensure proper snippet indent (#864)
Browse files Browse the repository at this point in the history
Fix `svelte/indent` rule for `SvelteSnippetBlock`, by ensuring that the
children and closing tags are properly indented.

Closes #863.

---------

Co-authored-by: Yosuke Ota <[email protected]>
  • Loading branch information
mikededo and ota-meshi authored Sep 27, 2024
1 parent c840776 commit 580e48a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-rings-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

fix(svelte/indent): ensure proper snippet indent
38 changes: 27 additions & 11 deletions packages/eslint-plugin-svelte/src/rules/indent-helpers/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,12 @@ export function defineVisitor(context: IndentContext): NodeListener {
offsets.setOffsetToken(token, 1, openToken);
}

const [openCloseTagToken, endAwaitToken, closeCloseTagToken] = sourceCode.getLastTokens(
node,
{
count: 3,
includeComments: false
}
);
const [openCloseTagToken, endKeyToken, closeCloseTagToken] = sourceCode.getLastTokens(node, {
count: 3,
includeComments: false
});
offsets.setOffsetToken(openCloseTagToken, 0, openToken);
offsets.setOffsetToken(endAwaitToken, 1, openCloseTagToken);
offsets.setOffsetToken(endKeyToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
SvelteSnippetBlock(node: AST.SvelteSnippetBlock) {
Expand All @@ -474,8 +471,8 @@ export function defineVisitor(context: IndentContext): NodeListener {
includeComments: false
});
offsets.setOffsetToken(snippetToken, 1, openToken);
const id = getFirstAndLastTokens(sourceCode, node.id);
offsets.setOffsetToken(id.firstToken, 1, snippetToken);
const snippetName = sourceCode.getTokenAfter(snippetToken)!;
offsets.setOffsetToken(snippetName, 1, snippetToken);

const leftParenToken = sourceCode.getTokenBefore(
node.params[0] || sourceCode.getLastToken(node),
Expand All @@ -492,8 +489,27 @@ export function defineVisitor(context: IndentContext): NodeListener {
includeComments: false
}
)!;
offsets.setOffsetToken(leftParenToken, 1, id.firstToken);
offsets.setOffsetToken(leftParenToken, 1, snippetName);
offsets.setOffsetElementList(node.params, leftParenToken, rightParenToken, 1);

const closeOpenTagToken = sourceCode.getTokenAfter(rightParenToken)!;
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);

for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}

const [openCloseTagToken, endSnippetToken, closeCloseTagToken] = sourceCode.getLastTokens(
node,
{ count: 3, includeComments: false }
);
offsets.setOffsetToken(openCloseTagToken, 0, openToken);
offsets.setOffsetToken(endSnippetToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
// ----------------------------------------------------------------------
// COMMENTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,63 @@
line: 15
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 18
column: 1
suggestions: null
- message: Expected indentation of 4 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: 22
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 2 spaces.
line: 23
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 24
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 25
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 26
column: 1
suggestions: null
- message: Expected indentation of 4 spaces but found 0 spaces.
line: 27
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 28
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 4 spaces but found 0 spaces.
line: 34
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 36
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 38
column: 1
suggestions: null
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,26 @@ a
}
)
}
<div>
{#snippet example()}
<div></div>
{/snippet}

{
#snippet example_2()
}
<div></div>
{
/snippet
}
</div>
{
#snippet example_3
(

)
}
<div></div>
{
/snippet
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,26 @@
}
)
}
<div>
{#snippet example()}
<div></div>
{/snippet}

{
#snippet example_2()
}
<div></div>
{
/snippet
}
</div>
{
#snippet example_3
(

)
}
<div></div>
{
/snippet
}

0 comments on commit 580e48a

Please sign in to comment.