From d1a6a5f7e8ec2c39f6b9425483a89afb14edb27e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 2 Feb 2025 00:21:04 +0100 Subject: [PATCH] Add Get-LineComment filter to extract inline comments from PowerShell code lines --- .../public/Lines/Get-LineComment.ps1 | 44 ++++++++++++++ .../public/Lines/Remove-LineComment.ps1 | 58 ------------------- 2 files changed, 44 insertions(+), 58 deletions(-) create mode 100644 src/functions/public/Lines/Get-LineComment.ps1 delete mode 100644 src/functions/public/Lines/Remove-LineComment.ps1 diff --git a/src/functions/public/Lines/Get-LineComment.ps1 b/src/functions/public/Lines/Get-LineComment.ps1 new file mode 100644 index 0000000..4dc64ed --- /dev/null +++ b/src/functions/public/Lines/Get-LineComment.ps1 @@ -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 +} diff --git a/src/functions/public/Lines/Remove-LineComment.ps1 b/src/functions/public/Lines/Remove-LineComment.ps1 deleted file mode 100644 index 0062b56..0000000 --- a/src/functions/public/Lines/Remove-LineComment.ps1 +++ /dev/null @@ -1,58 +0,0 @@ -filter Remove-LineComment { - <# - .SYNOPSIS - Removes inline comments from a single line of PowerShell code. - - .DESCRIPTION - Parses a given line of PowerShell code and removes any inline comments. - Returns the modified line without the comment while preserving the rest of the code. - - .EXAMPLE - 'Get-Process # This retrieves all processes' | Remove-LineComment - - Returns: 'Get-Process' - - This removes the comment from the input line and outputs only the valid PowerShell command. - - .EXAMPLE - 'Write-Host "Hello World"' | Remove-LineComment - - Returns: 'Write-Host "Hello World"' - - If no comment is present, the original line is returned unchanged. - - .LINK - https://psmodule.io/AST/Functions/Lines/Remove-LineComment/ - #> - [Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSUseShouldProcessForStateChangingFunctions', '', - Justification = 'Does not change state' - )] - [OutputType([string])] - [CmdletBinding()] - param ( - # Input line of PowerShell code to remove the comment from. - [Parameter( - Mandatory, - ValueFromPipeline - )] - [string] $Line - ) - - # Parse the single line using AST - $tokens = $null - $null = [System.Management.Automation.Language.Parser]::ParseInput($Line, [ref]$tokens, [ref]$null) - - # Find comment tokens in the line - $commentToken = $tokens | Where-Object { $_.Kind -eq 'Comment' } - - if ($commentToken) { - $commentStart = $commentToken.Extent.StartColumnNumber - 1 # Convert to zero-based index - - # Remove the comment by trimming the line up to the comment start position - return $Line.Substring(0, $commentStart) - } - - # Return original line if no comment was found - return $Line -}