Skip to content

Commit d048cc6

Browse files
Add local eslint rule to enforce naming styles
1 parent 1fefa22 commit d048cc6

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"parserOptions": {
88
"sourceType": "module"
99
},
10-
"plugins": ["@typescript-eslint", "import"],
10+
"plugins": ["@typescript-eslint", "import", "local-rules"],
1111
"rules": {
1212
"@typescript-eslint/adjacent-overload-signatures": "error",
1313
"@typescript-eslint/array-type": [
@@ -73,6 +73,7 @@
7373
"import/no-internal-modules": "off",
7474
"import/order": "error",
7575
"linebreak-style": "off",
76+
"local-rules/named-styles": "warn",
7677
"max-classes-per-file": "off",
7778
"max-len": "off",
7879
"new-parens": "off",

eslint-local-rules.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
module.exports = {
4+
"named-styles": require("./lint/rules/named-styles")
5+
};

lint/rules/named-styles.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
create: context => ({
3+
CallExpression: (codePath, node) => {
4+
if (
5+
["makeStyles", "withStyles"].includes(codePath.callee.name) &&
6+
codePath.arguments.length < 2
7+
) {
8+
context.report({
9+
loc: codePath.callee.loc,
10+
messageId:
11+
codePath.callee.name === "makeStyles"
12+
? "expectedNameHook"
13+
: "expectedNameHoc",
14+
node
15+
});
16+
}
17+
}
18+
}),
19+
meta: {
20+
messages: {
21+
expectedNameHoc: 'withStyles hook should have "name" property.',
22+
expectedNameHook: 'makeStyles hook should have "name" property.'
23+
},
24+
type: "problem"
25+
}
26+
};

lint/rules/named-styles.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const { RuleTester } = require("eslint");
2+
3+
const namedStylesRule = require("./named-styles");
4+
5+
const okCode =
6+
'const useStyles = makeStyles( \
7+
{ \
8+
root: { \
9+
alignItems: "center", \
10+
display: "flex", \
11+
height: "100vh", \
12+
justifyContent: "center" \
13+
} \
14+
}, \
15+
{ name: "LoginLoading" } \
16+
);';
17+
18+
const badCode = `const useStyles = makeStyles(theme => ({
19+
fileField: {
20+
display: "none"
21+
},
22+
image: {
23+
height: "100%",
24+
objectFit: "contain",
25+
userSelect: "none",
26+
width: "100%"
27+
},
28+
imageContainer: {
29+
background: "#ffffff",
30+
border: "1px solid #eaeaea",
31+
borderRadius: theme.spacing(),
32+
height: 148,
33+
justifySelf: "start",
34+
overflow: "hidden",
35+
padding: theme.spacing(2),
36+
position: "relative",
37+
width: 148
38+
}
39+
}));`;
40+
41+
const ruleTester = new RuleTester({
42+
parser: require.resolve("@typescript-eslint/parser")
43+
});
44+
45+
ruleTester.run("named-styles", namedStylesRule, {
46+
valid: [
47+
{
48+
code: okCode
49+
}
50+
],
51+
52+
invalid: [
53+
{
54+
code: badCode,
55+
errors: [{ message: "makeStyles hook should be named." }]
56+
}
57+
]
58+
});

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
"eslint": "^6.7.1",
121121
"eslint-loader": "^3.0.2",
122122
"eslint-plugin-import": "^2.18.2",
123+
"eslint-plugin-local-rules": "^0.1.1",
123124
"eslint-plugin-prefer-arrow": "^1.1.6",
124125
"file-loader": "^1.1.11",
125126
"fork-ts-checker-webpack-plugin": "^3.1.1",

0 commit comments

Comments
 (0)