Skip to content

Commit 2ec3ff6

Browse files
committed
style(import-default-name): Added import-default-name eslint rule
Added a rule to force the naming of the default import specified packages. This will fix inconsistencies with variable names.
1 parent 0bb3717 commit 2ec3ff6

24 files changed

+100
-43
lines changed

.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"jsx-a11y/click-events-have-key-events": "off",
77
"jsx-a11y/no-static-element-interactions": "off",
88
"jsx-a11y/no-noninteractive-element-interactions": "off",
9+
"import-default-name": [
10+
"error",
11+
{
12+
"classnames": "classNames",
13+
"prop-types": "PropTypes"
14+
}
15+
],
916
"import/no-extraneous-dependencies": [
1017
"error",
1118
{

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"editor.formatOnSave": true,
33
"prettier.eslintIntegration": true,
44
"prettier.singleQuote": true,
5-
"prettier.trailingComma": "none"
5+
"prettier.trailingComma": "none",
6+
"eslint.options": {
7+
"rulePaths": ["./lint-rules"]
8+
}
69
}

lint-rules/import-default-name.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module.exports = {
2+
meta: {
3+
docs: {
4+
description: 'Force default import names to match specified values',
5+
category: 'Possible Errors',
6+
recommended: true
7+
},
8+
fixable: 'code',
9+
schema: [
10+
{
11+
type: 'object',
12+
properties: {},
13+
additionalProperties: true
14+
}
15+
]
16+
},
17+
create(context) {
18+
const [importMap = {}] = context.options;
19+
return {
20+
ImportDeclaration(node) {
21+
const defaultImport = node.specifiers.find(
22+
spec => spec.type === 'ImportDefaultSpecifier'
23+
);
24+
if (!defaultImport) {
25+
return;
26+
}
27+
const expectedName = importMap[node.source.value];
28+
const receivedName = defaultImport.local.name;
29+
if (expectedName && expectedName !== receivedName) {
30+
context.report({
31+
node,
32+
message:
33+
'Expected default import to be named "{{ expected }}" but received "{{ received }}"',
34+
data: {
35+
expected: expectedName,
36+
received: receivedName
37+
},
38+
fix(fixer) {
39+
const [varDecl] = context.getDeclaredVariables(node);
40+
return [
41+
...varDecl.references.map(ref =>
42+
fixer.replaceText(ref.identifier, expectedName)
43+
),
44+
fixer.replaceText(defaultImport, expectedName)
45+
];
46+
}
47+
});
48+
}
49+
}
50+
};
51+
}
52+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"build:scripts": "babel src --out-dir dist/js --ignore .test.js,__mocks__",
111111
"build:less": "mkdir -p dist/less && cp -r less/* dist/less",
112112
"build:sass": "mkdir -p dist/sass && cp -r sass/patternfly-react/* dist/sass && node-sass --output-style compressed --include-path sass $npm_package_sassIncludes_patternfly $npm_package_sassIncludes_bootstrap $npm_package_sassIncludes_fontAwesome -o dist/css sass/patternfly-react.scss",
113-
"lint": "eslint --fix --max-warnings 0 src storybook && npm run stylelint",
113+
"lint": "eslint --rulesdir lint-rules/ --fix --max-warnings 0 src storybook && npm run stylelint",
114114
"prettier": "prettier --write --single-quote --trailing-comma=none '{src,storybook}/**/*.js'",
115115
"prepare": "npm run build",
116116
"test": "npm run lint && jest",

sass/patternfly-react/_patternfly-react.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Patternfly React Partials
33
*/
44
@import 'card';
5-
65
@import 'utilization-bar';
76
@import 'breadcrumb';
87
@import 'label-remove';

src/components/Cards/AggregateStatusCard/AggregateStatusCard.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import {
88
} from './index';
99

1010
test('Aggregate Status Card Count is working properly', () => {
11-
const component = mount(
12-
<AggregateStatusCount> 9 </AggregateStatusCount>
13-
);
11+
const component = mount(<AggregateStatusCount> 9 </AggregateStatusCount>);
1412

1513
expect(component.render()).toMatchSnapshot();
1614
});

src/components/Cards/AggregateStatusCard/AggregateStatusCount.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import ClassNames from 'classnames';
1+
import classNames from 'classnames';
22
import React from 'react';
33
import PropTypes from 'prop-types';
44

55
const AggregateStatusCount = ({ children, className, ...props }) => {
6-
const classes = ClassNames('card-pf-aggregate-status-count', className);
6+
const classes = classNames('card-pf-aggregate-status-count', className);
77

88
return (
99
<span className={classes} {...props}>

src/components/Cards/AggregateStatusCard/AggregateStatusNotification.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import ClassNames from 'classnames';
1+
import classNames from 'classnames';
22
import React from 'react';
33
import PropTypes from 'prop-types';
44

55
const AggregateStatusNotification = ({ children, className, ...props }) => {
6-
const classes = ClassNames(
6+
const classes = classNames(
77
'card-pf-aggregate-status-notification',
88
className
99
);

src/components/Cards/AggregateStatusCard/AggregateStatusNotifications.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import ClassNames from 'classnames';
1+
import classNames from 'classnames';
22
import React from 'react';
33
import PropTypes from 'prop-types';
44

55
const AggregateStatusNotifications = ({ children, className, ...props }) => {
6-
const classes = ClassNames(
6+
const classes = classNames(
77
'card-pf-aggregate-status-notifications',
88
className
99
);

src/components/Cards/Card.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ClassNames from 'classnames';
1+
import classNames from 'classnames';
22
import React from 'react';
33
import PropTypes from 'prop-types';
44

@@ -12,7 +12,7 @@ const Card = ({
1212
cardRef,
1313
...props
1414
}) => {
15-
const classes = ClassNames(
15+
const classes = classNames(
1616
'card-pf',
1717
{ 'card-pf-accented': accented },
1818
{ 'card-pf-aggregate-status': aggregated },

0 commit comments

Comments
 (0)