From bbd2695818280e7fb0c5eddcc43bfc6bed4a7ace Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Thu, 2 Feb 2023 12:07:01 +0100 Subject: [PATCH 1/2] fix(eslint-plugin): fix key-spacing when type starts on next line --- .../eslint-plugin/src/rules/key-spacing.ts | 13 ++++++-- .../tests/rules/key-spacing.test.ts | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/key-spacing.ts b/packages/eslint-plugin/src/rules/key-spacing.ts index 2562107ee050..9d1349861bf0 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 canBeAligned( + 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 (!canBeAligned(node)) { continue; } const { typeAnnotation } = node; @@ -356,7 +365,7 @@ export default util.createRule({ ? options.multiLine.mode : options.mode) ?? 'strict'; - if (isKeyTypeNode(node)) { + if (canBeAligned(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..65125d3aa4cc 100644 --- a/packages/eslint-plugin/tests/rules/key-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/key-spacing.test.ts @@ -14,6 +14,37 @@ ruleTester.run('key-spacing', rule, { // align: value { code: ` +interface X { + x: + | number + | string; +} + `, + options: [{ align: 'value' }], + }, + { + code: ` +interface X { + abcdef: string; + x: + | number + | string; + defgh: string; +} + `, + options: [{ align: 'value' }], + }, + { + code: ` +interface X { + x: + | number; abcd: string; +} + `, + options: [{ align: 'value' }], + }, + { + code: ` interface X { a: number; abc: string From bdb4594665f793fb02cdb1d9122845ced864b55c Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Thu, 2 Feb 2023 12:10:22 +0100 Subject: [PATCH 2/2] fixup! fix(eslint-plugin): fix key-spacing when type starts on next line --- packages/eslint-plugin/src/rules/key-spacing.ts | 6 +++--- .../eslint-plugin/tests/rules/key-spacing.test.ts | 13 ++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/key-spacing.ts b/packages/eslint-plugin/src/rules/key-spacing.ts index 9d1349861bf0..2d4d3f5c11df 100644 --- a/packages/eslint-plugin/src/rules/key-spacing.ts +++ b/packages/eslint-plugin/src/rules/key-spacing.ts @@ -85,7 +85,7 @@ export default util.createRule({ ); } - function canBeAligned( + function isApplicable( node: TSESTree.Node, ): node is KeyTypeNodeWithTypeAnnotation { return ( @@ -290,7 +290,7 @@ export default util.createRule({ } for (const node of group) { - if (!canBeAligned(node)) { + if (!isApplicable(node)) { continue; } const { typeAnnotation } = node; @@ -365,7 +365,7 @@ export default util.createRule({ ? options.multiLine.mode : options.mode) ?? 'strict'; - if (canBeAligned(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 65125d3aa4cc..e7828e01ffe0 100644 --- a/packages/eslint-plugin/tests/rules/key-spacing.test.ts +++ b/packages/eslint-plugin/tests/rules/key-spacing.test.ts @@ -11,7 +11,7 @@ const ruleTester = new RuleTester({ ruleTester.run('key-spacing', rule, { valid: [ - // align: value + // non-applicable { code: ` interface X { @@ -24,6 +24,16 @@ interface X { }, { code: ` +interface X { + x: + | number + | string; +} + `, + options: [{}], + }, + { + code: ` interface X { abcdef: string; x: @@ -43,6 +53,7 @@ interface X { `, options: [{ align: 'value' }], }, + // align: value { code: ` interface X { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy