PowerShell / PowerShell/PSScriptAnalyzer

Rule request: AvoidUsingBacktickLineTerminator

Aperta
#2,111 1 commento 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
C#
Stelle
2.2k
Fork
414
Merge medio
13h 1m
PR unite (30g)
2

Descrizione

Summary of the new feature

As a code reviewer, I want script/module writers to receive automated informational warnings about backtick usage so that my review time isn't consumed by catching maintainability issues that tooling should prevent upfront.

Problem Statement:
The backtick (`) character is commonly used for line continuation in PowerShell, but it's considered poor practice for several reasons:

  • Hard to see: Backticks are nearly invisible and easily missed during code review
  • Poor readability: Makes code harder to read and understand
  • Maintenance issues: Easy to accidentally remove or misplace during editing
  • Non-intuitive: New PowerShell users often struggle with backtick usage

PowerShell offers better alternatives like parameter splatting and natural line breaks after operators/pipelines that are more readable and less error-prone.

Proposed technical implementation details

Rule Name: PSAvoidUsingBacktickLineTerminator

Severity: Information

Behavior:

  • Flag any usage of backtick (`) character used for line continuation
  • Suggest appropriate alternatives based on context

Recommended alternatives to suggest:

  1. Parameter Splatting: For commands with multiple parameters
  2. Natural line breaks: After pipeline operators (|), logical operators (-and, -or), comparison operators
  3. Parentheses grouping: For complex expressions

Example violations:

# Backtick line continuation - Flagged
Get-Process -Name notepad `
    -ErrorAction SilentlyContinue `
    | Where-Object CPU -gt 100

# Complex command with backticks - Flagged  
$result = Get-ChildItem -Path C:\Temp `
    -Filter "*.txt" `
    -Recurse `
    -ErrorAction SilentlyContinue

Technical Implementation:

  • I plan on taking this issue if approved.
  • Simple class that inherits ITokenRule:
public IEnumerable<DiagnosticRecord> AnalyzeTokens(Token[] tokens, string fileName)
{
    if (tokens == null) throw new ArgumentNullException(Strings.NullTokensErrorMessage);

    var lineContinuationTokens = tokens.Where(token => token.Kind == TokenKind.LineContinuation);

    foreach (var tokenNode in lineContinuationTokens)
    {
        yield return new DiagnosticRecord(
            string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBacktickLineTerminatorError),
            tokenNode.Extent,
            GetName(),
            DiagnosticSeverity.Information,
            fileName
        );
    }
}

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

1.24.0

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando le implementazioni esistenti di ITokenRule e il punto di ingresso AnalyzeTokens descritto nell’issue, in particolare TokenKind.LineContinuation e il messaggio diagnostico in Strings. Verifica come vengono registrate e testate le regole comparabili. Il lavoro è completato quando i token di continuazione di riga con backtick producono diagnosi informative con il nome della regola e l’intervallo del sorgente, senza segnalare token non correlati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp, powershell
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
58/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.