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 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
24 changes: 23 additions & 1 deletion packages/eslint-plugin/src/rules/type-annotation-spacing.ts
Expand Up @@ -35,7 +35,8 @@ type MessageIds =
| 'expectedSpaceAfter'
| 'expectedSpaceBefore'
| 'unexpectedSpaceAfter'
| 'unexpectedSpaceBefore';
| 'unexpectedSpaceBefore'
| 'unexpectedSpaceBetween';

const definition = {
type: 'object',
Expand Down Expand Up @@ -122,6 +123,8 @@ export default util.createRule<Options, MessageIds>({
expectedSpaceBefore: "Expected a space before the '{{type}}'.",
unexpectedSpaceAfter: "Unexpected space after the '{{type}}'.",
unexpectedSpaceBefore: "Unexpected space before the '{{type}}'.",
unexpectedSpaceBetween:
"Unexpected space between the '{{previousToken}}' and the '{{type}}'.",
},
schema: [
{
Expand Down Expand Up @@ -177,6 +180,25 @@ export default util.createRule<Options, MessageIds>({
const { before, after } = getRules(ruleSet, typeAnnotation);

if (type === ':' && previousToken.value === '?') {
if (
sourceCode.isSpaceBetweenTokens(previousToken, punctuatorTokenStart)
) {
context.report({
node: punctuatorTokenStart,
messageId: 'unexpectedSpaceBetween',
data: {
type,
previousToken: previousToken.value,
},
fix(fixer) {
return fixer.removeRange([
previousToken.range[1],
punctuatorTokenStart.range[0],
]);
},
});
}

// shift the start to the ?
type = '?:';
punctuatorTokenStart = previousToken;
Expand Down
100 changes: 100 additions & 0 deletions packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts
Expand Up @@ -4593,6 +4593,54 @@ type Bar = Record<keyof Foo, string>
},
],
},
{
code: 'function foo(a? : string) {}',
output: 'function foo(a?: string) {}',
errors: [
{
messageId: 'unexpectedSpaceBetween',
data: { type: ':', previousToken: '?' },
line: 1,
column: 17,
},
],
},
{
code: 'function foo(a ? : string) {}',
output: 'function foo(a?: string) {}',
errors: [
{
messageId: 'unexpectedSpaceBefore',
data: { type: '?:' },
line: 1,
column: 16,
},
{
messageId: 'unexpectedSpaceBetween',
data: { type: ':', previousToken: '?' },
line: 1,
column: 18,
},
],
},
{
code: 'function foo(a ? : string) {}',
output: 'function foo(a?: string) {}',
errors: [
{
messageId: 'unexpectedSpaceBefore',
data: { type: '?:' },
line: 1,
column: 16,
},
{
messageId: 'unexpectedSpaceBetween',
data: { type: ':', previousToken: '?' },
line: 1,
column: 19,
},
],
},
{
code: `
class Foo {
Expand Down Expand Up @@ -4635,6 +4683,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: 'unexpectedSpaceBetween',
data: { type: ':', previousToken: '?' },
line: 3,
column: 27,
},
],
},
{
code: `
class Foo {
greet(name ?: string) : string { return name; }
}
Expand Down Expand Up @@ -4681,6 +4755,32 @@ interface Foo {
},
{
code: `
interface Foo {
name ? : string;
}
`,
output: `
interface Foo {
name?: string;
}
`,
errors: [
{
messageId: 'unexpectedSpaceBefore',
data: { type: '?:' },
line: 3,
column: 10,
},
{
messageId: 'unexpectedSpaceBetween',
data: { type: ':', previousToken: '?' },
line: 3,
column: 12,
},
],
},
{
code: `
interface Foo {
greet(name ?: string) : string;
}
Expand Down