From 0e57f7beca697a82ce8a1676fe3c3daf5961d806 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 27 Jun 2019 21:35:25 -0700 Subject: [PATCH] reuse invalid tests to test fixer --- .../src/rules/no-explicit-any.ts | 4 +-- .../tests/rules/no-explicit-any.test.ts | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-explicit-any.ts b/packages/eslint-plugin/src/rules/no-explicit-any.ts index 363f7fb841c..f5a55593361 100644 --- a/packages/eslint-plugin/src/rules/no-explicit-any.ts +++ b/packages/eslint-plugin/src/rules/no-explicit-any.ts @@ -5,13 +5,13 @@ import { import * as util from '../util'; import { TSESLint } from '@typescript-eslint/experimental-utils'; -type Options = [ +export type Options = [ { fixToUnknown?: boolean; ignoreRestArgs?: boolean; } ]; -type MessageIds = 'unexpectedAny'; +export type MessageIds = 'unexpectedAny'; export default util.createRule({ name: 'no-explicit-any', diff --git a/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts b/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts index cf596ffecbc..b27cc8111bc 100644 --- a/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts +++ b/packages/eslint-plugin/tests/rules/no-explicit-any.test.ts @@ -1,5 +1,8 @@ -import rule from '../../src/rules/no-explicit-any'; +import rule, { MessageIds, Options } from '../../src/rules/no-explicit-any'; import { RuleTester } from '../RuleTester'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; + +type InvalidTestCase = TSESLint.InvalidTestCase; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -187,7 +190,7 @@ type obj = { options: [{ ignoreRestArgs: true }], }, ], - invalid: [ + invalid: ([ { code: 'const number: any = 1', errors: [ @@ -784,5 +787,23 @@ type obj = { }, ], }, - ], + ] as InvalidTestCase[]).reduce((acc, testCase) => { + acc.push(testCase); + const options = testCase.options || []; + const code = `// fixToUnknown: true\n${testCase.code}`; + acc.push({ + code, + output: code.replace(/any/g, 'unknown'), + options: [{ ...options[0], fixToUnknown: true }], + errors: testCase.errors.map(err => { + if (err.line === undefined) { + return err; + } + + return { ...err, line: err.line + 1 }; + }), + }); + + return acc; + }, []), });