PowerShell / PowerShell/PSScriptAnalyzer

Rule Idea: .NET Constructor Auto Document/Align

Open
#1,587 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Rules Issue - Enhancement Issue - New Rule Up-for-Grabs
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Summary of the new feature

When instantiating dotnet objects with [Class]::new(), the syntax is generally obtuse for anyone coming along to read it. Reflection could be used to expand out the definition to the proper constructor from the OverloadDefinitions using the AST and comment the properties, since hashtable constructor syntax only works if the object has no constructors.

Proposed technical implementation details (optional)

Before:

[datetime]::new(2000, 6, 20)

After formatting:

[DateTime]::new(
    2000,   #int year
    6,      #int month
    20,     #int day
)

Major issue here would be variables, since Powershell is not statically typed it won't know what type the variable should be until runtime. This is only the problem in the case of overloaded constructors with the same number of arguments, maybe could still work if someone strongly typed the variable ahead of time that the AST could parse, e,g, [int]$Month?

What is the latest version of PSScriptAnalyzer at the point of writing
1.19.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed PowerShell AST and reflection approach, including constructor metadata from OverloadDefinitions and the limitations around variables and overloaded constructors. Define the supported cases and expected formatting for the example before/after output, then add focused coverage for those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.