PowerShell / PowerShell/PSScriptAnalyzer
PSUseOutputTypeCorrectly is triggered when it shouldn't.
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ả
It looks to me like PSUseOutputTypeCorrectly check is not taking very basic, crucial, fundamental fact about PowerShell into account: by default it will not keep any simple collection, unless you explicitly tell it to.
Result? I have PInvoke code (taken from here) and I correctly defined OutputType as [Connection] (I've actually created namespace for it, but that's irrelevant).
As expected, Get-Member confirms that PowerShell function will return stream of connections, not collection as a whole. Yet, PSScriptAnalyzer will complain about it:
Line: 45 - The cmdlet 'Get-OPNetStat' returns an object of type 'Optiver.Connection[]' but this type is not declared in the OutputType attribute.
Line: 54 - The cmdlet 'Get-OPNetStat' returns an object of type 'Optiver.Connection[]' but this type is not declared in the OutputType attribute.
In fact, if I would follow instructions and declare my OutputType as a collection it would be conflicting with what my function actually returns:
Set-StrictMode -Version Latest
Get-OPNetStat | % IsFixedSize
% : The input name "IsFixedSize" cannot be resolved to a member.
IsFixedSize was tab-completed, based on OutputType that PSScriptAnalyzer told me to define. As expected, my Connection object doesn't have it. The only workaround I've found that satisfies both PSScriptAnalyzer and my hope to get nice tab-completion for command I wrote is to pass result of the command thru ForEach-Object:
[Optiver.NetworkUtil]::GetTCP() } | ForEach-Object { $_ }
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 với quy tắc PSUseOutputTypeCorrectly và tái hiện ví dụ Get-OPNetStat đã được báo cáo, bao gồm đầu ra của [Optiver.NetworkUtil]::GetTCP() và hành vi của Get-Member. So sánh kiểu đầu ra [Connection] đã khai báo với chẩn đoán Connection[] của trình phân tích; hoàn tất khi đầu ra stream hợp lệ không còn tạo ra cảnh báo sai này, trong khi tính năng hoàn tất bằng phím Tab cho kiểu đầu ra vẫn chính xác.
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
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100