Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 3f5c55b

Browse files
committed
feat: Add interface-name-prefix rule
1 parent 0e8377c commit 3f5c55b

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

configs/.eslintrc.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ 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", "simple-import-sort", "unicorn", "import"],
11+
plugins: [
12+
"react",
13+
"simple-import-sort",
14+
"unicorn",
15+
"import",
16+
"@sj-distributor/react",
17+
],
1118
env: {
19+
node: true,
1220
es2022: true,
1321
browser: true,
14-
node: true,
1522
},
1623
parserOptions: {
1724
sourceType: "module",
@@ -54,6 +61,8 @@ module.exports = {
5461
next: "*",
5562
},
5663
],
64+
"@typescript-eslint/no-var-requires": 0, // 关闭禁止使用 require 语句
65+
"@sj-distributor/react/interface-name-prefix": ["error", "I"], // 默认强制 interface 大写 I 前缀
5766
},
5867
// 共享配置,提供给每一个将被执行的规则
5968
settings: {

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview ESLint presets for react native
2+
* @fileoverview ESLint presets for react
33
*/
44
"use strict";
55

@@ -8,14 +8,15 @@
88
//------------------------------------------------------------------------------
99

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

1314
//------------------------------------------------------------------------------
1415
// Plugin Definition
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)