@emotion/eslint-plugin provides ESLint rules specifically designed for emotion, a CSS-in-JS library. It offers linting rules to help developers migrate from older versions of emotion to newer ones, enforce proper imports, and maintain code quality when using emotion's CSS-in-JS features.
npm install @emotion/eslint-plugin --save-devThe plugin is used through ESLint configuration rather than direct imports:
{
"plugins": ["@emotion"],
"rules": {
"@emotion/jsx-import": "error",
"@emotion/no-vanilla": "error",
"@emotion/import-from-emotion": "error",
"@emotion/styled-import": "error",
"@emotion/syntax-preference": ["error", "object"],
"@emotion/pkg-renaming": "error"
}
}Configure the plugin in your .eslintrc configuration file:
{
"plugins": ["@emotion"],
"rules": {
"@emotion/jsx-import": "error"
}
}For Emotion 11 migration, enable the pkg-renaming rule:
{
"rules": {
"@emotion/pkg-renaming": "error"
}
}For comprehensive Emotion 10 to 11 migration:
{
"rules": {
"@emotion/jsx-import": "error",
"@emotion/no-vanilla": "error",
"@emotion/import-from-emotion": "error",
"@emotion/styled-import": "error"
}
}The plugin is built around:
Ensures proper jsx import and pragma setup when using the css prop with emotion/react.
interface JSXImportRule {
name: "jsx-import";
fixable: true;
schema: Array<JSXConfig | string>;
messages: {
cssProp: string;
cssPropWithPragma: string;
templateLiterals: string;
};
}
interface JSXConfig {
runtime: string;
importSource?: string;
}Ensures styled components are imported from the correct @emotion/styled package.
interface StyledImportRule {
name: "styled-import";
fixable: true;
messages: {
incorrectImport: string;
};
}Migrates imports from legacy react-emotion to modern @emotion packages.
interface ImportFromEmotionRule {
name: "import-from-emotion";
fixable: true;
messages: {
incorrectImport: string;
};
}Prevents usage of vanilla @emotion/css in React applications.
interface NoVanillaRule {
name: "no-vanilla";
fixable: false;
messages: {
vanillaEmotion: string;
};
}Enforces consistent CSS syntax choice between string and object styles.
interface SyntaxPreferenceRule {
name: "syntax-preference";
fixable: false;
schema: [('string' | 'object')?];
messages: {
preferStringStyle: string;
preferObjectStyle: string;
preferWrappingWithCSS: string;
emptyCssProp: string;
};
}Automates migration of package names from Emotion 10 to Emotion 11.
interface PkgRenamingRule {
name: "pkg-renaming";
fixable: true;
messages: {
renamePackage: string;
exportChange: string;
emotionTheming: string;
};
}interface ESLintRule {
name: string;
meta: {
docs: {
description: string;
recommended: boolean;
};
fixable?: "code" | "whitespace";
messages: Record<string, string>;
schema: any[];
type: "problem" | "suggestion" | "layout";
};
defaultOptions: any[];
create(context: RuleContext): RuleListener;
}
interface RuleContext {
getSourceCode(): SourceCode;
getFilename(): string;
getScope(): Scope;
settings: Record<string, any>;
options: any[];
report(descriptor: ReportDescriptor): void;
}
interface ReportDescriptor {
node: ESTreeNode;
messageId: string;
data?: Record<string, any>;
fix?: (fixer: RuleFixer) => Fix | Fix[] | null;
}