Skip to content
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

PsShouldProcess - ShouldContinue not included #1305

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

PsShouldProcess - ShouldContinue not included #1305

wants to merge 2 commits into from

Conversation

JohnLBevan
Copy link

fixes #1304

Copy link
Collaborator

@bergmeister bergmeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
Code looks very clean and awesome that you added quite a few test cases :-)
I'd be very happy taking this PR.
After re-reading the docs on ShouldContinue, I have an idea for additional (optional) feature since the docs say:

Cmdlets using ShouldContinue should also offer a "bool Force" parameter which bypasses the calls to ShouldContinue and ShouldProcess.

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.cmdlet.shouldcontinue

If we could also check for the usage of the -Force pattern (not sure how difficult or possible it is), then that would be great. Again, this could be an optional, separate, follow up PR in addition to this awesome PR, would you be interested in doing that as well (no pressure)?

@JohnLBevan
Copy link
Author

Thanks @bergmeister.

I'd be happy to give it a go sometime; though my knowledge of parsers isn't great / this fix was easy to code since I could mostly steal from the existing efforts made for ShouldProcess.

FYI: There is already a rule to ensure that the Force parameter is included: PSAvoidShouldContinueWithoutForce.

However, that rule can currently be fooled by adding the switch to the function's parameters, then neglecting to use it. I've created new issue: #1308 to track this.

@bergmeister
Copy link
Collaborator

@rjmholt @JamesWTruher I would be happy to merge this change in, are you ok with it as well?

Copy link
Contributor

@rjmholt rjmholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn't see this PR when it was originally opened. A couple of small suggestions, but not blocking.

return functionDefinitionAst.Extent;
}

return (invokeMemberExpressionAstFound as InvokeMemberExpressionAst).Member.Extent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (invokeMemberExpressionAstFound as InvokeMemberExpressionAst).Member.Extent;
return ((InvokeMemberExpressionAst)invokeMemberExpressionAstFound).Member.Extent;

/// </summary>
private static bool IsShouldContinueCall(Ast ast)
{
var invokeMemberExpressionAst = ast as InvokeMemberExpressionAst;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to do this:

return ast is InvokeMemberExpressionAst invokeMemberExpressionAst
    && invokeMemberExpressionAst.Member is StringConstantExpressionAst memberExprAst
    && string.Equals(memberExprAst.Value, "ShouldContinue", StringComparison.OrdinalIgnoreCase);

private bool callsShouldProcessDirectly(Vertex vertex)
{
return funcDigraph.GetNeighbors(vertex).Contains(shouldProcessVertex);
}
private bool callsShouldContinueDirectly(Vertex vertex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private bool callsShouldContinueDirectly(Vertex vertex)
private bool callsShouldContinueDirectly(Vertex vertex)

@bergmeister
Copy link
Collaborator

bergmeister commented Jun 11, 2020

@rjmholt Should we merge this PR as-is since @JohnLBevan hasn't provided any updates and did not tick the option for other people to allow push to his branch? We could then do a follow-up PR.

@JohnLBevan JohnLBevan marked this pull request as draft July 31, 2020 07:07
@JohnLBevan
Copy link
Author

Apologies, I missed your comments from Jan. I'm happy to make the suggested changes, though think I deleted the original branch a while back (i.e. the source of the pull request now shows as unknown repository, and I can't find a way to get it back, or to enable the allow edits from maintainers flag...
I'll try to pick this up over the weekend, maybe creating a new PR based on the current state of the code to minimise differences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSShouldProcess rule ignores ShouldContinue
3 participants