PowerShell / PowerShell/PSScriptAnalyzer

Analyzer for Type Reassignment in `foreach` Loop

Offen
#1,710 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Area - Rules Issue - New Rule Up-for-Grabs
Vorherrschende Sprache
C#
Sterne
2.2k
Forks
414
Ø Merge
13 Std. 1 Min.
Gemergte PRs (30 T.)
2

Beschreibung

Summary of the new feature
Consider the following script:

if ($true) {
    [String]$MyVar = "Blah"
}

[int[]]$MyInts = @(1,2,3)
foreach ($MyVar in $MyInts) {
    Write-Host "This is my type [$($MyVar.GetType())]"
}

A naïve C# reading of the above PowerShell would assume that the above would print out:

This is my type [int]
This is my type [int]
This is my type [int]

However this is PowerShell and due to Scoping Rules this actually returns:

This is my type [string]
This is my type [string]
This is my type [string]

Proposed technical implementation details (optional)

This has already burned me once in production.

As far as I can tell there is not an existing analyzer to determine this.

It is unclear if it is even feasible to do this reliably.

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

PS > Get-Module | Where-Object {$_.Name -eq 'PSScriptAnalyzer' }

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.19.1                PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke…

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Keine Quelldatei, kein Test und kein Einstiegspunkt werden genannt. Beginne damit, die vorhandenen Analyzer-Regeln und die verknüpften PowerShell-Scoping-Regeln zu überprüfen, und bestimme dann, ob die Neuzuweisung von Typen in foreach-Schleifen zuverlässig erkannt werden kann. Erledigt ist die Aufgabe, wenn eine dokumentierte Analyzer-Diagnose mit Tests vorhanden ist, die das Beispielverhalten abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp, powershell
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.