Skip to content

Commit

Permalink
feat(eslint-plugin): [type-annotation-spacing] handle space between ?…
Browse files Browse the repository at this point in the history
… and : (#3138)

* fix(eslint-plugin): [type-annotation-spacing] handle space after ?

* fix: skip before flag and add test for interface

* fix: add new message

* fix: use isSpaceBetweenTokens to support older ESLint
  • Loading branch information
Knutas committed Mar 28, 2021
1 parent eda9157 commit 40bdb0b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
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

0 comments on commit 40bdb0b

Please sign in to comment.