Skip to content

Commit dc6e8cd

Browse files
authored
feat: upgrade @typescript-eslint/utils to v6 (#1508)
* feat: upgrade to `@typescript-eslint/utils` v6 * chore: remove invalid module declarations * fix: update `docs` meta property on rules * chore: cast `context.settings` for now * refactor: update deprecated method calls * fix: update types for `JSONSchema4` * test(unbound-method): update cases and structure * test(unbound-method): use `TSESLint.RuleTester` * test(unbound-method): include filename for invalid cases * test(unbound-method): don't run broken test when using `@typescript-eslint` v5 * fix: address deprecation warning
1 parent 8408db9 commit dc6e8cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+219
-209
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
]
9696
},
9797
"dependencies": {
98-
"@typescript-eslint/utils": "^5.10.0"
98+
"@typescript-eslint/utils": "^6.0.0"
9999
},
100100
"devDependencies": {
101101
"@babel/cli": "^7.4.4",

src/index.ts

+1-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readdirSync } from 'fs';
22
import { join, parse } from 'path';
3-
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
3+
import type { TSESLint } from '@typescript-eslint/utils';
44
import {
55
name as packageName,
66
version as packageVersion,
@@ -12,39 +12,6 @@ type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
1212
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
1313
};
1414

15-
// v5 of `@typescript-eslint/experimental-utils` removed this
16-
declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
17-
export interface RuleMetaDataDocs {
18-
category: 'Best Practices' | 'Possible Errors';
19-
}
20-
}
21-
22-
declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
23-
export interface SourceCode {
24-
/**
25-
* Returns the scope of the given node.
26-
* This information can be used track references to variables.
27-
* @since 8.37.0
28-
*/
29-
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
30-
/**
31-
* Returns an array of the ancestors of the given node, starting at
32-
* the root of the AST and continuing through the direct parent of the current node.
33-
* This array does not include the currently-traversed node itself.
34-
* @since 8.38.0
35-
*/
36-
getAncestors(node: TSESTree.Node): TSESTree.Node[];
37-
/**
38-
* Returns a list of variables declared by the given node.
39-
* This information can be used to track references to variables.
40-
* @since 8.38.0
41-
*/
42-
getDeclaredVariables(
43-
node: TSESTree.Node,
44-
): readonly TSESLint.Scope.Variable[];
45-
}
46-
}
47-
4815
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
4916
/* istanbul ignore next */
5017
const interopRequireDefault = (obj: any): { default: any } =>

src/rules/__tests__/unbound-method.test.ts

+92-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 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';
34
import dedent from 'dedent';
45
import type { MessageIds, Options } from '../unbound-method';
56

