@foundbyte/eslint-plugin 1.0.2 → 1.0.3

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.js CHANGED
@@ -1,11 +1,13 @@
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')
4
5
 
5
6
  module.exports = {
6
- rules: {
7
- 'arrow-function-padding': arrowFunctionPaddingRule,
8
- 'vue-script-lines-limit': vueScriptLinesLimitRule,
9
- 'type-comment-requirement': typeCommentRequirementRule,
10
- },
11
- };
7
+ rules: {
8
+ 'arrow-function-padding': arrowFunctionPaddingRule,
9
+ 'vue-script-lines-limit': vueScriptLinesLimitRule,
10
+ 'type-comment-requirement': typeCommentRequirementRule,
11
+ 'hooks-export-limit': hooksExportLimitRule,
12
+ },
13
+ }
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foundbyte/eslint-plugin",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "ESLint plugin for enforcing code style rules including arrow function padding, Vue script lines limit, and TypeScript type comment requirements",
5
5
  "main": "index.js",
6
6
  "scripts": {