< Summary - Kestrun — Combined Coverage

Information
Class: Public.Helper.NoDefault
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Helper/NoDefault.ps1
Tag: Kestrun/Kestrun@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
0%
Covered lines: 0
Uncovered lines: 41
Coverable lines: 41
Total lines: 127
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 01/08/2026 - 02:20:28 Line coverage: 0% (0/30) Total lines: 105 Tag: Kestrun/Kestrun@4bc17b7e465c315de6386907c417e44fcb0fd3eb01/12/2026 - 18:03:06 Line coverage: 0% (0/41) Total lines: 127 Tag: Kestrun/Kestrun@956332ccc921363590dccd99d5707fb20b50966b

Metrics

Method
NoDefault()

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Helper/NoDefault.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Placeholder function to indicate no default value.
 4.DESCRIPTION
 5    This function serves as a marker to indicate that no default value is provided for a parameter.
 6    It returns $null when invoked.
 7    When used in parameter declarations, it allows the caller to distinguish between an explicit
 8    default value and the absence of a default.
 9.PARAMETER Value
 10    An optional value to return instead of the sentinel indicating no default.
 11    If provided, this value is returned immediately.
 12.EXAMPLE
 13    Usage example:
 14        function Test-Function {
 15            param(
 16                [datetime]$DateParam = (NoDefault)
 17            )
 18            if ($DateParam -eq [datetime]::MinValue) {
 19                Write-Output "No default provided for DateParam."
 20            } else {
 21                Write-Output "DateParam has value: $DateParam"
 22            }
 23        }
 24.NOTES
 25    When a parameter is declared with NoDefault as its default value, the function inspects
 26    the call stack to determine the static type of the parameter. If the type is a nullable
 27    type or a reference type, it returns $null. For non-nullable value types, it returns a sentinel
 28    value (e.g., [datetime]::MinValue for [datetime]) that can be detected by the caller.
 29    This allows functions to differentiate between parameters that have no default and those
 30    that have an explicit default value.
 31.OUTPUTS
 32    Returns $null.
 33#>
 34function NoDefault {
 35    param(
 36        [object] $Value = $null
 37    )
 38
 39    # If caller provided a real default, return it immediately.
 40    # Usage: [datetime]$x = (NoDefault ([datetime]'2026-01-01'))
 041    if ($PSBoundParameters.ContainsKey('Value')) {
 042        return $Value
 43    }
 44
 045    $call = (Get-PSCallStack)[1]
 046    if (-not $call.ScriptName) { return $null }
 47
 048    $tokens = $null
 049    $errors = $null
 050    $ast = [System.Management.Automation.Language.Parser]::ParseFile(
 51        $call.ScriptName, [ref]$tokens, [ref]$errors
 52    )
 53
 054    $line = $call.ScriptLineNumber
 55
 56    # Find the assignment ON THIS LINE whose RHS is NoDefault / (NoDefault) / $(NoDefault)
 057    $assign = $ast.FindAll({
 58            param($a)
 059            $a -is [System.Management.Automation.Language.AssignmentStatementAst] -and
 60            $a.Extent.StartLineNumber -le $line -and
 61            $a.Extent.EndLineNumber -ge $line -and
 062            ($a.Right.Extent.Text -match '^\s*\(?\s*NoDefault\b')
 063        }, $true) | Select-Object -First 1
 64
 065    if (-not $assign) { return $null }
 66
 67    # In script scope, LHS is typically an AttributedExpressionAst
 068    $lhs = $assign.Left
 69
 70    # Prefer the inner attributed child if present (matches what you printed)
 071    $attrib = $lhs.Child
 072    if ($attrib -isnot [System.Management.Automation.Language.AttributedExpressionAst]) {
 73        # fallback: maybe LHS itself is attributed
 074        if ($lhs -is [System.Management.Automation.Language.AttributedExpressionAst]) {
 075            $attrib = $lhs
 76        } else {
 077            return $null
 78        }
 79    }
 80
 081    switch ($attrib.Attribute.TypeName.name) {
 82        'ValidateSet' {
 083            $defaultNull = ($attrib.Attribute.Extent.Text -replace ".*ValidateSet\(|['\)]", '').Split(',')[0].Trim()
 084            $attrib = $attrib.Child
 85            break
 86        }
 87        'ValidateRange' {
 088            $defaultNull = ($attrib.Attribute.Extent.Text -replace '.*ValidateRange\(|[\]]', '').Split(',')[0].Trim()
 089            $attrib = $attrib.Child
 90            break
 91        }
 092        { $_ -in 'ValidateNotNull', 'ValidateNotNullOrWhiteSpace', 'ValidateNotNullOrEmpty' } {
 093            $defaultNull = '__FAKE_NULL__'
 094            $attrib = $attrib.Child
 95            break
 96        }
 097        default { $defaultNull = $null }
 98    }
 99
 0100    $t = $attrib.StaticType
 101
 102    # Nullable<T> => return $null
 0103    if ($t.IsGenericType -and $t.GetGenericTypeDefinition() -eq [Nullable`1]) {
 0104        return $defaultNull
 105    }
 106
 107    # Reference types => return $null
 0108    if (-not $t.IsValueType) {
 0109        return $defaultNull
 110    }
 111    # Nullable reference types => return $null
 0112    if ($null -ne $defaultNull) {
 0113        return $defaultNull
 114    }
 115    # Non-nullable value types => return a sentinel you can detect later
 0116    switch ($t.FullName) {
 0117        'System.DateTime' { return [datetime]::MinValue }
 0118        'System.DateTimeOffset' { return [datetimeoffset]::MinValue }
 0119        'System.Guid' { return [guid]::Empty }
 0120        'System.TimeSpan' { return [timespan]::Zero }
 121        default {
 122            # int => 0, bool => false, etc.
 0123            return [Activator]::CreateInstance($t)
 124        }
 125    }
 126}
 127

Methods/Properties

NoDefault()