Skip to content

Commit 7a8ff1c

Browse files
authored
added react-icons-remove-icon rules & tests (#106)
* added rules & tests * added check for imports.length before taking action on fort awesome import statement
1 parent c213a26 commit 7a8ff1c

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

packages/eslint-plugin-pf-codemods/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const rules = {
4343
"datatoolbar-rename-toolbar": require('./lib/rules/datatoolbar-rename-toolbar'),
4444
"chartVoronoiContainer-remove-allowTooltip": require('./lib/rules/chartVoronoiContainer-remove-allowTooltip'),
4545
"chart-remove-allowZoom": require('./lib/rules/chartVoronoiContainer-remove-allowTooltip'),
46+
"react-icons-remove-icon": require('./lib/rules/react-icons-remove-icon'),
4647
};
4748

4849
module.exports = {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const { getPackageImports } = require('../helpers');
2+
3+
// https://github.com/patternfly/patternfly-react/pull/YOURNUMBERHERE
4+
module.exports = {
5+
create: function(context) {
6+
const imports = getPackageImports(context, '@patternfly/react-icons')
7+
.filter(specifier => specifier.imported.name === 'OutlinedFontAwesomeLogoFullIcon');
8+
9+
const fortAwesomeImports = getPackageImports(context, '@fortawesome/free-regular-svg-icons');
10+
const importStatement = `import { fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`
11+
if (imports.length) {
12+
if (fortAwesomeImports.length === 0) {
13+
const lastImportNode = context.getSourceCode().ast.body
14+
.filter(node => node.type === 'ImportDeclaration')
15+
.pop();
16+
context.report({
17+
node: lastImportNode,
18+
message: `add missing ${importStatement}`,
19+
fix(fixer) {
20+
return fixer.insertTextAfter(lastImportNode, '\n' + importStatement)
21+
}
22+
});
23+
} else {
24+
const hasFontAwesomeLogoFullImport = fortAwesomeImports.find(imp => imp.imported.name === 'fontAwesomeLogoFull');
25+
if (!hasFontAwesomeLogoFullImport) {
26+
const lastImportedIcon = fortAwesomeImports[fortAwesomeImports.length - 1];
27+
context.report({
28+
node: lastImportedIcon.parent,
29+
message: `add missing ${importStatement}`,
30+
fix(fixer) {
31+
return fixer.insertTextAfter(lastImportedIcon, ', fontAwesomeLogoFull');
32+
}
33+
});
34+
}
35+
}
36+
}
37+
38+
return imports.length == 0 ? {} : {
39+
ImportSpecifier(node) {
40+
if (imports.map(imp => imp.local.name).includes(node.local.name)) {
41+
42+
const sourceCode = context.getSourceCode();
43+
const previousToken = sourceCode.getTokenBefore(node);
44+
const previousTokenText = sourceCode.getText(previousToken);
45+
const lastNodeToken = sourceCode.getLastToken(node);
46+
const nextToken = sourceCode.getTokenAfter(lastNodeToken);
47+
const nextTokenText = sourceCode.getText(nextToken);
48+
context.report({
49+
node,
50+
message: `Removed OutlinedFontAwesomeLogoFullIcon. Import it from @FortAwesome instead.`,
51+
fix(fixer) {
52+
const fixes = [
53+
fixer.remove(node)
54+
];
55+
if (nextTokenText === '}' && previousTokenText === ',') {
56+
// it's the last item but has a preceding item, clean up previous comma
57+
fixes.push(fixer.remove(previousToken));
58+
}
59+
if (nextTokenText === ',') {
60+
fixes.push(fixer.remove(nextToken));
61+
}
62+
return fixes;
63+
}
64+
})
65+
}
66+
}
67+
};
68+
}
69+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const ruleTester = require('../ruletester');
2+
const rule = require('../../lib/rules/react-icons-remove-icon');
3+
4+
ruleTester.run("react-icons-remove-icon", rule, {
5+
valid: [
6+
{
7+
code: `import { fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`,
8+
}
9+
],
10+
invalid: [
11+
{
12+
code: `import { OutlinedFontAwesomeLogoFullIcon } from '@patternfly/react-icons';`,
13+
output: `import { } from '@patternfly/react-icons';
14+
import { fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`,
15+
errors: [
16+
{
17+
message: `add missing import { fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`,
18+
type: "ImportDeclaration",
19+
},
20+
{
21+
message: `Removed OutlinedFontAwesomeLogoFullIcon. Import it from @FortAwesome instead.`,
22+
type: "ImportSpecifier",
23+
}
24+
]
25+
},
26+
{
27+
code: `import { OutlinedFontAwesomeLogoFullIcon } from '@patternfly/react-icons';
28+
import { fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`,
29+
output: `import { } from '@patternfly/react-icons';
30+
import { fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`,
31+
errors: [{
32+
message: `Removed OutlinedFontAwesomeLogoFullIcon. Import it from @FortAwesome instead.`,
33+
type: "ImportSpecifier",
34+
}]
35+
},
36+
{
37+
code: `import { OutlinedFontAwesomeLogoFullIcon } from '@patternfly/react-icons';
38+
import { anotherIcon } from '@fortawesome/free-regular-svg-icons';`,
39+
output: `import { } from '@patternfly/react-icons';
40+
import { anotherIcon, fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`,
41+
errors: [
42+
{
43+
message: `Removed OutlinedFontAwesomeLogoFullIcon. Import it from @FortAwesome instead.`,
44+
type: "ImportSpecifier",
45+
},
46+
{
47+
message: `add missing import { fontAwesomeLogoFull } from '@fortawesome/free-regular-svg-icons';`,
48+
type: "ImportDeclaration",
49+
}
50+
]
51+
},
52+
]
53+
});

0 commit comments

Comments
 (0)