-
Notifications
You must be signed in to change notification settings - Fork 10
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
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/fix-383
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f0db77a
Initial plan
Copilot 67bed06
Add no-deprecated-flash rule to discourage Flash component usage
Copilot 427e5b2
Fix review comments: remove auto-fixing, remove sx prop, add experime…
Copilot 159c6bb
Merge branch 'main' into copilot/fix-383
pksjce bd99191
Create chilled-masks-lay.md
pksjce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 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" 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
'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> | ||
}`, | ||
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}], | ||
}, | ||
|
||
// Flash with complex props | ||
{ | ||
code: `import {Flash} from '@primer/react' | ||
|
||
function Component() { | ||
return ( | ||
<Flash | ||
variant="warning" | ||
className="custom-class" | ||
> | ||
Banner content | ||
</Flash> | ||
) | ||
}`, | ||
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> | ||
) | ||
}`, | ||
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}], | ||
}, | ||
|
||
// Flash only import | ||
{ | ||
code: `import {Flash} from '@primer/react' | ||
|
||
function Component() { | ||
return <Flash>Just Flash</Flash> | ||
}`, | ||
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}], | ||
}, | ||
|
||
// Self-closing Flash | ||
{ | ||
code: `import {Flash} from '@primer/react' | ||
|
||
function Component() { | ||
return <Flash 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> | ||
) | ||
}`, | ||
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> | ||
) | ||
}`, | ||
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}], | ||
}, | ||
|
||
// Flash with existing experimental imports like TooltipV2 | ||
{ | ||
code: `import {Flash} from '@primer/react' | ||
import {TooltipV2} from '@primer/react/experimental' | ||
|
||
function Component() { | ||
return ( | ||
<div> | ||
<Flash variant="warning">Flash message</Flash> | ||
<TooltipV2>Tooltip content</TooltipV2> | ||
</div> | ||
) | ||
}`, | ||
errors: [{messageId: 'flashDeprecated'}, {messageId: 'flashDeprecated'}], | ||
}, | ||
], | ||
}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name') | ||
const {isPrimerComponent} = require('../utils/is-primer-component') | ||
const url = require('../url') | ||
|
||
/** | ||
* @type {import('eslint').Rule.RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Flash component is deprecated. Use Banner from @primer/react/experimental instead.', | ||
recommended: true, | ||
url: url(module), | ||
}, | ||
schema: [], | ||
messages: { | ||
flashDeprecated: 'Flash component is deprecated. Use Banner from @primer/react/experimental instead.', | ||
}, | ||
}, | ||
create(context) { | ||
const sourceCode = context.sourceCode || context.getSourceCode() | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
// Check if importing Flash from @primer/react | ||
if (node.source.value !== '@primer/react') { | ||
return | ||
} | ||
|
||
const flashSpecifier = node.specifiers.find( | ||
specifier => specifier.type === 'ImportSpecifier' && specifier.imported?.name === 'Flash', | ||
) | ||
|
||
if (!flashSpecifier) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node: flashSpecifier, | ||
messageId: 'flashDeprecated', | ||
}) | ||
}, | ||
|
||
JSXElement(node) { | ||
const elementName = getJSXOpeningElementName(node.openingElement) | ||
|
||
if (elementName !== 'Flash') { | ||
return | ||
} | ||
|
||
// Check if Flash is imported from @primer/react using isPrimerComponent | ||
const scope = sourceCode.getScope ? sourceCode.getScope(node.openingElement) : context.getScope() | ||
if (!isPrimerComponent(node.openingElement.name, scope)) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node: node.openingElement.name, | ||
messageId: 'flashDeprecated', | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
likeTooltipV2
There was a problem hiding this comment.
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)