11import path from 'path' ;
2- import { ESLintUtils , type TSESLint } from '@typescript-eslint/utils' ;
2+ import { version as rawTypeScriptESLintPluginVersion } from '@typescript-eslint/eslint-plugin/package.json' ;
3+ import { TSESLint } from '@typescript-eslint/utils' ;
34import dedent from 'dedent' ;
45import type { MessageIds , Options } from '../unbound-method' ;
56
@@ -9,15 +10,35 @@ function getFixturesRootDir(): string {
910
1011const rootPath = getFixturesRootDir ( ) ;
1112
12- const ruleTester = new ESLintUtils . RuleTester ( {
13- parser : '@typescript-eslint/parser' ,
13+ const ruleTester = new TSESLint . RuleTester ( {
14+ parser : require . resolve ( '@typescript-eslint/parser' ) ,
1415 parserOptions : {
1516 sourceType : 'module' ,
1617 tsconfigRootDir : rootPath ,
1718 project : './tsconfig.json' ,
1819 } ,
1920} ) ;
2021
22+ const fixtureFilename = path . join ( rootPath , 'file.ts' ) ;
23+
24+ const withFixtureFilename = <
25+ T extends Array <
26+ | ( TSESLint . ValidTestCase < Options > | string )
27+ | TSESLint . InvalidTestCase < MessageIds , Options >
28+ > ,
29+ > (
30+ cases : T ,
31+ ) : T extends Array < TSESLint . InvalidTestCase < MessageIds , Options > >
32+ ? Array < TSESLint . InvalidTestCase < MessageIds , Options > >
33+ : Array < TSESLint . ValidTestCase < Options > > => {
34+ // @ts -expect-error this is fine, and will go away later once we upgrade
35+ return cases . map ( code => {
36+ const test = typeof code === 'string' ? { code } : code ;
37+
38+ return { filename : fixtureFilename , ...test } ;
39+ } ) ;
40+ } ;
41+
2142const ConsoleClassAndVariableCode = dedent `
2243 class Console {
2344 log(str) {
@@ -164,8 +185,8 @@ describe('error handling', () => {
164185 } ) ;
165186
166187 describe ( 'when @typescript-eslint/eslint-plugin is not available' , ( ) => {
167- const ruleTester = new ESLintUtils . RuleTester ( {
168- parser : '@typescript-eslint/parser' ,
188+ const ruleTester = new TSESLint . RuleTester ( {
189+ parser : require . resolve ( '@typescript-eslint/parser' ) ,
169190 parserOptions : {
170191 sourceType : 'module' ,
171192 tsconfigRootDir : rootPath ,
@@ -177,16 +198,18 @@ describe('error handling', () => {
177198 'unbound-method jest edition without type service' ,
178199 requireRule ( true ) ,
179200 {
180- valid : validTestCases . concat ( invalidTestCases . map ( ( { code } ) => code ) ) ,
201+ valid : withFixtureFilename (
202+ validTestCases . concat ( invalidTestCases . map ( ( { code } ) => code ) ) ,
203+ ) ,
181204 invalid : [ ] ,
182205 } ,
183206 ) ;
184207 } ) ;
185208} ) ;
186209
187210ruleTester . run ( 'unbound-method jest edition' , requireRule ( false ) , {
188- valid : validTestCases ,
189- invalid : invalidTestCases ,
211+ valid : withFixtureFilename ( validTestCases ) ,
212+ invalid : withFixtureFilename ( invalidTestCases ) ,
190213} ) ;
191214
192215function addContainsMethodsClass ( code : string ) : string {
@@ -225,11 +248,14 @@ function addContainsMethodsClassInvalid(
225248}
226249
227250ruleTester . run ( 'unbound-method' , requireRule ( false ) , {
228- valid : [
251+ valid : withFixtureFilename ( [
229252 'Promise.resolve().then(console.log);' ,
230253 "['1', '2', '3'].map(Number.parseInt);" ,
231254 '[5.2, 7.1, 3.6].map(Math.floor);' ,
232255 'const x = console.log;' ,
256+ ...( parseInt ( rawTypeScriptESLintPluginVersion . split ( '.' ) [ 0 ] , 10 ) >= 6
257+ ? [ 'const x = Object.defineProperty;' ]
258+ : [ ] ) ,
233259 ...[
234260 'instance.bound();' ,
235261 'instance.unbound();' ,
@@ -455,8 +481,8 @@ class OtherClass extends BaseClass {
455481const oc = new OtherClass();
456482oc.superLogThis();
457483 ` ,
458- ] ,
459- invalid : [
484+ ] ) ,
485+ invalid : withFixtureFilename ( [
460486 {
461487 code : `
462488class Console {
@@ -762,5 +788,59 @@ class OtherClass extends BaseClass {
762788 } ,
763789 ] ,
764790 } ,
765- ] ,
791+ {
792+ code : `
793+ const values = {
794+ a() {},
795+ b: () => {},
796+ };
797+
798+ const { a, b } = values;
799+ ` ,
800+ errors : [
801+ {
802+ line : 7 ,
803+ column : 9 ,
804+ endColumn : 10 ,
805+ messageId : 'unboundWithoutThisAnnotation' ,
806+ } ,
807+ ] ,
808+ } ,
809+ {
810+ code : `
811+ const values = {
812+ a() {},
813+ b: () => {},
814+ };
815+
816+ const { a: c } = values;
817+ ` ,
818+ errors : [
819+ {
820+ line : 7 ,
821+ column : 9 ,
822+ endColumn : 10 ,
823+ messageId : 'unboundWithoutThisAnnotation' ,
824+ } ,
825+ ] ,
826+ } ,
827+ {
828+ code : `
829+ const values = {
830+ a() {},
831+ b: () => {},
832+ };
833+
834+ const { b, a } = values;
835+ ` ,
836+ errors : [
837+ {
838+ line : 7 ,
839+ column : 12 ,
840+ endColumn : 13 ,
841+ messageId : 'unboundWithoutThisAnnotation' ,
842+ } ,
843+ ] ,
844+ } ,
845+ ] ) ,
766846} ) ;
0 commit comments