-
Notifications
You must be signed in to change notification settings - Fork 10
Add new rule no-deprecated-experimental-components
#325
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
Changes from 6 commits
5781a76
8aebb76
0f06ed2
364ff17
a5d1f8e
ad1531b
d4353f4
50f33ef
d099c40
9c69048
0f96e8d
481a89c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-primer-react': minor | ||
--- | ||
|
||
Add `no-deprecated-experimental-components` rule |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# No deprecated experimental components | ||
|
||
## Rule Details | ||
|
||
This rule discourages the usage of specific imports from `@primer/react/experimental`. | ||
|
||
👎 Examples of **incorrect** code for this rule | ||
|
||
```jsx | ||
import {SelectPanel} from '@primer/react/experimental' | ||
|
||
function ExampleComponent() { | ||
return <SelectPanel /> | ||
} | ||
``` | ||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
You can satisfy the rule by either converting to the non-experimental version: | ||
|
||
```jsx | ||
import {SelectPane} from '@primer/react' | ||
|
||
function ExampleComponent() { | ||
return <SelectPanel /> | ||
} | ||
``` | ||
|
||
Or by removing usage of the component. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const {RuleTester} = require('eslint') | ||
const rule = require('../no-deprecated-experimental-components') | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('no-deprecated-experimental-components', rule, { | ||
valid: [ | ||
{ | ||
code: `import {SelectPanel} from '@primer/react'`, | ||
}, | ||
{ | ||
code: `import {DataTable} from '@primer/react/experimental'`, | ||
}, | ||
{ | ||
code: `import {DataTable, ActionBar} from '@primer/react/experimental'`, | ||
}, | ||
], | ||
invalid: [ | ||
// Single experimental import | ||
{ | ||
code: `import {SelectPanel} from '@primer/react/experimental'`, | ||
errors: [ | ||
'SelectPanel is deprecated. Please import them from the stable entrypoint (@primer/react) if available.', | ||
], | ||
}, | ||
// Multiple experimental import | ||
{ | ||
code: `import {SelectPanel, DataTable, ActionBar} from '@primer/react/experimental'`, | ||
errors: [ | ||
'SelectPanel is deprecated. Please import them from the stable entrypoint (@primer/react) if available.', | ||
], | ||
}, | ||
], | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const url = require('../url') | ||
|
||
const components = [ | ||
{ | ||
identifier: 'SelectPanel', | ||
entrypoint: '@primer/react/experimental', | ||
}, | ||
] | ||
|
||
const entrypoints = new Map() | ||
|
||
for (const component of components) { | ||
if (!entrypoints.has(component.entrypoint)) { | ||
entrypoints.set(component.entrypoint, new Set()) | ||
} | ||
entrypoints.get(component.entrypoint).add(component.identifier) | ||
} | ||
|
||
/** | ||
* @type {import('eslint').Rule.RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Use deprecated components from the `@primer/react/deprecated` entrypoint', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oop, can we fix this description? 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup! Should now be "Use a stable component from the
TylerJDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
recommended: true, | ||
url: url(module), | ||
}, | ||
fixable: true, | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
if (!entrypoints.has(node.source.value)) { | ||
return | ||
} | ||
|
||
const entrypoint = entrypoints.get(node.source.value) | ||
|
||
const experimental = node.specifiers.filter(specifier => { | ||
return entrypoint.has(specifier.imported.name) | ||
}) | ||
|
||
const components = experimental.map(specifier => specifier.imported.name) | ||
|
||
if (experimental.length === 0) { | ||
return | ||
} | ||
|
||
if (experimental.length > 0) { | ||
const message = `${components.join(', ')} ${ | ||
components.length > 1 ? 'are' : 'is' | ||
} deprecated. Please import them from the stable entrypoint (@primer/react) if available.` | ||
TylerJDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
context.report({ | ||
node, | ||
message, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
Uh oh!
There was an error while loading. Please reload this page.