10000 feat(eslint-plugin): [type-annotation-spacing] handle space between ?… · lorimccurry/typescript-eslint@40bdb0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 40bdb0b

Browse files
authored
feat(eslint-plugin): [type-annotation-spacing] handle space between ? and : (typescript-eslint#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
1 parent eda9157 commit 40bdb0b

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

packages/eslint-plugin/src/rules/type-annotation-spacing.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ type MessageIds =
3535
| 'expectedSpaceAfter'
3636
| 'expectedSpaceBefore'
3737
| 'unexpectedSpaceAfter'
38-
| 'unexpectedSpaceBefore';
38+
| 'unexpectedSpaceBefore'
39+
| 'unexpectedSpaceBetween';
3940

4041
const definition = {
4142
type: 'object',
@@ -122,6 +123,8 @@ export default util.createRule<Options, MessageIds>({
122123
expectedSpaceBefore: "Expected a space before the '{{type}}'.",
123124
unexpectedSpaceAfter: "Unexpected space after the '{{type}}'.",
124125
unexpectedSpaceBefore: "Unexpected space before the '{{type}}'.",
126+
unexpectedSpaceBetween:
127+
"Unexpected space between the '{{previousToken}}' and the '{{type}}'.",
125128
},
126129
schema: [
127130
{
@@ -177,6 +180,25 @@ export default util.createRule<Options, MessageIds>({
177180
const { before, after } = getRules(ruleSet, typeAnnotation);
178181

179182
if (type === ':' && previousToken.value === '?') {
183+
if (
184+
sourceCode.isSpaceBetweenTokens(previousToken, punctuatorTokenStart)
185+
) {
186+
context.report({
187+
node: punctuatorTokenStart,
188+
messageId: 'unexpectedSpaceBetween',
189+
data: {
190+
type,
191+
previousToken: previousToken.value,
192+
},
193+
fix(fixer) {
194+
return fixer.removeRange([
195+
previousToken.range[1],
196+
punctuatorTokenStart.range[0],
197+
]);
198+
},
199+
});
200+
}
201+
180202
// shift the start to the ?
181203
type = '?:';
182204
punctuatorTokenStart = previousToken;

packages/eslint-plugin/tests/rules/type-annotation-spacing.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4593,6 +4593,54 @@ type Bar = Record<keyof Foo, string>
45934593
},
45944594
],
45954595
},
4596+
{
4597+
code: 'function foo(a? : string) {}',
4598+
output: 'function foo(a?: string) {}',
4599+
errors: [
4600+
{
4601+
messageId: 'unexpectedSpaceBetween',
4602+
data: { type: ':', previousToken: '?' },
4603+
line: 1,
4604+
column: 17,
4605+
},
4606+
],
4607+
},
4608+
{
4609+
code: 'function foo(a ? : string) {}',
4610+
output: 'function foo(a?: string) {}',
4611+
errors: [
4612+
{
4613+
messageId: 'unexpectedSpaceBefore',
4614+
data: { type: '?:' },
4615+
line: 1,
4616+
column: 16,
4617+
},
4618+
{
4619+
messageId: 'unexpectedSpaceBetween',
4620+
data: { type: ':', previousToken: '?' },
4621+
line: 1,
4622+
column: 18,
4623+
},
4624+
],
4625+
},
4626+
{
4627+
code: 'function foo(a ? : string) {}',
4628+
output: 'function foo(a?: string) {}',
4629+
errors: [
4630+
{
4631+
messageId: 'unexpectedSpaceBefore',
4632+
data: { type: '?:' },
4633+
line: 1,
4634+
column: 16,
4635+
},
4636+
{
4637+
messageId: 'unexpectedSpaceBetween',
4638+
data: { type: ':', previousToken: '?' },
4639+
line: 1,
4640+
column: 19,
4641+
},
4642+
],
4643+
},
45964644
{
45974645
code: `
45984646
class Foo {
@@ -4635,6 +4683,32 @@ class Foo {
46354683
},
46364684
{
46374685
code: `
4686+
class Foo {
4687+
constructor(message ? : string);
4688+
}
4689+
`,
4690+
output: `
4691+
class Foo {
4692+
constructor(message?: string);
4693+
}
4694+
`,
4695+
errors: [
4696+
{
4697+
messageId: 'unexpectedSpaceBefore',
4698+
data: { type: '?:' },
4699+
line: 3,
4700+
column: 25,
4701+
},
4702+
{
4703+
messageId: 'unexpectedSpaceBetween',
4704+
data: { type: ':', previousToken: '?' },
4705+
line: 3,
4706+
column: 27,
4707+
},
4708+
],
4709+
},
4710+
{
4711+
code: `
46384712
class Foo {
46394713
greet(name ?: string) : string { return name; }
46404714
}
@@ -4681,6 +4755,32 @@ interface Foo {
46814755
},
46824756
{
46834757
code: `
4758+
interface Foo {
4759+
name ? : string;
4760+
}
4761+
`,
4762+
output: `
4763+
interface Foo {
4764+
name?: string;
4765+
}
4766+
`,
4767+
errors: [
4768+
{
4769+
messageId: 'unexpectedSpaceBefore',
4770+
data: { type: '?:' },
4771+
line: 3,
4772+
column: 10,
4773+
},
4774+
{
4775+
messageId: 'unexpectedSpaceBetween',
4776+
data: { type: ':', previousToken: '?' },
4777+
line: 3,
4778+
column: 12,
4779+
},
4780+
],
4781+
},
4782+
{
4783+
code: `
46844784
interface Foo {
46854785
greet(name ?: string) : string;
46864786
}

0 commit comments

Comments
 (0)
0