-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 1 commit
0ade52f
867ade6
b41cf31
5e1f9d9
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,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% | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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> | ||
</>`, | ||
}, | ||
], | ||
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 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", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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 createKey = (specifier: ImportSpecifier) => | ||
`${specifier.imported.name}:${specifier.local.name}`; | ||
|
||
const findDuplicates = (specifiers: ImportSpecifier[]) => { | ||
const occurrences = new Map<string, number>(); | ||
|
||
// Build a map to count occurrences of each similar import | ||
specifiers.forEach((specifier) => { | ||
const key = createKey(specifier); | ||
if (occurrences.has(key)) { | ||
occurrences.set(key, occurrences.get(key)! + 1); | ||
} else { | ||
occurrences.set(key, 1); | ||
} | ||
}); | ||
|
||
const seen = new Set<string>(); | ||
const duplicates: ImportSpecifier[] = []; | ||
|
||
specifiers.forEach((specifier) => { | ||
const key = createKey(specifier); | ||
|
||
if (occurrences.get(key) === 1) { | ||
return; | ||
} | ||
|
||
if (seen.has(key)) { | ||
duplicates.push(specifier); | ||
} else { | ||
seen.add(key); | ||
} | ||
}); | ||
|
||
return duplicates; | ||
}; | ||
wise-king-sullyman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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> | ||
); |
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> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
import { | ||
Content, | ||
Content, | ||
Content, | ||
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. Should this unrelated rules output be updated? Or are we keeping the *Output.tsx files as the output of running just the associated rule on the *Input.tsx file? 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. That's a good question. I maybe lean a bit more towards having it as the output of running all rules on the file. But I don't know how we approached this in other rules. What is your opinion on this? Also tagging @thatblindgeye 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. I lean more towards having it the output of just the associated rule since these files get pulled into the readme as depictions of what each rule will do, and that's at least what I've been doing and what I thought yall were doing. Something that's been talked about in the past is the idea of compiling all of input/output files together into master input/output files to ensure that rules play nice together, would you feel better about the "just the associated rule" approach if we rolled out something like that? 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. I had been going with having each individual Output file only apply fixes for that specific rule. A "master" file of all in/outputs may not be bad, assuming it's more just for our own eyes (and not something we publish in any README or anything). That's basically what we used to do for v5 was the single file where we added everything to ensure all the rules that ran didn't conflict with one another (too badly). 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. Yeah in my head at least it would just be for us to look at, and maybe to build a new CI check around 🤔 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. A CI check would be less tedious for us to have to constantly check that master file and maybe notice any issues 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. I reverted the file so it is not updated by the no-duplicate-imports rule. We may have the master file + CI check, I don't find it super necessary, but if there will be time for that, why not, created this issue for it. |
||
Content, | ||
ContentProps, | ||
ContentVariants, | ||
|
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]?