The jsx-import rule ensures that jsx from @emotion/react is imported when using the css prop. This rule can usually be auto-fixed so manual intervention is typically not required.
Configures and validates jsx import requirements for emotion's css prop usage.
/**
* Rule for ensuring jsx from @emotion/react is imported when using css prop
* Supports both classic and automatic JSX runtime modes
*/
interface JSXImportRuleConfig {
name: "jsx-import";
fixable: true;
schema: {
type: "array";
items: Array<JSXConfig | string>;
uniqueItems: true;
minItems: 0;
};
messages: {
cssProp: string;
cssPropWithPragma: string;
templateLiterals: string;
};
}
interface JSXConfig {
runtime: "automatic";
importSource?: string; // defaults to "@emotion/react"
}The rule operates in two modes based on configuration:
Automatic JSX Runtime Mode (when JSXConfig with runtime: "automatic" is provided):
Classic JSX Runtime Mode (default):
{
"rules": {
"@emotion/jsx-import": [
"error",
{ "runtime": "automatic", "importSource": "@emotion/react" }
]
}
}{
"rules": {
"@emotion/jsx-import": "error"
}
}Incorrect code for this rule:
// Missing jsx import and pragma
let element = (
<div
css={{
color: 'green'
}}
/>
);Correct code for this rule:
/** @jsx jsx */
import { jsx } from '@emotion/react';
let element = (
<div
css={{
color: 'green'
}}
/>
);Automatic JSX Runtime example:
/** @jsxImportSource @emotion/react */
let element = (
<div
css={{
color: 'green'
}}
/>
);The rule also handles template literal css prop usage:
Before (triggers fix):
/** @jsx jsx */
import { jsx } from '@emotion/react';
<div css={`color: hotpink;`} />After (auto-fixed):
/** @jsx jsx */
import { jsx, css } from '@emotion/react';
<div css={css`color: hotpink;`} />Disable this rule if you are using: