Skip to content

Commit ab6b0c2

Browse files
authored
Fix false positives for html text in indent rule. (#41)
1 parent e28166d commit ab6b0c2

10 files changed

+110
-3
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"debug": "^4.3.1",
5050
"eslint-utils": "^3.0.0",
5151
"sourcemap-codec": "^1.4.8",
52-
"svelte-eslint-parser": "^0.4.0"
52+
"svelte-eslint-parser": "^0.4.3"
5353
},
5454
"peerDependencies": {
5555
"eslint": "^7.0.0",

src/rules/indent-helpers/commons.ts

+33
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,36 @@ export function isBeginningOfLine(
7575

7676
return !prevToken || prevToken.loc.end.line < node.loc!.start.line
7777
}
78+
79+
/**
80+
* Check whether the given node is the beginning of element.
81+
*/
82+
export function isBeginningOfElement(node: AST.SvelteText): boolean {
83+
if (
84+
node.parent.type === "SvelteElement" ||
85+
node.parent.type === "SvelteAwaitCatchBlock" ||
86+
node.parent.type === "SvelteAwaitPendingBlock" ||
87+
node.parent.type === "SvelteAwaitThenBlock" ||
88+
node.parent.type === "SvelteEachBlock" ||
89+
node.parent.type === "SvelteElseBlock" ||
90+
node.parent.type === "SvelteIfBlock" ||
91+
node.parent.type === "SvelteKeyBlock" ||
92+
node.parent.type === "SvelteStyleElement"
93+
) {
94+
return node.parent.children[0] === node
95+
}
96+
if (node.parent.type === "Program") {
97+
return node.parent.body[0] === node
98+
}
99+
assertNever(node.parent)
100+
return false
101+
}
102+
103+
/**
104+
* Throws an error when invoked.
105+
*/
106+
function assertNever(value: never): never {
107+
throw new Error(
108+
`This part of the code should never be reached but ${value} made it through.`,
109+
)
110+
}

src/rules/indent-helpers/svelte.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AST } from "svelte-eslint-parser"
22
import type { ASTNode } from "../../types"
33
import { isNotWhitespace } from "./ast"
44
import type { IndentContext } from "./commons"
5+
import { isBeginningOfElement } from "./commons"
56
import { isBeginningOfLine } from "./commons"
67
import { getFirstAndLastTokens } from "./commons"
78
type NodeWithoutES = Exclude<
@@ -12,7 +13,7 @@ type NodeWithoutES = Exclude<
1213
type NodeListener = {
1314
[key in NodeWithoutES["type"]]: (node: NodeWithoutES & { type: key }) => void
1415
}
15-
const PREFORMATTED_ELEMENT_NAMES = ["pre", "textarea"]
16+
const PREFORMATTED_ELEMENT_NAMES = ["pre", "textarea", "template"]
1617

1718
/**
1819
* Creates AST event handlers for svelte nodes.
@@ -169,7 +170,11 @@ export function defineVisitor(context: IndentContext): NodeListener {
169170
}
170171
offsets.setOffsetToken(
171172
tokens,
172-
isBeginningOfLine(sourceCode, first) ? 0 : 1,
173+
isBeginningOfLine(sourceCode, first)
174+
? 0
175+
: isBeginningOfElement(node)
176+
? 1
177+
: 0,
173178
first,
174179
)
175180
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
4+
"line": 7,
5+
"column": 1
6+
},
7+
{
8+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
9+
"line": 8,
10+
"column": 1
11+
}
12+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
export let name = "s"
3+
</script>
4+
5+
<!-- prettier-ignore -->
6+
<div>
7+
{name}a
8+
b
9+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
export let name = "s"
3+
</script>
4+
5+
<!-- prettier-ignore -->
6+
<div>
7+
{name}a
8+
b
9+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
4+
"line": 3,
5+
"column": 1
6+
},
7+
{
8+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
9+
"line": 4,
10+
"column": 1
11+
},
12+
{
13+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
14+
"line": 5,
15+
"column": 1
16+
}
17+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- prettier-ignore -->
2+
<div>
3+
a
4+
<div />b
5+
c
6+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- prettier-ignore -->
2+
<div>
3+
a
4+
<div />b
5+
c
6+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
export const hello = "world"
3+
</script>
4+
5+
<template lang="pug">
6+
div Posts +each('posts as post')
7+
a(href="{post.url}") {post.title}
8+
</template>
9+
10+
<style src="./style.scss"></style>

0 commit comments

Comments
 (0)