@@ -34,17 +34,17 @@ describe("The 'getFunctionNameWithKind' function", () => {
34
34
"({ foo: function foo() {} })" : "method 'foo'" ,
35
35
"({ foo: function() {} })" : "method 'foo'" ,
36
36
"({ ['foo']: function() {} })" : "method 'foo'" ,
37
- "({ [foo]: function() {} })" : "method" ,
37
+ "({ [foo]: function() {} })" : "method [foo] " ,
38
38
"({ foo() {} })" : "method 'foo'" ,
39
39
"({ foo: function* foo() {} })" : "generator method 'foo'" ,
40
40
"({ foo: function*() {} })" : "generator method 'foo'" ,
41
41
"({ ['foo']: function*() {} })" : "generator method 'foo'" ,
42
- "({ [foo]: function*() {} })" : "generator method" ,
42
+ "({ [foo]: function*() {} })" : "generator method [foo] " ,
43
43
"({ *foo() {} })" : "generator method 'foo'" ,
44
44
"({ foo: async function foo() {} })" : "async method 'foo'" ,
45
45
"({ foo: async function() {} })" : "async method 'foo'" ,
46
46
"({ ['foo']: async function() {} })" : "async method 'foo'" ,
47
- "({ [foo]: async function() {} })" : "async method" ,
47
+ "({ [foo]: async function() {} })" : "async method [foo] " ,
48
48
"({ async foo() {} })" : "async method 'foo'" ,
49
49
"({ get foo() {} })" : "getter 'foo'" ,
50
50
"({ set foo(a) {} })" : "setter 'foo'" ,
@@ -55,9 +55,9 @@ describe("The 'getFunctionNameWithKind' function", () => {
55
55
"class A { ['foo']() {} }" : "method 'foo'" ,
56
56
"class A { *['foo']() {} }" : "generator method 'foo'" ,
57
57
"class A { async ['foo']() {} }" : "async method 'foo'" ,
58
- "class A { [foo]() {} }" : "method" ,
59
- "class A { *[foo]() {} }" : "generator method" ,
60
- "class A { async [foo]() {} }" : "async method" ,
58
+ "class A { [foo]() {} }" : "method [foo] " ,
59
+ "class A { *[foo]() {} }" : "generator method [foo] " ,
60
+ "class A { async [foo]() {} }" : "async method [foo] " ,
61
61
"class A { get foo() {} }" : "getter 'foo'" ,
62
62
"class A { set foo(a) {} }" : "setter 'foo'" ,
63
63
"class A { static foo() {} }" : "static method 'foo'" ,
@@ -69,64 +69,67 @@ describe("The 'getFunctionNameWithKind' function", () => {
69
69
70
70
semver . gte ( eslint . CLIEngine . version , "7.0.0" )
71
71
? {
72
- "class A { #foo() {} }" : "private method ' #foo' " ,
73
- "class A { * #foo() {} }" : "private generator method '#foo'" ,
74
- "class A { async #foo() {} }" : "private async method ' #foo' " ,
75
- "class A { get #foo() {} }" : "private getter ' #foo' " ,
76
- "class A { set #foo(a ) {} }" : "private setter ' #foo' " ,
77
- "class A { static #foo() {} }" :
78
- "private static method ' #foo' ",
72
+ "class A { #foo() {} }" : "private method #foo" ,
73
+ "class A { ' #foo' () {} }" : "method '#foo'" ,
74
+ "class A { * #foo() {} }" : "private generator method #foo" ,
75
+ "class A { async #foo() {} }" : "private async method #foo" ,
76
+ "class A { get #foo() {} }" : "private getter #foo" ,
77
+ "class A { set #foo(a ) {} }" : "private setter #foo" ,
78
+ "class A { static #foo() {} }" : " static private method #foo",
79
79
"class A { static *#foo() {} }" :
80
- "private static generator method ' #foo' " ,
80
+ "static private generator method #foo" ,
81
81
"class A { static async #foo() {} }" :
82
- "private static async method ' #foo' " ,
82
+ "static private async method #foo" ,
83
83
"class A { static get #foo() {} }" :
84
- "private static getter ' #foo' " ,
84
+ "static private getter #foo" ,
85
85
"class A { static set #foo(a) {} }" :
86
- "private static setter ' #foo' " ,
86
+ "static private setter #foo" ,
87
87
"class A { foo = function() {} }" : "method 'foo'" ,
88
- "class A { foo = () => {} }" : "arrow function 'foo'" ,
88
+ "class A { foo = () => {} }" : "method 'foo'" ,
89
89
"class A { foo = function*() {} }" : "generator method 'foo'" ,
90
90
"class A { foo = async function() {} }" : "async method 'foo'" ,
91
91
"class A { ['foo'] = function() {} }" : "method 'foo'" ,
92
- "class A { ['foo'] = () => {} }" : "arrow function 'foo'" ,
92
+ "class A { ['foo'] = () => {} }" : "method 'foo'" ,
93
93
"class A { ['foo'] = function*() {} }" :
94
94
"generator method 'foo'" ,
95
95
"class A { ['foo'] = async function() {} }" :
96
96
"async method 'foo'" ,
97
- "class A { [foo] = function() {} }" : "method" ,
98
- "class A { [foo] = () => {} }" : "arrow function" ,
99
- "class A { [foo] = function*() {} }" : "generator method" ,
100
- "class A { [foo] = async function() {} }" : "async method" ,
97
+ "class A { [foo] = function() {} }" : "method [foo]" ,
98
+ "class A { [foo] = () => {} }" : "method [foo]" ,
99
+ "class A { [foo] = function*() {} }" :
100
+ "generator method [foo]" ,
101
+ "class A { [foo] = async function() {} }" :
102
+ "async method [foo]" ,
101
103
"class A { static foo = function() {} }" :
102
104
"static method 'foo'" ,
103
- "class A { static foo = () => {} }" :
104
- "static arrow function 'foo'" ,
105
+ "class A { static foo = () => {} }" : "static method 'foo'" ,
105
106
"class A { static foo = function*() {} }" :
106
107
"static generator method 'foo'" ,
107
108
"class A { static foo = async function() {} }" :
108
109
"static async method 'foo'" ,
109
- "class A { #foo = function() {} }" : "private method '#foo'" ,
110
- "class A { #foo = () => {} }" :
111
- "private arrow function '#foo'" ,
110
+ "class A { #foo = function() {} }" : "private method #foo" ,
111
+ "class A { #foo = () => {} }" : "private method #foo" ,
112
112
"class A { #foo = function*() {} }" :
113
- "private generator method ' #foo' " ,
113
+ "private generator method #foo" ,
114
114
"class A { #foo = async function() {} }" :
115
- "private async method ' #foo' " ,
115
+ "private async method #foo" ,
116
116
"class A { static #foo = function() {} }" :
117
- "private static method ' #foo' " ,
117
+ "static private method #foo" ,
118
118
"class A { static #foo = () => {} }" :
119
- "private static arrow function ' #foo' " ,
119
+ "static private method #foo" ,
120
120
"class A { static #foo = function*() {} }" :
121
- "private static generator method ' #foo' " ,
121
+ "static private generator method #foo" ,
122
122
"class A { static #foo = async function() {} }" :
123
- "private static async method ' #foo' " ,
123
+ "static private async method #foo" ,
124
124
}
125
125
: { }
126
126
)
127
127
128
128
for ( const key of Object . keys ( expectedResults ) ) {
129
- it ( `should return "${ expectedResults [ key ] } " for "${ key } ".` , ( ) => {
129
+ const expectedResult1 = expectedResults [ key ] . replace ( / \s + \[ .+ ?\] / gu, "" )
130
+ const expectedResult2 = expectedResults [ key ]
131
+
132
+ it ( `should return "${ expectedResult1 } " for "${ key } ".` , ( ) => {
130
133
const linter = new eslint . Linter ( )
131
134
132
135
let actualResult = null
@@ -149,7 +152,36 @@ describe("The 'getFunctionNameWithKind' function", () => {
149
152
0 ,
150
153
messages [ 0 ] && messages [ 0 ] . message
151
154
)
152
- assert . strictEqual ( actualResult , expectedResults [ key ] )
155
+ assert . strictEqual ( actualResult , expectedResult1 )
156
+ } )
157
+
158
+ it ( `should return "${ expectedResult2 } " for "${ key } " if sourceCode is present.` , ( ) => {
159
+ const linter = new eslint . Linter ( )
160
+
161
+ let actualResult = null
162
+ linter . defineRule ( "test" , context => ( {
163
+ ":function" ( node ) {
164
+ actualResult = getFunctionNameWithKind (
165
+ node ,
166
+ context . getSourceCode ( )
167
+ )
168
+ } ,
169
+ } ) )
170
+ const messages = linter . verify ( key , {
171
+ rules : { test : "error" } ,
172
+ parserOptions : {
173
+ ecmaVersion : semver . gte ( eslint . CLIEngine . version , "7.0.0" )
174
+ ? 2022
175
+ : 2018 ,
176
+ } ,
177
+ } )
178
+
179
+ assert . strictEqual (
180
+ messages . length ,
181
+ 0 ,
182
+ messages [ 0 ] && messages [ 0 ] . message
183
+ )
184
+ assert . strictEqual ( actualResult , expectedResult2 )
153
185
} )
154
186
}
155
187
} )
0 commit comments