Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove RuleTester in /utils in favour of the new /rule-tester package #6816

Merged
merged 5 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,8 @@
/* eslint-disable @typescript-eslint/internal/prefer-ast-types-enum */
import { RuleTester } from '@typescript-eslint/rule-tester';

import rule from '../../src/rules/no-poorly-typed-ts-props';
import { getFixturesRootDir, RuleTester } from '../RuleTester';
import { getFixturesRootDir } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
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/no-typescript-default-import';
import { batchedSingleLineTests, RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand All @@ -16,30 +17,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,6 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import rule from '../../src/rules/no-typescript-estree-import';
import { batchedSingleLineTests, RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand All @@ -14,48 +15,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,7 @@
import { RuleTester } from '@typescript-eslint/rule-tester';

import rule from '../../src/rules/plugin-test-formatting';
import { getFixturesRootDir, RuleTester } from '../RuleTester';
import { getFixturesRootDir } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { RuleTester } from '@typescript-eslint/rule-tester';
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';

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as util from '../util';

// intentionally mirroring the options
type MessageIds =
export type MessageIds =
| 'as'
| 'angle-bracket'
| 'never'
Expand All @@ -19,7 +19,7 @@ type OptUnion =
| {
assertionStyle: 'never';
};
type Options = [OptUnion];
export type Options = readonly [OptUnion];

export default util.createRule<Options, MessageIds>({
name: 'consistent-type-assertions',
Expand Down
87 changes: 75 additions & 12 deletions packages/eslint-plugin/tests/RuleTester.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,80 @@
import { ESLintUtils } from '@typescript-eslint/utils';
import type {
InvalidTestCase,
ValidTestCase,
} from '@typescript-eslint/rule-tester';
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 };
/**
* 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.
*
* @deprecated - DO NOT USE THIS FOR NEW RULES
*/
export function batchedSingleLineTests<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!
*
* @deprecated - DO NOT USE THIS FOR NEW RULES
*/
export function batchedSingleLineTests<
TMessageIds extends string,
TOptions extends readonly unknown[],
>(
test: InvalidTestCase<TMessageIds, TOptions>,
): InvalidTestCase<TMessageIds, TOptions>[];
export function batchedSingleLineTests<
TMessageIds extends string,
TOptions extends readonly unknown[],
>(
options: ValidTestCase<TOptions> | InvalidTestCase<TMessageIds, TOptions>,
): (ValidTestCase<TOptions> | InvalidTestCase<TMessageIds, TOptions>)[] {
// -- eslint counts lines from 1
const lineOffset = options.code.startsWith('\n') ? 2 : 1;
const output =
'output' in options && options.output
? options.output.trim().split('\n')
: null;
return options.code
.trim()
.split('\n')
.map((code, i) => {
const lineNum = i + lineOffset;
const errors =
'errors' in options
? options.errors.filter(e => e.line === lineNum)
: [];
const returnVal = {
...options,
code,
errors: errors.map(e => ({
...e,
line: 1,
})),
};
if (output?.[i]) {
return {
...returnVal,
output: output[i],
};
}
return returnVal;
});
}
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