Skip to content

Commit 0ee8bd6

Browse files
[Feature]: Add Remove-LineComment filter to strip comments from code 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
1 parent da0f883 commit 0ee8bd6

8 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/functions/public/Core/Get-FunctionAST.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
Get-FunctionAST -Name "Get-Data" -Path "C:\Scripts\MyScript.ps1"
1717
1818
Retrieves only the function definition named "Get-Data" from "MyScript.ps1".
19+
20+
.LINK
21+
https://psmodule.io/AST/Functions/Core/Get-FunctionAST/
1922
#>
2023
[CmdletBinding()]
2124
param (

src/functions/public/Core/Get-ScriptAST.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
Get-ScriptAST -Path "C:\Scripts\MyScript.ps1" -Tokens ([ref]$tokens) -Errors ([ref]$errors)
2222
2323
Parses the script file while also capturing tokens and errors.
24+
25+
.LINK
26+
https://psmodule.io/AST/Functions/Core/Get-ScriptAST/
2427
#>
2528
[outputType([System.Management.Automation.Language.ScriptBlockAst])]
2629
[CmdletBinding()]

src/functions/public/Functions/Get-FunctionAlias.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
Get-FunctionAlias -Name "Get-Data" -Path "C:\Scripts\MyScript.ps1"
1717
1818
Retrieves the alias information for the function named "Get-Data" from the specified script file.
19+
20+
.LINK
21+
https://psmodule.io/AST/Functions/Functions/Get-FunctionAlias/
1922
#>
2023
[CmdletBinding()]
2124
param (

src/functions/public/Functions/Get-FunctionName.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
1616
.NOTES
1717
Uses PowerShell's AST to analyze script structure.
18+
19+
.LINK
20+
https://psmodule.io/AST/Functions/Functions/Get-FunctionName/
1821
#>
1922

2023
[CmdletBinding()]

src/functions/public/Functions/Get-FunctionType.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
Get-FunctionType -Path "C:\Scripts\MyScript.ps1"
1313
1414
Retrieves all function types defined in the specified script file.
15+
16+
.LINK
17+
https://psmodule.io/AST/Functions/Functions/Get-FunctionType/
1518
#>
1619
[OutputType([pscustomobject])]
1720
[CmdletBinding()]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
filter Get-LineComment {
2+
<#
3+
.SYNOPSIS
4+
Extracts the inline comment from a single line of PowerShell code.
5+
6+
.DESCRIPTION
7+
Parses a given line of PowerShell code and extracts any inline comment.
8+
If an inline comment exists, the function returns the comment text; otherwise, it returns nothing.
9+
10+
.EXAMPLE
11+
'Get-Process # This retrieves all processes' | Get-LineComment
12+
13+
Returns: '# This retrieves all processes'
14+
15+
.EXAMPLE
16+
'Write-Host "Hello World"' | Get-LineComment
17+
18+
Returns: $null (no comment found)
19+
20+
.LINK
21+
https://psmodule.io/AST/Functions/Lines/Get-LineComment/
22+
#>
23+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
24+
'PSUseShouldProcessForStateChangingFunctions', '',
25+
Justification = 'Does not change state'
26+
)]
27+
[OutputType([string])]
28+
[CmdletBinding()]
29+
param (
30+
# Input line of PowerShell code from which to extract the comment.
31+
[Parameter(
32+
Mandatory,
33+
ValueFromPipeline
34+
)]
35+
[string] $Line
36+
)
37+
38+
# Parse the line using the PowerShell parser to obtain its tokens.
39+
$tokens = $null
40+
$null = [System.Management.Automation.Language.Parser]::ParseInput($Line, [ref]$tokens, [ref]$null)
41+
42+
# Find comment token(s) in the line.
43+
($tokens | Where-Object { $_.Kind -eq 'Comment' }).Extent.Text
44+
}

src/functions/public/Scripts/Get-ScriptCommand.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
Get-ScriptCommand -Path "C:\Scripts\example.ps1" -IncludeCallOperators
1818
1919
Extracts all commands, including those executed with call operators (& and .).
20+
21+
.LINK
22+
https://psmodule.io/AST/Functions/Scripts/Get-ScriptCommand/
2023
#>
2124
[Alias('Get-ScriptCommands')]
2225
[CmdletBinding()]

tests/Module.Tests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ Describe 'Functions' {
4343
}
4444
}
4545

46+
Describe 'Line' {
47+
Context 'Function: Get-LineComment' {
48+
It 'Get-LineComment gets the line comment' {
49+
$line = '# This is a comment'
50+
$line = Get-LineComment -Line $line
51+
$line | Should -Be '# This is a comment'
52+
}
53+
It 'Get-LineComment gets the line comment without leading whitespace' {
54+
$line = ' # This is a comment'
55+
$line = Get-LineComment -Line $line
56+
$line | Should -Be '# This is a comment'
57+
}
58+
It 'Get-LineComment gets the line comment but not the command' {
59+
$line = ' Get-Command # This is a comment '
60+
$line = Get-LineComment -Line $line
61+
$line | Should -Be '# This is a comment '
62+
}
63+
It 'Get-LineComment returns nothing when no comment is present' {
64+
$line = 'Get-Command'
65+
$line | Get-LineComment | Should -BeNullOrEmpty
66+
}
67+
}
68+
}
69+
4670
Describe 'Scripts' {
4771
Context "Function: 'Get-ScriptCommands'" {
4872
It 'Get-ScriptCommands gets the script commands' {

0 commit comments

Comments
 (0)