@@ -9,15 +10,35 @@ function getFixturesRootDir(): string {
910

1011
const 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+
2142
const 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

187210
ruleTester.run('unbound-method jest edition', requireRule(false), {
188-
valid: validTestCases,
189-
invalid: invalidTestCases,
211+
valid: withFixtureFilename(validTestCases),
212+
invalid: withFixtureFilename(invalidTestCases),
190213
});
191214

192215
function addContainsMethodsClass(code: string): string {
@@ -225,11 +248,14 @@ function addContainsMethodsClassInvalid(
225248
}
226249

227250
ruleTester.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 {
455481
const oc = new OtherClass();
456482
oc.superLogThis();
457483
`,
458-
],
459-
invalid: [
484+
]),
485+
invalid: withFixtureFilename([
460486
{
461487
code: `
462488
class 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
});

src/rules/consistent-test-it.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export default createRule<
3535
name: __filename,
3636
meta: {
3737
docs: {
38-
category: 'Best Practices',
3938
description: 'Enforce `test` and `it` usage conventions',
40-
recommended: false,
4139
},
4240
fixable: 'code',
4341
messages: {
@@ -51,9 +49,11 @@ export default createRule<
5149
type: 'object',
5250
properties: {
5351
fn: {
52+
type: 'string',
5453
enum: [TestCaseName.it, TestCaseName.test],
5554
},
5655
withinDescribe: {
56+
type: 'string',
5757
enum: [TestCaseName.it, TestCaseName.test],
5858
},
5959
},

src/rules/expect-expect.ts

-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ export default createRule<
5454
name: __filename,
5555
meta: {
5656
docs: {
57-
category: 'Best Practices',
5857
description: 'Enforce assertion to be made in a test body',
59-
recommended: 'warn',
6058
},
6159
messages: {
6260
noAssertions: 'Test has no assertions',

src/rules/max-expects.ts

-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export default createRule({
1010
name: __filename,
1111
meta: {
1212
docs: {
13-
category: 'Best Practices',
1413
description: 'Enforces a maximum number assertion calls in a test body',
15-
recommended: false,
1614
},
1715
messages: {
1816
exceededMaxAssertion:

src/rules/max-nested-describe.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ export default createRule({
55
name: __filename,
66
meta: {
77
docs: {
8-
category: 'Best Practices',
98
description: 'Enforces a maximum depth to nested describe calls',
10-
recommended: false,
119
},
1210
messages: {
1311
exceededMaxDepth:

src/rules/no-alias-methods.ts

-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ export default createRule({
99
name: __filename,
1010
meta: {
1111
docs: {
12-
category: 'Best Practices',
1312
description: 'Disallow alias methods',
14-
recommended: 'error',
1513
},
1614
messages: {
1715
replaceAlias: `Replace {{ alias }}() with its canonical name of {{ canonical }}()`,

src/rules/no-commented-out-tests.ts

-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ export default createRule({
1111
name: __filename,
1212
meta: {
1313
docs: {
14-
category: 'Best Practices',
1514
description: 'Disallow commented out tests',
16-
recommended: 'warn',
1715
},
1816
messages: {
1917
commentedTests: 'Some tests seem to be commented',

src/rules/no-conditional-expect.ts

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export default createRule({
2020
meta: {
2121
docs: {
2222
description: 'Disallow calling `expect` conditionally',
23-
category: 'Best Practices',
24-
recommended: 'error',
2523
},
2624
messages: {
2725
conditionalExpect: 'Avoid calling `expect` conditionally`',

src/rules/no-conditional-in-test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ export default createRule({
66
meta: {
77
docs: {
88
description: 'Disallow conditional logic in tests',
9-
category: 'Best Practices',
10-
recommended: false,
119
},
1210
messages: {
1311
conditionalInTest: 'Avoid having conditionals in tests',

src/rules/no-confusing-set-timeout.ts

-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ export default createRule({
1818
name: __filename,
1919
meta: {
2020
docs: {
21-
category: 'Best Practices',
2221
description: 'Disallow confusing usages of jest.setTimeout',
23-
recommended: false,
2422
},
2523
messages: {
2624
globalSetTimeout: '`jest.setTimeout` should be call in `global` scope',

src/rules/no-deprecated-functions.ts

-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ export default createRule({
2828
name: __filename,
2929
meta: {
3030
docs: {
31-
category: 'Best Practices',
3231
description: 'Disallow use of deprecated functions',
33-
recommended: 'error',
3432
},
3533
messages: {
3634
deprecatedFunction:

src/rules/no-disabled-tests.ts

-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export default createRule({
1010
name: __filename,
1111
meta: {
1212
docs: {
13-
category: 'Best Practices',
1413
description: 'Disallow disabled tests',
15-
recommended: 'warn',
1614
},
1715
messages: {
1816
missingFunction: 'Test is missing function argument',

src/rules/no-done-callback.ts

-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ export default createRule({
3737
name: __filename,
3838
meta: {
3939
docs: {
40-
category: 'Best Practices',
4140
description: 'Disallow using a callback in asynchronous tests and hooks',
42-
recommended: 'error',
4341
},
4442
messages: {
4543
noDoneCallback:

src/rules/no-duplicate-hooks.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ export default createRule({
44
name: __filename,
55
meta: {
66
docs: {
7-
category: 'Best Practices',
87
description: 'Disallow duplicate setup and teardown hooks',
9-
recommended: false,
108
},
119
messages: {
1210
noDuplicateHook: 'Duplicate {{hook}} in describe block',

src/rules/no-export.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ export default createRule({
55
name: __filename,
66
meta: {
77
docs: {
8-
category: 'Best Practices',
98
description: 'Disallow using `exports` in files containing tests',
10-
recommended: 'error',
119
},
1210
messages: {
1311
unexpectedExport: `Do not export from a test file`,

src/rules/no-focused-tests.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ export default createRule({
55
name: __filename,
66
meta: {
77
docs: {
8-
category: 'Best Practices',
98
description: 'Disallow focused tests',
10-
recommended: 'error',
119
},
1210
messages: {
1311
focusedTest: 'Unexpected focused test',

0 commit comments

Comments
 (0)