-
Notifications
You must be signed in to change notification settings - Fork 407
Add PSAvoidUsingNewObject Rule #2109
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
base: main
Are you sure you want to change the base?
Conversation
|
@microsoft-github-policy-service agree |
|
👋 Hey @DrSkillIssue, thanks for putting this together; you've clearly put a lot of thought into the many ways you could try and sneak a Some things to look into:
I really liked your rule documentation, lots of detail and further reading links. ⭐ |
|
@DrSkillIssue There are 2 test failures, can you please look into fixing them? |
bergmeister
left a comment
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.
Once my comments are being addressed, I'd be happy to approve, the most important one is that I think rule should not be enabled by default to start with
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.
please tidy up diff
|
|
||
| foreach (var assignment in assignments) | ||
| { | ||
| if (assignment.Right is CommandExpressionAst cmdExpr) |
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.
there are a few areas like this one where indentation could be reduced. Here we could e.g. negate statement and continue with next loop member so that other code can just be below at same indentation level: if (assignment.Right is not CommandExpressionAst cmdExpr) { continue; }
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 file is to replicate setting used by PSGallery to scan code when users upload new module and send email if there are violations. I don't even know whether this file is still in sync as of today but this file should only be updated by PSGallery members who know the rules used by PSGallery (and consider adding new ones).
Can you comment on whether this file is up to date and if not open separate PR to sync please @alerickson ?
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 am not sure what the purpose of this settings file is but until then I'd not recommend touching it please
| #if !CORECLR | ||
| [Export(typeof(IScriptRule))] | ||
| #endif | ||
| public class AvoidUsingNewObject : IScriptRule |
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.
With this inheritance rule will run by default but I fear it will cause a big backlash as many people run PSSA in CI. I suggest to inherit from ConfigurableRule so that it's not enabled by default. See this PR for an example of how to: #2124

PR Summary
#2046
Adds a new built-in rule
PSAvoidUsingNewObjectthat flags usage of theNew-Objectcmdlet and recommends using type literals with::new()syntax instead for better performance and more idiomatic PowerShell code.What Changed
AvoidUsingNewObjectNew-Object -TypeNameusage while preservingNew-Object -ComObject(COM objects cannot use type literals)Key Features
New-Object -ComObjectcalls since they cannot be replaced with type literalsNew-Object @params)$using:variablesPR Checklist
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.