-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [type-annotation-spacing] handle space between ? and : #3138
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
Conversation
Thanks for the PR, @Knutas! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
Codecov Report
@@ Coverage Diff @@
## master #3138 +/- ##
==========================================
+ Coverage 92.88% 92.90% +0.02%
==========================================
Files 315 316 +1
Lines 10706 10842 +136
Branches 3025 3065 +40
==========================================
+ Hits 9944 10073 +129
- Misses 342 343 +1
- Partials 420 426 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
unless the before option is set to true.
before
only applies to space before the pair.
I don't think overloading it with additional meaning is correct here.
If we wanted to allow space between the pair that should be a separate option - an option I would hold off on implementing for now (until someone asks for it).
So change it to always raise this error regardless of options? |
Yes please! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic itself LGTM, a few comments.
Thanks for your work on this!
@@ -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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
if (sourceCode.isSpaceBetween!(previousToken, punctuatorTokenStart)) { | ||
context.report({ | ||
node: punctuatorTokenStart, | ||
messageId: 'unexpectedSpaceBefore', |
There was a problem hiding this comment.
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}}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks for your contribution!
Fixes type-annotation-spacing rule to give an error if there is a space between
?
and:
in a type annotation.Fixes #3140