Skip to content

feat: indent/indentScript option #180

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 2 commits into from
Jul 7, 2022
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
1 change: 1 addition & 0 deletions docs/rules/indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ This rule only checks `.svelte` files and does not interfere with other `.js` fi
```

- `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.
- `indentScript` (`boolean`) ... If contents within a `<script>` block should be indented a level or not. Default is `true`.
- `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.
- `switchCase` ... Enforces indentation level for case clauses in switch statements. Default is `1`.
- `alignAttributesVertically` ... Condition for whether attributes should be vertically aligned to the first attribute in multiline case or not. Default is `false`
Expand Down
1 change: 1 addition & 0 deletions src/rules/indent-helpers/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type MaybeNode = {

export type IndentOptions = {
indentChar: " " | "\t"
indentScript: boolean
indentSize: number
switchCase: number
alignAttributesVertically: boolean
Expand Down
6 changes: 6 additions & 0 deletions src/rules/indent-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { OffsetContext } from "./offset-context"

type IndentUserOptions = {
indent?: number | "tab"
indentScript?: boolean
switchCase?: number
alignAttributesVertically?: boolean
ignoredNodes?: string[]
Expand All @@ -30,6 +31,7 @@ function parseOptions(
): IndentOptions {
const ret: IndentOptions = {
indentChar: " ",
indentScript: true,
indentSize: 2,
switchCase: 1,
alignAttributesVertically: false,
Expand All @@ -44,6 +46,10 @@ function parseOptions(
ret.indentSize = 1
}

if (typeof options.indentScript === "boolean") {
ret.indentScript = options.indentScript
}

if (options.switchCase != null && Number.isSafeInteger(options.switchCase)) {
ret.switchCase = options.switchCase
}
Expand Down
7 changes: 6 additions & 1 deletion src/rules/indent-helpers/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export function defineVisitor(context: IndentContext): NodeListener {
// ELEMENTS
// ----------------------------------------------------------------------
SvelteScriptElement(node: AST.SvelteScriptElement) {
offsets.setOffsetElementList(node.body, node.startTag, node.endTag, 1)
offsets.setOffsetElementList(
node.body,
node.startTag,
node.endTag,
options.indentScript ? 1 : 0,
)
},
SvelteStyleElement(node: AST.SvelteStyleElement) {
node.children.forEach((n) => offsets.ignore(n))
Expand Down
1 change: 1 addition & 0 deletions src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default createRule("indent", {
indent: {
anyOf: [{ type: "integer", minimum: 1 }, { enum: ["tab"] }],
},
indentScript: { type: "boolean" },
switchCase: { type: "integer", minimum: 0 },
alignAttributesVertically: { type: "boolean" },
ignoredNodes: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "indentScript": false }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 3,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- prettier-ignore -->
<script>
const a = "indent-script-input.svelte"
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- prettier-ignore -->
<script>
const a = "indent-script-input.svelte"
</script>
4 changes: 4 additions & 0 deletions tests/fixtures/rules/indent/valid/indent-script-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- prettier-ignore -->
<script>
const a = "indent-script-input.svelte"
</script>