1
1
'use strict' ;
2
2
3
- const typedNodeSourceFileTesters = [
3
+ const defaultTypedNodeSourceFileTesters = [
4
4
/ @ t y p e s [ / \\ ] e s t r e e [ / \\ ] i n d e x \. d \. t s / ,
5
5
/ @ t y p e s c r i p t - e s l i n t [ / \\ ] t y p e s [ / \\ ] d i s t [ / \\ ] g e n e r a t e d [ / \\ ] a s t - s p e c \. d \. t s / ,
6
6
] ;
@@ -26,9 +26,10 @@ const typedNodeSourceFileTesters = [
26
26
* ```
27
27
*
28
28
* @param {import('typescript').Type } type
29
+ * @param {RegExp[] } typedNodeSourceFileTesters
29
30
* @returns Whether the type seems to include a known ESTree or TSESTree AST node.
30
31
*/
31
- function isAstNodeType ( type ) {
32
+ function isAstNodeType ( type , typedNodeSourceFileTesters ) {
32
33
return ( type . types || [ type ] )
33
34
. filter ( ( typePart ) => typePart . getProperty ( 'type' ) )
34
35
. flatMap (
@@ -55,13 +56,33 @@ module.exports = {
55
56
requiresTypeChecking : true ,
56
57
url : 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-property-in-node.md' ,
57
58
} ,
58
- schema : [ ] ,
59
+ schema : [
60
+ {
61
+ type : 'object' ,
62
+ properties : {
63
+ additionalNodeTypeFiles : {
64
+ description :
65
+ 'Any additional regular expressions to consider source files defining AST Node types.' ,
66
+ elements : { type : 'string' } ,
67
+ type : 'array' ,
68
+ } ,
69
+ } ,
70
+ additionalProperties : false ,
71
+ } ,
72
+ ] ,
59
73
messages : {
60
74
in : 'Prefer checking specific node properties instead of a broad `in`.' ,
61
75
} ,
62
76
} ,
63
77
64
78
create ( context ) {
79
+ const typedNodeSourceFileTesters = [
80
+ ...defaultTypedNodeSourceFileTesters ,
81
+ ...( context . options [ 0 ] ?. additionalNodeTypeFiles ?. map (
82
+ ( filePath ) => new RegExp ( filePath ) ,
83
+ ) ?? [ ] ) ,
84
+ ] ;
85
+
65
86
return {
66
87
'BinaryExpression[operator=in]' ( node ) {
67
88
// TODO: Switch this to ESLintUtils.getParserServices with typescript-eslint@>=6
@@ -77,7 +98,7 @@ module.exports = {
77
98
const tsNode = services . esTreeNodeToTSNodeMap . get ( node . right ) ;
78
99
const type = checker . getTypeAtLocation ( tsNode ) ;
79
100
80
- if ( isAstNodeType ( type ) ) {
101
+ if ( isAstNodeType ( type , typedNodeSourceFileTesters ) ) {
81
102
context . report ( { messageId : 'in' , node } ) ;
82
103
}
83
104
} ,
0 commit comments