PowerShell / PowerShell/PSScriptAnalyzer
Rule request: AvoidUsingBacktickLineTerminator
还没有人认领这个 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:
- Parameter Splatting: For commands with multiple parameters
- Natural line breaks: After pipeline operators (
|), logical operators (-and,-or), comparison operators - 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先检查现有的 ITokenRule 实现以及 issue 中描述的 AnalyzeTokens 入口点,尤其是 TokenKind.LineContinuation 和 Strings 中的诊断消息。确认可比规则是如何注册和测试的。完成的标准是:反引号行继续 token 会生成包含规则名称和源范围的信息性诊断,同时不会标记不相关的 token。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- csharp, powershell
- 领域
- tooling
- Issue 类型
- 功能
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 58/100