Skip to content

Commit

Permalink
Add Get-LineComment filter to extract inline comments from PowerShell…
Browse files Browse the repository at this point in the history
… code lines
  • Loading branch information
MariusStorhaug committed Feb 1, 2025
1 parent ad2089b commit d1a6a5f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 58 deletions.
44 changes: 44 additions & 0 deletions src/functions/public/Lines/Get-LineComment.ps1
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
}
58 changes: 0 additions & 58 deletions src/functions/public/Lines/Remove-LineComment.ps1

This file was deleted.

0 comments on commit d1a6a5f

Please sign in to comment.