|
| 1 | +const rule = require('../use-styled-react-import') |
| 2 | +const {RuleTester} = require('eslint') |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + languageOptions: { |
| 6 | + ecmaVersion: 'latest', |
| 7 | + sourceType: 'module', |
| 8 | + parserOptions: { |
| 9 | + ecmaFeatures: { |
| 10 | + jsx: true, |
| 11 | + }, |
| 12 | + }, |
| 13 | + }, |
| 14 | +}) |
| 15 | + |
| 16 | +ruleTester.run('use-styled-react-import', rule, { |
| 17 | + valid: [ |
| 18 | + // Valid: Component used without sx prop |
| 19 | + `import { Button } from '@primer/react' |
| 20 | + const Component = () => <Button>Click me</Button>`, |
| 21 | + |
| 22 | + // Valid: Component with sx prop imported from styled-react |
| 23 | + `import { Button } from '@primer/styled-react' |
| 24 | + const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`, |
| 25 | + |
| 26 | + // Valid: Utilities imported from styled-react |
| 27 | + `import { sx } from '@primer/styled-react'`, |
| 28 | + |
| 29 | + // Valid: Component not in the styled list |
| 30 | + `import { Avatar } from '@primer/react' |
| 31 | + const Component = () => <Avatar sx={{ color: 'red' }} />`, |
| 32 | + |
| 33 | + // Valid: Component not imported from @primer/react |
| 34 | + `import { Button } from '@github-ui/button' |
| 35 | + const Component = () => <Button sx={{ color: 'red' }} />`, |
| 36 | + |
| 37 | + // Valid: Mixed imports - component without sx prop |
| 38 | + `import { Button, Text } from '@primer/react' |
| 39 | + const Component = () => <Button>Click me</Button>`, |
| 40 | + |
| 41 | + // Valid: Component without sx prop imported from styled-react (when not used) |
| 42 | + `import { Button } from '@primer/styled-react'`, |
| 43 | + ], |
| 44 | + invalid: [ |
| 45 | + // Invalid: Box with sx prop imported from @primer/react |
| 46 | + { |
| 47 | + code: `import { Box } from '@primer/react' |
| 48 | + const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`, |
| 49 | + output: `import { Box } from '@primer/styled-react' |
| 50 | + const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`, |
| 51 | + errors: [ |
| 52 | + { |
| 53 | + messageId: 'useStyledReactImport', |
| 54 | + data: {componentName: 'Box'}, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + |
| 59 | + // Invalid: Button with sx prop imported from @primer/react |
| 60 | + { |
| 61 | + code: `import { Button } from '@primer/react' |
| 62 | + const Component = () => <Button sx={{ margin: 2 }}>Click me</Button>`, |
| 63 | + output: `import { Button } from '@primer/styled-react' |
| 64 | + const Component = () => <Button sx={{ margin: 2 }}>Click me</Button>`, |
| 65 | + errors: [ |
| 66 | + { |
| 67 | + messageId: 'useStyledReactImport', |
| 68 | + data: {componentName: 'Button'}, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + |
| 73 | + // Invalid: Multiple components, one with sx prop |
| 74 | + { |
| 75 | + code: `import { Button, Box, Avatar } from '@primer/react' |
| 76 | + const Component = () => ( |
| 77 | + <div> |
| 78 | + <Button>Regular button</Button> |
| 79 | + <Box sx={{ padding: 2 }}>Styled box</Box> |
| 80 | + <Avatar /> |
| 81 | + </div> |
| 82 | + )`, |
| 83 | + output: `import { Button, Avatar } from '@primer/react' |
| 84 | +import { Box } from '@primer/styled-react' |
| 85 | + const Component = () => ( |
| 86 | + <div> |
| 87 | + <Button>Regular button</Button> |
| 88 | + <Box sx={{ padding: 2 }}>Styled box</Box> |
| 89 | + <Avatar /> |
| 90 | + </div> |
| 91 | + )`, |
| 92 | + errors: [ |
| 93 | + { |
| 94 | + messageId: 'useStyledReactImport', |
| 95 | + data: {componentName: 'Box'}, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + |
| 100 | + // Invalid: Utility import from @primer/react that should be from styled-react |
| 101 | + { |
| 102 | + code: `import { sx } from '@primer/react'`, |
| 103 | + output: `import { sx } from '@primer/styled-react'`, |
| 104 | + errors: [ |
| 105 | + { |
| 106 | + messageId: 'moveToStyledReact', |
| 107 | + data: {importName: 'sx'}, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + |
| 112 | + // Invalid: Button and Link, only Button uses sx |
| 113 | + { |
| 114 | + code: `import { Button, Link } from '@primer/react' |
| 115 | + const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`, |
| 116 | + output: `import { Link } from '@primer/react' |
| 117 | +import { Button } from '@primer/styled-react' |
| 118 | + const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`, |
| 119 | + errors: [ |
| 120 | + { |
| 121 | + messageId: 'useStyledReactImport', |
| 122 | + data: {componentName: 'Button'}, |
| 123 | + }, |
| 124 | + ], |
| 125 | + }, |
| 126 | + |
| 127 | + // Invalid: Button imported from styled-react but used without sx prop |
| 128 | + { |
| 129 | + code: `import { Button } from '@primer/styled-react' |
| 130 | + const Component = () => <Button>Click me</Button>`, |
| 131 | + output: `import { Button } from '@primer/react' |
| 132 | + const Component = () => <Button>Click me</Button>`, |
| 133 | + errors: [ |
| 134 | + { |
| 135 | + messageId: 'usePrimerReactImport', |
| 136 | + data: {componentName: 'Button'}, |
| 137 | + }, |
| 138 | + ], |
| 139 | + }, |
| 140 | + |
| 141 | + // Invalid: <Link /> and <StyledButton /> imported from styled-react but used without sx prop |
| 142 | + { |
| 143 | + code: `import { Button } from '@primer/react' |
| 144 | +import { Button as StyledButton, Link } from '@primer/styled-react' |
| 145 | + const Component = () => ( |
| 146 | + <div> |
| 147 | + <Link /> |
| 148 | + <Button>Regular button</Button> |
| 149 | + <StyledButton>Styled button</StyledButton> |
| 150 | + </div> |
| 151 | + )`, |
| 152 | + output: `import { Button, Link } from '@primer/react' |
| 153 | +
|
| 154 | + const Component = () => ( |
| 155 | + <div> |
| 156 | + <Link /> |
| 157 | + <Button>Regular button</Button> |
| 158 | + <Button>Styled button</Button> |
| 159 | + </div> |
| 160 | + )`, |
| 161 | + errors: [ |
| 162 | + { |
| 163 | + messageId: 'usePrimerReactImport', |
| 164 | + data: {componentName: 'Button'}, |
| 165 | + }, |
| 166 | + { |
| 167 | + messageId: 'usePrimerReactImport', |
| 168 | + data: {componentName: 'Link'}, |
| 169 | + }, |
| 170 | + { |
| 171 | + messageId: 'usePrimerReactImport', |
| 172 | + data: {componentName: 'Button'}, |
| 173 | + }, |
| 174 | + ], |
| 175 | + }, |
| 176 | + |
| 177 | + // Invalid: Box imported from styled-react but used without sx prop |
| 178 | + { |
| 179 | + code: `import { Box } from '@primer/styled-react' |
| 180 | + const Component = () => <Box>Content</Box>`, |
| 181 | + output: `import { Box } from '@primer/react' |
| 182 | + const Component = () => <Box>Content</Box>`, |
| 183 | + errors: [ |
| 184 | + { |
| 185 | + messageId: 'usePrimerReactImport', |
| 186 | + data: {componentName: 'Box'}, |
| 187 | + }, |
| 188 | + ], |
| 189 | + }, |
| 190 | + |
| 191 | + // Invalid: Multiple components from styled-react, one used without sx |
| 192 | + { |
| 193 | + code: `import { Button, Box } from '@primer/styled-react' |
| 194 | + const Component = () => ( |
| 195 | + <div> |
| 196 | + <Button>Regular button</Button> |
| 197 | + <Box sx={{ padding: 2 }}>Styled box</Box> |
| 198 | + </div> |
| 199 | + )`, |
| 200 | + output: `import { Box } from '@primer/styled-react' |
| 201 | +import { Button } from '@primer/react' |
| 202 | + const Component = () => ( |
| 203 | + <div> |
| 204 | + <Button>Regular button</Button> |
| 205 | + <Box sx={{ padding: 2 }}>Styled box</Box> |
| 206 | + </div> |
| 207 | + )`, |
| 208 | + errors: [ |
| 209 | + { |
| 210 | + messageId: 'usePrimerReactImport', |
| 211 | + data: {componentName: 'Button'}, |
| 212 | + }, |
| 213 | + ], |
| 214 | + }, |
| 215 | + |
| 216 | + // Invalid: Button used both with and without sx prop - should use alias |
| 217 | + { |
| 218 | + code: `import { Button, Link } from '@primer/react' |
| 219 | + const Component = () => ( |
| 220 | + <div> |
| 221 | + <Link sx={{ color: 'red' }} /> |
| 222 | + <Button>Regular button</Button> |
| 223 | + <Button sx={{ color: 'red' }}>Styled button</Button> |
| 224 | + </div> |
| 225 | + )`, |
| 226 | + output: `import { Button } from '@primer/react' |
| 227 | +import { Button as StyledButton, Link } from '@primer/styled-react' |
| 228 | + const Component = () => ( |
| 229 | + <div> |
| 230 | + <Link sx={{ color: 'red' }} /> |
| 231 | + <Button>Regular button</Button> |
| 232 | + <StyledButton sx={{ color: 'red' }}>Styled button</StyledButton> |
| 233 | + </div> |
| 234 | + )`, |
| 235 | + errors: [ |
| 236 | + { |
| 237 | + messageId: 'useStyledReactImportWithAlias', |
| 238 | + data: {componentName: 'Button', aliasName: 'StyledButton'}, |
| 239 | + }, |
| 240 | + { |
| 241 | + messageId: 'useStyledReactImport', |
| 242 | + data: {componentName: 'Link'}, |
| 243 | + }, |
| 244 | + { |
| 245 | + messageId: 'useAliasedComponent', |
| 246 | + data: {componentName: 'Button', aliasName: 'StyledButton'}, |
| 247 | + }, |
| 248 | + ], |
| 249 | + }, |
| 250 | + ], |
| 251 | +}) |
0 commit comments