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
Update: Fix accessor-pairs to enforce pairs per property in literals #12062
Conversation
Just to note that this is a major bug for this rule, i.e. the logic is broken. |
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.
Some minor changes requested to make the tests a little clearer.
I am also slightly worried about comparing token lists in computed properties in case of side effects (e.g., ({ get [a++]() {}, set [a++](foo) {} })
). That said, maybe the best way to handle that going forward would be to have an ESLint rule that focused on computed property/method keys with side effects, and then we would be able to do whatever is easiest here (whether that is to report all equivalent token sequences, or to restrict reporting to identifiers and literals).
What do you think?
tests/lib/rules/accessor-pairs.js
Outdated
{ | ||
code: "var o = { get a() {}, get b() {}, set b(foo) {} };", | ||
options: [{ setWithoutGet: true, getWithoutSet: true }], | ||
errors: [setterError] |
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.
It would be useful if the test could confirm that a
is the property missing a setter. Could you please add that to the test? (Request applies to similar tests below)
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.
Yes, this is definitely an incomplete test that doesn't show much.
This could be tested using location, but it might be also good to change not just the test but the rule itself, to show a message with the name instead of just Setter is not present.
?
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 would love to see the property name added to the message, but I won't insist on it happening in this PR.
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'll do it, better now in this PR where the tests have to be modified anyway than to fix the whole test file again later.
Just a small question, as there will be different messages for property descriptors (the old generic ones) and object literal properties (which will have the name). Is messageId
a public data, can I change it?
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.
Great question. We haven't really established that messageId
is in the public API, because we don't yet support message handlers/translators anyway, so I think we can consider the message IDs an implementation detail for now.
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 all done now, using astUtils.getFunctionNameWithKind()
.
The function doesn't return full name for computed ones, just "getter"
or "setter"
. This might be a future enhancement, maybe like how no-self-assign
shows source code without whitespace in the message.
There is a minor bug in getFunctionNameWithKind()
related to empty string names, I'll fix that in another PR.
Also, the error will now highlight only the header (e.g. get a
) instead of the whole property, as there is nothing wrong with the code in the getter, it's just missing the setter.
Tests are fixed and should be much better now.
tests/lib/rules/accessor-pairs.js
Outdated
code: "var o = { get a() {}, get a() {} };", | ||
options: [{ setWithoutGet: true, getWithoutSet: true }], | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [setterError, setterError] |
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 it would be useful to include location data in these errors, to confirm that the same node is not reported twice. Could you please add location data to this test and the one below?
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 a very good point, it would be very useful indeed. I'll fix this, just to see first what other changes should be made.
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 also done :)
As noted in #12070 comparing tokens is not always safe, but it might be useful to analyze how this imperfection affects the particular rule. E.g. in In this Overall, I'm not sure what would be more in line with the ESLint's standards - to not enforce the rule on something because it isn't 100% reliable (but still pretty good), or enforce knowing that some (very) edge cases will not be caught. |
There are already some rules that compare tokens and thus have small flaws, e.g. these are all false positives: /*eslint no-useless-call:error*/
a[++i].foo.call(a[++i])
/*eslint prefer-spread:error*/
a[++i].foo.apply(a[++i], args)
/*eslint no-self-compare:error*/
a[++i] < a[++i] while someone could see these as false negatives: /*eslint no-useless-call:error*/
a[i+1].foo.call(a[1+i])
/*eslint prefer-spread:error*/
a[i+1].foo.apply(a[1+i], args)
/*eslint no-self-compare:error*/
a[i+1] < a[1+i] There is a known limitation section in prefer-spread:
(Offtopic, I don't think that these examples match the description which says that the rule cannot detect some violations. The first example is detected while it shouldn't be, the second is not detected and it actually isn't a violation.) Edit: my mistake, the example is ok. |
Given all this, perhaps the rule could indeed check computed keys (i.e. compare tokens) and have a 'known limitations' section in the docs with an example when it would fail to warn: var foo = {
get [i++]() {
},
set [i++]() {
}
} |
That works for me. Thanks for the great discussion! |
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 contributing! Really appreciate the extra details in the tests.
I've added the "Known Limitations" section, I guess it should be okay. @platinumazure thank you for the detailed review, I didn't realize how bad was the first version of the tests. And the rule itself got some significant improvements :) |
I've reproduced the bug here. Marking as accepted. |
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.
Latest changes LGTM, thanks! Waiting for another team member to review before merging.
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.
One suggestion about documentation, but this LGTM. Thanks for the extensive test suite - it'll make it so much easier for the next person to touch the code to understand what the rule's expected behavior is!
* However, this edge case is not covered, it should be reported by no-dupe-keys anyway. | ||
*/ | ||
{ | ||
code: "var o = { get a() {}, a:1, set a(foo) {} };", |
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.
Do we want to add this to the "known limitations "documentation as well? I could see someone reporting this as a bug in the future.
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 want to block this from landing in this release, so I'm going to go ahead and merge this. If we decide the documentation could be improved, we can always do that in a follow up PR.
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.
Was about to reply when you merged :)
I think this is a good idea, I'll make a small docs PR to add this.
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.
Sounds good!
What is the purpose of this pull request? (put an "X" next to item)
[X] Bug fix
This is a bug fix, not an enhancement, but it will produce more errors by default.
Tell us about your environment
What parser (default, Babel-ESLint, etc.) are you using?
default
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue.
What did you expect to happen?
By default, this rule reports setters without getters.
I would expect 1 error for the
b
setter.What actually happened? Please include the actual, raw output from ESLint.
No warnings.
The rule doesn't read property names in object literals. An object literal with a getter and a setter that are unrelated to each other is never reported, regardless of the settings.
What changes did you make? (Give an overview)
Fixed the logic for object literals.
Is there anything you'd like reviewers to focus on?
astUtils.getStaticPropertyName()
but I didn't change this as it would be an enhancement, not just a bug fix. I can do this in another PR, and there should be also more test cases for property descriptors (e.g. there are no test cases for getters without setters).string
are compared by their tokens. This could be seen as an enhancement, but the rule didn't compare names anyway. This allows the rule to report cases like{ set [a]() {} }
.This is interpreted as 3 unrelated attempts to create the same key, the last wins and creates a setter without getter. I guess this isn't so important as the
no-dupe-keys
will report this literal.As a side note, this could also happen in cases like this:
perhaps it would be good to have a rule like
no-loose-accessors
(I'd be glad to implement it)