From 0cd911a916805d3b1f8043584e4685f3edd5c427 Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 21 Dec 2021 21:42:40 +0100 Subject: [PATCH] fix(eslint-plugin): [consistent-type-definitions] correct fixer with declare keyword (#4334) --- .../src/rules/consistent-type-definitions.ts | 4 +- .../rules/consistent-type-definitions.test.ts | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts index efb7bdd8d5d..c71d812efd9 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts @@ -58,7 +58,7 @@ export default util.createRule({ const typeNode = node.typeParameters ?? node.id; const fixes: TSESLint.RuleFix[] = []; - const firstToken = sourceCode.getFirstToken(node); + const firstToken = sourceCode.getTokenBefore(node.id); if (firstToken) { fixes.push(fixer.replaceText(firstToken, 'interface')); fixes.push( @@ -98,7 +98,7 @@ export default util.createRule({ const typeNode = node.typeParameters ?? node.id; const fixes: TSESLint.RuleFix[] = []; - const firstToken = sourceCode.getFirstToken(node); + const firstToken = sourceCode.getTokenBefore(node.id); if (firstToken) { fixes.push(fixer.replaceText(firstToken, 'type')); fixes.push( 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 f96672a213f..9bcd9574986 100644 --- a/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts @@ -305,5 +305,51 @@ export default Test }, ], }, + { + // https://github.com/typescript-eslint/typescript-eslint/issues/4333 + code: ` +export declare type Test = { + foo: string; + bar: string; +}; + `, + output: ` +export declare interface Test { + foo: string; + bar: string; +} + `, + options: ['interface'], + errors: [ + { + messageId: 'interfaceOverType', + line: 2, + column: 21, + }, + ], + }, + { + // https://github.com/typescript-eslint/typescript-eslint/issues/4333 + code: ` +export declare interface Test { + foo: string; + bar: string; +} + `, + output: noFormat` +export declare type Test = { + foo: string; + bar: string; +} + `, + options: ['type'], + errors: [ + { + messageId: 'typeOverInterface', + line: 2, + column: 26, + }, + ], + }, ], });