Skip to content

Commit

Permalink
add no-unused-imports (#90)
Browse files Browse the repository at this point in the history
* add no-unused-imports

* fix bug

* add docs
  • Loading branch information
redallen authored Jun 4, 2020
1 parent 5b7d787 commit d4490ab
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 174 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ jobs:
- run:
name: Test rules
command: yarn test
- run:
name: Test single file
command: yarn test:single
when: always

deploy:
docker:
- image: circleci/node:12
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-pf-codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const rules = {
"nav-list-variant": require('./lib/rules/nav-list-variant'),
"table-removed-transforms": require('./lib/rules/table-removed-transforms'),
"select-rename-checkbox": require('./lib/rules/select-rename-checkbox'),
"remove-unused-imports": require('./lib/rules/remove-unused-imports'),
"no-unused-imports": require('./lib/rules/no-unused-imports'),
"tab-rename-variant": require('./lib/rules/tab-rename-variant'),
"form-fix-isValid": require('./lib/rules/form-fix-isValid'),
"expandable-rename-expandablesection": require('./lib/rules/expandable-rename-expandablesection'),
Expand Down
34 changes: 34 additions & 0 deletions packages/eslint-plugin-pf-codemods/lib/rules/no-unused-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Cleanup from other rules
module.exports = {
create: function(context) {
const allTokens = context.getSourceCode().ast.body
.filter(node => node.type !== 'ImportDeclaration')
.map(node => context.getSourceCode().getTokens(node).map(token => token.value))
.reduce((acc, val) => acc.concat(val), []);

return {
ImportDeclaration(node) {
// We should visit all nodes in the AST like eslint/lib/rules/no-unused-vars
// and add typescript nodes like @typescript-eslint/eslint-plugin/lib/rules/no-unused-vars
// ... but let's cheat and string compare to all tokens in the source file instead.
if (/@patternfly\/react/.test(node.source.value)) {
node.specifiers
.filter(spec => !allTokens.includes(spec.local.name))
.forEach(spec => context.report({
node,
message: `unused patternfly import ${spec.local.name}`,
fix(fixer) {
const fixes = [fixer.remove(spec)];
const prevToken = context.getSourceCode().getTokenBefore(spec);
if (prevToken && prevToken.value === ',') {
fixes.push(fixer.remove(prevToken));
}
return fixes;
}
})
);
}
}
};
}
};

This file was deleted.

20 changes: 20 additions & 0 deletions packages/eslint-plugin-pf-codemods/test/rules/no-unused-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const ruleTester = require('../ruletester');
const rule = require('../../lib/rules/no-unused-imports');

ruleTester.run("no-unused-imports", rule, {
valid: [
{
code: `import { Nav, NavVariants } from '@patternfly/react-core'; <Nav variant={NavVariants.default} />`,
}
],
invalid: [
{
code: `import { Nav, NavVariants } from '@patternfly/react-core'; <Nav variant="default" />`,
output: `import { Nav } from '@patternfly/react-core'; <Nav variant="default" />`,
errors: [{
message: `unused patternfly import NavVariants`,
type: "ImportDeclaration",
}]
},
]
});

This file was deleted.

14 changes: 12 additions & 2 deletions packages/pf-codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,25 @@ Out:
```jsx
```
### remove-unused-imports
This rule cleans up the following removed imports:
### no-unused-imports
This rule is meant to be run after all other rules to remove unneeded imports added or no longer necessary such as:
- 'NavVariants', Use the variant prop on the Nav component with one of these values: 'default' | 'horizontal' | 'tertiary'
- 'CardHead', See card-rename-components rule for more info
- 'CardHeadMain', See card-rename-components rule for more info
- 'BackgroundImgSrc', See background-image-src-enum rule for more info
- 'ChipButton', See chipgroup-remove-chipbutton rule for more info
- 'ChipGroupToolbarItem' See chipgroup-remove-chipgrouptoolbaritem rule for more info
#### Examples
In:
```jsx
import { Nav, NavVariants } from '@patternfly/react-core'; <Nav variant="default" />
```
Out:
```jsx
import { Nav } from '@patternfly/react-core'; <Nav variant="default" />
```
### rename-noPadding [(#4133)](https://github.com/patternfly/patternfly-react/pull/4133)
We've renamed `noPadding` to `hasNoPadding` for DataListContent, DrawerHead, DrawerPanelBody, and PageSection components.
Expand Down

0 comments on commit d4490ab

Please sign in to comment.