Skip to content

Commit 8e63c1b

Browse files
authored
feat: indent/indentScript option (#180)
* feat: indent/indentScript option Allows to configure whether the contents of <script> blocks should be indented a single tabstop or not. Defaults to true for back-compat. * fix: remove errant changed line
1 parent 1c7cbb4 commit 8e63c1b

File tree

10 files changed

+37
-1
lines changed

10 files changed

+37
-1
lines changed

docs/rules/indent.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ This rule only checks `.svelte` files and does not interfere with other `.js` fi
8888
```
8989

9090
- `indent` (`number | "tab"`) ... The type of indentation. Default is `2`. If this is a number, it's the number of spaces for one indent. If this is `"tab"`, it uses one tab for one indent.
91+
- `indentScript` (`boolean`) ... If contents within a `<script>` block should be indented a level or not. Default is `true`.
9192
- `ignoredNodes` ... Can be used to disable indentation checking for any AST node. This accepts an array of [selectors](https://eslint.org/docs/developer-guide/selectors). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern.
9293
- `switchCase` ... Enforces indentation level for case clauses in switch statements. Default is `1`.
9394
- `alignAttributesVertically` ... Condition for whether attributes should be vertically aligned to the first attribute in multiline case or not. Default is `false`

src/rules/indent-helpers/commons.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type MaybeNode = {
1313

1414
export type IndentOptions = {
1515
indentChar: " " | "\t"
16+
indentScript: boolean
1617
indentSize: number
1718
switchCase: number
1819
alignAttributesVertically: boolean

src/rules/indent-helpers/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { OffsetContext } from "./offset-context"
1212

1313
type IndentUserOptions = {
1414
indent?: number | "tab"
15+
indentScript?: boolean
1516
switchCase?: number
1617
alignAttributesVertically?: boolean
1718
ignoredNodes?: string[]
@@ -30,6 +31,7 @@ function parseOptions(
3031
): IndentOptions {
3132
const ret: IndentOptions = {
3233
indentChar: " ",
34+
indentScript: true,
3335
indentSize: 2,
3436
switchCase: 1,
3537
alignAttributesVertically: false,
@@ -44,6 +46,10 @@ function parseOptions(
4446
ret.indentSize = 1
4547
}
4648

49+
if (typeof options.indentScript === "boolean") {
50+
ret.indentScript = options.indentScript
51+
}
52+
4753
if (options.switchCase != null && Number.isSafeInteger(options.switchCase)) {
4854
ret.switchCase = options.switchCase
4955
}

src/rules/indent-helpers/svelte.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ export function defineVisitor(context: IndentContext): NodeListener {
2323
// ELEMENTS
2424
// ----------------------------------------------------------------------
2525
SvelteScriptElement(node: AST.SvelteScriptElement) {
26-
offsets.setOffsetElementList(node.body, node.startTag, node.endTag, 1)
26+
offsets.setOffsetElementList(
27+
node.body,
28+
node.startTag,
29+
node.endTag,
30+
options.indentScript ? 1 : 0,
31+
)
2732
},
2833
SvelteStyleElement(node: AST.SvelteStyleElement) {
2934
node.children.forEach((n) => offsets.ignore(n))

src/rules/indent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default createRule("indent", {
1616
indent: {
1717
anyOf: [{ type: "integer", minimum: 1 }, { enum: ["tab"] }],
1818
},
19+
indentScript: { type: "boolean" },
1920
switchCase: { type: "integer", minimum: 0 },
2021
alignAttributesVertically: { type: "boolean" },
2122
ignoredNodes: {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"options": [{ "indentScript": false }]
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Expected indentation of 0 spaces but found 2 spaces.",
4+
"line": 3,
5+
"column": 1
6+
}
7+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- prettier-ignore -->
2+
<script>
3+
const a = "indent-script-input.svelte"
4+
</script>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- prettier-ignore -->
2+
<script>
3+
const a = "indent-script-input.svelte"
4+
</script>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- prettier-ignore -->
2+
<script>
3+
const a = "indent-script-input.svelte"
4+
</script>

0 commit comments

Comments
 (0)