Skip to content

Commit

Permalink
chore: migrate codebase to use the new rule-tester package
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Apr 27, 2023
1 parent 2ce1c1d commit 3a2d8da
Show file tree
Hide file tree
Showing 158 changed files with 559 additions and 478 deletions.
1 change: 1 addition & 0 deletions packages/eslint-plugin-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@types/prettier": "*",
"@typescript-eslint/rule-tester": "5.59.1",
"@typescript-eslint/scope-manager": "5.59.1",
"@typescript-eslint/type-utils": "5.59.1",
"@typescript-eslint/utils": "5.59.1",
Expand Down
6 changes: 2 additions & 4 deletions packages/eslint-plugin-internal/tests/RuleTester.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ESLintUtils } from '@typescript-eslint/utils';
import path from 'path';

function getFixturesRootDir(): string {
return path.join(__dirname, 'fixtures');
}

const { batchedSingleLineTests, RuleTester } = ESLintUtils;

export { RuleTester, batchedSingleLineTests, getFixturesRootDir };
export { RuleTester } from '@typescript-eslint/rule-tester';
export { getFixturesRootDir };
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/internal/prefer-ast-types-enum */
import rule from '../../src/rules/no-poorly-typed-ts-props';
import { getFixturesRootDir, RuleTester } from '../RuleTester';
import { getFixturesRootDir } from '../RuleTester';
import { RuleTester } from '@typescript-eslint/rule-tester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rule from '../../src/rules/no-typescript-default-import';
import { batchedSingleLineTests, RuleTester } from '../RuleTester';
import { RuleTester } from '@typescript-eslint/rule-tester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand All @@ -16,30 +16,21 @@ ruleTester.run('no-typescript-default-import', rule, {
'import ts = foo;',
"import ts = require('nottypescript');",
],
invalid: batchedSingleLineTests({
code: `
import ts from 'typescript';
import ts, { SyntaxKind } from 'typescript';
import ts = require('typescript');
`,
output: `
import * as ts from 'typescript';
import ts, { SyntaxKind } from 'typescript';
import * as ts from 'typescript';
`,
errors: [
{
messageId: 'noTSDefaultImport',
line: 2,
},
{
messageId: 'noTSDefaultImport',
line: 3,
},
{
messageId: 'noTSDefaultImport',
line: 4,
},
],
}),
invalid: [
{
code: "import ts from 'typescript';",
output: `import * as ts from 'typescript';`,
errors: [{ messageId: 'noTSDefaultImport' }],
},
{
code: "import ts, { SyntaxKind } from 'typescript';",
output: null,
errors: [{ messageId: 'noTSDefaultImport' }],
},
{
code: "import ts = require('typescript');",
output: `import * as ts from 'typescript';`,
errors: [{ messageId: 'noTSDefaultImport' }],
},
],
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rule from '../../src/rules/no-typescript-estree-import';
import { batchedSingleLineTests, RuleTester } from '../RuleTester';
import { RuleTester } from '@typescript-eslint/rule-tester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand All @@ -14,48 +14,36 @@ ruleTester.run('no-typescript-estree-import', rule, {
"import foo from '@typescript-eslint/utils';",
"import * as foo from '@typescript-eslint/utils';",
],
invalid: batchedSingleLineTests({
code: `
import { foo } from '@typescript-eslint/typescript-estree';
import foo from '@typescript-eslint/typescript-estree';
import * as foo from '@typescript-eslint/typescript-estree';
import { foo } from '@typescript-eslint/types';
import foo from '@typescript-eslint/types';
import * as foo from '@typescript-eslint/types';
`,
output: `
import { foo } from '@typescript-eslint/utils';
import foo from '@typescript-eslint/utils';
import * as foo from '@typescript-eslint/utils';
import { foo } from '@typescript-eslint/utils';
import foo from '@typescript-eslint/utils';
import * as foo from '@typescript-eslint/utils';
`,
errors: [
{
messageId: 'dontImportPackage',
line: 2,
},
{
messageId: 'dontImportPackage',
line: 3,
},
{
messageId: 'dontImportPackage',
line: 4,
},
{
messageId: 'dontImportPackage',
line: 5,
},
{
messageId: 'dontImportPackage',
line: 6,
},
{
messageId: 'dontImportPackage',
line: 7,
},
],
}),
invalid: [
{
code: "import { foo } from '@typescript-eslint/typescript-estree';",
output: "import { foo } from '@typescript-eslint/utils';",
errors: [{ messageId: 'dontImportPackage' }],
},
{
code: "import foo from '@typescript-eslint/typescript-estree';",
output: "import foo from '@typescript-eslint/utils';",
errors: [{ messageId: 'dontImportPackage' }],
},
{
code: "import * as foo from '@typescript-eslint/typescript-estree';",
output: "import * as foo from '@typescript-eslint/utils';",
errors: [{ messageId: 'dontImportPackage' }],
},
{
code: "import { foo } from '@typescript-eslint/types';",
output: "import { foo } from '@typescript-eslint/utils';",
errors: [{ messageId: 'dontImportPackage' }],
},
{
code: "import foo from '@typescript-eslint/types';",
output: "import foo from '@typescript-eslint/utils';",
errors: [{ messageId: 'dontImportPackage' }],
},
{
code: "import * as foo from '@typescript-eslint/types';",
output: "import * as foo from '@typescript-eslint/utils';",
errors: [{ messageId: 'dontImportPackage' }],
},
],
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import rule from '../../src/rules/plugin-test-formatting';
import { getFixturesRootDir, RuleTester } from '../RuleTester';
import { getFixturesRootDir } from '../RuleTester';
import { RuleTester } from '@typescript-eslint/rule-tester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DefinitionType } from '@typescript-eslint/scope-manager';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';

