Skip to content

Commit

Permalink
fix(eslint-plugin): fix key-spacing when type starts on next line (#6412
Browse files Browse the repository at this point in the history
)

* fix(eslint-plugin): fix key-spacing when type starts on next line

* fixup! fix(eslint-plugin): fix key-spacing when type starts on next line
  • Loading branch information
coyotte508 committed Feb 10, 2023
1 parent 113640e commit 3eb2eed
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/eslint-plugin/src/rules/key-spacing.ts
Expand Up @@ -85,6 +85,15 @@ export default util.createRule<Options, MessageIds>({
);
}

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
*/
Expand Down Expand Up @@ -281,7 +290,7 @@ export default util.createRule<Options, MessageIds>({
}

for (const node of group) {
if (!isKeyTypeNode(node)) {
if (!isApplicable(node)) {
continue;
}
const { typeAnnotation } = node;
Expand Down Expand Up @@ -356,7 +365,7 @@ export default util.createRule<Options, MessageIds>({
? options.multiLine.mode
: options.mode) ?? 'strict';

if (isKeyTypeNode(node)) {
if (isApplicable(node)) {
checkBeforeColon(node, expectedWhitespaceBeforeColon, mode);
checkAfterColon(node, expectedWhitespaceAfterColon, mode);
}
Expand Down
42 changes: 42 additions & 0 deletions packages/eslint-plugin/tests/rules/key-spacing.test.ts
Expand Up @@ -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: `
Expand Down

0 comments on commit 3eb2eed

Please sign in to comment.