generated from PSModule/Template-PSModule
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]: Add
Remove-LineComment
filter to strip comments from cod…
…e lines (#14) ## Description This pull request introduces a new PowerShell filter `Remove-LineComment`. * Addition of the `Remove-LineComment` filter, which uses the PowerShell Abstract Syntax Tree (AST) to parse the input line and identify comment tokens. If a comment is found, it is removed, and the modified line is returned. If no comment is present, the original line is returned unchanged. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ]⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
- Loading branch information
1 parent
da0f883
commit 0ee8bd6
Showing
8 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
filter Get-LineComment { | ||
<# | ||
.SYNOPSIS | ||
Extracts the inline comment from a single line of PowerShell code. | ||
.DESCRIPTION | ||
Parses a given line of PowerShell code and extracts any inline comment. | ||
If an inline comment exists, the function returns the comment text; otherwise, it returns nothing. | ||
.EXAMPLE | ||
'Get-Process # This retrieves all processes' | Get-LineComment | ||
Returns: '# This retrieves all processes' | ||
.EXAMPLE | ||
'Write-Host "Hello World"' | Get-LineComment | ||
Returns: $null (no comment found) | ||
.LINK | ||
https://psmodule.io/AST/Functions/Lines/Get-LineComment/ | ||
#> | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute( | ||
'PSUseShouldProcessForStateChangingFunctions', '', | ||
Justification = 'Does not change state' | ||
)] | ||
[OutputType([string])] | ||
[CmdletBinding()] | ||
param ( | ||
# Input line of PowerShell code from which to extract the comment. | ||
[Parameter( | ||
Mandatory, | ||
ValueFromPipeline | ||
)] | ||
[string] $Line | ||
) | ||
|
||
# Parse the line using the PowerShell parser to obtain its tokens. | ||
$tokens = $null | ||
$null = [System.Management.Automation.Language.Parser]::ParseInput($Line, [ref]$tokens, [ref]$null) | ||
|
||
# Find comment token(s) in the line. | ||
($tokens | Where-Object { $_.Kind -eq 'Comment' }).Extent.Text | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters