Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/eslint-plugin/src/rules/key-spacing.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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' }],
},
auvred marked this conversation as resolved.
Show resolved Hide resolved
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
{
code: `
class Foo {
a: (string | number)
}
`,
output: `
class Foo {
a: (string | number)
}
`,
errors: [{ messageId: 'extraValue' }],
},
],
});