Skip to content

Commit

Permalink
feat: Add new rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Simoon-F committed Oct 6, 2023
1 parent 7c3614a commit 2717293
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
23 changes: 18 additions & 5 deletions configs/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ module.exports = {
"eslint:recommended",
"plugin:prettier/recommended",
"plugin:react-hooks/recommended",
"plugin:eslint-plugin/recommended",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
plugins: ["react", "react-native", "simple-import-sort", "unicorn", "import"],
plugins: [
"react",
"react-native",
"simple-import-sort",
"unicorn",
"import",
"@sj-distributor/react-native",
],
env: {
node: true,
es2022: true,
browser: true,
"react-native/react-native": true,
Expand All @@ -34,10 +43,10 @@ module.exports = {
"error",
{
cases: {
kebabCase: true, // 是否支持横杠 (-) 命名
camelCase: false, // 是否支持小驼峰命名
snakeCase: false, // 是否支持 (_) 下划线命名
pascalCase: false, // 是否支持大坨峰命名
kebabCase: true, // 支持横杠 (-) 命名
camelCase: false, // 支持小驼峰命名
snakeCase: false, // 支持 (_) 下划线命名
pascalCase: false, // 支持大坨峰命名
},
},
],
Expand All @@ -54,6 +63,10 @@ module.exports = {
next: "*",
},
],
"@typescript-eslint/no-var-requires": 0, // (关闭) 禁止使用 require 语句
"@sj-distributor/react-native/interface-name-prefix": ["error", "I"], // 默认强制 interface 大写 I 前缀
"react/display-name": 0, // (关闭) 不允许在 React 组件定义中缺少 displayName
"react/self-closing-comp": 2, // 检测 JSX 中的所有组件和 HTML 元素,如果元素没有子元素,就会自动转换为自闭合形式
},
// 共享配置,提供给每一个将被执行的规则
settings: {
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Requirements
//------------------------------------------------------------------------------

const path = require("path");
const requireIndex = require("requireindex");
const eslintrc = require("../configs/.eslintrc");

Expand All @@ -15,7 +16,7 @@ const eslintrc = require("../configs/.eslintrc");
//------------------------------------------------------------------------------
module.exports = {
// 引入所有的自定义的规则
rules: requireIndex(__dirname + "/rules"),
rules: requireIndex(path.join(__dirname + "/rules")),
configs: {
recommended: eslintrc,
},
Expand Down
64 changes: 64 additions & 0 deletions lib/rules/interface-name-prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @fileoverview Require interface names to begin with a specified prefix
*/
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
category: "Naming",
recommended: false,
description: "Require interface names to begin with a specified prefix",
},
fixable: "code",
schema: [
{
type: "string",
},
],
messages: {
missingPrefix:
"Interface name '{{ name }}' should start with '{{ prefix }}'",
},
},

create(context) {
const [prefix = "I"] = context.options;

function checkPrefix(node) {
const name = node.id.name;

if (
typeof name !== "string" ||
name.startsWith(prefix) ||
node.parent.type === "TSModuleDeclaration"
) {
return;
}

context.report({
node,
messageId: "missingPrefix",
data: {
name,
prefix,
},
fix: (fixer) => {
const newName = `${prefix}${name}`;

return fixer.replaceText(node.id, newName);
},
});
}

return {
TSInterfaceDeclaration: checkPrefix,
};
},
};

0 comments on commit 2717293

Please sign in to comment.