The syntax-preference rule enforces consistent CSS syntax choice between string-based (template literals) and object-based styles throughout your codebase.
Enforces consistent usage of either string-based or object-based CSS syntax in emotion.
/**
* Rule for enforcing consistent CSS syntax preference
* Supports 'string' or 'object' preference configuration
*/
interface SyntaxPreferenceRuleConfig {
name: "syntax-preference";
fixable: false; // Reports violations, no auto-fix
schema: [('string' | 'object')?];
messages: {
preferStringStyle: string;
preferObjectStyle: string;
preferWrappingWithCSS: string;
emptyCssProp: string;
};
}
type RuleOptions = [('string' | 'object')?];Based on configuration preference:
Object Preference ("object"):
{ color: 'red' }String Preference ("string"):
css\color: red;``{
"rules": {
"@emotion/syntax-preference": ["error", "object"]
}
}{
"rules": {
"@emotion/syntax-preference": ["error", "string"]
}
}Incorrect (when configured for objects):
import styled from '@emotion/styled';
const Button = styled.button`
color: hotpink;
background: white;
`;
// Also incorrect
const styles = css`
padding: 10px;
margin: 5px;
`;Correct (when configured for objects):
import styled from '@emotion/styled';
const Button = styled.button({
color: 'hotpink',
background: 'white'
});
// Also correct
const styles = css({
padding: '10px',
margin: '5px'
});Incorrect (when configured for strings):
const Button = styled.button({
color: 'hotpink',
background: 'white'
});Correct (when configured for strings):
const Button = styled.button`
color: hotpink;
background: white;
`;The rule also validates JSX css prop usage:
Object preference examples:
// Incorrect
<div css="color: red;" />
<div css={`color: red;`} />
// Correct
<div css={{ color: 'red' }} />
<div css={css({ color: 'red' })} />String preference examples:
// Incorrect
<div css={{ color: 'red' }} />
// Correct
<div css={css`color: red;`} />The rule handles complex nested structures:
// Array of styles (object preference)
const multipleStyles = [
{ color: 'red' }, // Correct
`color: blue;`, // Incorrect - reports violation
{ padding: '10px' } // Correct
];With string preference, the rule distinguishes:
Plain strings (discouraged):
<div css="color: red;" /> // Reports: prefer wrapping with cssCSS tagged templates (preferred):
<div css={css`color: red;`} /> // CorrectDisable this rule if: