Skip to content

Commit

Permalink
[Feature]: Add Remove-LineComment filter to strip comments from cod…
Browse files Browse the repository at this point in the history
…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
MariusStorhaug authored Feb 1, 2025
1 parent da0f883 commit 0ee8bd6
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/functions/public/Core/Get-FunctionAST.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
Get-FunctionAST -Name "Get-Data" -Path "C:\Scripts\MyScript.ps1"
Retrieves only the function definition named "Get-Data" from "MyScript.ps1".
.LINK
https://psmodule.io/AST/Functions/Core/Get-FunctionAST/
#>
[CmdletBinding()]
param (
Expand Down
3 changes: 3 additions & 0 deletions src/functions/public/Core/Get-ScriptAST.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
Get-ScriptAST -Path "C:\Scripts\MyScript.ps1" -Tokens ([ref]$tokens) -Errors ([ref]$errors)
Parses the script file while also capturing tokens and errors.
.LINK
https://psmodule.io/AST/Functions/Core/Get-ScriptAST/
#>
[outputType([System.Management.Automation.Language.ScriptBlockAst])]
[CmdletBinding()]
Expand Down
3 changes: 3 additions & 0 deletions src/functions/public/Functions/Get-FunctionAlias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
Get-FunctionAlias -Name "Get-Data" -Path "C:\Scripts\MyScript.ps1"
Retrieves the alias information for the function named "Get-Data" from the specified script file.
.LINK
https://psmodule.io/AST/Functions/Functions/Get-FunctionAlias/
#>
[CmdletBinding()]
param (
Expand Down
3 changes: 3 additions & 0 deletions src/functions/public/Functions/Get-FunctionName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
.NOTES
Uses PowerShell's AST to analyze script structure.
.LINK
https://psmodule.io/AST/Functions/Functions/Get-FunctionName/
#>

[CmdletBinding()]
Expand Down
3 changes: 3 additions & 0 deletions src/functions/public/Functions/Get-FunctionType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
Get-FunctionType -Path "C:\Scripts\MyScript.ps1"
Retrieves all function types defined in the specified script file.
.LINK
https://psmodule.io/AST/Functions/Functions/Get-FunctionType/
#>
[OutputType([pscustomobject])]
[CmdletBinding()]
Expand Down
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
}
3 changes: 3 additions & 0 deletions src/functions/public/Scripts/Get-ScriptCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
Get-ScriptCommand -Path "C:\Scripts\example.ps1" -IncludeCallOperators
Extracts all commands, including those executed with call operators (& and .).
.LINK
https://psmodule.io/AST/Functions/Scripts/Get-ScriptCommand/
#>
[Alias('Get-ScriptCommands')]
[CmdletBinding()]
Expand Down
24 changes: 24 additions & 0 deletions tests/Module.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ Describe 'Functions' {
}
}

Describe 'Line' {
Context 'Function: Get-LineComment' {
It 'Get-LineComment gets the line comment' {
$line = '# This is a comment'
$line = Get-LineComment -Line $line
$line | Should -Be '# This is a comment'
}
It 'Get-LineComment gets the line comment without leading whitespace' {
$line = ' # This is a comment'
$line = Get-LineComment -Line $line
$line | Should -Be '# This is a comment'
}
It 'Get-LineComment gets the line comment but not the command' {
$line = ' Get-Command # This is a comment '
$line = Get-LineComment -Line $line
$line | Should -Be '# This is a comment '
}
It 'Get-LineComment returns nothing when no comment is present' {
$line = 'Get-Command'
$line | Get-LineComment | Should -BeNullOrEmpty
}
}
}

Describe 'Scripts' {
Context "Function: 'Get-ScriptCommands'" {
It 'Get-ScriptCommands gets the script commands' {
Expand Down

0 comments on commit 0ee8bd6

Please sign in to comment.