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
Stricter validation of return value from a rule's create()
function
#16086
Comments
Can you explain what currently happens for various types of values and then explain what you would like to happen instead? When you say “not allowed”, does that mean an error is thrown? Something else? |
@nzakas good point, I updated the description to include a more detailed table of the return types. |
Thanks that’s very helpful. Some thoughts:
So overall, I think I’m all cases we want to err on the side of helping the developer identify unintentional errors with clear messages. |
@nzakas okay, given your preference, I'm fine with keeping the current behavior of throwing an exception for null/undefined. The benefit of this choice is that we maintain only a single way Regarding other data types, I'm still unclear why/how arrays or functions could be an acceptable return type here, given that we use it as an object. If someone is somehow getting by using an array/function with additional properties on it, this seems like a good chance for us to catch that so they can fix it up into a standard object. |
If arrays or functions have own properties defining visitors, they will still work today. Why someone would want to do that is beyond me, but it’s not really an error condition. These are still just objects so I’m not sure if there’s a good reason to make them an exception case, but we can see what others think. |
My thoughts:
|
@mdjermanovic I agree with your thoughts (throw on everything except an object). However, I am still a bit perplexed by the concern around disallowing arrays/functions. In general, I believe:
In the ESLint Working with Rules documentation, we specify that the Note: if there's still disagreement, I could try to put together an RFC at some point, but that seems like a lot of overhead for what could be just one of many similar type-strictness improvements we could make. |
In general, breaking changes require an RFC to get as much feedback as possible. Strictness is useful for finding conditions that will not work. My concern is that arrays and functions are just specific types of objects (fulfilling what is specified in the documentation) and will also currently work without error. I’m not sure that there’s a benefit to rule developers to disallow something that currently works even if it is unusual. It’s possible you are right, and this really is an error condition, but then why are we cutting off at testing for arrays and functions? Why not maps and sets? Why not proxies? Requiring the return value constructor to be Object is another possibility, but that still seems a bit overboard to me. It also seems like maybe this type of check is more appropriately done as a build time check via TypeScript than a runtime check? @btmills still looking for feedback from you here. |
This is a good point. I think the reasoning comes from how one pattern commonly found on the internet to "check if a variable is an object" goes like this: if (
typeof yourVariable === 'object' &&
!Array.isArray(yourVariable) &&
yourVariable !== null
) {
executeSomeCode();
} As you point out, the array part of this could be considered arbitrary/incomplete given that it's just one of many possible data structures. Given this, would you support enforcing a simple Regarding TypeScript, I'd love to take advantage of it, but how would this come into play given that ESLint isn't written in TypeScript? |
Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update. |
Current behavior
ESLint currently allows objects, arrays, functions, and literal values to be returned by a rule's
create()
function. I suspect it's unintentional that we allow anything but objects right now.Rule visitor functions should be defined in a returned object. For someone wanting to bail out of a rule early, the current convention is to return an empty object:
Proposed behavior
Require the return value from a rule's
create()
function to be one of the following:{ /* visitors */ }
{}
null
/undefined
Here's the exception referenced in the table:
Links
Originally posted by @bmish in #16075 (comment)
See more context in #16075 where I added some messaging around this when the wrong return type is provided.
The text was updated successfully, but these errors were encountered: