February 2019
Intermediate to advanced
626 pages
15h 51m
English
Rules based on tokens evaluate an array of tokens to make a decision. The following example looks for empty single-line comments in a block of code. Comments are not a part of the syntax tree, so using tokens is the only option:
using namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Genericusing namespace System.Management.Automation.Languagefunction PSAvoidEmptyComments { [CmdletBinding()] [OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord])] param ( [Token[]]$token ) $ruleName = $myinvocation.MyCommand.Name $token.Where{ $_.Kind -eq 'Comment' -and $_.Text.Trim() -eq '#' }.ForEach{ [DiagnosticRecord]@{ Message = 'Empty comment.' Extent = $_.Extent RuleName = $ruleName Severity = 'Information' ...Read now
Unlock full access