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: allow sync operation at root level (fixes: #7985) #8859
Conversation
@VictorHom, thanks for your PR! By analyzing the history of the files in this pull request, we identified @mduvall, @vitorbal and @btmills to be potential reviewers. |
LGTM |
LGTM |
LGTM |
LGTM |
lib/rules/no-sync.js
Outdated
} | ||
|
||
return { | ||
"MemberExpression[property.name=/.*Sync$/]"(node) { |
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.
Instead of manually keeping track of function scope, I think it would be simpler to use a descendant AST selector:
module.exports = {
meta: {
// ...
},
create(context) {
const selector = context.options[0] && context.options[0].allowAtRootLevel
? ":function MemberExpression[property.name=/.*Sync$/]"
: "MemberExpression[property.name=/.*Sync$/]"
return {
[selector](node) {
context.report({
node,
message: "Unexpected sync method: '{{propertyName}}'.",
data: {
propertyName: node.property.name
}
});
}
}
}
};
That way you won't have to do any of the function scope tracking yourself.
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.
that's what I wanted to do, but didn't know how to. Will know better next time!
@not-an-aardvark I fixed with your changes
LGTM |
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.
Looks great to me, thanks!
Unrelated to your PR, I'm wondering if we should add tests for chained calls off of a .*Sync
function call (e.g. foo.somethingSync(). transform()
). I'll try to hold onto that thought and maybe make a PR later.
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!
What is the purpose of this pull request? (put an "X" next to item)
[X] Changes an existing rule (template)
What rule do you want to change?
This address issues #7985
This update lets the no-sync rule take an option parameter allowAtRootLevel: boolean that will allow sync operations at the root level.
Does this change cause the rule to produce more or fewer warnings?
less errors since it is making the rule more lenient.
How will the change be implemented? (New option, new default behavior, etc.)?
New option
Please provide some example code that this change will affect:
What does the rule currently do for this code?
If sync operations are found, it reports
What will the rule do after it's changed?
if allowAtRootLevel:true is used, no errors will be reported for sync operations at the root level. If functions are at in function declaration/ function expressions and sync operations are inside, it will be reported