-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add no-unused-imports * fix bug * add docs
- Loading branch information
Showing
7 changed files
with
72 additions
and
174 deletions.
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
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
34 changes: 34 additions & 0 deletions
34
packages/eslint-plugin-pf-codemods/lib/rules/no-unused-imports.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,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; | ||
} | ||
}) | ||
); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
85 changes: 0 additions & 85 deletions
85
packages/eslint-plugin-pf-codemods/lib/rules/remove-unused-imports.js
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
packages/eslint-plugin-pf-codemods/test/rules/no-unused-imports.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,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", | ||
}] | ||
}, | ||
] | ||
}); |
86 changes: 0 additions & 86 deletions
86
packages/eslint-plugin-pf-codemods/test/rules/remove-unused-imports.js
This file was deleted.
Oops, something went wrong.
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