|
| 1 | +# Disallow references to deprecated color variables (no-deprecated-colors) |
| 2 | + |
| 3 | +🔧 The `--fix` option on the [ESLint CLI](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can |
| 4 | +automatically fix some of the problems reported by this rule. |
| 5 | + |
| 6 | +[Theming](https://primer.style/react/theming) in Primer React is made possible by a theme object that defines your |
| 7 | +application's colors, spacing, fonts, and more. The color variables in Primer React's |
| 8 | +[default theme object](https://primer.style/react/theme-reference) are pulled from |
| 9 | +[Primer Primitives](https://github.com/primer/primitives). When a color variable is deprecated in Primer Primitives, |
| 10 | +it's important to remove references to that color variable in your application before it's removed from the library. |
| 11 | + |
| 12 | +## Rule details |
| 13 | + |
| 14 | +This rule disallows references to color variables that are deprecated in |
| 15 | +[Primer Primitives](https://github.com/primer/primitives). |
| 16 | + |
| 17 | +👎 Examples of **incorrect** code for this rule: |
| 18 | + |
| 19 | +```jsx |
| 20 | +/* eslint primer-react/no-deprecated-colors: "error" */ |
| 21 | +import {Box, themeGet} from '@primer/react' |
| 22 | +import styled from 'styled-components' |
| 23 | + |
| 24 | +const SystemPropExample() = () => <Box color="some.deprecated.color">Incorrect</Box> |
| 25 | + |
| 26 | +const SxPropExample() = () => <Box sx={{color: 'some.deprecated.color'}}>Incorrect</Box> |
| 27 | + |
| 28 | +const SxPropExample2() = () => <Box sx={{boxShadow: theme => `0 1px 2px ${theme.colors.some.deprecated.color}`}}>Incorrect</Box> |
| 29 | + |
| 30 | +const ThemeGetExample = styled.div` |
| 31 | + color: ${themeGet('colors.some.deprecated.color')}; |
| 32 | +` |
| 33 | +``` |
| 34 | + |
| 35 | +👍 Examples of **correct** code for this rule: |
| 36 | + |
| 37 | +```jsx |
| 38 | +/* eslint primer-react/no-deprecated-colors: "error" */ |
| 39 | +import {Box, themeGet} from '@primer/react' |
| 40 | +import styled from 'styled-components' |
| 41 | + |
| 42 | +const SystemPropExample() = () => <Box color="some.color">Correct</Box> |
| 43 | + |
| 44 | +const SxPropExample() = () => <Box sx={{color: 'some.color'}}>Correct</Box> |
| 45 | + |
| 46 | +const SxPropExample2() = () => <Box sx={{boxShadow: theme => `0 1px 2px ${theme.colors.some.color}`}}>Correct</Box> |
| 47 | + |
| 48 | +const ThemeGetExample = styled.div` |
| 49 | + color: ${themeGet('colors.some.color')}; |
| 50 | +` |
| 51 | +``` |
| 52 | + |
| 53 | +## Options |
| 54 | + |
| 55 | +- `skipImportCheck` (default: `false`) |
| 56 | + |
| 57 | + By default, the `no-deprecated-colors` rule will only check for deprecated colors used in functions and components |
| 58 | + that are imported from `@primer/react`. You can disable this behavior by setting `skipImportCheck` to `true`. This is |
| 59 | + useful for linting custom components that pass color-related props down to Primer React components. |
| 60 | + |
| 61 | + ```js |
| 62 | + /* eslint primer-react/no-deprecated-colors: ["warn", {"skipImportCheck": true}] */ |
| 63 | + import {Box} from '@primer/react' |
| 64 | + |
| 65 | + function MyBox({color, children}) { |
| 66 | + return <Box color={color}>{children}</Box> |
| 67 | + } |
| 68 | + |
| 69 | + function App() { |
| 70 | + // Enabling `skipImportCheck` will find deprecated colors used like this: |
| 71 | + return <MyBox color="text.primary">Hello</MyBox> |
| 72 | + } |
| 73 | + ``` |
| 74 | + |
| 75 | +- `checkAllStrings` (default: `false`) |
| 76 | + |
| 77 | + If `checkAllStrings` is set to `true`, the `no-deprecated-colors` rule will check for deprecated colors in all |
| 78 | + strings. This is useful for catching uses of deprecated colors outside system props and the `sx` prop. |
| 79 | + |
| 80 | + ```js |
| 81 | + /* eslint primer-react/no-deprecated-colors: ["warn", {"checkAllStrings": true}] */ |
| 82 | + import {Box} from '@primer/react' |
| 83 | + |
| 84 | + function ExampleComponent() { |
| 85 | + const styles = { |
| 86 | + // Enabling `checkAllStrings` will find deprecated colors used like this: |
| 87 | + color: 'text.primary', |
| 88 | + } |
| 89 | + return <Box sx={styles}>Hello</Box> |
| 90 | + } |
| 91 | + ``` |
0 commit comments