diff --git a/packages/eslint-plugin/src/rules/key-spacing.ts b/packages/eslint-plugin/src/rules/key-spacing.ts index 2562107ee050..2d4d3f5c11df 100644 --- a/packages/eslint-plugin/src/rules/key-spacing.ts +++ b/packages/eslint-plugin/src/rules/key-spacing.ts @@ -85,6 +85,15 @@ export default util.createRule({ ); } + function isApplicable( + node: TSESTree.Node, + ): node is KeyTypeNodeWithTypeAnnotation { + return ( + isKeyTypeNode(node) && + node.typeAnnotation.loc.start.line === node.loc.end.line + ); + } + /** * To handle index signatures, to get the whole text for the parameters */ @@ -281,7 +290,7 @@ export default util.createRule({ } for (const node of group) { - if (!isKeyTypeNode(node)) { + if (!isApplicable(node)) { continue; } const { typeAnnotation } = node; @@ -356,7 +365,7 @@ export default util.createRule({ ? options.multiLine.mode : options.mode) ?? 'strict'; - if (isKeyTypeNode(node)) { + if (isApplicable(node)) { checkBeforeColon(node, expectedWhitespaceBeforeColon, mode); checkAfterColon(node, expectedWhitespaceAfterColon, mode); } diff --git a/packages/eslint-plugin/tests/rules/key-spacing.test.ts b/packages/eslint-plugin/tests/rules/key-spacing.test.ts index 40206258671c..e7828e01ffe0 100644 --- a/packages/eslint-plugin/tests/rules/key-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/key-spacing.test.ts @@ -11,6 +11,48 @@ const ruleTester = new RuleTester({ ruleTester.run('key-spacing', rule, { valid: [ + // non-applicable + { + code: ` +interface X { + x: + | number + | string; +} + `, + options: [{ align: 'value' }], + }, + { + code: ` +interface X { + x: + | number + | string; +} + `, + options: [{}], + }, + { + code: ` +interface X { + abcdef: string; + x: + | number + | string; + defgh: string; +} + `, + options: [{ align: 'value' }], + }, + { + code: ` +interface X { + x: + | number; abcd: string; +} + `, + options: [{ align: 'value' }], + }, // align: value { code: `