4
4
*/
5
5
"use strict"
6
6
7
+ const typeMatchesSpecifier =
8
+ /** @type {import('ts-declaration-location').default } */ (
9
+ /** @type {unknown } */ ( require ( "ts-declaration-location" ) )
10
+ )
11
+ const getTypeOfNode = require ( "../util/get-type-of-node" )
12
+ const getParserServices = require ( "../util/get-parser-services" )
13
+ const getFullTypeName = require ( "../util/get-full-type-name" )
14
+
7
15
const selectors = [
8
16
// fs.readFileSync()
9
17
// readFileSync.call(null, 'path')
@@ -32,7 +40,56 @@ module.exports = {
32
40
} ,
33
41
ignores : {
34
42
type : "array" ,
35
- items : { type : "string" } ,
43
+ items : {
44
+ oneOf : [
45
+ { type : "string" } ,
46
+ {
47
+ type : "object" ,
48
+ properties : {
49
+ from : { const : "file" } ,
50
+ path : {
51
+ type : "string" ,
52
+ } ,
53
+ name : {
54
+ type : "array" ,
55
+ items : {
56
+ type : "string" ,
57
+ } ,
58
+ } ,
59
+ } ,
60
+ additionalProperties : false ,
61
+ } ,
62
+ {
63
+ type : "object" ,
64
+ properties : {
65
+ from : { const : "lib" } ,
66
+ name : {
67
+ type : "array" ,
68
+ items : {
69
+ type : "string" ,
70
+ } ,
71
+ } ,
72
+ } ,
73
+ additionalProperties : false ,
74
+ } ,
75
+ {
76
+ type : "object" ,
77
+ properties : {
78
+ from : { const : "package" } ,
79
+ package : {
80
+ type : "string" ,
81
+ } ,
82
+ name : {
83
+ type : "array" ,
84
+ items : {
85
+ type : "string" ,
86
+ } ,
87
+ } ,
88
+ } ,
89
+ additionalProperties : false ,
90
+ } ,
91
+ ] ,
92
+ } ,
36
93
default : [ ] ,
37
94
} ,
38
95
} ,
@@ -57,15 +114,64 @@ module.exports = {
57
114
* @returns {void }
58
115
*/
59
116
[ selector . join ( "," ) ] ( node ) {
60
- if ( ignores . includes ( node . name ) ) {
61
- return
117
+ const parserServices = getParserServices ( context )
118
+
119
+ /**
120
+ * @type {import('typescript').Type | undefined | null }
121
+ */
122
+ let type = undefined
123
+
124
+ /**
125
+ * @type {string | undefined | null }
126
+ */
127
+ let fullName = undefined
128
+
129
+ for ( const ignore of ignores ) {
130
+ if ( typeof ignore === "string" ) {
131
+ if ( ignore === node . name ) {
132
+ return
133
+ }
134
+
135
+ continue
136
+ }
137
+
138
+ if (
139
+ parserServices === null ||
140
+ parserServices . program === null
141
+ ) {
142
+ throw new Error (
143
+ 'TypeScript parser services not available. Rule "n/no-sync" is configured to use "ignores" option with a non-string value. This requires TypeScript parser services to be available.'
144
+ )
145
+ }
146
+
147
+ type =
148
+ type === undefined
149
+ ? getTypeOfNode ( node , parserServices )
150
+ : type
151
+
152
+ fullName =
153
+ fullName === undefined
154
+ ? getFullTypeName ( type )
155
+ : fullName
156
+
157
+ if (
158
+ typeMatchesSpecifier (
159
+ parserServices . program ,
160
+ ignore ,
161
+ type
162
+ ) &&
163
+ ( ignore . name === undefined ||
164
+ ignore . name . includes ( fullName ?? node . name ) )
165
+ ) {
166
+ return
167
+ }
62
168
}
63
169
64
170
context . report ( {
65
171
node : node . parent ,
66
172
messageId : "noSync" ,
67
173
data : {
68
- propertyName : node . name ,
174
+ propertyName : fullName ?? node . name ,
69
175
} ,
70
176
} )
71
177
} ,
0 commit comments