Skip to content

Add no-deprecated-flash ESLint rule to warn against Flash component usage #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions docs/rules/no-deprecated-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# No Deprecated Flash

## Rule Details

This rule discourages the use of Flash component and suggests using Banner component from `@primer/react/experimental` instead.

Flash component is deprecated and will be removed from @primer/react. The Banner component provides the same functionality and should be used instead.

👎 Examples of **incorrect** code for this rule

```jsx
import {Flash} from '@primer/react'

function ExampleComponent() {
return <Flash variant="warning">Warning message</Flash>
}
```

```jsx
import {Flash} from '@primer/react'

function ExampleComponent() {
return (
<Flash
variant="warning"
sx={{fontSize: 0, borderRadius: 0, py: 2, px: 3}}
className="custom-class"
>
Banner content
</Flash>
)
}
```

👍 Examples of **correct** code for this rule:

```jsx
import {Banner} from '@primer/react/experimental'

function ExampleComponent() {
return <Banner variant="warning">Warning message</Banner>
}
```

```jsx
import {Banner} from '@primer/react/experimental'

function ExampleComponent() {
return (
<Banner
variant="warning"
sx={{fontSize: 0, borderRadius: 0, py: 2, px: 3}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this sx prop as we are discouraging those too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the sx prop from the documentation example as requested. The example now only shows variant and className props. (427e5b2)

className="custom-class"
>
Banner content
</Banner>
)
}
```

## Auto-fix

This rule provides automatic fixes that:
- Replace `Flash` component usage with `Banner`
- Update import statements from `@primer/react` to `@primer/react/experimental`
- Preserve all props, attributes, and children content
- Handle mixed imports appropriately
- Avoid duplicate Banner imports when they already exist
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
'prefer-action-list-item-onselect': require('./rules/prefer-action-list-item-onselect'),
'enforce-css-module-identifier-casing': require('./rules/enforce-css-module-identifier-casing'),
'enforce-css-module-default-import': require('./rules/enforce-css-module-default-import'),
'no-deprecated-flash': require('./rules/no-deprecated-flash'),
},
configs: {
recommended: require('./configs/recommended'),
Expand Down
201 changes: 201 additions & 0 deletions src/rules/__tests__/no-deprecated-flash.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
'use strict'

const {RuleTester} = require('eslint')
const rule = require('../no-deprecated-flash')

const ruleTester = new RuleTester({
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
})

ruleTester.run('no-deprecated-flash', rule, {
valid: [
// Banner import and usage is valid
{
code: `import {Banner} from '@primer/react/experimental'

function Component() {
return <Banner variant="warning">Content</Banner>
}`,
},
// Flash imported from other packages is valid
{
code: `import {Flash} from 'some-other-package'

function Component() {
return <Flash>Content</Flash>
}`,
},
// No import of Flash
{
code: `import {Button} from '@primer/react'

function Component() {
return <Button>Click me</Button>
}`,
},
],
invalid: [
// Basic Flash import and usage
{
code: `import {Flash} from '@primer/react'

function Component() {
return <Flash variant="warning">Banner content</Flash>
}`,
output: `import {Banner} from '@primer/react/experimental'

function Component() {
return <Banner variant="warning">Banner content</Banner>
}`,
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}],
},

// Flash with complex props
{
code: `import {Flash} from '@primer/react'

function Component() {
return (
<Flash
variant="warning"
sx={{fontSize: 0, borderRadius: 0, py: 2, px: 3}}
className="custom-class"
>
Banner content
</Flash>
)
}`,
output: `import {Banner} from '@primer/react/experimental'

function Component() {
return (
<Banner
variant="warning"
sx={{fontSize: 0, borderRadius: 0, py: 2, px: 3}}
className="custom-class"
>
Banner content
</Banner>
)
}`,
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}],
},

// Mixed imports - Flash with other components
{
code: `import {Button, Flash, Text} from '@primer/react'

function Component() {
return (
<div>
<Button>Click me</Button>
<Flash variant="error">Error message</Flash>
<Text>Some text</Text>
</div>
)
}`,
output: `import {Button, Text} from '@primer/react'
import {Banner} from '@primer/react/experimental'

function Component() {
return (
<div>
<Button>Click me</Button>
<Banner variant="error">Error message</Banner>
<Text>Some text</Text>
</div>
)
}`,
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}],
},

// Flash only import
{
code: `import {Flash} from '@primer/react'

function Component() {
return <Flash>Just Flash</Flash>
}`,
output: `import {Banner} from '@primer/react/experimental'

function Component() {
return <Banner>Just Flash</Banner>
}`,
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}],
},

// Self-closing Flash
{
code: `import {Flash} from '@primer/react'

function Component() {
return <Flash variant="info" />
}`,
output: `import {Banner} from '@primer/react/experimental'

function Component() {
return <Banner variant="info" />
}`,
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}],
},

// Multiple Flash components
{
code: `import {Flash} from '@primer/react'

function Component() {
return (
<div>
<Flash variant="warning">Warning</Flash>
<Flash variant="error">Error</Flash>
</div>
)
}`,
output: `import {Banner} from '@primer/react/experimental'

function Component() {
return (
<div>
<Banner variant="warning">Warning</Banner>
<Banner variant="error">Error</Banner>
</div>
)
}`,
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}],
},

// Flash with existing Banner import (should not duplicate)
{
code: `import {Flash} from '@primer/react'
import {Banner} from '@primer/react/experimental'

function Component() {
return (
<div>
<Flash variant="warning">Flash message</Flash>
<Banner variant="info">Banner message</Banner>
</div>
)
}`,
output: `import {Banner} from '@primer/react/experimental'

function Component() {
return (
<div>
<Banner variant="warning">Flash message</Banner>
<Banner variant="info">Banner message</Banner>
</div>
)
}`,
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}],
},
],
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also consider a case where other stuff is already being imported from primer/react/experimental like TooltipV2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case for existing imports from @primer/react/experimental like TooltipV2. The rule now properly handles scenarios where other experimental components are already being imported. (427e5b2)

Loading