PowerShell / PowerShell/PSScriptAnalyzer

Rule request: AvoidUsingBacktickLineTerminator

未关闭
#2,111 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
C#
星标
2.2k
派生
414
平均合并
13 小时 1 分钟
30 天内合并 PR
2

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先检查现有的 ITokenRule 实现以及 issue 中描述的 AnalyzeTokens 入口点,尤其是 TokenKind.LineContinuation 和 Strings 中的诊断消息。确认可比规则是如何注册和测试的。完成的标准是:反引号行继续 token 会生成包含规则名称和源范围的信息性诊断,同时不会标记不相关的 token。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp, powershell
领域
tooling
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
58/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。