-
Notifications
You must be signed in to change notification settings - Fork 9
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
feat: add use-deprecated-from-deprecated #204
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,5 @@ | ||
--- | ||
'eslint-plugin-primer-react': minor | ||
--- | ||
|
||
Add use-deprecated-from-deprecated rule |
This file contains 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,25 @@ | ||
# Use Deprecated from Deprecated | ||
|
||
## Rule Details | ||
|
||
This rule enforces the usage of deprecated imports from `@primer/react/deprecated`. | ||
|
||
👎 Examples of **incorrect** code for this rule | ||
|
||
```jsx | ||
import {Dialog} from '@primer/react' | ||
|
||
function ExampleComponent() { | ||
return <Dialog>{/* ... */}</Dialog> | ||
} | ||
``` | ||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
import {Dialog} from '@primer/react/deprecated' | ||
|
||
function ExampleComponent() { | ||
return <Dialog>{/* ... */}</Dialog> | ||
} | ||
``` |
This file contains 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
66 changes: 66 additions & 0 deletions
66
src/rules/__tests__/use-deprecated-from-deprecated.test.js
This file contains 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,66 @@ | ||
'use strict' | ||
|
||
const {RuleTester} = require('eslint') | ||
const rule = require('../use-deprecated-from-deprecated') | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('use-deprecated-from-deprecated', rule, { | ||
valid: [], | ||
invalid: [ | ||
// Single deprecated import | ||
{ | ||
code: `import {Tooltip} from '@primer/react'`, | ||
output: `import {Tooltip} from '@primer/react/deprecated'`, | ||
errors: ['Import deprecated components from @primer/react/deprecated'], | ||
}, | ||
|
||
// Single deprecated import with existing deprecated entrypoint | ||
{ | ||
code: `import {Tooltip} from '@primer/react' | ||
import {Dialog} from '@primer/react/deprecated'`, | ||
output: `\nimport {Dialog, Tooltip} from '@primer/react/deprecated'`, | ||
errors: ['Import deprecated components from @primer/react/deprecated'], | ||
}, | ||
|
||
// Multiple deprecated imports | ||
{ | ||
code: `import {Dialog, Tooltip} from '@primer/react'`, | ||
output: `import {Dialog, Tooltip} from '@primer/react/deprecated'`, | ||
errors: ['Import deprecated components from @primer/react/deprecated'], | ||
}, | ||
|
||
// Mixed deprecated and non-deprecated imports | ||
{ | ||
code: `import {Button, Tooltip} from '@primer/react'`, | ||
output: `import {Button, } from '@primer/react' | ||
import {Tooltip} from '@primer/react/deprecated'`, | ||
errors: ['Import deprecated components from @primer/react/deprecated'], | ||
}, | ||
|
||
// Mixed deprecated and non-deprecated imports with existing deprecated | ||
{ | ||
code: `import {Button, Tooltip} from '@primer/react' | ||
import {Dialog} from '@primer/react/deprecated'`, | ||
output: `import {Button, } from '@primer/react' | ||
import {Dialog, Tooltip} from '@primer/react/deprecated'`, | ||
errors: ['Import deprecated components from @primer/react/deprecated'], | ||
}, | ||
|
||
// Multiple mixed deprecated and non-deprecated imports | ||
{ | ||
code: `import {Button, Dialog, Tooltip} from '@primer/react'`, | ||
output: `import {Button, } from '@primer/react' | ||
import {Dialog, Tooltip} from '@primer/react/deprecated'`, | ||
errors: ['Import deprecated components from @primer/react/deprecated'], | ||
}, | ||
], | ||
}) |
This file contains 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,130 @@ | ||
'use strict' | ||
|
||
const url = require('../url') | ||
|
||
const components = [ | ||
{ | ||
identifier: 'Dialog', | ||
entrypoint: '@primer/react', | ||
}, | ||
{ | ||
identifier: 'Octicon', | ||
entrypoint: '@primer/react', | ||
}, | ||
{ | ||
identifier: 'Pagehead', | ||
entrypoint: '@primer/react', | ||
}, | ||
{ | ||
identifier: 'TabNav', | ||
entrypoint: '@primer/react', | ||
}, | ||
{ | ||
identifier: 'Tooltip', | ||
entrypoint: '@primer/react', | ||
}, | ||
] | ||
|
||
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', | ||
recommended: true, | ||
url: url(module), | ||
}, | ||
fixable: true, | ||
schema: [], | ||
}, | ||
create(context) { | ||
const sourceCode = context.getSourceCode() | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
if (!entrypoints.has(node.source.value)) { | ||
return | ||
} | ||
|
||
const entrypoint = entrypoints.get(node.source.value) | ||
const deprecated = node.specifiers.filter(specifier => { | ||
return entrypoint.has(specifier.imported.name) | ||
}) | ||
|
||
if (deprecated.length === 0) { | ||
return | ||
} | ||
|
||
const deprecatedEntrypoint = node.parent.body.find(node => { | ||
if (node.type !== 'ImportDeclaration') { | ||
return false | ||
} | ||
|
||
return node.source.value === '@primer/react/deprecated' | ||
}) | ||
|
||
// All imports are deprecated | ||
if (deprecated.length === node.specifiers.length) { | ||
context.report({ | ||
node, | ||
message: 'Import deprecated components from @primer/react/deprecated', | ||
*fix(fixer) { | ||
if (deprecatedEntrypoint) { | ||
const lastSpecifier = deprecatedEntrypoint.specifiers[deprecatedEntrypoint.specifiers.length - 1] | ||
|
||
yield fixer.remove(node) | ||
yield fixer.insertTextAfter( | ||
lastSpecifier, | ||
`, ${node.specifiers.map(specifier => specifier.imported.name).join(', ')}`, | ||
) | ||
} else { | ||
yield fixer.replaceText(node.source, `'@primer/react/deprecated'`) | ||
} | ||
}, | ||
}) | ||
} else { | ||
// There is a mix of deprecated and non-deprecated imports | ||
context.report({ | ||
node, | ||
message: 'Import deprecated components from @primer/react/deprecated', | ||
*fix(fixer) { | ||
for (const specifier of deprecated) { | ||
yield fixer.remove(specifier) | ||
const comma = sourceCode.getTokenAfter(specifier) | ||
if (comma.value === ',') { | ||
yield fixer.remove(comma) | ||
} | ||
} | ||
|
||
if (deprecatedEntrypoint) { | ||
const lastSpecifier = deprecatedEntrypoint.specifiers[deprecatedEntrypoint.specifiers.length - 1] | ||
yield fixer.insertTextAfter( | ||
lastSpecifier, | ||
`, ${deprecated.map(specifier => specifier.imported.name).join(', ')}`, | ||
) | ||
} else { | ||
yield fixer.insertTextAfter( | ||
node, | ||
`\nimport {${deprecated | ||
.map(specifier => specifier.imported.name) | ||
.join(', ')}} from '@primer/react/deprecated'`, | ||
) | ||
} | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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.
Is this
,
after the Button expected? Seems like tests are succeeding so maybe it is expected. If so, are we relying on prettier to remove the comma after the fix?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.
Yeah, let me know if you have a good way to remove it 😅 I couldn't really figure it out. Definitely relying on prettier for this