GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add the AvoidExclaimOperator rule to warn about the use of the ! negation operator. Fixes #1826 by liamjpeters · Pull Request #1922 · PowerShell/PSScriptAnalyzer

Conversation

@liamjpeters

@liamjpeters Liam Peters (liamjpeters) commented Jun 14, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Add a rule to warn about using the exclamation mark (!) as the negation operator. Suggests using -not instead.

PR Summary

Add the PSAvoidExclaimOperator rule to warn about the use of ! for negation. The rule is disabled by default as many people see this as a stylistic preference. Covered by tests .Fixes #1826

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Liam Peters (liamjpeters) added 2 commits June 14, 2023 12:32
@liamjpeters
Added the AvoidExclamationPointOperator rule to warn about the use of…
fd926e3
… the negation operator !. Fixes PowerShell#1826
@liamjpeters
Updated license header of Rules/AvoidExclamationPointOperator.cs
16ba414
@liamjpeters

Liam Peters (liamjpeters) commented Jun 14, 2023

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@liamjpeters Liam Peters (liamjpeters) changed the title WIP: Add the AvoidExclamationPointOperator rule to warn about the use of the exclamation point negation operator. Fixes #1826 Add the AvoidExclamationPointOperator rule to warn about the use of the exclamation point negation operator. Fixes #1826 Jun 14, 2023
@liamjpeters
Liam Peters (liamjpeters) marked this pull request as ready for review June 14, 2023 11:56
@liamjpeters
Liam Peters (liamjpeters) marked this pull request as draft June 16, 2023 13:29
@liamjpeters
Liam Peters (liamjpeters) marked this pull request as ready for review June 16, 2023 13:29
@liamjpeters

Liam Peters (liamjpeters) commented Jun 16, 2023

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister) - 👋 Can I get a review please? Sorry I didn't see a way to add a reviewer myself 😊 Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister
Christoph Bergmeister (bergmeister) self-requested a review June 16, 2023 14:44
bergmeister
Christoph Bergmeister (bergmeister) reviewed Jun 16, 2023
View reviewed changes

@bergmeister Christoph Bergmeister (bergmeister) left a comment

Copy link
Copy Markdown
Collaborator

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 good overall, just a few comments. Not sure on the rule name though, it's definitely rather exclamation mark and not exclamation point. Maybe even as short as ExclaimOperator, since that is the name of the token.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread docs/Rules/AvoidExclamationPointOperator.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/Rules/AvoidExclamationPointOperator.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidExclamationPointOperator.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/AvoidExclamationPointOperator.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/Strings.resx Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister Christoph Bergmeister (bergmeister) added the Area - Rules label Jun 16, 2023
Liam Peters (liamjpeters) and others added 4 commits June 18, 2023 14:56
@liamjpeters @bergmeister
Updated the description of the rule in docs
01633c5
Co-authored-by: Christoph Bergmeister <[email protected]>
@liamjpeters @bergmeister
Fix alignment
1edbcf1
Co-authored-by: Christoph Bergmeister <[email protected]>
@liamjpeters @bergmeister
Update Rules/Strings.resx
69af14f
Co-authored-by: Christoph Bergmeister <[email protected]>
@liamjpeters
Renamed rule from AvoidExclamationPointOperator to AvoidExclaimOperator
5e1b945
@liamjpeters Liam Peters (liamjpeters) changed the title Add the AvoidExclamationPointOperator rule to warn about the use of the exclamation point negation operator. Fixes #1826 Add the AvoidExclaimOperator rule to warn about the use of the ! negation operator. Fixes #1826 Jun 18, 2023
@liamjpeters

Liam Peters (liamjpeters) commented Jun 18, 2023

Copy link
Copy Markdown
Contributor Author

Renamed rule from AvoidExclamationPointOperator to AvoidExclaimOperator

