Skip to content

Commit

Permalink
feat: add support for {@snippet}and {@render} in indent rule (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Nov 22, 2023
1 parent 67583f4 commit 470ef6c
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-fans-refuse.md
@@ -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
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
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
@@ -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
@@ -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
@@ -0,0 +1,16 @@
<!-- prettier-ignore -->
{#snippet
foo(
{
a
}
)
}
{/snippet}
{@render
foo(
{
a
}
)
}
@@ -0,0 +1,3 @@
{
"svelte": ">=5.0.0-0"
}

0 comments on commit 470ef6c

Please sign in to comment.