Skip to content
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

feat: Add prefer option to prefer-class-directive #690

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/popular-hotels-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

Added prefer option to prefer-class-directive rule ('always' or 'empty'). The default is now 'empty' which is a slight relaxation of the rule.
20 changes: 18 additions & 2 deletions docs/rules/prefer-class-directive.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ This rule aims to replace a class with ternary operator with the class directive

```svelte
<script>
/* eslint svelte/prefer-class-directive: "error" */
/* eslint svelte/prefer-class-directive: "empty" */
const selected = true;
</script>

<!-- ✓ GOOD -->
<button class:selected>foo</button>
<button class:selected={current === 'foo'}>foo</button>

<!-- ✗ BAD -->
<button class={selected ? 'selected' : ''}>foo</button>
<button class={current === 'foo' ? 'selected' : ''}>foo</button>
```

Expand All @@ -40,7 +43,20 @@ You cannot enforce this style by using [prettier-plugin-svelte]. That is, this r

## :wrench: Options

Nothing.
```json
{
"svelte/html-quotes": [
"error",
{
"prefer": "empty" // or "always"
}
]
}
```

- `prefer` ... Whether to apply this rule always or just when there's an empty string. Default is `"empty"`.
- `"empty"` ... Requires class directives only if one of the strings is empty.
- `"always"` ... Requires class directives always rather than interpolation.

## :couple: Related Rules

Expand Down
16 changes: 15 additions & 1 deletion src/rules/prefer-class-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ export default createRule('prefer-class-directive', {
conflictWithPrettier: false
},
fixable: 'code',
schema: [],
schema: [
{
type: 'object',
properties: {
prefer: { enum: ['always', 'empty'] }
},
additionalProperties: false
}
],
messages: {
unexpected: 'Unexpected class using the ternary operator.'
},
type: 'suggestion'
},
create(context) {
const sourceCode = getSourceCode(context);
const preferEmpty = context.options[0]?.prefer !== 'always';

type Expr = {
not?: true;
Expand Down Expand Up @@ -301,6 +310,7 @@ export default createRule('prefer-class-directive', {
const prevIsWord = !startsWithNonWord(attr, index + 1);
const nextIsWord = !endsWithNonWord(attr, index - 1);
let canTransform = true;
let foundEmpty = false;
for (const className of map.values()) {
if (className) {
if (!/^[\w-]*$/u.test(className.trim())) {
Expand All @@ -317,13 +327,17 @@ export default createRule('prefer-class-directive', {
break;
}
} else {
foundEmpty = true;
if (prevIsWord && nextIsWord) {
// The previous and next may be connected.
canTransform = false;
break;
}
}
}
if (preferEmpty && !foundEmpty) {
return;
}
if (!canTransform) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "prefer": "always" }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "prefer": "always" }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": [{ "prefer": "empty" }]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let a = true;
let a7 = 7;
let danger = false;
</script>

<button class={a ? 'a' : 'b'}>foo</button>
<button class={!a ? 'b' : 'a'}>foo</button>
<button class={a7 === 7 ? 'a' : 'b'}>foo</button>
<button class="btn {danger ? 'btn-danger' : 'btn-primary'}">foo</button>
Loading