PowerShell / PowerShell/PSScriptAnalyzer
Rule request: AvoidUsingBacktickLineTerminator
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C#
- Star
- 2.2k
- Fork
- 414
- Merge trung bình
- 13 giờ 1 phút
- Pull request đã merge (30 ngày)
- 2
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách kiểm tra các triển khai ITokenRule hiện có và điểm vào AnalyzeTokens được mô tả trong issue, đặc biệt là TokenKind.LineContinuation và thông báo chẩn đoán trong Strings. Xác nhận cách các rule tương tự được đăng ký và kiểm thử. Hoàn thành khi các token tiếp tục dòng bằng backtick tạo ra các chẩn đoán thông tin kèm tên rule và phạm vi nguồn, mà không đánh dấu các token không liên quan.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- csharp, powershell
- Lĩnh vực
- tooling
- Loại issue
- Tính năng
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 58/100