eslint-plugin-primer-react 8.6.1 → 9.0.0-rc.e1d461c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,537 +0,0 @@
1
- const rule = require('../use-styled-react-import')
2
- const {RuleTester} = require('eslint')
3
-
4
- const ruleTester = new RuleTester({
5
- languageOptions: {
6
- parser: require(require.resolve('@typescript-eslint/parser', {paths: [require.resolve('eslint-plugin-github')]})),
7
- ecmaVersion: 'latest',
8
- sourceType: 'module',
9
- parserOptions: {
10
- ecmaFeatures: {
11
- jsx: true,
12
- },
13
- },
14
- },
15
- })
16
-
17
- ruleTester.run('use-styled-react-import', rule, {
18
- valid: [
19
- // Valid: Component used without sx prop
20
- `import { Button } from '@primer/react'
21
- const Component = () => <Button>Click me</Button>`,
22
-
23
- // Valid: Component with sx prop imported from styled-react
24
- `import { Button } from '@primer/styled-react'
25
- const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`,
26
-
27
- // Valid: Utilities imported from styled-react
28
- `import { sx } from '@primer/styled-react'`,
29
- `import { useTheme } from '@primer/styled-react'`,
30
- `import { sx, useTheme } from '@primer/styled-react'`,
31
-
32
- // Valid: Component not in the styled list
33
- `import { Avatar } from '@primer/react'
34
- const Component = () => <Avatar sx={{ color: 'red' }} />`,
35
-
36
- // Valid: Component not imported from @primer/react
37
- `import { Button } from '@github-ui/button'
38
- const Component = () => <Button sx={{ color: 'red' }} />`,
39
-
40
- // Valid: Mixed imports - component without sx prop
41
- `import { Button, Text } from '@primer/react'
42
- const Component = () => <Button>Click me</Button>`,
43
-
44
- // Valid: Component without sx prop imported from styled-react (when not used)
45
- `import { Button } from '@primer/styled-react'`,
46
-
47
- // Valid: allowedComponents without sx prop imported from styled-react
48
- `import { ThemeProvider, BaseStyles } from '@primer/styled-react'
49
- const Component = ({children}) => <ThemeProvider><BaseStyles>{children}</BaseStyles></ThemeProvider>`,
50
-
51
- // Valid: Component with sx prop AND allowedComponents
52
- `import { ThemeProvider, Button } from '@primer/styled-react'
53
- const Component = () => <ThemeProvider><Button sx={{ color: 'btn.bg'}}>Click me</Button></ThemeProvider>`,
54
- ],
55
- invalid: [
56
- // Invalid: Box with sx prop imported from @primer/react
57
- {
58
- code: `import { Box } from '@primer/react'
59
- const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`,
60
- output: `import { Box } from '@primer/styled-react'
61
- const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`,
62
- errors: [
63
- {
64
- messageId: 'useStyledReactImport',
65
- data: {componentName: 'Box'},
66
- },
67
- ],
68
- },
69
-
70
- // Invalid: Imports from /experimental and /deprecated paths
71
- {
72
- code: `import { Button } from '@primer/react/experimental'
73
- import { Box } from '@primer/react/deprecated'
74
- const Component = () => <Box sx={{ color: 'red' }}><Button sx={{ margin: 2 }}>Click me</Button></Box>`,
75
- output: `import { Button } from '@primer/styled-react/experimental'
76
- import { Box } from '@primer/styled-react/deprecated'
77
- const Component = () => <Box sx={{ color: 'red' }}><Button sx={{ margin: 2 }}>Click me</Button></Box>`,
78
- errors: [
79
- {
80
- messageId: 'useStyledReactImport',
81
- data: {componentName: 'Button'},
82
- },
83
- {
84
- messageId: 'useStyledReactImport',
85
- data: {componentName: 'Box'},
86
- },
87
- ],
88
- },
89
-
90
- // Invalid: ActionList.Item with sx prop and ActionList imported from @primer/react
91
- {
92
- code: `import { ActionList } from '@primer/react'
93
- const Component = () => <ActionList.Item sx={{ color: 'red' }}>Content</ActionList.Item>`,
94
- output: `import { ActionList } from '@primer/styled-react'
95
- const Component = () => <ActionList.Item sx={{ color: 'red' }}>Content</ActionList.Item>`,
96
- errors: [
97
- {
98
- messageId: 'useStyledReactImport',
99
- data: {componentName: 'ActionList'},
100
- },
101
- ],
102
- },
103
-
104
- // Invalid: FormControl used both with and without sx prop - should move to styled-react
105
- {
106
- code: `import { FormControl } from '@primer/react'
107
- const Component = () => (
108
- <div>
109
- <FormControl></FormControl>
110
- <FormControl sx={{ color: 'red' }}>
111
- <FormControl.Label visuallyHidden>Label</FormControl.Label>
112
- </FormControl>
113
- </div>
114
- )`,
115
- output: `import { FormControl } from '@primer/styled-react'
116
- const Component = () => (
117
- <div>
118
- <FormControl></FormControl>
119
- <FormControl sx={{ color: 'red' }}>
120
- <FormControl.Label visuallyHidden>Label</FormControl.Label>
121
- </FormControl>
122
- </div>
123
- )`,
124
- errors: [
125
- {
126
- messageId: 'useStyledReactImport',
127
- data: {componentName: 'FormControl'},
128
- },
129
- ],
130
- },
131
-
132
- // Invalid: Button with sx prop imported from @primer/react
133
- {
134
- code: `import { Button } from '@primer/react'
135
- const Component = () => <Button sx={{ margin: 2 }}>Click me</Button>`,
136
- output: `import { Button } from '@primer/styled-react'
137
- const Component = () => <Button sx={{ margin: 2 }}>Click me</Button>`,
138
- errors: [
139
- {
140
- messageId: 'useStyledReactImport',
141
- data: {componentName: 'Button'},
142
- },
143
- ],
144
- },
145
-
146
- // Invalid: ActionList used without sx, ActionList.Item used with sx - should move ActionList to styled-react
147
- {
148
- code: `import { ActionList, ActionMenu } from '@primer/react'
149
- const Component = () => (
150
- <ActionMenu>
151
- <ActionMenu.Overlay>
152
- <ActionList>
153
- <ActionList.Item sx={{ paddingLeft: 'calc(1 * var(--base-size-12))' }}>
154
- Item
155
- </ActionList.Item>
156
- </ActionList>
157
- </ActionMenu.Overlay>
158
- </ActionMenu>
159
- )`,
160
- output: `import { ActionMenu } from '@primer/react'
161
- import { ActionList } from '@primer/styled-react'
162
- const Component = () => (
163
- <ActionMenu>
164
- <ActionMenu.Overlay>
165
- <ActionList>
166
- <ActionList.Item sx={{ paddingLeft: 'calc(1 * var(--base-size-12))' }}>
167
- Item
168
- </ActionList.Item>
169
- </ActionList>
170
- </ActionMenu.Overlay>
171
- </ActionMenu>
172
- )`,
173
- errors: [
174
- {
175
- messageId: 'useStyledReactImport',
176
- data: {componentName: 'ActionList'},
177
- },
178
- ],
179
- },
180
-
181
- // Invalid: Multiple components, one with sx prop
182
- {
183
- code: `import { Button, Box, Avatar } from '@primer/react'
184
- const Component = () => (
185
- <div>
186
- <Button>Regular button</Button>
187
- <Box sx={{ padding: 2 }}>Styled box</Box>
188
- <Avatar />
189
- </div>
190
- )`,
191
- output: `import { Button, Avatar } from '@primer/react'
192
- import { Box } from '@primer/styled-react'
193
- const Component = () => (
194
- <div>
195
- <Button>Regular button</Button>
196
- <Box sx={{ padding: 2 }}>Styled box</Box>
197
- <Avatar />
198
- </div>
199
- )`,
200
- errors: [
201
- {
202
- messageId: 'useStyledReactImport',
203
- data: {componentName: 'Box'},
204
- },
205
- ],
206
- },
207
-
208
- // Invalid: Utility import from @primer/react that should be from styled-react
209
- {
210
- code: `import { sx } from '@primer/react'`,
211
- output: `import { sx } from '@primer/styled-react'`,
212
- errors: [
213
- {
214
- messageId: 'moveToStyledReact',
215
- data: {importName: 'sx'},
216
- },
217
- ],
218
- },
219
- {
220
- code: `import { useTheme } from '@primer/react'`,
221
- output: `import { useTheme } from '@primer/styled-react'`,
222
- errors: [
223
- {
224
- messageId: 'moveToStyledReact',
225
- data: {importName: 'useTheme'},
226
- },
227
- ],
228
- },
229
- {
230
- code: `import { sx, useTheme } from '@primer/react'`,
231
- output: `import { sx, useTheme } from '@primer/styled-react'`,
232
- errors: [
233
- {
234
- messageId: 'moveToStyledReact',
235
- data: {importName: 'sx'},
236
- },
237
- {
238
- messageId: 'moveToStyledReact',
239
- data: {importName: 'useTheme'},
240
- },
241
- ],
242
- },
243
-
244
- // Invalid: Utility import from @primer/react that should be from styled-react, mixed with other imports
245
- {
246
- code: `import { sx, useAnchoredPosition } from '@primer/react'`,
247
- output: `import { useAnchoredPosition } from '@primer/react'
248
- import { sx } from '@primer/styled-react'`,
249
- errors: [
250
- {
251
- messageId: 'moveToStyledReact',
252
- data: {importName: 'sx'},
253
- },
254
- ],
255
- },
256
-
257
- // Invalid: Button and Link, only Button uses sx
258
- {
259
- code: `import { Button, Link } from '@primer/react'
260
- const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`,
261
- output: `import { Link } from '@primer/react'
262
- import { Button } from '@primer/styled-react'
263
- const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`,
264
- errors: [
265
- {
266
- messageId: 'useStyledReactImport',
267
- data: {componentName: 'Button'},
268
- },
269
- ],
270
- },
271
-
272
- // Invalid: Button imported from styled-react but used without sx prop
273
- {
274
- code: `import { Button } from '@primer/styled-react'
275
- const Component = () => <Button>Click me</Button>`,
276
- output: `import { Button } from '@primer/react'
277
- const Component = () => <Button>Click me</Button>`,
278
- errors: [
279
- {
280
- messageId: 'usePrimerReactImport',
281
- data: {componentName: 'Button'},
282
- },
283
- ],
284
- },
285
-
286
- // Invalid: <Link /> and <Button /> imported from styled-react but used without sx prop
287
- {
288
- code: `import { Button } from '@primer/react'
289
- import { Button as StyledButton, Link } from '@primer/styled-react'
290
- const Component = () => (
291
- <div>
292
- <Link />
293
- <Button>Regular button</Button>
294
- <StyledButton>Styled button</StyledButton>
295
- </div>
296
- )`,
297
- output: `import { Button, Link } from '@primer/react'
298
-
299
- const Component = () => (
300
- <div>
301
- <Link />
302
- <Button>Regular button</Button>
303
- <StyledButton>Styled button</StyledButton>
304
- </div>
305
- )`,
306
- errors: [
307
- {
308
- messageId: 'usePrimerReactImport',
309
- data: {componentName: 'Button'},
310
- },
311
- {
312
- messageId: 'usePrimerReactImport',
313
- data: {componentName: 'Link'},
314
- },
315
- ],
316
- },
317
-
318
- // Invalid: Box imported from styled-react but used without sx prop
319
- {
320
- code: `import { Box } from '@primer/styled-react'
321
- const Component = () => <Box>Content</Box>`,
322
- output: `import { Box } from '@primer/react'
323
- const Component = () => <Box>Content</Box>`,
324
- errors: [
325
- {
326
- messageId: 'usePrimerReactImport',
327
- data: {componentName: 'Box'},
328
- },
329
- ],
330
- },
331
-
332
- // Invalid: Multiple components from styled-react, one used without sx
333
- {
334
- code: `import { Button, Box } from '@primer/styled-react'
335
- const Component = () => (
336
- <div>
337
- <Button>Regular button</Button>
338
- <Box sx={{ padding: 2 }}>Styled box</Box>
339
- </div>
340
- )`,
341
- output: `import { Box } from '@primer/styled-react'
342
- import { Button } from '@primer/react'
343
- const Component = () => (
344
- <div>
345
- <Button>Regular button</Button>
346
- <Box sx={{ padding: 2 }}>Styled box</Box>
347
- </div>
348
- )`,
349
- errors: [
350
- {
351
- messageId: 'usePrimerReactImport',
352
- data: {componentName: 'Button'},
353
- },
354
- ],
355
- },
356
-
357
- // Invalid: Button and Link used with sx prop - should move both to styled-react
358
- {
359
- code: `import { Button, Link } from '@primer/react'
360
- const Component = () => (
361
- <div>
362
- <Link sx={{ color: 'red' }} />
363
- <Button>Regular button</Button>
364
- <Button sx={{ color: 'red' }}>Styled button</Button>
365
- </div>
366
- )`,
367
- output: `import { Button, Link } from '@primer/styled-react'
368
- const Component = () => (
369
- <div>
370
- <Link sx={{ color: 'red' }} />
371
- <Button>Regular button</Button>
372
- <Button sx={{ color: 'red' }}>Styled button</Button>
373
- </div>
374
- )`,
375
- errors: [
376
- {
377
- messageId: 'useStyledReactImport',
378
- data: {componentName: 'Button'},
379
- },
380
- {
381
- messageId: 'useStyledReactImport',
382
- data: {componentName: 'Link'},
383
- },
384
- ],
385
- },
386
-
387
- // Invalid: ThemeProvider and BaseStyles - should move to styled-react
388
- {
389
- code: `
390
- import { ThemeProvider, BaseStyles } from '@primer/react'
391
- `,
392
- output: `
393
- import { ThemeProvider, BaseStyles } from '@primer/styled-react'
394
- `,
395
- errors: [
396
- {
397
- messageId: 'moveToStyledReact',
398
- data: {importName: 'ThemeProvider'},
399
- },
400
- {
401
- messageId: 'moveToStyledReact',
402
- data: {importName: 'BaseStyles'},
403
- },
404
- ],
405
- },
406
-
407
- // Invalid: ThemeProvider, Button without sx prop - only ThemeProvider should be from styled-react
408
- {
409
- code: `
410
- import { ThemeProvider, Button } from '@primer/react'
411
- const Component = () => <ThemeProvider><Button>Click me</Button></ThemeProvider>
412
- `,
413
- output: `
414
- import { Button } from '@primer/react'
415
- import { ThemeProvider } from '@primer/styled-react'
416
- const Component = () => <ThemeProvider><Button>Click me</Button></ThemeProvider>
417
- `,
418
- errors: [
419
- {
420
- messageId: 'moveToStyledReact',
421
- data: {importName: 'ThemeProvider'},
422
- },
423
- ],
424
- },
425
-
426
- // Invalid: Utility and type imports from @primer/react that should be from styled-react
427
- {
428
- code: `import { sx, type SxProp } from '@primer/react'`,
429
- output: `import { sx, type SxProp } from '@primer/styled-react'`,
430
- errors: [
431
- {
432
- messageId: 'moveToStyledReact',
433
- data: {importName: 'sx'},
434
- },
435
- {
436
- messageId: 'moveToStyledReact',
437
- data: {importName: 'SxProp'},
438
- },
439
- ],
440
- },
441
-
442
- // Invalid: type keyword preserved when splitting imports - component with sx moved to styled-react
443
- {
444
- code: `import { Button, type ButtonProps } from '@primer/react'
445
- const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`,
446
- output: `import { type ButtonProps } from '@primer/react'
447
- import { Button } from '@primer/styled-react'
448
- const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`,
449
- errors: [
450
- {
451
- messageId: 'useStyledReactImport',
452
- data: {componentName: 'Button'},
453
- },
454
- ],
455
- },
456
-
457
- // Invalid: type keyword preserved on moved specifiers (type + utility)
458
- {
459
- code: `import { type SxProp, sx, Button } from '@primer/react'
460
- const Component = () => <Button>Click me</Button>`,
461
- output: `import { Button } from '@primer/react'
462
- import { type SxProp, sx } from '@primer/styled-react'
463
- const Component = () => <Button>Click me</Button>`,
464
- errors: [
465
- {
466
- messageId: 'moveToStyledReact',
467
- data: {importName: 'SxProp'},
468
- },
469
- {
470
- messageId: 'moveToStyledReact',
471
- data: {importName: 'sx'},
472
- },
473
- ],
474
- },
475
- ],
476
- })
477
-
478
- // Test configuration options
479
- ruleTester.run('use-styled-react-import with custom configuration', rule, {
480
- valid: [
481
- // Valid: Custom component not in default list
482
- {
483
- code: `import { CustomButton } from '@primer/react'
484
- const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
485
- options: [{}], // Using default configuration
486
- },
487
-
488
- // Valid: Custom component in custom list used without sx prop
489
- {
490
- code: `import { CustomButton } from '@primer/react'
491
- const Component = () => <CustomButton>Click me</CustomButton>`,
492
- options: [{styledComponents: ['CustomButton']}],
493
- },
494
-
495
- // Valid: Custom component with sx prop imported from styled-react
496
- {
497
- code: `import { CustomButton } from '@primer/styled-react'
498
- const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
499
- options: [{styledComponents: ['CustomButton']}],
500
- },
501
-
502
- // Valid: Box not in custom list, so sx usage is allowed from @primer/react
503
- {
504
- code: `import { Box } from '@primer/react'
505
- const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`,
506
- options: [{styledComponents: ['CustomButton']}], // Box not included
507
- },
508
- ],
509
- invalid: [
510
- // Invalid: Custom component with sx prop should be from styled-react
511
- {
512
- code: `import { CustomButton } from '@primer/react'
513
- const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
514
- output: `import { CustomButton } from '@primer/styled-react'
515
- const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
516
- options: [{styledComponents: ['CustomButton']}],
517
- errors: [
518
- {
519
- messageId: 'useStyledReactImport',
520
- data: {componentName: 'CustomButton'},
521
- },
522
- ],
523
- },
524
- // Invalid: Custom utility should be from styled-react
525
- {
526
- code: `import { customSx } from '@primer/react'`,
527
- output: `import { customSx } from '@primer/styled-react'`,
528
- options: [{styledUtilities: ['customSx']}],
529
- errors: [
530
- {
531
- messageId: 'moveToStyledReact',
532
- data: {importName: 'customSx'},
533
- },
534
- ],
535
- },
536
- ],
537
- })