Skip to content

Commit 2717293

Browse files
committed
feat: Add new rule
1 parent 7c3614a commit 2717293

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

configs/.eslintrc.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ module.exports = {
44
"eslint:recommended",
55
"plugin:prettier/recommended",
66
"plugin:react-hooks/recommended",
7+
"plugin:eslint-plugin/recommended",
78
"plugin:@typescript-eslint/recommended",
89
],
910
parser: "@typescript-eslint/parser",
10-
plugins: ["react", "react-native", "simple-import-sort", "unicorn", "import"],
11+
plugins: [
12+
"react",
13+
"react-native",
14+
"simple-import-sort",
15+
"unicorn",
16+
"import",
17+
"@sj-distributor/react-native",
18+
],
1119
env: {
20+
node: true,
1221
es2022: true,
1322
browser: true,
1423
"react-native/react-native": true,
@@ -34,10 +43,10 @@ module.exports = {
3443
"error",
3544
{
3645
cases: {
37-
kebabCase: true, // 是否支持横杠 (-) 命名
38-
camelCase: false, // 是否支持小驼峰命名
39-
snakeCase: false, // 是否支持 (_) 下划线命名
40-
pascalCase: false, // 是否支持大坨峰命名
46+
kebabCase: true, // 支持横杠 (-) 命名
47+
camelCase: false, // 支持小驼峰命名
48+
snakeCase: false, // 支持 (_) 下划线命名
49+
pascalCase: false, // 支持大坨峰命名
4150
},
4251
},
4352
],
@@ -54,6 +63,10 @@ module.exports = {
5463
next: "*",
5564
},
5665
],
66+
"@typescript-eslint/no-var-requires": 0, // (关闭) 禁止使用 require 语句
67+
"@sj-distributor/react-native/interface-name-prefix": ["error", "I"], // 默认强制 interface 大写 I 前缀
68+
"react/display-name": 0, // (关闭) 不允许在 React 组件定义中缺少 displayName
69+
"react/self-closing-comp": 2, // 检测 JSX 中的所有组件和 HTML 元素,如果元素没有子元素,就会自动转换为自闭合形式
5770
},
5871
// 共享配置,提供给每一个将被执行的规则
5972
settings: {

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// Requirements
88
//------------------------------------------------------------------------------
99

10+
const path = require("path");
1011
const requireIndex = require("requireindex");
1112
const eslintrc = require("../configs/.eslintrc");
1213

@@ -15,7 +16,7 @@ const eslintrc = require("../configs/.eslintrc");
1516
//------------------------------------------------------------------------------
1617
module.exports = {
1718
// 引入所有的自定义的规则
18-
rules: requireIndex(__dirname + "/rules"),
19+
rules: requireIndex(path.join(__dirname + "/rules")),
1920
configs: {
2021
recommended: eslintrc,
2122
},

lib/rules/interface-name-prefix.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @fileoverview Require interface names to begin with a specified prefix
3+
*/
4+
"use strict";
5+
6+
//------------------------------------------------------------------------------
7+
// Rule Definition
8+
//------------------------------------------------------------------------------
9+
10+
/** @type {import('eslint').Rule.RuleModule} */
11+
module.exports = {
12+
meta: {
13+
type: "suggestion",
14+
docs: {
15+
category: "Naming",
16+
recommended: false,
17+
description: "Require interface names to begin with a specified prefix",
18+
},
19+
fixable: "code",
20+
schema: [
21+
{
22+
type: "string",
23+
},
24+
],
25+
messages: {
26+
missingPrefix:
27+
"Interface name '{{ name }}' should start with '{{ prefix }}'",
28+
},
29+
},
30+
31+
create(context) {
32+
const [prefix = "I"] = context.options;
33+
34+
function checkPrefix(node) {
35+
const name = node.id.name;
36+
37+
if (
38+
typeof name !== "string" ||
39+
name.startsWith(prefix) ||
40+
node.parent.type === "TSModuleDeclaration"
41+
) {
42+
return;
43+
}
44+
45+
context.report({
46+
node,
47+
messageId: "missingPrefix",
48+
data: {
49+
name,
50+
prefix,
51+
},
52+
fix: (fixer) => {
53+
const newName = `${prefix}${name}`;
54+
55+
return fixer.replaceText(node.id, newName);
56+
},
57+
});
58+
}
59+
60+
return {
61+
TSInterfaceDeclaration: checkPrefix,
62+
};
63+
},
64+
};

0 commit comments

Comments
 (0)