Skip to content

A mechanism to allow types in other files to be seen by VS Code #3082

@nathan-alden-hp

Description

@nathan-alden-hp

Recreating PowerShell/PSScriptAnalyzer#1617 here for visibility, as I'm not sure whether this feature request relates to the analyzer, the extension, or both.

Summary of the new feature/enhancement

In the programming world, developers are encouraged to follow a "one type per file" rule. I am trying to apply this rule to classes in PowerShell. Unfortunately, I can't find a way to allow my editor (VS Code in my case) to "see" types in other files. Consider this trivial case where I have a module consisting of two classes:

Class1.ps1

class Class1 {
    [void]Method() {
    }
}

Class2.ps1

using module .\Class1.ps1

class Class2 {
    Class2() {
        $c = [Class1]::new() # Class1 is visible to the analyzer
        $c. # Method() is not visible
    }
}

I experimented with using module .\Class1.ps1 to fake out the editor into thinking the script file was a module. This works, but only the type becomes visible; its members do not.

image

If I inline the class, its members become visible, as one would expect:

image

Is there a way around this problem?

Proposed technical implementation details (optional)

I'm not sure what can be done to improve the developer experience, but as it stands now this is preventing me from using classes to their full extent in my more complex PowerShell modules. I know I could always create a full-blown C# project, but that introduces build pipeline and CI dependencies I'm trying to avoid. It also means developers who work on my module need to be more aware of .NET tools like Visual Studio than they otherwise would need to be.

Some kind of special comment used by the extension and/or analyzer to temporarily import other files for the purposes of parsing, perhaps?

#analyze Class1.ps1

This has the following benefits:

  • It's a comment, so no special scripting is required to remove the line before packaging for deployment
  • Functions similar to #region in that it's an editor-specific feature
  • Familiar to programmers used to #include from C or other similar languages

VS Code PowerShell extension version

2020.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions