Skip to content

Commit 9749457

Browse files
authored
Merge pull request #5 from sj-distributor/4-add-new-rule
Add new rule
2 parents 779b13a + 2b6f10e commit 9749457

File tree

6 files changed

+98
-34
lines changed

6 files changed

+98
-34
lines changed

README-ZH.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,16 @@
99

1010
## 安装
1111

12-
你首先需要安装 [ESLint](https://eslint.org/):
12+
你首先需要安装 [ESLint](https://eslint.org/) 和约定的第三方插件包:
1313

1414
```sh
15-
yarn add eslint
16-
17-
or
18-
19-
npm i eslint --save-dev
15+
yarn add eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier eslint-plugin-import eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-simple-import-sort eslint-plugin-unicorn prettier typescript eslint-plugin-eslint-plugin --dev
2016
```
2117

2218
接下来,安装 `@sj-distributor/eslint-plugin-react-native`:
2319

2420
```sh
25-
26-
yarn add @sj-distributor/eslint-plugin-react-native
27-
28-
or
29-
30-
npm install @sj-distributor/eslint-plugin-react-native --save-dev
21+
yarn add @sj-distributor/eslint-plugin-react-native --dev
3122
```
3223

3324
## 使用
@@ -36,7 +27,7 @@ Add `eslint-plugin-react-native` to the extends section of your `.eslintrc` conf
3627

3728
```json
3829
{
39-
"extends": ["plugin:@sj-distributor/eslint-plugin-react-native/recommended"]
30+
"extends": ["plugin:@sj-distributor/react-native/recommended"]
4031
}
4132
```
4233

@@ -52,3 +43,5 @@ Add `eslint-plugin-react-native` to the extends section of your `.eslintrc` conf
5243
- [eslint-plugin-react-hooks](https://www.npmjs.com/package/eslint-plugin-react-hooks)
5344
- [eslint-plugin-react-native](https://github.com/intellicode/eslint-plugin-react-native)
5445
- [eslint-plugin-simple-import-sort](https://github.com/lydell/eslint-plugin-simple-import-sort#readme)
46+
- [@typescript-eslint](https://typescript-eslint.io/)
47+
- [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn)

README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,16 @@ ESLint presets for react native
1111

1212
## Installation
1313

14-
You'll first need to install [ESLint](https://eslint.org/):
14+
You'll first need to install [ESLint](https://eslint.org/) and the agreed third-party plug-in package:
1515

1616
```sh
17-
yarn add eslint
18-
19-
or
20-
21-
npm i eslint --save-dev
17+
yarn add eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier eslint-plugin-import eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-simple-import-sort eslint-plugin-unicorn prettier typescript eslint-plugin-eslint-plugin --dev
2218
```
2319

2420
Next, install `@sj-distributor/eslint-plugin-react-native`:
2521

2622
```sh
27-
28-
yarn add @sj-distributor/eslint-plugin-react-native
29-
30-
or
31-
32-
npm install @sj-distributor/eslint-plugin-react-native --save-dev
23+
yarn add @sj-distributor/eslint-plugin-react-native --dev
3324
```
3425

3526
## Usage
@@ -38,7 +29,7 @@ Add `eslint-plugin-react-native` to the extends section of your `.eslintrc` conf
3829

3930
```json
4031
{
41-
"extends": ["plugin:@sj-distributor/eslint-plugin-react-native/recommended"]
32+
"extends": ["plugin:@sj-distributor/react-native/recommended"]
4233
}
4334
```
4435

@@ -54,3 +45,5 @@ Add `eslint-plugin-react-native` to the extends section of your `.eslintrc` conf
5445
- [eslint-plugin-react-hooks](https://www.npmjs.com/package/eslint-plugin-react-hooks)
5546
- [eslint-plugin-react-native](https://github.com/intellicode/eslint-plugin-react-native)
5647
- [eslint-plugin-simple-import-sort](https://github.com/lydell/eslint-plugin-simple-import-sort#readme)
48+
- [@typescript-eslint](https://typescript-eslint.io/)
49+
- [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn)

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+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sj-distributor/eslint-plugin-react-native",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "ESLint presets for react native",
55
"keywords": [
66
"eslint",
@@ -11,7 +11,7 @@
1111
"exports": "./lib/index.js",
1212
"scripts": {
1313
"lint": "eslint .",
14-
"release": "release-it",
14+
"release": "release-it --no-increment",
1515
"test": "mocha tests --recursive"
1616
},
1717
"dependencies": {

0 commit comments

Comments
 (0)