Skip to content

Commit

Permalink
fix(eslint-plugin): [key-spacing] consider properties with parens and…
Browse files Browse the repository at this point in the history
… comments (#7525)

* fix(eslint-plugin): [key-spacing] consider properties with parens and comments

* test: add case for `(string | number)`
  • Loading branch information
auvred committed Sep 3, 2023
1 parent e79e027 commit 7012279
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/eslint-plugin/src/rules/key-spacing.ts
Expand Up @@ -163,24 +163,27 @@ export default util.createRule<Options, MessageIds>({
mode: 'minimum' | 'strict',
): void {
const { typeAnnotation } = node;
const colon = typeAnnotation.loc.start.column;
const typeStart = typeAnnotation.typeAnnotation.loc.start.column;
const difference = typeStart - colon - 1 - expectedWhitespaceAfterColon;
const colonToken = sourceCode.getFirstToken(typeAnnotation)!;
const typeStart = sourceCode.getTokenAfter(colonToken, {
includeComments: true,
})!.loc.start.column;
const difference =
typeStart -
colonToken.loc.start.column -
1 -
expectedWhitespaceAfterColon;
if (mode === 'strict' ? difference : difference < 0) {
context.report({
node,
messageId: difference > 0 ? 'extraValue' : 'missingValue',
fix: fixer => {
if (difference > 0) {
return fixer.removeRange([
typeAnnotation.typeAnnotation.range[0] - difference,
typeAnnotation.typeAnnotation.range[0],
colonToken.range[1],
colonToken.range[1] + difference,
]);
}
return fixer.insertTextBefore(
typeAnnotation.typeAnnotation,
' '.repeat(-difference),
);
return fixer.insertTextAfter(colonToken, ' '.repeat(-difference));
},
data: {
computed: '',
Expand Down
135 changes: 135 additions & 0 deletions packages/eslint-plugin/tests/rules/key-spacing.test.ts
Expand Up @@ -580,6 +580,46 @@ interface X { a:number; abc:string; };
},
],
},
{
code: `
class Foo {
a: (b)
}
`,
},
{
code: `
interface Foo {
a: (b)
}
`,
},
{
code: `
class Foo {
a: /** comment */ b
}
`,
},
{
code: `
class Foo {
a: ( b)
}
`,
},
{
code: `
class Foo { a: (b) }
`,
},
{
code: `
class Foo {
a?: (string | number)
}
`,
},
],
invalid: [
// align: value
Expand Down Expand Up @@ -1317,5 +1357,100 @@ class Wacky {
{ messageId: 'missingValue' },
],
},
{
code: `
class Foo {
a: (b)
}
`,
output: `
class Foo {
a: (b)
}
`,
errors: [{ messageId: 'extraValue' }],
},
{
code: `
interface Foo {
a: (b)
}
`,
output: `
interface Foo {
a: (b)
}
`,
errors: [{ messageId: 'extraValue' }],
},
{
code: `
class Foo {
a: /** comment */ b
}
`,
output: `
class Foo {
a: /** comment */ b
}
`,
errors: [{ messageId: 'extraValue' }],
},
{
code: `
class Foo {
a: ( b)
}
`,
output: `
class Foo {
a: ( b)
}
`,
errors: [{ messageId: 'extraValue' }],
},
{
code: `
interface X { a:(number); };
`,
output: `
interface X { a : (number); };
`,
options: [
{
singleLine: { beforeColon: true, afterColon: true, mode: 'strict' },
multiLine: { beforeColon: true, afterColon: true },
},
],
errors: [{ messageId: 'missingKey' }, { messageId: 'missingValue' }],
},
{
code: `
interface X { a:/** comment */ number; };
`,
output: `
interface X { a : /** comment */ number; };
`,
options: [
{
singleLine: { beforeColon: true, afterColon: true, mode: 'strict' },
multiLine: { beforeColon: true, afterColon: true },
},
],
errors: [{ messageId: 'missingKey' }, { messageId: 'missingValue' }],
},
{
code: `
class Foo {
a: (string | number)
}
`,
output: `
class Foo {
a: (string | number)
}
`,
errors: [{ messageId: 'extraValue' }],
},
],
});

0 comments on commit 7012279

Please sign in to comment.