From ebb33ed8bc29f69ca2a657ec5b31857c0aeb4b56 Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Mon, 20 Sep 2021 18:48:27 -0300 Subject: [PATCH] fix(eslint-plugin): [consistent-type-definitions] correct fix for `export default` (#3899) --- .../src/rules/consistent-type-definitions.ts | 13 ++++++++++ .../rules/consistent-type-definitions.test.ts | 26 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts index f5b2c48a44f..07d848db298 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts @@ -122,6 +122,19 @@ export default util.createRule({ }); } + if ( + node.parent?.type === + AST_NODE_TYPES.ExportDefaultDeclaration + ) { + fixes.push( + fixer.removeRange([node.parent.range[0], node.range[0]]), + fixer.insertTextAfter( + node.body, + `\nexport default ${node.id.name}`, + ), + ); + } + return fixes; }, }); diff --git a/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts b/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts index 105c25724c2..f96672a213f 100644 --- a/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/consistent-type-definitions'; -import { RuleTester, noFormat } from '../RuleTester'; +import { noFormat, RuleTester } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -281,5 +281,29 @@ declare global { }, ], }, + { + // https://github.com/typescript-eslint/typescript-eslint/issues/3894 + code: ` +export default interface Test { + bar(): string; + foo(): number; +} + `, + output: noFormat` +type Test = { + bar(): string; + foo(): number; +} +export default Test + `, + options: ['type'], + errors: [ + { + messageId: 'typeOverInterface', + line: 2, + column: 26, + }, + ], + }, ], });