import rule from '../../src/rules/prefer-ast-types-enum';
import { batchedSingleLineTests, RuleTester } from '../RuleTester';
import { RuleTester } from '@typescript-eslint/rule-tester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand All @@ -29,33 +29,42 @@ ruleTester.run('prefer-ast-types-enum', rule, {
}
`,
],
invalid: batchedSingleLineTests({
code: `
node.type === 'Literal';
node.type === 'Keyword';
node.type === 'Parameter';
`,
output: `
node.type === AST_NODE_TYPES.Literal;
node.type === AST_TOKEN_TYPES.Keyword;
node.type === DefinitionType.Parameter;
`,
errors: [
{
data: { enumName: 'AST_NODE_TYPES', literal: AST_NODE_TYPES.Literal },
messageId: 'preferEnum',
line: 2,
},
{
data: { enumName: 'AST_TOKEN_TYPES', literal: AST_TOKEN_TYPES.Keyword },
messageId: 'preferEnum',
line: 3,
},
{
data: { enumName: 'DefinitionType', literal: DefinitionType.Parameter },
messageId: 'preferEnum',
line: 4,
},
],
}),
invalid: [
{
code: "node.type === 'Literal';",
output: 'node.type === AST_NODE_TYPES.Literal;',
errors: [
{
data: { enumName: 'AST_NODE_TYPES', literal: AST_NODE_TYPES.Literal },
messageId: 'preferEnum',
},
],
},
{
code: "node.type === 'Keyword';",
output: 'node.type === AST_TOKEN_TYPES.Keyword;',
errors: [
{
data: {
enumName: 'AST_TOKEN_TYPES',
literal: AST_TOKEN_TYPES.Keyword,
},
messageId: 'preferEnum',
},
],
},
{
code: "node.type === 'Parameter';",
output: 'node.type === DefinitionType.Parameter;',
errors: [
{
data: {
enumName: 'DefinitionType',
literal: DefinitionType.Parameter,
},
messageId: 'preferEnum',
},
],
},
],
});
1 change: 1 addition & 0 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@types/natural-compare": "*",
"@types/prettier": "*",
"@typescript-eslint/rule-schema-to-typescript-types": "5.59.1",
"@typescript-eslint/rule-tester": "5.59.1",
"cross-fetch": "*",
"jest-specific-snapshot": "*",
"json-schema": "*",
Expand Down
47 changes: 35 additions & 12 deletions packages/eslint-plugin/tests/RuleTester.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import { ESLintUtils } from '@typescript-eslint/utils';
import type {
InvalidTestCase,
ValidTestCase,
} from '@typescript-eslint/rule-tester';
import { batchedSingleLineTests as batchedSingleLineTestsBase } from '@typescript-eslint/utils/eslint-utils';
import * as path from 'path';

function getFixturesRootDir(): string {
export function getFixturesRootDir(): string {
return path.join(__dirname, 'fixtures');
}

const { batchedSingleLineTests } = ESLintUtils;
export {
RuleTester,
RunTests,
ValidTestCase,
InvalidTestCase,
noFormat,
} from '@typescript-eslint/utils/eslint-utils/rule-tester';

export { batchedSingleLineTests, getFixturesRootDir };
// TODO - migrate the codebase off of this utility
// this re-export exists to re-type the utility with the types from @typescript-eslint/rule-tester
export const batchedSingleLineTests = batchedSingleLineTestsBase as {
/**
* Converts a batch of single line tests into a number of separate test cases.
* This makes it easier to write tests which use the same options.
*
* Why wouldn't you just leave them as one test?
* Because it makes the test error messages harder to decipher.
* This way each line will fail separately, instead of them all failing together.
*/
<TOptions extends Readonly<unknown[]>>(
test: ValidTestCase<TOptions>,
): ValidTestCase<TOptions>[];
/**
* Converts a batch of single line tests into a number of separate test cases.
* This makes it easier to write tests which use the same options.
*
* Why wouldn't you just leave them as one test?
* Because it makes the test error messages harder to decipher.
* This way each line will fail separately, instead of them all failing together.
*
* Make sure you have your line numbers correct for error reporting, as it will match
* the line numbers up with the split tests!
*/
<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(
test: InvalidTestCase<TMessageIds, TOptions>,
): InvalidTestCase<TMessageIds, TOptions>[];
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { noFormat, RuleTester } from '@typescript-eslint/rule-tester';

import { getESLintCoreRule } from '../../src/util/getESLintCoreRule';
import { noFormat, RuleTester } from '../RuleTester';

const rule = getESLintCoreRule('arrow-parens');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import { getESLintCoreRule } from '../../src/util/getESLintCoreRule';
import { RuleTester } from '../RuleTester';

const rule = getESLintCoreRule('no-dupe-args');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import { getESLintCoreRule } from '../../src/util/getESLintCoreRule';
import { RuleTester } from '../RuleTester';

const rule = getESLintCoreRule('no-implicit-globals');
const ruleTester = new RuleTester({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import { getESLintCoreRule } from '../../src/util/getESLintCoreRule';
import { RuleTester } from '../RuleTester';

const rule = getESLintCoreRule('no-restricted-globals');

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import { getESLintCoreRule } from '../../src/util/getESLintCoreRule';
import { RuleTester } from '../RuleTester';

const rule = getESLintCoreRule('no-undef');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import { getESLintCoreRule } from '../../src/util/getESLintCoreRule';
import { RuleTester } from '../RuleTester';

const rule = getESLintCoreRule('prefer-const');

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/tests/eslint-rules/strict.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import { getESLintCoreRule } from '../../src/util/getESLintCoreRule';
import { RuleTester } from '../RuleTester';

const rule = getESLintCoreRule('strict');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import rule from '../../src/rules/adjacent-overload-signatures';
import { RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/tests/rules/array-type.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as parser from '@typescript-eslint/parser';
import { RuleTester } from '@typescript-eslint/rule-tester';
import { TSESLint } from '@typescript-eslint/utils';

import type { OptionString } from '../../src/rules/array-type';
import rule from '../../src/rules/array-type';
import { RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down

0 comments on commit 3a2d8da

Please sign in to comment.