diff --git a/src/functions/public/Core/Get-FunctionAST.ps1 b/src/functions/public/Core/Get-FunctionAST.ps1 index 5333c85..7843ce5 100644 --- a/src/functions/public/Core/Get-FunctionAST.ps1 +++ b/src/functions/public/Core/Get-FunctionAST.ps1 @@ -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 ( diff --git a/src/functions/public/Core/Get-ScriptAST.ps1 b/src/functions/public/Core/Get-ScriptAST.ps1 index d9f7fdf..242968e 100644 --- a/src/functions/public/Core/Get-ScriptAST.ps1 +++ b/src/functions/public/Core/Get-ScriptAST.ps1 @@ -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()] diff --git a/src/functions/public/Functions/Get-FunctionAlias.ps1 b/src/functions/public/Functions/Get-FunctionAlias.ps1 index f96c081..3265c7e 100644 --- a/src/functions/public/Functions/Get-FunctionAlias.ps1 +++ b/src/functions/public/Functions/Get-FunctionAlias.ps1 @@ -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 ( diff --git a/src/functions/public/Functions/Get-FunctionName.ps1 b/src/functions/public/Functions/Get-FunctionName.ps1 index b9b4a20..4277f9f 100644 --- a/src/functions/public/Functions/Get-FunctionName.ps1 +++ b/src/functions/public/Functions/Get-FunctionName.ps1 @@ -15,6 +15,9 @@ .NOTES Uses PowerShell's AST to analyze script structure. + + .LINK + https://psmodule.io/AST/Functions/Functions/Get-FunctionName/ #> [CmdletBinding()] diff --git a/src/functions/public/Functions/Get-FunctionType.ps1 b/src/functions/public/Functions/Get-FunctionType.ps1 index 4b8c5fd..cc66efa 100644 --- a/src/functions/public/Functions/Get-FunctionType.ps1 +++ b/src/functions/public/Functions/Get-FunctionType.ps1 @@ -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()] 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/Scripts/Get-ScriptCommand.ps1 b/src/functions/public/Scripts/Get-ScriptCommand.ps1 index 0b02044..6c4c080 100644 --- a/src/functions/public/Scripts/Get-ScriptCommand.ps1 +++ b/src/functions/public/Scripts/Get-ScriptCommand.ps1 @@ -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()] diff --git a/tests/Module.Tests.ps1 b/tests/Module.Tests.ps1 index 201d4cc..00a6d2b 100644 --- a/tests/Module.Tests.ps1 +++ b/tests/Module.Tests.ps1 @@ -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' {