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 2 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
16 changes: 16 additions & 0 deletions packages/eslint-plugin/src/rules/type-annotation-spacing.ts
Expand Up @@ -177,6 +177,22 @@ export default util.createRule<Options, MessageIds>({
const { before, after } = getRules(ruleSet, typeAnnotation);

if (type === ':' && previousToken.value === '?') {
if (sourceCode.isSpaceBetween!(previousToken, punctuatorTokenStart)) {
Copy link
Member

Choose a reason for hiding this comment

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

isSpaceBetween was only added in ESLint v6.7.0
We currently support as far back as ESLint v5.0.0.

Meaning this is not safe.

You need to fall back to the old sourceCode.isSpaceBetweenTokens() API for those cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I saw that isSpaceBetween was already used by object-curly-spacing so I assumed it was fine to use.

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
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: '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 +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: 'unexpectedSpaceBefore',
data: { type: ':' },
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: 'unexpectedSpaceBefore',
data: { type: ':' },
line: 3,
column: 12,
},
],
},
{
code: `
interface Foo {
greet(name ?: string) : string;
}
Expand Down