bergmeister reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@liamjpeters
Liam Peters (liamjpeters) requested a review from Christoph Bergmeister (bergmeister) June 21, 2023 10:25
bergmeister
Christoph Bergmeister (bergmeister) reviewed Jun 21, 2023
View reviewed changes
Comment thread Rules/AvoidExclaimOperator.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@liamjpeters
Added comment explanation of the replacement suggestion logic and ren…
eb1deed
…amed loop variable for clarity
@liamjpeters
Liam Peters (liamjpeters) requested a review from Christoph Bergmeister (bergmeister) June 21, 2023 18:56
@liamjpeters

Liam Peters (liamjpeters) commented Jun 28, 2023

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister) - Thanks for your feedback so far - really helpful 😀. Was there anything else that needs to change for this to be considered for a merge? Anyone else I should be asking for a review from?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister
Christoph Bergmeister (bergmeister) requested a review from James Truher (JamesWTruher) June 28, 2023 10:15
@bergmeister

Christoph Bergmeister (bergmeister) commented Jun 28, 2023

Copy link
Copy Markdown
Collaborator

Added James as well to review. Other than that I just want to test drive it a bit myself for some exploratory testing as the rule is disabled by default so wouldn't get exercised as part of existing test suite scenarios

liamjpeters reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@liamjpeters

Liam Peters (liamjpeters) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor Author

Any news on this? I don't really know what my expectations should be, as this is my first contribution to this project - so please feel free to tell me to cool my jets and just be more patient.

I appreciate this PR doesn't introduce some killer feature that will be used by loads of people - but I'm excited for it and don't want the PR to be abandoned. Equally I'm excited to pick up other issues and open more PRs to improve the tool and my own c#, after testing the waters here.

Is there anything I can do to help?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister

Christoph Bergmeister (bergmeister) commented Aug 18, 2023

Copy link
Copy Markdown
Collaborator

Sorry for the delay Liam Peters (@liamjpeters) . It's not a bumper feature but because it can run on any PowerShell code, it's easy to miss a case that could result e.g. in a NullReferenceException. But the feature is not enabled by default, which is great to limit blast radius.
There's actually one thing you could help with, could you run PSSA with this rule enabled against a more complex script like this one as a one off and check there are no exceptions https://github.com/PowerShell/PowerShell/blob/master/build.psm1
Then happy to merge, the rest of the code looks good to me :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@liamjpeters

Liam Peters (liamjpeters) commented Aug 30, 2023

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister) - Good idea to run it on a more complex test file.

I've run the existing and updated PSSA against the Powershell project's build file. No exceptions thrown and no difference in execution time (both take an average of 170ms on my machine over multiple runs - even with my additional rule enabled).

Invoke-ScriptAnalyzer -Path .\build.psm1 -Settings @{Rules = @{ 'PSAvoidExclaimOperator' = @{ Enable = $true }}}

The build file has a mix of usages of -not and ! so is a good candidate. Using a text search there are 33 instances of the "!" character, 9 of them are within strings or comments (leaving 24 being used as negation).

PSSA with the rule enabled correctly flags the 24 negation instances.

Calling Invoke-ScriptAnalyzer with -Fix correctly changes the 24 negation instances.

image

Anything else you can think I should check - please let me know.

bergmeister reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bergmeister
Christoph Bergmeister (bergmeister) approved these changes Sep 6, 2023
View reviewed changes
@bergmeister
Christoph Bergmeister (bergmeister) merged commit b0383ee into PowerShell:master Sep 6, 2023
@github-actions github-actions Bot mentioned this pull request Mar 16, 2024
Update PSScriptAnalyzer to v1.22.0 ForNeVeR/intellij-powershell-fork#1
Closed
This was referenced Mar 16, 2024
Update PSScriptAnalyzer to v1.22.0 intellij-powershell/intellij-powershell#244
Merged
Update PowerShellEditorServices to v3.18.1 and PSScriptAnalyzer to v1.22.0 ForNeVeR/intellij-powershell-fork#2
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@bergmeister Christoph Bergmeister (bergmeister) bergmeister approved these changes
@JamesWTruher James Truher (JamesWTruher) Awaiting requested review from JamesWTruher

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Area - Rules

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Avoid exclamation point operator.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@liamjpeters @bergmeister
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.