@@ -34,17 +34,17 @@ describe("The 'getFunctionNameWithKind' function", () => {
3434 "({ foo: function foo() {} })" : "method 'foo'" ,
3535 "({ foo: function() {} })" : "method 'foo'" ,
3636 "({ ['foo']: function() {} })" : "method 'foo'" ,
37- "({ [foo]: function() {} })" : "method" ,
37+ "({ [foo]: function() {} })" : "method [foo] " ,
3838 "({ foo() {} })" : "method 'foo'" ,
3939 "({ foo: function* foo() {} })" : "generator method 'foo'" ,
4040 "({ foo: function*() {} })" : "generator method 'foo'" ,
4141 "({ ['foo']: function*() {} })" : "generator method 'foo'" ,
42- "({ [foo]: function*() {} })" : "generator method" ,
42+ "({ [foo]: function*() {} })" : "generator method [foo] " ,
4343 "({ *foo() {} })" : "generator method 'foo'" ,
4444 "({ foo: async function foo() {} })" : "async method 'foo'" ,
4545 "({ foo: async function() {} })" : "async method 'foo'" ,
4646 "({ ['foo']: async function() {} })" : "async method 'foo'" ,
47- "({ [foo]: async function() {} })" : "async method" ,
47+ "({ [foo]: async function() {} })" : "async method [foo] " ,
4848 "({ async foo() {} })" : "async method 'foo'" ,
4949 "({ get foo() {} })" : "getter 'foo'" ,
5050 "({ set foo(a) {} })" : "setter 'foo'" ,
@@ -55,9 +55,9 @@ describe("The 'getFunctionNameWithKind' function", () => {
5555 "class A { ['foo']() {} }" : "method 'foo'" ,
5656 "class A { *['foo']() {} }" : "generator method 'foo'" ,
5757 "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] " ,
6161 "class A { get foo() {} }" : "getter 'foo'" ,
6262 "class A { set foo(a) {} }" : "setter 'foo'" ,
6363 "class A { static foo() {} }" : "static method 'foo'" ,
@@ -69,64 +69,67 @@ describe("The 'getFunctionNameWithKind' function", () => {
6969
7070 semver . gte ( eslint . CLIEngine . version , "7.0.0" )
7171 ? {
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",
7979 "class A { static *#foo() {} }" :
80- "private static generator method ' #foo' " ,
80+ "static private generator method #foo" ,
8181 "class A { static async #foo() {} }" :
82- "private static async method ' #foo' " ,
82+ "static private async method #foo" ,
8383 "class A { static get #foo() {} }" :
84- "private static getter ' #foo' " ,
84+ "static private getter #foo" ,
8585 "class A { static set #foo(a) {} }" :
86- "private static setter ' #foo' " ,
86+ "static private setter #foo" ,
8787 "class A { foo = function() {} }" : "method 'foo'" ,
88- "class A { foo = () => {} }" : "arrow function 'foo'" ,
88+ "class A { foo = () => {} }" : "method 'foo'" ,
8989 "class A { foo = function*() {} }" : "generator method 'foo'" ,
9090 "class A { foo = async function() {} }" : "async method 'foo'" ,
9191 "class A { ['foo'] = function() {} }" : "method 'foo'" ,
92- "class A { ['foo'] = () => {} }" : "arrow function 'foo'" ,
92+ "class A { ['foo'] = () => {} }" : "method 'foo'" ,
9393 "class A { ['foo'] = function*() {} }" :
9494 "generator method 'foo'" ,
9595 "class A { ['foo'] = async function() {} }" :
9696 "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]" ,
101103 "class A { static foo = function() {} }" :
102104 "static method 'foo'" ,
103- "class A { static foo = () => {} }" :
104- "static arrow function 'foo'" ,
105+ "class A { static foo = () => {} }" : "static method 'foo'" ,
105106 "class A { static foo = function*() {} }" :
106107 "static generator method 'foo'" ,
107108 "class A { static foo = async function() {} }" :
108109 "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" ,
112112 "class A { #foo = function*() {} }" :
113- "private generator method ' #foo' " ,
113+ "private generator method #foo" ,
114114 "class A { #foo = async function() {} }" :
115- "private async method ' #foo' " ,
115+ "private async method #foo" ,
116116 "class A { static #foo = function() {} }" :
117- "private static method ' #foo' " ,
117+ "static private method #foo" ,
118118 "class A { static #foo = () => {} }" :
119- "private static arrow function ' #foo' " ,
119+ "static private method #foo" ,
120120 "class A { static #foo = function*() {} }" :
121- "private static generator method ' #foo' " ,
121+ "static private generator method #foo" ,
122122 "class A { static #foo = async function() {} }" :
123- "private static async method ' #foo' " ,
123+ "static private async method #foo" ,
124124 }
125125 : { }
126126 )
127127
128128 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 } ".` , ( ) => {
130133 const linter = new eslint . Linter ( )
131134
132135 let actualResult = null
@@ -149,7 +152,36 @@ describe("The 'getFunctionNameWithKind' function", () => {
149152 0 ,
150153 messages [ 0 ] && messages [ 0 ] . message
151154 )
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 )
153185 } )
154186 }
155187} )
0 commit comments