# svelte/prefer-class-directive
require class directives instead of ternary expressions
- π§ The
--fix
option on the command line can automatically fix some of the problems reported by this rule.
# π Rule Details
This rule aims to replace a class with ternary operator with the class directive.
<script>
/* eslint svelte/prefer-class-directive: ["error", {"prefer": "empty"}] */
const selected = true;
</script>
<!-- β GOOD -->
<button class:selected>foo</button>
<button class:selected={current === 'foo'}>foo</button>
<!-- β BAD -->
<button class=Unexpected class using the ternary operator. (svelte/prefer-class-directive){selected ? 'selected' : ''}>foo</button>
<button class=Unexpected class using the ternary operator. (svelte/prefer-class-directive){current === 'foo' ? 'selected' : ''}>foo</button>
You cannot enforce this style by using prettier-plugin-svelte. That is, this rule does not conflict with prettier-plugin-svelte and can be used with prettier-plugin-svelte.
# π§ Options
{
"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.
# π« Related Rules
# π Further Reading
# π Version
This rule was introduced in eslint-plugin-svelte v0.0.1