-
-
Notifications
You must be signed in to change notification settings - Fork 80
feat: add assertion options to RuleTester #137
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: add assertion options to RuleTester #137
Conversation
|
I think it's a great idea to drive this kind of consistency, but I do wonder if this is the best place to set that kind of expectation. It seems like something that might be more appropriate as an eslint rule in |
nzakas
left a comment
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 putting this together. I left some comments inline.
|
I think I covered all comments. |
|
All addressed |
|
All addressed |
|
All pending comments addressed. |
|
All pending comments addressed. |
mdjermanovic
left a comment
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!
fasttime
left a comment
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 left a suggestion, but otherwise LGTM.
Co-authored-by: Francesco Trotta <[email protected]>
| * | ||
| * @default false | ||
| */ | ||
| requireMessage?: boolean | 'message' | 'messageId'; |
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 there should be an extra assertion to disallow using a number for errors (maybe named disallowErrorShorthand).
As a result the boolean variant would be useless as the rule tester checks this by default for error objects.
Further this allows an easier migration, as the plugin maintainers can gradually enable the rules by either:
- First disallowing the error shorthand and then requiring the properties
- First adding the required location or message properties and then handling the error shorthands
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.
Just for clarification, so instead of
errors: 5
You have to use
A) errors: ['foo',...]
or B) errors: [{...}]
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.
This is dependent on the other assertions.
- none: allows both
requireMessage: "message"would allow both as the message providedrequireMessage: "messageId"would only allow B) as the message is providedrequireLocation: truewould only allow B) as a location is required
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.
IMO this is entirely/mostly covered by adding requireMessage.
What do the others think?
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.
@DMartens Are you suggesting that requireMessage and requireLocation alone should not disallow numeric values for errors, and instead there should be a separate option to do so?
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.
So if disallowShorthand is false/undefined, you can just use the error count even if requireMessage is enabled?
I dont like that. Because then it behaves like requireMessage/location isnt set in the first place.
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.
We can handle the option as true by default (when not specified) for the other options but I think the possibility to differentiate should be there.
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.
The requirement for errors to include either a message or messageId was discussed in #84. While this makes sense in many scenarios, it can be bypassed by specifying errors as a number (accepting all the implied limitations of doing so). That discussion also affirmed that using a numeric value for errors remains a valid option.
Given that ESLint has supported this behavior by default, I tend to agree with @DMartens that it makes sense to maintain the possibility of specifying a number for errors, even if requireMessage will enforce one of message or messageId when errors is an array. I'm open to revisiting the scope of this RFC if the team can reach an agreement.
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'm not sure I agree that we should make a change here. To me, if someone wants to use errors: number, then they wouldn't opt-in to these assertions in the first place.
I think requireMessage: true explicitly affirms that someone does not want to allow errors: number.
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 this is solved with the mentioned additional assertion disallowErrorShorthand, which defaults to true.
This keeps the current proposed behavior but allows users to opt-out.
This behaviour could also be implemented with requireMessage: false but I think disallowErrorShorthand should be preferred as requireLoc (and maybe requireData) is also affected.
| * @default false | ||
| */ | ||
| requireLocation?: boolean; | ||
| }, |
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.
Another helpful check would be requireData which requires the data error property when a messageId is used and the referenced message has placeholders ({{ placeholder }}).
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.
Would this imply requireMessage: 'messageId'?
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 also considered requiring the data block (if needed) into requireMessage: 'messageId'.
What do you+the others think?
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.
This would not imply requireMessage: 'messageId' as the message already has the placeholders filled in. So there is no need for data.
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'm not sure whether I understand the last comment.
IMO requireData results in ({data: *})[]
AFAICT The current code only checks for data in the messageId block. Which makes sense to me, as data is used to fill in the placeholders in its message.
Most messageId checks are very broad as most rules only have one or two messages. So also checking for data is kind of relevant. Which is why I would implicitly attach it to the requireMessageId option.
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.
This option (requireData) should only be active when the error has a messageId as data is useless when message is specified (you already provide the message with every placeholder filled in).
The validator only has to check whether data is specified as there is already an error when data is specified and placeholders are missing.
Coupling this with the requireMessage: "messageId" would be unintuitive as that reads as only the "messageId" is required.
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 agree a requireData assertion could be helpful. As this would be a separate option, it could be also discussed in a follow-up RFC.
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.
Agreed, the scope of this RFC is already well-defined and I don't think we should be expanding at this point in time. @DMartens feel free to open an issue to start the discussion around a requireData assertion option.
nzakas
left a comment
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'm happy with the current state of this RFC.
|
@fasttime can you check if your review comments have been addressed? |
fasttime
left a comment
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 don't have any concerns with the proposed solution. @DMartens, I understand you see room for improvement and would prefer some changes. Do you think the RFC is acceptable in its current form nonetheless?
|
Yes, I see both of the suggestions to be non-blocking:
|
|
Moving to final commenting. |
|
No further comments here, so merging. |
|
Should I create a PR for the implementation now? |
@ST-DDT yes, please! Issue eslint/eslint#19921 is already assigned to you. |
Summary
Add options that control which assertions are required for each
RuleTestertest case.Related Issues
The idea was initially sparked by this comment: vuejs/eslint-plugin-vue#2773 (comment)
The first steps have been taken in: eslint/eslint#19904
This lead to the issue that this RFC is based on: eslint/eslint#19921