@foundbyte/eslint-plugin 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { Config, Rules } from './lib/rules'
2
+
3
+ declare const eslintPlugin: {
4
+ rules: Rules
5
+ configs: {
6
+ recommended: Config
7
+ }
8
+ }
9
+
10
+ export = eslintPlugin
package/index.js CHANGED
@@ -1,11 +1,15 @@
1
- const arrowFunctionPaddingRule = require('./lib/rules/arrow-function-padding');
2
- const vueScriptLinesLimitRule = require('./lib/rules/vue-script-lines-limit');
3
- const typeCommentRequirementRule = require('./lib/rules/type-comment-requirement');
1
+ const arrowFunctionPaddingRule = require('./lib/rules/arrow-function-padding')
2
+ const vueScriptLinesLimitRule = require('./lib/rules/vue-script-lines-limit')
3
+ const typeCommentRequirementRule = require('./lib/rules/type-comment-requirement')
4
+ const hooksExportLimitRule = require('./lib/rules/hooks-export-limit')
5
+ const hooksArrowFunctionOnlyRule = require('./lib/rules/hooks-arrow-function-only')
4
6
 
5
7
  module.exports = {
6
- rules: {
7
- 'arrow-function-padding': arrowFunctionPaddingRule,
8
- 'vue-script-lines-limit': vueScriptLinesLimitRule,
9
- 'type-comment-requirement': typeCommentRequirementRule,
10
- },
11
- };
8
+ rules: {
9
+ 'arrow-function-padding': arrowFunctionPaddingRule,
10
+ 'vue-script-lines-limit': vueScriptLinesLimitRule,
11
+ 'type-comment-requirement': typeCommentRequirementRule,
12
+ 'hooks-export-limit': hooksExportLimitRule,
13
+ 'hooks-arrow-function-only': hooksArrowFunctionOnlyRule,
14
+ },
15
+ }
@@ -0,0 +1,14 @@
1
+ import { Rule } from 'eslint'
2
+
3
+ /**
4
+ * 箭头函数间距规则配置
5
+ */
6
+ export interface ArrowFunctionPaddingOptions {
7
+ // 此规则没有配置选项
8
+ }
9
+
10
+ /**
11
+ * 箭头函数间距规则
12
+ */
13
+ declare const arrowFunctionPaddingRule: Rule.RuleModule
14
+ export default arrowFunctionPaddingRule
@@ -0,0 +1,14 @@
1
+ import { Rule } from 'eslint'
2
+
3
+ /**
4
+ * Hooks 箭头函数要求规则配置
5
+ */
6
+ export interface HooksArrowFunctionOnlyOptions {
7
+ // 此规则没有配置选项
8
+ }
9
+
10
+ /**
11
+ * Hooks 箭头函数要求规则
12
+ */
13
+ declare const hooksArrowFunctionOnlyRule: Rule.RuleModule
14
+ export default hooksArrowFunctionOnlyRule
@@ -0,0 +1,69 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: 'suggestion',
4
+ docs: {
5
+ description: '禁止 hooks 使用 function 声明,必须使用箭头函数',
6
+ category: 'Best Practices',
7
+ recommended: false,
8
+ },
9
+ fixable: 'code',
10
+ schema: [],
11
+ messages: {
12
+ useArrowFunction: 'hooks 必须使用箭头函数,不能使用 function 声明',
13
+ },
14
+ },
15
+
16
+ create(context) {
17
+ return {
18
+ // 检查函数声明
19
+ FunctionDeclaration(node) {
20
+ // 检查函数名是否以 use 开头(hooks 命名约定)
21
+ if (node.id && node.id.name && node.id.name.startsWith('use')) {
22
+ context.report({
23
+ node: node,
24
+ messageId: 'useArrowFunction',
25
+ fix(fixer) {
26
+ // 将 function 声明转换为箭头函数
27
+ const sourceCode = context.getSourceCode()
28
+ const functionName = node.id.name
29
+ const params = node.params.map(param => sourceCode.getText(param)).join(', ')
30
+ const body = sourceCode.getText(node.body)
31
+
32
+ // 构建箭头函数
33
+ const arrowFunction = `const ${functionName} = (${params}) => ${body}`
34
+
35
+ return fixer.replaceText(node, arrowFunction)
36
+ },
37
+ })
38
+ }
39
+ },
40
+
41
+ // 检查函数表达式(变量声明中的函数表达式)
42
+ VariableDeclarator(node) {
43
+ if (
44
+ node.init &&
45
+ node.init.type === 'FunctionExpression' &&
46
+ node.id &&
47
+ node.id.name &&
48
+ node.id.name.startsWith('use')
49
+ ) {
50
+ context.report({
51
+ node: node.init,
52
+ messageId: 'useArrowFunction',
53
+ fix(fixer) {
54
+ // 将函数表达式转换为箭头函数
55
+ const sourceCode = context.getSourceCode()
56
+ const params = node.init.params.map(param => sourceCode.getText(param)).join(', ')
57
+ const body = sourceCode.getText(node.init.body)
58
+
59
+ // 构建箭头函数
60
+ const arrowFunction = `(${params}) => ${body}`
61
+
62
+ return fixer.replaceText(node.init, arrowFunction)
63
+ },
64
+ })
65
+ }
66
+ },
67
+ }
68
+ },
69
+ }
@@ -0,0 +1,18 @@
1
+ import { Rule } from 'eslint'
2
+
3
+ /**
4
+ * Hooks 导出限制规则配置
5
+ */
6
+ export interface HooksExportLimitOptions {
7
+ /**
8
+ * 最大导出数量
9
+ * @default 20
10
+ */
11
+ maxExports?: number
12
+ }
13
+
14
+ /**
15
+ * Hooks 导出限制规则
16
+ */
17
+ declare const hooksExportLimitRule: Rule.RuleModule
18
+ export default hooksExportLimitRule
@@ -0,0 +1,76 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: 'problem',
4
+ docs: {
5
+ description: '限制 hooks 导出的变量和方法数量不超过20个',
6
+ category: 'Best Practices',
7
+ recommended: false,
8
+ },
9
+ fixable: null,
10
+ schema: [
11
+ {
12
+ type: 'object',
13
+ properties: {
14
+ maxExports: {
15
+ type: 'number',
16
+ minimum: 1,
17
+ },
18
+ },
19
+ additionalProperties: false,
20
+ },
21
+ ],
22
+ messages: {
23
+ exceedMaxExports: 'hooks 导出的变量和方法数量不能超过 {{maxExports}} 个,当前有 {{currentExports}} 个',
24
+ },
25
+ },
26
+
27
+ create(context) {
28
+ const options = context.options[0] || {}
29
+ const maxExports = options.maxExports || 20
30
+
31
+ // 只处理 .vue 文件
32
+ const filename = context.getFilename()
33
+ if (!filename.endsWith('.vue')) {
34
+ return {}
35
+ }
36
+
37
+ return {
38
+ Program() {
39
+ // 获取整个文件的源代码
40
+ const sourceCode = context.getSourceCode()
41
+ const text = sourceCode.getText()
42
+
43
+ // 使用正则表达式匹配所有 hooks 的解构赋值
44
+ const hooksPattern = /const\s*{\s*([^}]+)\s*}\s*=\s*use[A-Z][a-zA-Z]*\(\s*\)/g
45
+ let match
46
+
47
+ while ((match = hooksPattern.exec(text)) !== null) {
48
+ const exportsString = match[1]
49
+
50
+ // 计算导出的变量和方法数量
51
+ const exports = exportsString.split(',').map((item) => item.trim()).filter((item) => item)
52
+ const exportCount = exports.length
53
+
54
+ if (exportCount > maxExports) {
55
+ // 找到 hooks 解构赋值的位置
56
+ const hooksStartIndex = match.index
57
+ const hooksEndIndex = hooksStartIndex + match[0].length
58
+
59
+ // 在 hooks 解构赋值的位置报告错误
60
+ context.report({
61
+ loc: {
62
+ start: sourceCode.getLocFromIndex(hooksStartIndex),
63
+ end: sourceCode.getLocFromIndex(hooksEndIndex),
64
+ },
65
+ messageId: 'exceedMaxExports',
66
+ data: {
67
+ maxExports,
68
+ currentExports: exportCount,
69
+ },
70
+ })
71
+ }
72
+ }
73
+ },
74
+ }
75
+ },
76
+ }
@@ -0,0 +1,47 @@
1
+ export { ArrowFunctionPaddingOptions } from './arrow-function-padding'
2
+ export { HooksExportLimitOptions } from './hooks-export-limit'
3
+ export { TypeCommentRequirementOptions } from './type-comment-requirement'
4
+ export { VueScriptLinesLimitOptions } from './vue-script-lines-limit'
5
+ export { HooksArrowFunctionOnlyOptions } from './hooks-arrow-function-only'
6
+
7
+ import { Rule } from 'eslint'
8
+
9
+ /**
10
+ * @foundbyte/eslint-plugin 规则配置
11
+ */
12
+ export interface Rules {
13
+ /**
14
+ * 要求箭头函数声明之间有空行
15
+ */
16
+ 'arrow-function-padding': Rule.RuleModule
17
+
18
+ /**
19
+ * 限制 hooks 导出的变量和方法数量
20
+ */
21
+ 'hooks-export-limit': Rule.RuleModule
22
+
23
+ /**
24
+ * 要求 TypeScript interface 和 type 定义中字段必须有注释
25
+ */
26
+ 'type-comment-requirement': Rule.RuleModule
27
+
28
+ /**
29
+ * 限制 Vue 文件 script 标签内的行数
30
+ */
31
+ 'vue-script-lines-limit': Rule.RuleModule
32
+
33
+ /**
34
+ * 禁止 hooks (useXXX) 使用 function 声明,必须使用箭头函数
35
+ */
36
+ 'hooks-arrow-function-only': Rule.RuleModule
37
+ }
38
+
39
+ /**
40
+ * @foundbyte/eslint-plugin 配置
41
+ */
42
+ export interface Config {
43
+ rules: Rules
44
+ }
45
+
46
+ declare const config: Config
47
+ export default config
@@ -0,0 +1,33 @@
1
+ import { Rule } from 'eslint'
2
+
3
+ /**
4
+ * 类型注释要求规则配置
5
+ */
6
+ export interface TypeCommentRequirementOptions {
7
+ /**
8
+ * 每多少个字段为一组进行检查
9
+ * @default 5
10
+ */
11
+ groupSize?: number
12
+ /**
13
+ * 每组中至少有多少个字段必须加注释
14
+ * @default 1
15
+ */
16
+ minCommentsPerGroup?: number
17
+ /**
18
+ * 是否检查 type 定义
19
+ * @default true
20
+ */
21
+ includeTypes?: boolean
22
+ /**
23
+ * 是否检查 interface 定义
24
+ * @default true
25
+ */
26
+ includeInterfaces?: boolean
27
+ }
28
+
29
+ /**
30
+ * 类型注释要求规则
31
+ */
32
+ declare const typeCommentRequirementRule: Rule.RuleModule
33
+ export default typeCommentRequirementRule
@@ -0,0 +1,18 @@
1
+ import { Rule } from 'eslint'
2
+
3
+ /**
4
+ * Vue Script 行数限制规则配置
5
+ */
6
+ export interface VueScriptLinesLimitOptions {
7
+ /**
8
+ * 最大行数限制
9
+ * @default 300
10
+ */
11
+ maxLines?: number
12
+ }
13
+
14
+ /**
15
+ * Vue Script 行数限制规则
16
+ */
17
+ declare const vueScriptLinesLimitRule: Rule.RuleModule
18
+ export default vueScriptLinesLimitRule
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
1
  {
2
- "name": "@foundbyte/eslint-plugin",
3
- "version": "1.0.2",
4
- "description": "ESLint plugin for enforcing code style rules including arrow function padding, Vue script lines limit, and TypeScript type comment requirements",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "keywords": [
10
- "eslint",
11
- "eslintplugin",
12
- "arrow-function",
13
- "padding",
14
- "vue",
15
- "script",
16
- "lines-limit",
17
- "typescript",
18
- "type-comment",
19
- "comment-requirement"
20
- ],
21
- "author": "",
22
- "license": "ISC",
23
- "publishConfig": {
24
- "access": "public"
25
- },
26
- "peerDependencies": {
27
- "eslint": ">=8.0.0"
28
- }
29
- }
2
+ "name": "@foundbyte/eslint-plugin",
3
+ "version": "1.0.4",
4
+ "description": "ESLint plugin for enforcing code style rules including arrow function padding, Vue script lines limit, and TypeScript type comment requirements",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "keywords": [
8
+ "eslint",
9
+ "eslintplugin",
10
+ "arrow-function",
11
+ "padding",
12
+ "vue",
13
+ "script",
14
+ "lines-limit",
15
+ "typescript",
16
+ "type-comment",
17
+ "comment-requirement"
18
+ ],
19
+ "author": "",
20
+ "license": "ISC",
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "peerDependencies": {
25
+ "eslint": ">=8.0.0"
26
+ },
27
+ "scripts": {
28
+ "test": "echo \"Error: no test specified\" && exit 1"
29
+ }
30
+ }
package/types.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ // 导出所有规则配置类型
2
+ export type { ArrowFunctionPaddingOptions, HooksExportLimitOptions, TypeCommentRequirementOptions, VueScriptLinesLimitOptions, HooksArrowFunctionOnlyOptions } from './lib/rules'