The import-from-emotion rule ensures that emotion's exports are imported directly from the appropriate @emotion packages rather than from the legacy react-emotion package.
Migrates named exports from react-emotion to the correct modern @emotion packages.
/**
* Rule for migrating imports from react-emotion to modern @emotion packages
* Handles complex import patterns including mixed default and named exports
*/
interface ImportFromEmotionRuleConfig {
name: "import-from-emotion";
fixable: true;
schema: never[]; // No configuration options
messages: {
incorrectImport: string;
};
}The rule:
{
"rules": {
"@emotion/import-from-emotion": "error"
}
}Incorrect code for this rule:
import styled, { css, keyframes } from 'react-emotion';Correct code for this rule:
import styled from '@emotion/styled';
import { css, keyframes } from 'emotion';Before (with aliases):
import styled, { css as emotionCss, keyframes as kf } from 'react-emotion';After (auto-fixed):
import styled from '@emotion/styled';
import { css as emotionCss, keyframes as kf } from 'emotion';The rule handles various import patterns:
Mixed imports (default + named):
// Before
import styled, { css } from 'react-emotion';
// After
import styled from '@emotion/styled';
import { css } from 'emotion';Named-only imports:
// Before
import { css, keyframes } from 'react-emotion';
// After
import { css, keyframes } from 'emotion';Namespace imports (reported but not auto-fixed):
// Not auto-fixed for safety
import * as emotion from 'react-emotion';This rule is essential for Emotion 10 to 11 migration. It works alongside:
{
"rules": {
"@emotion/jsx-import": "error",
"@emotion/no-vanilla": "error",
"@emotion/import-from-emotion": "error",
"@emotion/styled-import": "error"
}
}The migration path:
react-emotion default export → @emotion/styledreact-emotion named exports → emotion packageThe rule uses sophisticated logic to handle import splitting:
@emotion/styledemotioncss as emotionCss → css as emotionCssDisable this rule if: