Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
DrSkillIssue wants to merge5 commits intoPowerShell:main
base:main
Choose a base branch
Loading
fromDrSkillIssue:avoid-new-object

Conversation

@DrSkillIssue
Copy link

@DrSkillIssueDrSkillIssue commentedJun 15, 2025
edited
Loading

PR Summary

#2046

Adds a new built-in rulePSAvoidUsingNewObject that flags usage of theNew-Object cmdlet and recommends using type literals with::new() syntax instead for better performance and more idiomatic PowerShell code.

What Changed

  • New Rule:AvoidUsingNewObject
  • Severity: Warning
  • Scope: FlagsNew-Object -TypeName usage while preservingNew-Object -ComObject (COM objects cannot use type literals)

Key Features

  • Smart COM Object Detection: ExcludesNew-Object -ComObject calls since they cannot be replaced with type literals
  • Parameter Splatting Support: Handles complex scenarios where parameters are passed via hashtable splatting (New-Object @params)
  • Variable Chain Resolution: Recursively follows variable assignments to determine if splatted parameters contain COM object references
  • Comprehensive AST Analysis: Supports both direct parameter usage and various splatting patterns including$using: variables

PR Checklist

iRon7, andyleejordan, and dan-hughes reacted with thumbs up emoji
@DrSkillIssue
Copy link
Author

@microsoft-github-policy-service agree

@DrSkillIssueDrSkillIssue changed the titleAdd PSAvoidUsingNewObject RuleWIP: Add PSAvoidUsingNewObject RuleJun 15, 2025
@DrSkillIssueDrSkillIssue changed the titleWIP: Add PSAvoidUsingNewObject RuleAdd PSAvoidUsingNewObject RuleJun 15, 2025
@liamjpeters
Copy link
Contributor

liamjpeters commentedAug 17, 2025
edited
Loading

👋 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-ComObject in 😎.

Some things to look into:

  • When checking for splatting use ofComObject in a hashtable, you dismiss keys that are less than 3 characters. You do this to avoid false positives, but I'm not sure what would lead to a false positive?

    https://github.com/DrSkillIssue/PSScriptAnalyzer/blob/26c779cdf5c42da4ac5e5edc4a09e7954d8f85ad/Rules/AvoidUsingNewObject.cs#L423-L425

    Skipping 1 and 2 character keys does miss the below, which are both valid.New-Object only has 1 parameter starting with aC, soC andCo are both valid (if not ideal) ways to supply theComObject parameter.

    $Splat=@{'C'='Word.Application'}New-Object@Splat$Splat2=@{'Co'='Word.Application'}New-Object@Splat2
  • When a switch parameter (such as-Strict or-Verbose) precedes-ComObject in the command expression, a violation is still emitted.

    New-Object-Verbose-ComObject'Word.Application'

    This is probably coming from theHasComObjectParameter function.

    https://github.com/DrSkillIssue/PSScriptAnalyzer/blob/26c779cdf5c42da4ac5e5edc4a09e7954d8f85ad/Rules/AvoidUsingNewObject.cs#L134-L138

    It's looping through theCommandElements of theCommandAst. Each time it encounters aCommandParameterAst it setswaitingForParamValue to true, which skips the next element (assuming it's a parameter value).

    I like to useJason Shirk's Show-AST to visualise this stuff.CommandParameterAst can be sequential, without a variable expression or constant expression between them.

    image
  • You're getting 2 failing tests (you can run tests locally using.\build.ps1 -Test):

    1. You need to add an entry for your rule into the table indocs/Rules/README.md. The format should be obvious from all the other rules.
    2. You need to increment the rule count of the default rules. With your new rule, there'd be 71.
      It"get Rules with no parameters supplied" {
      $defaultRules=Get-ScriptAnalyzerRule
      $expectedNumRules=70
      if ($PSVersionTable.PSVersion.Major-le4)
      {
      # for PSv3 PSAvoidGlobalAliases is not shipped because
      # it uses StaticParameterBinder.BindCommand which is
      # available only on PSv4 and above
      $expectedNumRules--
      }
      $defaultRules.Count| Should-Be$expectedNumRules
      }
  • Regarding tests: it's great that you've got so many test cases documented forpassing andfailing scenarios. It would be so much better if these were all individual tests.

    Currently there are only 3 tests. One for the error message description, one for there being 0 violations in one file, another for there being 20 violations in the other file.

    Small focused tests help pinpoint any future regressions and ensure coverage across edge-cases (rather than a number of violations going from 20 to 19 - it would show which test case has changed).

  • You've changed some formatting inEngine/Commands/InvokeScriptAnalyzerCommand.cs. This formatting change is the only change you're making in that file and it's not related to your new rule or this PR.

I really liked your rule documentation, lots of detail and further reading links. ⭐

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@andyleejordanandyleejordanAwaiting requested review from andyleejordan

@bergmeisterbergmeisterAwaiting requested review from bergmeisterbergmeister is a code owner

@sdwheelersdwheelerAwaiting requested review from sdwheelersdwheeler is a code owner

@michaeltlombardimichaeltlombardiAwaiting requested review from michaeltlombardimichaeltlombardi is a code owner

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@DrSkillIssue@liamjpeters@bergmeister

[8]ページ先頭

©2009-2025 Movatter.jp