-
Notifications
You must be signed in to change notification settings - Fork 19
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: No duplicate import specifiers rule #713
Merged
thatblindgeye
merged 4 commits into
patternfly:main
from
adamviktora:no-duplicate-import-specifiers
Jul 31, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0ade52f
feat: no-duplicate-import-specifiers cleanup rule
adamviktora 867ade6
refactor: findDuplicates function
adamviktora b41cf31
fix: don't update example with unrelated rule
adamviktora 5e1f9d9
test(noDuplicateImportSpecifiers): add tests of different packages
adamviktora 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
17 changes: 17 additions & 0 deletions
17
...mods/src/rules/v6/noDuplicateImportSpecifiers/no-duplicate-import-specifiers.md
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,17 @@ | ||
### no-duplicate-import-specifiers | ||
|
||
Duplicate import specifiers should be removed. This is a cleanup rule which runs after other rules. | ||
|
||
#### Examples | ||
|
||
In: | ||
|
||
```jsx | ||
%inputExample% | ||
``` | ||
|
||
Out: | ||
|
||
```jsx | ||
%outputExample% | ||
``` |
94 changes: 94 additions & 0 deletions
94
...-codemods/src/rules/v6/noDuplicateImportSpecifiers/no-duplicate-import-specifiers.test.ts
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,94 @@ | ||
const ruleTester = require("../../ruletester"); | ||
import * as rule from "./no-duplicate-import-specifiers"; | ||
|
||
ruleTester.run("no-duplicate-import-specifiers", rule, { | ||
valid: [ | ||
{ | ||
// we care only about imports from "@patternfly/react-core" | ||
code: `import { Button, Button } from "somewhere"`, | ||
}, | ||
{ | ||
code: `import { Button, Button as AnotherButton } from "@patternfly/react-core"; | ||
wise-king-sullyman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<> | ||
<Button>Sample button</Button> | ||
<AnotherButton>Another one</AnotherButton> | ||
</>`, | ||
}, | ||
{ | ||
code: `import { Select } from '@patternfly/react-core/deprecated'; | ||
import { Select } from '@patternfly/react-core';` | ||
}, | ||
{ | ||
code: `import { Select } from '@patternfly/react-core/deprecated'; | ||
import { Select } from '@patternfly/react-core/dist/dynamic/components/Select';` | ||
}, | ||
{ | ||
code: `import { Select } from '@patternfly/react-core/next'; | ||
import { Select } from '@patternfly/react-core';` | ||
}, | ||
{ | ||
code: `import { Select } from '@patternfly/react-core/next'; | ||
import { Select } from '@patternfly/react-core/dist/dynamic/components/Select';` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: `import { Button, Button } from "@patternfly/react-core"; | ||
<Button>Sample button</Button>`, | ||
output: `import { Button, } from "@patternfly/react-core"; | ||
<Button>Sample button</Button>`, | ||
errors: [ | ||
{ | ||
message: `Duplicate import specifier Button imported from '@patternfly/react-core'.`, | ||
type: "ImportSpecifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Button } from "@patternfly/react-core"; | ||
import { Button } from "@patternfly/react-core"; | ||
Comment on lines
+48
to
+49
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. Could we also add an invalid test where Component is imported from react-core, but also imported from react-core/dist/... |
||
<Button>Sample button</Button>`, | ||
output: `import { Button } from "@patternfly/react-core"; | ||
|
||
<Button>Sample button</Button>`, | ||
errors: [ | ||
{ | ||
message: `Duplicate import specifier Button imported from '@patternfly/react-core'.`, | ||
type: "ImportSpecifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Button } from "@patternfly/react-core"; | ||
import { Button } from '@patternfly/react-core/dist/dynamic/components/Button'; | ||
<Button>Sample button</Button>`, | ||
output: `import { Button } from "@patternfly/react-core"; | ||
|
||
<Button>Sample button</Button>`, | ||
errors: [ | ||
{ | ||
message: `Duplicate import specifier Button imported from '@patternfly/react-core/dist/dynamic/components/Button'.`, | ||
type: "ImportSpecifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `import { Button as BTN, TextInput, Button as BTN } from "@patternfly/react-core"; | ||
<> | ||
<BTN>Sample button</BTN> | ||
<TextInput>Text</TextInput> | ||
</>`, | ||
output: `import { Button as BTN, TextInput, } from "@patternfly/react-core"; | ||
<> | ||
<BTN>Sample button</BTN> | ||
<TextInput>Text</TextInput> | ||
</>`, | ||
errors: [ | ||
{ | ||
message: `Duplicate import specifier BTN imported from '@patternfly/react-core'.`, | ||
type: "ImportSpecifier", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
50 changes: 50 additions & 0 deletions
50
...in-pf-codemods/src/rules/v6/noDuplicateImportSpecifiers/no-duplicate-import-specifiers.ts
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,50 @@ | ||
import { Rule } from "eslint"; | ||
import { ImportDeclaration, ImportSpecifier } from "estree-jsx"; | ||
import { getFromPackage, removeSpecifierFromDeclaration } from "../../helpers"; | ||
|
||
// Cleanup from other rules | ||
module.exports = { | ||
meta: { fixable: "code" }, | ||
create: function (context: Rule.RuleContext) { | ||
const { imports } = getFromPackage(context, "@patternfly/react-core"); | ||
|
||
const findDuplicates = (specifiers: ImportSpecifier[]) => { | ||
const localNames = specifiers.map((spec) => spec.local.name); | ||
|
||
return specifiers.filter( | ||
(specifier, index) => localNames.indexOf(specifier.local.name) !== index | ||
); | ||
}; | ||
|
||
const duplicatesToRemove = findDuplicates(imports); | ||
|
||
return !duplicatesToRemove.length | ||
? {} | ||
: { | ||
ImportSpecifier(node: ImportSpecifier) { | ||
if (duplicatesToRemove.includes(node)) { | ||
const importDeclaration = context | ||
.getAncestors() | ||
.find( | ||
(ancestor) => ancestor.type === "ImportDeclaration" | ||
) as ImportDeclaration; | ||
|
||
if (importDeclaration) { | ||
context.report({ | ||
node, | ||
message: `Duplicate import specifier ${node.local.name} imported from '${importDeclaration.source.value}'.`, | ||
fix(fixer) { | ||
return removeSpecifierFromDeclaration( | ||
fixer, | ||
context, | ||
importDeclaration, | ||
node | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
...pf-codemods/src/rules/v6/noDuplicateImportSpecifiers/noDuplicateImportSpecifiersInput.tsx
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 @@ | ||
import { Button, Button } from "@patternfly/react-core"; | ||
|
||
export const NoDuplicateImportSpecifiersInput = () => ( | ||
<Button>Sample button</Button> | ||
); |
5 changes: 5 additions & 0 deletions
5
...f-codemods/src/rules/v6/noDuplicateImportSpecifiers/noDuplicateImportSpecifiersOutput.tsx
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 @@ | ||
import { Button } from "@patternfly/react-core"; | ||
|
||
export const NoDuplicateImportSpecifiersInput = () => ( | ||
<Button>Sample button</Button> | ||
); |
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.
And maybe a valid test where Component is imported from react-core, but also react-core/[deprecated|next]?