PowerShell / PowerShell/PSScriptAnalyzer

Rule Idea: Pipeline Type Checking against Output

Open
#1,760 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue - Discussion
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

This is just a spitball idea to contribute feedback if it is maybe feasible, I might consider attempting a PR

Problem

Often when writing a pipeline you may pipe an incompatible type, and you won't find out until runtime.

Proposed solution

If the output type of a function can be explicitly determined by:

  1. Use the .OUTPUT comment based help section to define the output type of a function
  2. Take all explicit return keywords that explicitly cast to a type e.g. return [MyType]@{} or can be simply derived from a typed variable e.g. $f = [MyType]::new();return $f

And the function being called either to or from meets the following criteria:

  1. Has strictly typed non-primitive parameter or ValueFromPipeline

Then we should be able to safely issue a warning or error stating something like "MyFunction expected [MyType] but is receiving [WrongType] via the pipeline" as a static analysis.

#Issues

  1. Easily castable types like string we have to exclude, they will basically always match
  2. Probably should support/detect explicit casts or interfaces

Prior Art

How JSDoc can be used to test for type safety in normally-untyped javascript.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed output-type sources, pipeline parameter checks, casting exceptions, and the JSDoc prior art described in the issue. Define the analyzer entry point and scope before implementation; done means a decided approach can detect incompatible pipeline types while excluding safely castable values and handling explicit casts or interfaces.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.