Skip to content

Commit 25fc427

Browse files
gurgundaylydell
andauthored
turn off unicorn/template-indent (#269)
Co-authored-by: Simon Lydell <[email protected]>
1 parent bebd55e commit 25fc427

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

README.md

+62
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,67 @@ Prettier:
648648
}
649649
```
650650

651+
### [unicorn/template-indent]
652+
653+
**This rule can be used with certain options.**
654+
655+
This rule will automatically fix the indentation of multiline string templates, to keep them in alignment with the code they are found in. A configurable whitelist is used to ensure no whitespace-sensitive strings are edited.
656+
657+
Prettier deals with:
658+
659+
- HTML
660+
- CSS
661+
- GraphQL
662+
- markdown
663+
664+
Using various tags, functions and comments.
665+
666+
`unicorn/template-indent` by default formats some of the same tagged templates, which can cause conflicts. For example, the rule and Prettier disagree about indentation in ternaries:
667+
668+
```js
669+
condition
670+
? null
671+
: html`
672+
<p>
673+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in dui
674+
mauris.
675+
</p>
676+
`;
677+
```
678+
679+
If you like this rule, it can be used just fine with Prettier as long as you configure the rule to not deal with the same templates as Prettier.
680+
681+
Example ESLint configuration:
682+
683+
<!-- prettier-ignore -->
684+
```json
685+
{
686+
"rules": {
687+
"unicorn/template-indent": [
688+
"error",
689+
{
690+
"tags": [
691+
"outdent",
692+
"dedent",
693+
"sql",
694+
"styled"
695+
],
696+
"functions": [
697+
"dedent",
698+
"stripIndent"
699+
],
700+
"selectors": [],
701+
"comments": [
702+
"indent"
703+
]
704+
}
705+
]
706+
}
707+
}
708+
```
709+
710+
Note: If you use `"selectors"`, the CLI helper tool cannot detect if your selectors might cause conflicts.
711+
651712
### [vue/html-self-closing]
652713

653714
**This rule requires certain options.**
@@ -825,5 +886,6 @@ When you’re done, run `npm test` to verify that you got it all right. It runs
825886
[quotes]: https://eslint.org/docs/rules/quotes
826887
[singlequote]: https://prettier.io/docs/en/options.html#quotes
827888
[string formatting rules]: https://prettier.io/docs/en/rationale.html#strings
889+
[unicorn/template-indent]: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/template-indent.md
828890
[vue/html-self-closing]: https://github.com/vuejs/eslint-plugin-vue/blob/master/docs/rules/html-self-closing.md
829891
[vue/max-len]: https://github.com/vuejs/eslint-plugin-vue/blob/master/docs/rules/max-len.md

bin/validators.js

+23
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ module.exports = {
4848
return Boolean(firstOption && firstOption.allowIndentationTabs);
4949
},
5050

51+
"unicorn/template-indent"({ options }) {
52+
if (options.length === 0) {
53+
return false;
54+
}
55+
56+
const { comments = [], tags = [] } = options[0] || {};
57+
58+
return (
59+
Array.isArray(comments) &&
60+
Array.isArray(tags) &&
61+
!(
62+
comments.includes("GraphQL") ||
63+
comments.includes("HTML") ||
64+
tags.includes("css") ||
65+
tags.includes("graphql") ||
66+
tags.includes("gql") ||
67+
tags.includes("html") ||
68+
tags.includes("markdown") ||
69+
tags.includes("md")
70+
)
71+
);
72+
},
73+
5174
"vue/html-self-closing"({ options }) {
5275
if (options.length === 0) {
5376
return false;

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
"@typescript-eslint/lines-around-comment": 0,
1919
"@typescript-eslint/quotes": 0,
2020
"babel/quotes": 0,
21+
"unicorn/template-indent": 0,
2122
"vue/html-self-closing": 0,
2223
"vue/max-len": 0,
2324

test/cli.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ test("all the things", () => {
126126
"vue/html-self-closing",
127127
"prefer-arrow-callback",
128128
"arrow-body-style",
129+
"unicorn/template-indent",
129130
];
130131
expect(cli.processRules(createRules(rules, "error"))).toMatchInlineSnapshot(`
131132
{
@@ -144,6 +145,7 @@ test("all the things", () => {
144145
- lines-around-comment
145146
- no-confusing-arrow
146147
- no-tabs
148+
- unicorn/template-indent
147149
- vue/html-self-closing
148150
149151
The following rules are enabled but cannot be automatically checked. See:

test/validators.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ rule("no-tabs", {
6565
invalid: [[], [null], [{ allowIndentationTabs: false }], [{ other: true }]],
6666
});
6767

68+
rule("unicorn/template-indent", {
69+
valid: [
70+
[
71+
{
72+
tags: ["outdent", "dedent", "sql", "styled"],
73+
functions: ["dedent", "stripIndent"],
74+
selectors: [],
75+
comments: ["indent"],
76+
},
77+
],
78+
[
79+
{
80+
comments: ["indent"],
81+
},
82+
],
83+
],
84+
invalid: [
85+
[],
86+
[{ comments: ["GraphQL"] }],
87+
[{ comments: ["HTML"] }],
88+
[{ tags: ["css"] }],
89+
[{ tags: ["graphql"] }],
90+
[{ tags: ["gql"] }],
91+
[{ tags: ["html"] }],
92+
[{ tags: ["markdown"] }],
93+
[{ tags: ["md"] }],
94+
[{ comments: 5 }],
95+
[{ tags: {} }],
96+
[
97+
{
98+
tags: ["outdent", "dedent", "gql", "sql", "html", "styled"],
99+
},
100+
],
101+
],
102+
});
103+
68104
rule("vue/html-self-closing", {
69105
valid: [
70106
[{ html: { void: "any" } }],

0 commit comments

Comments
 (0)