@foundbyte/eslint-plugin 1.1.4-alpha.4 → 1.1.4-alpha.6
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/README.md +97 -0
- package/index.js +2 -0
- package/lib/rules/index.d.ts +6 -0
- package/lib/rules/no-disabled-comments.d.ts +18 -0
- package/lib/rules/no-disabled-comments.js +127 -0
- package/lib/rules/tag-attribute-restriction.js +1 -1
- package/package.json +1 -1
- package/types.d.ts +1 -1
package/README.md
CHANGED
|
@@ -48,6 +48,14 @@ module.exports = {
|
|
|
48
48
|
]
|
|
49
49
|
}],
|
|
50
50
|
'@foundbyte/no-excessive-any': ['error', { maxAnyCount: 5 }],
|
|
51
|
+
'@foundbyte/no-disabled-comments': ['error', {
|
|
52
|
+
patterns: [
|
|
53
|
+
'/^\\s*eslint-disable-next-line\\s*$/',
|
|
54
|
+
'/^\\s*eslint-disable\\s*$/',
|
|
55
|
+
'/no-explicit-any/',
|
|
56
|
+
'/vue-script-lines-limit/',
|
|
57
|
+
'/hooks-export-limit/'
|
|
58
|
+
]}]
|
|
51
59
|
},
|
|
52
60
|
}
|
|
53
61
|
```
|
|
@@ -341,6 +349,95 @@ export default {
|
|
|
341
349
|
</script>
|
|
342
350
|
```
|
|
343
351
|
|
|
352
|
+
### no-disabled-comments
|
|
353
|
+
|
|
354
|
+
禁止使用特定的 ESLint 禁用注释,如 `eslint-disable`、`eslint-disable-next-line` 等。支持 JavaScript/TypeScript 注释和 Vue 模板中的 HTML 注释。
|
|
355
|
+
|
|
356
|
+
**配置示例:**
|
|
357
|
+
```javascript
|
|
358
|
+
'@foundbyte/no-disabled-comments': ['error', {
|
|
359
|
+
patterns: [
|
|
360
|
+
'/^\\s*eslint-disable-next-line\\s*$/', // 匹配 "eslint-disable-next-line"(不加后缀规则)
|
|
361
|
+
'/^\\s*eslint-disable\\s*$/', // 匹配 "eslint-disable"
|
|
362
|
+
'/no-explicit-any/', // 模糊匹配包含 "no-explicit-any" 的注释
|
|
363
|
+
'/vue-script-lines-limit/', // 模糊匹配包含 "vue-script-lines-limit" 的注释
|
|
364
|
+
'/hooks-export-limit/' // 模糊匹配包含 "hooks-export-limit" 的注释
|
|
365
|
+
]
|
|
366
|
+
}]
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
**支持的注释类型:**
|
|
370
|
+
1. **JavaScript/TypeScript 注释**:
|
|
371
|
+
- 行注释:`// eslint-disable-next-line`
|
|
372
|
+
- 块注释:`/* eslint-disable */`
|
|
373
|
+
|
|
374
|
+
2. **Vue 模板中的 HTML 注释**:
|
|
375
|
+
- `<!-- eslint-disable -->`
|
|
376
|
+
- `<!-- eslint-disable-next-line -->`
|
|
377
|
+
|
|
378
|
+
**检测的注释内容:**
|
|
379
|
+
- 注释内容经过 `trim()` 处理,去除前后空白字符
|
|
380
|
+
- 支持正则表达式模糊匹配
|
|
381
|
+
- 支持多个模式同时匹配
|
|
382
|
+
|
|
383
|
+
**示例:**
|
|
384
|
+
```javascript
|
|
385
|
+
// ✅ 正确 - 不匹配任何禁用模式
|
|
386
|
+
// 普通注释
|
|
387
|
+
/* 另一个普通注释 */
|
|
388
|
+
|
|
389
|
+
// ❌ 错误 - 匹配 "eslint-disable-next-line" 模式
|
|
390
|
+
// eslint-disable-next-line
|
|
391
|
+
console.log('test');
|
|
392
|
+
|
|
393
|
+
// ❌ 错误 - 匹配 "eslint-disable" 模式
|
|
394
|
+
/* eslint-disable */
|
|
395
|
+
console.log('test');
|
|
396
|
+
|
|
397
|
+
// ❌ 错误 - 模糊匹配包含 "no-explicit-any" 的注释
|
|
398
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
399
|
+
const x: any = 1;
|
|
400
|
+
|
|
401
|
+
// ❌ 错误 - 模糊匹配包含 "vue-script-lines-limit" 的注释
|
|
402
|
+
/* eslint-disable vue-script-lines-limit */
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
```vue
|
|
406
|
+
<!-- ✅ 正确 - 普通 HTML 注释 -->
|
|
407
|
+
<!-- 这是一个普通注释 -->
|
|
408
|
+
|
|
409
|
+
<!-- ❌ 错误 - 匹配 "eslint-disable" 模式 -->
|
|
410
|
+
<!-- eslint-disable -->
|
|
411
|
+
|
|
412
|
+
<!-- ❌ 错误 - 匹配 "eslint-disable-next-line" 模式 -->
|
|
413
|
+
<!-- eslint-disable-next-line -->
|
|
414
|
+
|
|
415
|
+
<!-- ❌ 错误 - 模糊匹配包含 "no-explicit-any" 的注释 -->
|
|
416
|
+
<!-- eslint-disable @typescript-eslint/no-explicit-any -->
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
**配置选项:**
|
|
420
|
+
|
|
421
|
+
- `patterns` (array, 必填): 要禁用的注释模式列表
|
|
422
|
+
- 可以是正则表达式字符串,如 `'/^\\s*eslint-disable-next-line\\s*$/'`
|
|
423
|
+
- 可以是普通字符串,会自动转义并创建正则表达式
|
|
424
|
+
- 支持多个模式,只要匹配任意一个模式就会报错
|
|
425
|
+
|
|
426
|
+
- `caseSensitive` (boolean, 默认 false): 是否区分大小写
|
|
427
|
+
- 当为 false 时,匹配不区分大小写
|
|
428
|
+
- 当为 true 时,匹配区分大小写
|
|
429
|
+
|
|
430
|
+
**错误消息:**
|
|
431
|
+
- `禁止使用注释: "eslint-disable-next-line",如必须排除,请在 .eslintignore 中排除,并写清楚排除原因`
|
|
432
|
+
- `禁止使用注释: "eslint-disable",如必须排除,请在 .eslintignore 中排除,并写清楚排除原因`
|
|
433
|
+
- `禁止使用注释: "eslint-disable-next-line @typescript-eslint/no-explicit-any",如必须排除,请在 .eslintignore 中排除,并写清楚排除原因`
|
|
434
|
+
|
|
435
|
+
**注意:**
|
|
436
|
+
- 该规则会检查所有 JavaScript/TypeScript 注释和 Vue 模板中的 HTML 注释
|
|
437
|
+
- 对于 Vue 文件,规则会同时检查 `<script>` 中的 JavaScript 注释和 `<template>` 中的 HTML 注释
|
|
438
|
+
- 建议只禁用特定的、有问题的规则,而不是使用无参数的 `eslint-disable` 禁用所有规则
|
|
439
|
+
- 如果确实需要排除某些文件,建议使用 `.eslintignore` 文件
|
|
440
|
+
|
|
344
441
|
### tag-attribute-restriction
|
|
345
442
|
|
|
346
443
|
限制特定标签使用某些属性或属性值。
|
package/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const hooksArrowFunctionOnlyRule = require('./lib/rules/hooks-arrow-function-onl
|
|
|
6
6
|
const tagAttributeRestrictionRule = require('./lib/rules/tag-attribute-restriction')
|
|
7
7
|
const noInitialAsAssertionRule = require('./lib/rules/no-initial-as-assertion')
|
|
8
8
|
const noExcessiveAnyRule = require('./lib/rules/no-excessive-any')
|
|
9
|
+
const noDisabledCommentsRule = require('./lib/rules/no-disabled-comments')
|
|
9
10
|
|
|
10
11
|
module.exports = {
|
|
11
12
|
rules: {
|
|
@@ -17,5 +18,6 @@ module.exports = {
|
|
|
17
18
|
'tag-attribute-restriction': tagAttributeRestrictionRule,
|
|
18
19
|
'no-initial-as-assertion': noInitialAsAssertionRule,
|
|
19
20
|
'no-excessive-any': noExcessiveAnyRule,
|
|
21
|
+
'no-disabled-comments': noDisabledCommentsRule,
|
|
20
22
|
},
|
|
21
23
|
}
|
package/lib/rules/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export { HooksArrowFunctionOnlyOptions } from './hooks-arrow-function-only'
|
|
|
6
6
|
export { TagAttributeRestrictionOptions } from './tag-attribute-restriction'
|
|
7
7
|
export { NoInitialAsAssertionOptions } from './no-initial-as-assertion'
|
|
8
8
|
export { NoExcessiveAnyOptions } from './no-excessive-any'
|
|
9
|
+
export { NoDisabledCommentsOptions } from './no-disabled-comments'
|
|
9
10
|
|
|
10
11
|
import { Rule } from 'eslint'
|
|
11
12
|
|
|
@@ -53,6 +54,11 @@ export interface Rules {
|
|
|
53
54
|
*/
|
|
54
55
|
'no-excessive-any': Rule.RuleModule
|
|
55
56
|
|
|
57
|
+
/**
|
|
58
|
+
* 禁止使用特定的注释,如 // eslint-disable-next-line no-console
|
|
59
|
+
*/
|
|
60
|
+
'no-disabled-comments': Rule.RuleModule
|
|
61
|
+
|
|
56
62
|
/**
|
|
57
63
|
* 索引签名,用于兼容 ESLint 插件类型
|
|
58
64
|
*/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Rule } from 'eslint'
|
|
2
|
+
|
|
3
|
+
export interface NoDisabledCommentsOptions {
|
|
4
|
+
/**
|
|
5
|
+
* 要禁用的注释模式列表
|
|
6
|
+
* 可以是普通字符串或正则表达式字符串(以 / 开头和结尾)
|
|
7
|
+
*/
|
|
8
|
+
patterns?: string[]
|
|
9
|
+
/**
|
|
10
|
+
* 是否区分大小写
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
caseSensitive?: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const rule: Rule.RuleModule
|
|
17
|
+
|
|
18
|
+
export default rule
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'problem',
|
|
4
|
+
docs: {
|
|
5
|
+
description: '禁止使用特定的注释,如 // eslint-disable-next-line no-console',
|
|
6
|
+
category: 'Best Practices',
|
|
7
|
+
recommended: false,
|
|
8
|
+
},
|
|
9
|
+
fixable: null,
|
|
10
|
+
schema: [
|
|
11
|
+
{
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
patterns: {
|
|
15
|
+
type: 'array',
|
|
16
|
+
items: {
|
|
17
|
+
type: 'string',
|
|
18
|
+
},
|
|
19
|
+
description: '要禁用的注释模式列表',
|
|
20
|
+
},
|
|
21
|
+
caseSensitive: {
|
|
22
|
+
type: 'boolean',
|
|
23
|
+
default: false,
|
|
24
|
+
description: '是否区分大小写',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
messages: {
|
|
31
|
+
disabledComment: '禁止使用注释: "{{comment}}",如必须排除,请在 .eslintignore 中排除,并写清楚排除原因',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
create(context) {
|
|
36
|
+
const options = context.options[0] || {}
|
|
37
|
+
const patterns = options.patterns || []
|
|
38
|
+
const caseSensitive = options.caseSensitive || false
|
|
39
|
+
|
|
40
|
+
// 如果没有配置模式,则不进行检查
|
|
41
|
+
if (patterns.length === 0) {
|
|
42
|
+
return {}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 将字符串模式转换为正则表达式
|
|
46
|
+
const regexPatterns = patterns.map(pattern => {
|
|
47
|
+
// 如果模式已经是正则表达式字符串,如 "/eslint-disable-next-line/"
|
|
48
|
+
if (pattern.startsWith('/') && pattern.endsWith('/')) {
|
|
49
|
+
const regexBody = pattern.slice(1, -1)
|
|
50
|
+
return new RegExp(regexBody, caseSensitive ? '' : 'i')
|
|
51
|
+
}
|
|
52
|
+
// 普通字符串,进行转义并创建正则表达式
|
|
53
|
+
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
54
|
+
return new RegExp(escapedPattern, caseSensitive ? '' : 'i')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 检查注释是否匹配任何禁用模式
|
|
59
|
+
*/
|
|
60
|
+
function isCommentDisabled(comment) {
|
|
61
|
+
const commentText = comment.value.trim()
|
|
62
|
+
return regexPatterns.some(regex => regex.test(commentText))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 检查 Vue 文件中的 HTML 注释
|
|
67
|
+
*/
|
|
68
|
+
function checkVueHtmlComments(context, sourceCode, regexPatterns) {
|
|
69
|
+
const text = sourceCode.getText()
|
|
70
|
+
|
|
71
|
+
// 匹配 HTML 注释:<!-- 注释内容 -->
|
|
72
|
+
// 包括多行注释
|
|
73
|
+
const htmlCommentRegex = /<!--([\s\S]*?)-->/g
|
|
74
|
+
let match
|
|
75
|
+
|
|
76
|
+
while ((match = htmlCommentRegex.exec(text)) !== null) {
|
|
77
|
+
const commentContent = match[1].trim()
|
|
78
|
+
|
|
79
|
+
// 检查注释内容是否匹配任何禁用模式
|
|
80
|
+
const isDisabled = regexPatterns.some(regex => regex.test(commentContent))
|
|
81
|
+
|
|
82
|
+
if (isDisabled) {
|
|
83
|
+
// 计算注释的位置
|
|
84
|
+
const startIndex = match.index
|
|
85
|
+
const endIndex = match.index + match[0].length
|
|
86
|
+
|
|
87
|
+
context.report({
|
|
88
|
+
loc: {
|
|
89
|
+
start: sourceCode.getLocFromIndex(startIndex),
|
|
90
|
+
end: sourceCode.getLocFromIndex(endIndex),
|
|
91
|
+
},
|
|
92
|
+
messageId: 'disabledComment',
|
|
93
|
+
data: {
|
|
94
|
+
comment: commentContent,
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
Program() {
|
|
103
|
+
const sourceCode = context.getSourceCode()
|
|
104
|
+
const comments = sourceCode.getAllComments()
|
|
105
|
+
|
|
106
|
+
// 检查 JavaScript/TypeScript 注释
|
|
107
|
+
comments.forEach(comment => {
|
|
108
|
+
if (isCommentDisabled(comment)) {
|
|
109
|
+
context.report({
|
|
110
|
+
node: comment,
|
|
111
|
+
messageId: 'disabledComment',
|
|
112
|
+
data: {
|
|
113
|
+
comment: comment.value.trim(),
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
// 检查 Vue 文件中的 HTML 注释
|
|
120
|
+
const filename = context.getFilename()
|
|
121
|
+
if (filename.endsWith('.vue')) {
|
|
122
|
+
checkVueHtmlComments(context, sourceCode, regexPatterns)
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foundbyte/eslint-plugin",
|
|
3
|
-
"version": "1.1.4-alpha.
|
|
3
|
+
"version": "1.1.4-alpha.6",
|
|
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
|
"types": "index.d.ts",
|
package/types.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// 导出所有规则配置类型
|
|
2
|
-
export type { ArrowFunctionPaddingOptions, HooksExportLimitOptions, TypeCommentRequirementOptions, VueScriptLinesLimitOptions, HooksArrowFunctionOnlyOptions, NoExcessiveAnyOptions } from './lib/rules'
|
|
2
|
+
export type { ArrowFunctionPaddingOptions, HooksExportLimitOptions, TypeCommentRequirementOptions, VueScriptLinesLimitOptions, HooksArrowFunctionOnlyOptions, NoExcessiveAnyOptions, NoDisabledCommentsOptions } from './lib/rules'
|