Skip to content

Commit e74d997

Browse files
committed
fix: Add invalid test for empty setting, and fix markdown example
Also now considers the trimmed class name when looking for empty strings.
1 parent e896d7a commit e74d997

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

docs/rules/prefer-class-directive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This rule aims to replace a class with ternary operator with the class directive
2222

2323
```svelte
2424
<script>
25-
/* eslint svelte/prefer-class-directive: "empty" */
25+
/* eslint svelte/prefer-class-directive: ["error", {"prefer": "empty"}] */
2626
const selected = true;
2727
</script>
2828

src/rules/prefer-class-directive.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,14 @@ export default createRule('prefer-class-directive', {
307307
// It's too complicated.
308308
return;
309309
}
310+
if (preferEmpty && [...map.values()].every((x) => x.trim())) {
311+
// We prefer directives when there's an empty string, but they're all not empty
312+
return;
313+
}
314+
310315
const prevIsWord = !startsWithNonWord(attr, index + 1);
311316
const nextIsWord = !endsWithNonWord(attr, index - 1);
312317
let canTransform = true;
313-
let foundEmpty = false;
314318
for (const className of map.values()) {
315319
if (className) {
316320
if (!/^[\w-]*$/u.test(className.trim())) {
@@ -327,17 +331,13 @@ export default createRule('prefer-class-directive', {
327331
break;
328332
}
329333
} else {
330-
foundEmpty = true;
331334
if (prevIsWord && nextIsWord) {
332335
// The previous and next may be connected.
333336
canTransform = false;
334337
break;
335338
}
336339
}
337340
}
338-
if (preferEmpty && !foundEmpty) {
339-
return;
340-
}
341341
if (!canTransform) {
342342
return;
343343
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
- message: Unexpected class using the ternary operator.
2+
line: 6
3+
column: 15
4+
suggestions: null
5+
- message: Unexpected class using the ternary operator.
6+
line: 7
7+
column: 18
8+
suggestions: null
9+
- message: Unexpected class using the ternary operator.
10+
line: 8
11+
column: 17
12+
suggestions: null
13+
- message: Unexpected class using the ternary operator.
14+
line: 10
15+
column: 20
16+
suggestions: null
17+
- message: Unexpected class using the ternary operator.
18+
line: 11
19+
column: 20
20+
suggestions: null
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let selected = 'foo';
3+
let children = 1;
4+
</script>
5+
6+
<button class={selected ? 'selected' : ''}>foo</button>
7+
<button class="a {selected ? 'selected' : ''} b">foo</button>
8+
<button class="a{selected ? ' selected ' : ' '}b">foo</button>
9+
10+
<div class="d-flex {children > 1 ? 'gap-3' : ''}">foo</div>
11+
<div class="d-flex {children === 1 ? '' : 'gap-3'}">foo</div>
12+
13+
<!-- test01-input.svelte -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let selected = 'foo';
3+
let children = 1;
4+
</script>
5+
6+
<button class:selected={selected}>foo</button>
7+
<button class="a b" class:selected={selected}>foo</button>
8+
<button class="a b" class:selected={selected}>foo</button>
9+
10+
<div class="d-flex" class:gap-3={children > 1}>foo</div>
11+
<div class="d-flex" class:gap-3={children !== 1}>foo</div>
12+
13+
<!-- test01-input.svelte -->

0 commit comments

Comments
 (0)