@foundbyte/eslint-plugin 1.1.4-alpha → 1.1.4-alpha.1

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.
@@ -32,6 +32,14 @@ module.exports = {
32
32
  // 存储 any 类型使用的位置
33
33
  const anyUsages = []
34
34
 
35
+ /**
36
+ * 检查文件路径是否应该被排除
37
+ */
38
+ function shouldExcludeFile() {
39
+ // 不再排除任何文件,所有文件都检查
40
+ return false
41
+ }
42
+
35
43
  /**
36
44
  * 递归检查类型节点中的 any 类型
37
45
  */
@@ -79,25 +87,65 @@ module.exports = {
79
87
  }
80
88
  }
81
89
 
82
- /**
83
- * 检查 TSAnyKeyword 节点(显式 any 类型)
84
- */
85
- function checkTSAnyKeyword(node) {
86
- anyUsages.push({
87
- node,
88
- type: 'explicit',
89
- })
90
- }
91
-
92
90
  /**
93
91
  * 检查类型注解中的 any 类型
94
92
  */
95
93
  function checkTypeAnnotation(node) {
94
+ if (shouldExcludeFile()) return
96
95
  if (node.typeAnnotation && node.typeAnnotation.typeAnnotation) {
97
96
  checkTypeNodeForAny(node.typeAnnotation.typeAnnotation, node)
98
97
  }
99
98
  }
100
99
 
100
+ /**
101
+ * 检查是否是内置函数
102
+ */
103
+ function isBuiltInFunction(node) {
104
+ if (node.type !== 'CallExpression') return false
105
+
106
+ // 检查全局对象的方法调用,如 JSON.parse, Promise.all 等
107
+ if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier') {
108
+ const objectName = node.callee.object.name
109
+
110
+ // 内置全局对象列表 - 这些对象的所有方法都认为是内置的
111
+ const builtInObjects = ['JSON', 'Promise', 'Array', 'Object', 'String', 'Number', 'Boolean', 'Date', 'Math', 'RegExp', 'Map', 'Set', 'WeakMap', 'WeakSet']
112
+
113
+ if (builtInObjects.includes(objectName)) {
114
+ // 对于这些内置对象,假设它们的所有方法都是内置的
115
+ // 这样可以避免维护庞大的方法列表
116
+ return true
117
+ }
118
+ }
119
+
120
+ // 检查可选链调用,如 spotArr?.find()
121
+ if (node.callee.type === 'ChainExpression' && node.callee.expression.type === 'CallExpression') {
122
+ const chainCall = node.callee.expression
123
+ if (chainCall.callee.type === 'MemberExpression' && chainCall.callee.object.type === 'Identifier') {
124
+ const objectName = chainCall.callee.object.name
125
+
126
+ // 内置全局对象列表
127
+ const builtInObjects = ['JSON', 'Promise', 'Array', 'Object', 'String', 'Number', 'Boolean', 'Date', 'Math', 'RegExp', 'Map', 'Set', 'WeakMap', 'WeakSet']
128
+
129
+ if (builtInObjects.includes(objectName)) {
130
+ // 对于这些内置对象,假设它们的所有方法都是内置的
131
+ return true
132
+ }
133
+ }
134
+ }
135
+
136
+ // 检查全局函数调用,如 parseInt, parseFloat, isNaN 等
137
+ if (node.callee.type === 'Identifier') {
138
+ const functionName = node.callee.name
139
+ const globalFunctions = [
140
+ 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'eval', 'encodeURI',
141
+ 'encodeURIComponent', 'decodeURI', 'decodeURIComponent', 'escape', 'unescape'
142
+ ]
143
+ return globalFunctions.includes(functionName)
144
+ }
145
+
146
+ return false
147
+ }
148
+
101
149
  /**
102
150
  * 检查表达式是否可以推导类型
103
151
  */
