Skip to content

Conversation

@yotsuda
Copy link
Contributor

@yotsuda yotsuda commented Nov 23, 2025

PR Summary

Allow HiddenAttribute to be applied to classes and exclude hidden classes from type name completion.

PR Context

Fixes #18914

The HiddenAttribute can be applied to properties, methods, and other members to hide them from tab completion and Get-Member, but could not be applied to classes themselves. This PR extends the attribute to support classes and ensures hidden classes are excluded from type name completion.

Before this fix:

[Hidden]  // Error: Not valid on this declaration type
public class InternalHelperClass { }

After this fix:

[Hidden]  // Now works - class is hidden from completion
public class InternalHelperClass { }

var obj = new InternalHelperClass();  // Can still instantiate explicitly
[Internal<Tab>]  // Does NOT suggest InternalHelperClass

PR Checklist

Description

Implementation

1. Extended HiddenAttribute target (Attributes.cs)

  • Added AttributeTargets.Class to AttributeUsage

2. Added IsHidden property to TypeDefinitionAst (ast.cs)

  • Cached property following the same pattern as PropertyMemberAst.IsHidden
  • Uses GetReflectionAttributeType() for attribute checking

3. Filtered hidden classes from type completion (CompletionCompleters.cs)

  • PowerShell-defined types: Filter using !ast.IsHidden
  • C#-defined types: Filter using type.IsDefined(typeof(HiddenAttribute), false)

Files Changed

  • src/System.Management.Automation/engine/Attributes.cs - Extended AttributeUsage
  • src/System.Management.Automation/engine/parser/ast.cs - Added IsHidden property
  • src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs - Filter hidden types
  • test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 - Added tests

Testing

Test Coverage

Added 3 comprehensive test cases:

  1. Instance creation - Verifies hidden classes can be instantiated
  2. Attribute presence - Validates attribute is correctly applied
  3. Type completion exclusion - Confirms hidden classes don't appear in tab completion

Results

Describing HiddenAttribute on Class Test
  [+] Should be able to create an instance of hidden class 7ms
  [+] HiddenAttribute should be present on the class 14ms
  [+] Hidden class should not appear in type name completion 142ms

Tests Passed: 327, Failed: 0, Skipped: 11, Pending: 13, Inconclusive: 0

Additional Notes

  • No breaking changes - only adds new functionality
  • Consistent with existing HiddenAttribute behavior for members
  • Performance: Attribute check is cached to avoid repeated reflection calls
  • Useful for hiding internal helper classes in modules

…etion

- Extend AttributeUsage to include AttributeTargets.Class
- Add IsHidden property to TypeDefinitionAst with caching
- Filter hidden classes from type name completion in CompleteType
- Add comprehensive tests for hidden class functionality
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 24, 2025
@iSazonov iSazonov requested a review from Copilot November 24, 2025 05:11
@iSazonov
Copy link
Collaborator

@MartinGC94 Please review.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the HiddenAttribute to support class-level application, allowing developers to hide internal helper classes from type name completion while still allowing explicit instantiation. The feature complements the existing ability to hide class members from Get-Member and Format-* cmdlets.

Key changes:

  • Extended HiddenAttribute target to include classes
  • Added cached IsHidden property to TypeDefinitionAst for PowerShell-defined classes
  • Updated type completion to filter out hidden classes from both C# and PowerShell definitions

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/System.Management.Automation/engine/Attributes.cs Added AttributeTargets.Class to HiddenAttribute.AttributeUsage
src/System.Management.Automation/engine/parser/ast.cs Added cached IsHidden property to TypeDefinitionAst using reflection-based attribute checking
src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs Filtered hidden classes from type completion for both loaded assemblies (C# types) and script-defined types (PowerShell classes)
test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 Added test cases verifying instantiation, attribute presence, and completion exclusion for C#-defined hidden classes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@MartinGC94
Copy link
Contributor

It seems fine but I don't see a test for hiding PowerShell classes from tabcompletion: TabExpansion2 '[hidden()] class Test1{} [Test1'

This uses the AST so it's handled differently than C# classes are.

@yotsuda yotsuda force-pushed the PowerShell-issue-18914 branch from faa095d to a5344fe Compare November 25, 2025 14:29
@yotsuda
Copy link
Contributor Author

yotsuda commented Nov 25, 2025

@MartinGC94 Thank you for the insightful review! You're absolutely right - the [hidden()] syntax wasn't available for PowerShell classes (missing type accelerator), and there were no tests for the AST-based scenario you mentioned.

I've added the following in a5344fe:

  1. Hidden type accelerator - Registered HiddenAttribute in CoreTypes to enable [hidden()] syntax for PowerShell classes, consistent with other attributes like [Parameter()]

  2. Comprehensive tests - Including your specific scenario [hidden()] class Test1{} [Test1, plus regression tests to ensure visible classes still appear in completion

All tests pass (332 passed).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Dec 3, 2025
@microsoft-github-policy-service
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow HiddenAttribute to decorate classes

3 participants