Skip to content

feat: add support for {@snippet}and {@render} in indent rule #622

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 4 commits into from
Nov 22, 2023
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/calm-fans-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

feat: add support for `{@snippet}`and `{@render}` in indent rule
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/prettier-playground
/tests/fixtures/rules/indent/invalid/ts
/tests/fixtures/rules/indent/invalid/ts-v5
/tests/fixtures/rules/indent/invalid/snippets01-input.svelte
/tests/fixtures/rules/indent/valid/
/tests/fixtures/rules/no-unused-class-name/valid/invalid-style01-input.svelte
/tests/fixtures/rules/no-unused-class-name/valid/unknown-lang01-input.svelte
Expand Down
40 changes: 40 additions & 0 deletions src/rules/indent-helpers/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { IndentContext } from './commons';
import { isBeginningOfElement } from './commons';
import { isBeginningOfLine } from './commons';
import { getFirstAndLastTokens } from './commons';
import { isClosingParenToken, isOpeningParenToken } from '@eslint-community/eslint-utils';

type NodeListener = SvelteNodeListener;
const PREFORMATTED_ELEMENT_NAMES = ['pre', 'textarea', 'template'];
Expand Down Expand Up @@ -213,6 +214,21 @@ export function defineVisitor(context: IndentContext): NodeListener {
offsets.setOffsetToken(declarationToken, 1, openToken);
offsets.setOffsetToken(closeToken, 0, openToken);
},
SvelteRenderTag(node: AST.SvelteRenderTag) {
const openToken = sourceCode.getFirstToken(node);
const renderToken = sourceCode.getTokenAfter(openToken)!;
offsets.setOffsetToken(renderToken, 1, openToken);
const calleeToken = sourceCode.getFirstToken(node.callee);
offsets.setOffsetToken(calleeToken, 1, renderToken);
const leftParenToken = sourceCode.getTokenAfter(node.callee, {
filter: isOpeningParenToken,
includeComments: false
})!;
const rightParenToken = sourceCode.getTokenBefore(sourceCode.getLastToken(node));

offsets.setOffsetToken(leftParenToken, 1, calleeToken);
offsets.setOffsetElementList([node.argument], leftParenToken, rightParenToken, 1);
},
// ----------------------------------------------------------------------
// BLOCKS
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -462,6 +478,30 @@ export function defineVisitor(context: IndentContext): NodeListener {
offsets.setOffsetToken(endAwaitToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
SvelteSnippetBlock(node: AST.SvelteSnippetBlock) {
const [openToken, snippetToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(snippetToken, 1, openToken);
const id = getFirstAndLastTokens(sourceCode, node.id);
offsets.setOffsetToken(id.firstToken, 1, snippetToken);

const leftParenToken = sourceCode.getTokenBefore(
node.context || sourceCode.getLastToken(node),
{
filter: isOpeningParenToken,
includeComments: false
}
)!;

const rightParenToken = sourceCode.getTokenAfter(node.context || leftParenToken, {
filter: isClosingParenToken,
includeComments: false
})!;
offsets.setOffsetToken(leftParenToken, 1, id.firstToken);
offsets.setOffsetElementList([node.context], leftParenToken, rightParenToken, 1);
},
// ----------------------------------------------------------------------
// COMMENTS
// ----------------------------------------------------------------------
Expand Down
40 changes: 40 additions & 0 deletions tests/fixtures/rules/indent/invalid/snippets01-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- 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 4 spaces but found 0 spaces.
line: 6
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 7
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 4 spaces but found 0 spaces.
line: 14
column: 1
suggestions: null
- message: Expected indentation of 2 spaces but found 0 spaces.
line: 15
column: 1
suggestions: null
16 changes: 16 additions & 0 deletions tests/fixtures/rules/indent/invalid/snippets01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- prettier-ignore -->
{#snippet
foo(
{
a
}
)
}
{/snippet}
{@render
foo(
{
a
}
)
}
16 changes: 16 additions & 0 deletions tests/fixtures/rules/indent/invalid/snippets01-output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- prettier-ignore -->
{#snippet
foo(
{
a
}
)
}
{/snippet}
{@render
foo(
{
a
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.0.0-0"
}