@@ -126,12 +174,46 @@ module.exports = {
126
174
 
127
175
  // 函数调用:检查参数是否可以推导类型
128
176
  if (node.type === 'CallExpression') {
129
- // 如果函数调用有参数,并且所有参数都可以推导类型,则认为可以推导返回类型
130
- if (node.arguments && node.arguments.length > 0) {
131
- return node.arguments.every(arg => canInferTypeFromExpression(arg))
177
+ // 检查是否是第三方库函数调用
178
+ if (isThirdPartyFunctionCall(node)) {
179
+ return true // 第三方库函数调用,假设可以推导类型
180
+ }
181
+
182
+ // 检查是否是 Vue 响应式函数调用且有明确的类型参数
183
+ if (isTypedVueReactiveCall(node)) {
184
+ return true // 有明确的类型参数,可以推导类型
132
185
  }
133
- // 没有参数的函数调用无法推导类型(如 ref())
134
- return false
186
+
187
+ // 检查是否是 Vue 响应式函数(无论是否有类型参数)
188
+ if (isVueReactiveFunction(node)) {
189
+ // 对于 Vue 响应式函数调用,需要检查是否有参数可以推导类型
190
+ if (node.arguments && node.arguments.length > 0) {
191
+ // 如果有参数,并且所有参数都可以推导类型,则认为可以推导返回类型
192
+ return node.arguments.every(arg => canInferTypeFromExpression(arg))
193
+ }
194
+
195
+ // 检查是否有类型参数(通过源代码文本检查)
196
+ // 由于 Vue SFC 文件中的 TypeScript 泛型可能没有被正确解析为 AST 节点,
197
+ // 我们通过检查源代码文本来判断是否有类型参数
198
+ const sourceCode = context.getSourceCode()
199
+ const callText = sourceCode.getText(node)
200
+
201
+ // 检查调用文本中是否包含泛型类型参数(如 <string>)
202
+ const hasTypeParameters = callText.includes('<') && callText.includes('>')
203
+
204
+ // 没有参数的 Vue 响应式函数调用,如果有类型参数,可以推导类型
205
+ // 如 ref<string>() 可以推导为 Ref<string | undefined>
206
+ if (hasTypeParameters) {
207
+ return true
208
+ }
209
+
210
+ // 没有类型参数也没有参数的 Vue 响应式函数调用(如 ref())无法推导类型
211
+ return false
212
+ }
213
+
214
+ // 对于其他函数调用,假设可以推导类型
215
+ // 因为本地函数有明确的返回类型,TypeScript 可以推导
216
+ return true
135
217
  }
136
218
 
137
219
  // 属性访问通常可以推导类型(如 ref.value)
@@ -139,6 +221,11 @@ module.exports = {
139
221
  return true
140
222
  }
141
223
 
224
+ // 可选链表达式可以推导类型(如 spotArr?.find())
225
+ if (node.type === 'ChainExpression') {
226
+ return true
227
+ }
228
+
142
229
  // await 表达式可以推导类型(等待 Promise 的结果)
143
230
  if (node.type === 'AwaitExpression') {
144
231
  return true
@@ -148,10 +235,159 @@ module.exports = {
148
235
  return false
149
236
  }
150
237
 
238
+ /**
239
+ * 检查是否是第三方库函数调用
240
+ */
241
+ function isThirdPartyFunctionCall(node) {
242
+ if (node.type !== 'CallExpression') return false
243
+
244
+ // 检查是否是内置函数
245
+ if (isBuiltInFunction(node)) {
246
+ return true
247
+ }
248
+
249
+ // 获取当前文件的导入信息
250
+ const imports = getImportsForCurrentFile()
251
+
252
+ // 检查标识符调用,如 isEqualWith(a, b)
253
+ if (node.callee.type === 'Identifier') {
254
+ const functionName = node.callee.name
255
+ const importInfo = imports.find(imp => imp.localName === functionName)
256
+
257
+ if (importInfo) {
258
+ // 检查导入的模块是否在排除模式中
259
+ return isModuleExcluded(importInfo.modulePath)
260
+ }
261
+ }
262
+
263
+ // 检查成员表达式调用,如 lodash.isEqualWith(a, b)
264
+ if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier') {
265
+ const objectName = node.callee.object.name
266
+ const importInfo = imports.find(imp => imp.localName === objectName)
267
+
268
+ if (importInfo) {
269
+ // 检查导入的模块是否在排除模式中
270
+ return isModuleExcluded(importInfo.modulePath)
271
+ }
272
+ }
273
+
274
+ return false
275
+ }
276
+
277
+ /**
278
+ * 检查是否是内置函数
279
+ */
280
+ function isBuiltInFunction(node) {
281
+ if (node.type !== 'CallExpression') return false
282
+
283
+ // 检查全局对象的方法调用,如 JSON.parse, Promise.all 等
284
+ if (node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier') {
285
+ const objectName = node.callee.object.name
286
+ const propertyName = node.callee.property.type === 'Identifier' ? node.callee.property.name : ''
287
+
288
+ // 内置全局对象列表
289
+ const builtInObjects = ['JSON', 'Promise', 'Array', 'Object', 'String', 'Number', 'Boolean', 'Date', 'Math', 'RegExp', 'Map', 'Set', 'WeakMap', 'WeakSet']
290
+
291
+ if (builtInObjects.includes(objectName)) {
292
+ // 对于这些内置对象,假设它们的所有方法都是内置的
293
+ // 这样可以避免维护庞大的方法列表
294
+ return true
295
+ }
296
+ }
297
+
298
+ // 检查可选链调用,如 spotArr?.find()
299
+ if (node.callee.type === 'ChainExpression' && node.callee.expression.type === 'CallExpression') {
300
+ const chainCall = node.callee.expression
301
+ if (chainCall.callee.type === 'MemberExpression' && chainCall.callee.object.type === 'Identifier') {
302
+ const objectName = chainCall.callee.object.name
303
+
304
+ // 内置全局对象列表
305
+ const builtInObjects = ['JSON', 'Promise', 'Array', 'Object', 'String', 'Number', 'Boolean', 'Date', 'Math', 'RegExp', 'Map', 'Set', 'WeakMap', 'WeakSet']
306
+
307
+ if (builtInObjects.includes(objectName)) {
308
+ // 对于这些内置对象,假设它们的所有方法都是内置的
309
+ return true
310
+ }
311
+ }
312
+ }
313
+
314
+ // 检查全局函数调用,如 parseInt, parseFloat, isNaN 等
315
+ if (node.callee.type === 'Identifier') {
316
+ const functionName = node.callee.name
317
+ const globalFunctions = [
318
+ 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'eval', 'encodeURI',
319
+ 'encodeURIComponent', 'decodeURI', 'decodeURIComponent', 'escape', 'unescape'
320
+ ]
321
+ return globalFunctions.includes(functionName)
322
+ }
323
+
324
+ return false
325
+ }
326
+
327
+ /**
328
+ * 检查模块路径是否应该被排除
329
+ */
330
+ function isModuleExcluded(modulePath) {
331
+ // 检查是否是第三方模块(不以 ./ 或 ../ 开头)
332
+ const isThirdPartyModule = !modulePath.startsWith('./') && !modulePath.startsWith('../')
333
+
334
+ if (isThirdPartyModule) {
335
+ return true // 所有第三方模块都排除
336
+ }
337
+
338
+ return excludePatterns.some(pattern => {
339
+ if (pattern.includes('*')) {
340
+ const regex = new RegExp(pattern.replace(/\*/g, '.*'))
341
+ return regex.test(modulePath)
342
+ }
343
+ return modulePath.includes(pattern)
344
+ })
345
+ }
346
+
347
+ /**
348
+ * 获取当前文件的导入信息
349
+ */
350
+ function getImportsForCurrentFile() {
351
+ const imports = []
352
+ const programNode = context.getSourceCode().ast
353
+
354
+ // 遍历所有导入声明
355
+ programNode.body.forEach(node => {
356
+ if (node.type === 'ImportDeclaration') {
357
+ const modulePath = node.source.value
358
+
359
+ node.specifiers.forEach(specifier => {
360
+ if (specifier.type === 'ImportSpecifier') {
361
+ imports.push({
362
+ localName: specifier.local.name,
363
+ importedName: specifier.imported.name,
364
+ modulePath: modulePath
365
+ })
366
+ } else if (specifier.type === 'ImportDefaultSpecifier') {
367
+ imports.push({
368
+ localName: specifier.local.name,
369
+ importedName: 'default',
370
+ modulePath: modulePath
371
+ })
372
+ } else if (specifier.type === 'ImportNamespaceSpecifier') {
373
+ imports.push({
374
+ localName: specifier.local.name,
375
+ importedName: '*',
376
+ modulePath: modulePath
377
+ })
378
+ }
379
+ })
380
+ }
381
+ })
382
+
383
+ return imports
384
+ }
385
+
151
386
  /**
152
387
  * 检查变量声明中的隐式 any 类型
153
388
  */
154
389
  function checkVariableDeclaration(node) {
390
+ if (shouldExcludeFile()) return
155
391
  // 遍历所有声明
156
392
  node.declarations.forEach(declaration => {
157
393
  if (declaration.id && declaration.id.type === 'Identifier') {
@@ -182,10 +418,14 @@ module.exports = {
182
418
  declaration.init.type === 'ArrowFunctionExpression'
183
419
 
184
420
  if (!isFunctionDeclaration) {
185
- anyUsages.push({
186
- node: declaration,
187
- type: 'implicit',
188
- })
421
+ // 检查是否是简单的 Map/Set 实例化,这些可以通过上下文推导类型
422
+ const isSimpleCollection = isSimpleCollectionInstantiation(declaration.init)
423
+ if (!isSimpleCollection) {
424
+ anyUsages.push({
425
+ node: declaration,
426
+ type: 'implicit',
427
+ })
428
+ }
189
429
  }
190
430
  }
191
431
  }
@@ -198,10 +438,114 @@ module.exports = {
198
438
  })
199
439
  }
200
440
 
441
+ /**
442
+ * 检查是否是 Vue 响应式函数(无论是否有类型参数)
443
+ */
444
+ function isVueReactiveFunction(node) {
445
+ if (!node || node.type !== 'CallExpression') return false
446
+
447
+ // 检查是否是 ref() 或 reactive() 调用
448
+ if (node.callee.type === 'Identifier') {
449
+ const functionName = node.callee.name
450
+ const vueReactiveFunctions = ['ref', 'reactive', 'computed', 'watch', 'watchEffect']
451
+
452
+ return vueReactiveFunctions.includes(functionName)
453
+ }
454
+
455
+ return false
456
+ }
457
+
458
+ /**
459
+ * 检查是否是 Vue 响应式函数调用且有明确的类型参数
460
+ * 这个函数专门处理所有有类型参数的 Vue 响应式函数调用
461
+ */
462
+ function isTypedVueReactiveCall(node) {
463
+ if (!node || node.type !== 'CallExpression') return false
464
+
465
+ // 检查是否是 Vue 响应式函数调用
466
+ if (node.callee.type === 'Identifier') {
467
+ const functionName = node.callee.name
468
+ const vueReactiveFunctions = ['ref', 'reactive', 'computed', 'watch', 'watchEffect']
469
+
470
+ if (vueReactiveFunctions.includes(functionName)) {
471
+ // 检查是否有类型参数(TypeScript 泛型)
472
+ if (node.typeParameters && node.typeParameters.params && node.typeParameters.params.length > 0) {
473
+ return true // 有明确的类型参数
474
+ }
475
+ }
476
+ }
477
+
478
+ return false
479
+ }
480
+
481
+ /**
482
+ * 检查是否是简单的集合实例化(如 new Map(), new Set())
483
+ * 这些可以通过上下文推导类型,不应该算作隐式 any
484
+ */
485
+ function isSimpleCollectionInstantiation(node) {
486
+ if (!node) return false
487
+
488
+ // 检查 new Map() 或 new Set()
489
+ if (node.type === 'NewExpression') {
490
+ const calleeName = node.callee.type === 'Identifier' ? node.callee.name : ''
491
+ if (calleeName === 'Map' || calleeName === 'Set') {
492
+ // 检查是否有参数(无参数的 new Map() 或 new Set() 是简单的实例化)
493
+ if (!node.arguments || node.arguments.length === 0) {
494
+ return true
495
+ }
496
+ }
497
+ }
498
+
499
+ return false
500
+ }
501
+
502
+ /**
503
+ * 检查函数声明中的 any 类型
504
+ */
505
+ function checkFunctionDeclaration(node) {
506
+ if (shouldExcludeFile()) return
507
+
508
+ // 检查函数返回类型中的 any
509
+ if (node.returnType && node.returnType.typeAnnotation) {
510
+ checkTypeNodeForAny(node.returnType.typeAnnotation, node)
511
+ }
512
+
513
+ // 检查函数参数类型中的 any
514
+ if (node.params) {
515
+ node.params.forEach(param => {
516
+ if (param.typeAnnotation) {
517
+ checkTypeAnnotation(param)
518
+ }
519
+ })
520
+ }
521
+ }
522
+
523
+ /**
524
+ * 检查箭头函数中的 any 类型
525
+ */
526
+ function checkArrowFunctionExpression(node) {
527
+ if (shouldExcludeFile()) return
528
+
529
+ // 检查箭头函数返回类型中的 any
530
+ if (node.returnType && node.returnType.typeAnnotation) {
531
+ checkTypeNodeForAny(node.returnType.typeAnnotation, node)
532
+ }
533
+
534
+ // 检查箭头函数参数类型中的 any
535
+ if (node.params) {
536
+ node.params.forEach(param => {
537
+ if (param.typeAnnotation) {
538
+ checkTypeAnnotation(param)
539
+ }
540
+ })
541
+ }
542
+ }
543
+
201
544
  /**
202
545
  * 检查类型别名和接口中的 any 类型
203
546
  */
204
547
  function checkTypeDeclaration(node) {
548
+ if (shouldExcludeFile()) return
205
549
  if (node.typeAnnotation) {
206
550
  checkTypeNodeForAny(node.typeAnnotation, node)
207
551
  }
@@ -211,6 +555,7 @@ module.exports = {
211
555
  * 在文件结束时检查 any 类型使用次数
212
556
  */
213
557
  function checkAnyUsageCount() {
558
+ if (shouldExcludeFile()) return
214
559
  if (anyUsages.length > maxAnyCount) {
215
560
  // 报告第一个超出限制的 any 使用(第 maxAnyCount + 1 个)
216
561
  const firstExceededUsage = anyUsages[maxAnyCount]
@@ -226,8 +571,9 @@ module.exports = {
226
571
  }
227
572
 
228
573
  return {
229
- TSAnyKeyword: checkTSAnyKeyword,
230
574
  VariableDeclaration: checkVariableDeclaration,
575
+ FunctionDeclaration: checkFunctionDeclaration,
576
+ ArrowFunctionExpression: checkArrowFunctionExpression,
231
577
  TSTypeAliasDeclaration: checkTypeDeclaration,
232
578
  TSInterfaceDeclaration: checkTypeDeclaration,
233
579
  'Program:exit': checkAnyUsageCount,
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.1",
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",