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

feat(eslint-plugin): [type-annotation-spacing] handle space between ? and : #3138

Merged
merged 4 commits into from Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions packages/eslint-plugin/src/rules/type-annotation-spacing.ts
Expand Up @@ -177,6 +177,25 @@ export default util.createRule<Options, MessageIds>({
const { before, after } = getRules(ruleSet, typeAnnotation);

if (type === ':' && previousToken.value === '?') {
if (
!before &&
sourceCode.isSpaceBetween!(previousToken, punctuatorTokenStart)
) {
context.report({
node: punctuatorTokenStart,
messageId: 'unexpectedSpaceBefore',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could add a custom message here for additional clarity.
Something like Unexpected space between the {{previousToken}} and the {{type}}.

data: {
type,
},
fix(fixer) {
return fixer.removeRange([
previousToken.range[1],
punctuatorTokenStart.range[0],
]);
},
});
}

// shift the start to the ?
type = '?:';
punctuatorTokenStart = previousToken;
Expand Down
78 changes: 78 additions & 0 deletions packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts
Expand Up @@ -314,6 +314,10 @@ type Foo = {
code: 'function foo(a : string) {}',
options: [{ after: true, before: true }],
},
{
code: 'function foo(a ? : string) {}',
options: [{ after: true, before: true }],
},
{
code: `
class Foo {
Expand Down Expand Up @@ -4593,6 +4597,54 @@ type Bar = Record<keyof Foo, string>
},
],
},
{
code: 'function foo(a? : string) {}',
output: 'function foo(a?: string) {}',
errors: [
{
messageId: 'unexpectedSpaceBefore',
data: { type: ':' },
line: 1,
column: 17,
},
],
},
{
code: 'function foo(a ? : string) {}',
output: 'function foo(a?: string) {}',
errors: [
{
messageId: 'unexpectedSpaceBefore',
data: { type: '?:' },
line: 1,
column: 16,
},
{
messageId: 'unexpectedSpaceBefore',
data: { type: ':' },
line: 1,
column: 18,
},
],
},
{
code: 'function foo(a ? : string) {}',
output: 'function foo(a?: string) {}',
errors: [
{
messageId: 'unexpectedSpaceBefore',
data: { type: '?:' },
line: 1,
column: 16,
},
{
messageId: 'unexpectedSpaceBefore',
data: { type: ':' },
line: 1,
column: 19,
},
],
},
{
code: `
class Foo {
Expand Down Expand Up @@ -4635,6 +4687,32 @@ class Foo {
},
{
code: `
class Foo {
constructor(message ? : string);
}
`,
output: `
class Foo {
constructor(message?: string);
}
`,
errors: [
{
messageId: 'unexpectedSpaceBefore',
data: { type: '?:' },
line: 3,
column: 25,
},
{
messageId: 'unexpectedSpaceBefore',
data: { type: ':' },
line: 3,
column: 27,
},
],
},
{
code: `
class Foo {
greet(name ?: string) : string { return name; }
}
Expand Down