@foundbyte/eslint-plugin 1.1.5-alpha.1 → 1.1.5-alpha.2

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.
@@ -1,3 +1,5 @@
1
+ const { getSourceCode } = require('../utils/eslint-compat')
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'suggestion',
@@ -24,7 +26,7 @@ module.exports = {
24
26
  messageId: 'useArrowFunction',
25
27
  fix(fixer) {
26
28
  // 将 function 声明转换为箭头函数
27
- const sourceCode = context.getSourceCode()
29
+ const sourceCode = getSourceCode(context)
28
30
  const functionName = node.id.name
29
31
  const params = node.params.map(param => sourceCode.getText(param)).join(', ')
30
32
  const body = sourceCode.getText(node.body)
@@ -52,7 +54,7 @@ module.exports = {
52
54
  messageId: 'useArrowFunction',
53
55
  fix(fixer) {
54
56
  // 将函数表达式转换为箭头函数
55
- const sourceCode = context.getSourceCode()
57
+ const sourceCode = getSourceCode(context)
56
58
  const params = node.init.params.map(param => sourceCode.getText(param)).join(', ')
57
59
  const body = sourceCode.getText(node.init.body)
58
60
 
@@ -1,3 +1,5 @@
1
+ const { getFilename, getSourceCode } = require('../utils/eslint-compat')
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'problem',
@@ -29,7 +31,7 @@ module.exports = {
29
31
  const maxExports = options.maxExports || 20
30
32
 
31
33
  // 只处理 .vue 文件
32
- const filename = context.getFilename()
34
+ const filename = getFilename(context)
33
35
  if (!filename.endsWith('.vue')) {
34
36
  return {}
35
37
  }
@@ -37,7 +39,7 @@ module.exports = {
37
39
  return {
38
40
  Program() {
39
41
  // 获取整个文件的源代码
40
- const sourceCode = context.getSourceCode()
42
+ const sourceCode = getSourceCode(context)
41
43
  const text = sourceCode.getText()
42
44
 
43
45
  // 使用正则表达式匹配所有 hooks 的解构赋值
@@ -1,3 +1,5 @@
1
+ const { getFilename, getSourceCode } = require('../utils/eslint-compat')
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'problem',
@@ -100,7 +102,7 @@ module.exports = {
100
102
 
101
103
  return {
102
104
  Program() {
103
- const sourceCode = context.getSourceCode()
105
+ const sourceCode = getSourceCode(context)
104
106
  const comments = sourceCode.getAllComments()
105
107
 
106
108
  // 检查 JavaScript/TypeScript 注释
@@ -117,7 +119,7 @@ module.exports = {
117
119
  })
118
120
 
119
121
  // 检查 Vue 文件中的 HTML 注释
120
- const filename = context.getFilename()
122
+ const filename = getFilename(context)
121
123
  if (filename.endsWith('.vue')) {
122
124
  checkVueHtmlComments(context, sourceCode, regexPatterns)
123
125
  }
@@ -1,3 +1,5 @@
1
+ const { getSourceCode } = require('../utils/eslint-compat')
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'problem',
@@ -208,7 +210,7 @@ const anyVariables = new Set()
208
210
  // 检查是否有类型参数(通过源代码文本检查)
209
211
  // 由于 Vue SFC 文件中的 TypeScript 泛型可能没有被正确解析为 AST 节点,
210
212
  // 我们通过检查源代码文本来判断是否有类型参数
211
- const sourceCode = context.getSourceCode()
213
+ const sourceCode = getSourceCode(context)
212
214
  const callText = sourceCode.getText(node)
213
215
 
214
216
  // 检查调用文本中是否包含泛型类型参数(如 <string>)
@@ -325,7 +327,7 @@ const anyVariables = new Set()
325
327
  */
326
328
  function getImportsForCurrentFile() {
327
329
  const imports = []
328
- const programNode = context.getSourceCode().ast
330
+ const programNode = getSourceCode(context).ast
329
331
 
330
332
  // 遍历所有导入声明
331
333
  programNode.body.forEach(node => {
@@ -529,7 +531,7 @@ const anyVariables = new Set()
529
531
 
530
532
  // 检查函数调用中的类型参数
531
533
  if (node.type === 'CallExpression') {
532
- const sourceCode = context.getSourceCode()
534
+ const sourceCode = getSourceCode(context)
533
535
  const callText = sourceCode.getText(node)
534
536
 
535
537
  // 检查类型参数(泛型)
@@ -1,3 +1,5 @@
1
+ const { getSourceCode } = require('../utils/eslint-compat');
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'suggestion',
@@ -112,7 +114,7 @@ module.exports = {
112
114
  }
113
115
 
114
116
  // 获取类型注解的源代码
115
- const sourceCode = context.getSourceCode();
117
+ const sourceCode = getSourceCode(context);
116
118
  const typeAnnotationText = sourceCode.getText(node.typeAnnotation);
117
119
 
118
120
  // 检查是否匹配任何排除模式
@@ -1,3 +1,5 @@
1
+ const { getFilename, getSourceCode } = require('../utils/eslint-compat');
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'problem',
@@ -64,7 +66,7 @@ module.exports = {
64
66
  });
65
67
 
66
68
  // 获取文件名,只对 .vue 文件生效
67
- const filename = context.getFilename();
69
+ const filename = getFilename(context);
68
70
  if (!filename.endsWith('.vue')) {
69
71
  return {};
70
72
  }
@@ -72,7 +74,7 @@ module.exports = {
72
74
  return {
73
75
  Program(node) {
74
76
  // 获取整个文件的源代码
75
- const sourceCode = context.getSourceCode();
77
+ const sourceCode = getSourceCode(context);
76
78
  const text = sourceCode.getText();
77
79
 
78
80
  // 使用正则表达式匹配模板中的标签
@@ -1,3 +1,5 @@
1
+ const { getFilename, getSourceCode } = require('../utils/eslint-compat');
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'problem',
@@ -63,7 +65,7 @@ module.exports = {
63
65
  }
64
66
 
65
67
  // 获取文件名,只对 .vue 文件生效
66
- const filename = context.getFilename();
68
+ const filename = getFilename(context);
67
69
  if (!filename.endsWith('.vue')) {
68
70
  return {};
69
71
  }
@@ -77,7 +79,7 @@ module.exports = {
77
79
  return {
78
80
  Program(node) {
79
81
  // 获取整个文件的源代码
80
- const sourceCode = context.getSourceCode();
82
+ const sourceCode = getSourceCode(context);
81
83
  const text = sourceCode.getText();
82
84
 
83
85
  // 使用正则表达式匹配模板中的标签和属性
@@ -1,3 +1,5 @@
1
+ const { getSourceCode } = require('../utils/eslint-compat');
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'suggestion',
@@ -53,7 +55,7 @@ module.exports = {
53
55
  * @returns {boolean} 是否有注释
54
56
  */
55
57
  function hasComment(node) {
56
- const sourceCode = context.getSourceCode();
58
+ const sourceCode = getSourceCode(context);
57
59
  const comments = sourceCode.getCommentsBefore(node);
58
60
 
59
61
  // 检查是否有任何类型的注释(行注释 // 或块注释 /** */)
@@ -1,3 +1,5 @@
1
+ const { getFilename, getSourceCode } = require('../utils/eslint-compat');
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: 'problem',
@@ -29,7 +31,7 @@ module.exports = {
29
31
  const maxLines = options.maxLines || 300;
30
32
 
31
33
  // 只处理 .vue 文件
32
- const filename = context.getFilename();
34
+ const filename = getFilename(context);
33
35
  if (!filename.endsWith('.vue')) {
34
36
  return {};
35
37
  }
@@ -37,7 +39,7 @@ module.exports = {
37
39
  return {
38
40
  Program(node) {
39
41
  // 获取整个文件的源代码
40
- const sourceCode = context.getSourceCode();
42
+ const sourceCode = getSourceCode(context);
41
43
  const text = sourceCode.getText();
42
44
 
43
45
  // 使用正则表达式匹配 script 标签
@@ -0,0 +1,46 @@
1
+ /**
2
+ * ESLint 8 / 9 / 10 兼容工具
3
+ *
4
+ * 背景:
5
+ * - `context.sourceCode`、`context.filename` 属性自 ESLint v8.40.0 引入
6
+ * - `context.getSourceCode()`、`context.getFilename()` 方法在 ESLint v9 中废弃,
7
+ * 并在 ESLint v10.0.0 中被彻底移除
8
+ *
9
+ * 因此这里优先读取新属性,不存在时回退到旧方法,
10
+ * 使规则可以同时运行在 ESLint 8(含 8.0 ~ 8.39)、9、10 三个大版本上。
11
+ */
12
+
13
+ /**
14
+ * 获取 SourceCode 实例(兼容 ESLint 8/9/10)
15
+ * @param {import('eslint').Rule.RuleContext} context 规则上下文
16
+ * @returns {import('eslint').SourceCode} SourceCode 实例
17
+ */
18
+ function getSourceCode(context) {
19
+ if (context.sourceCode) {
20
+ return context.sourceCode
21
+ }
22
+ if (typeof context.getSourceCode === 'function') {
23
+ return context.getSourceCode()
24
+ }
25
+ throw new Error('[@foundbyte/eslint-plugin] 无法从规则上下文获取 SourceCode')
26
+ }
27
+
28
+ /**
29
+ * 获取当前被 lint 文件的文件名(兼容 ESLint 8/9/10)
30
+ * @param {import('eslint').Rule.RuleContext} context 规则上下文
31
+ * @returns {string} 文件名
32
+ */
33
+ function getFilename(context) {
34
+ if (typeof context.filename === 'string') {
35
+ return context.filename
36
+ }
37
+ if (typeof context.getFilename === 'function') {
38
+ return context.getFilename()
39
+ }
40
+ return '<input>'
41
+ }
42
+
43
+ module.exports = {
44
+ getSourceCode,
45
+ getFilename,
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foundbyte/eslint-plugin",
3
- "version": "1.1.5-alpha.1",
3
+ "version": "1.1.5-alpha.2",
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
  "author": "",
6
6
  "license": "ISC",
@@ -18,13 +18,13 @@
18
18
  ],
19
19
  "main": "index.js",
20
20
  "types": "index.d.ts",
21
+ "scripts": {
22
+ "test": "echo \"Error: no test specified\" && exit 1"
23
+ },
21
24
  "publishConfig": {
22
25
  "access": "public"
23
26
  },
24
27
  "peerDependencies": {
25
- "eslint": ">=8.0.0"
26
- },
27
- "scripts": {
28
- "test": "echo \"Error: no test specified\" && exit 1"
28
+ "eslint": "^8.0.0 || ^9.0.0 || ^10.0.0"
29
29
  }
30
- }
30
+ }