Files
OnebotCatalog/sample-data/tools/parse-check.ps1

21 lines
769 B
PowerShell

# 用真实解析器定位 .ps1 语法错误, 并打印出错行上下文
param([string]$File = (Join-Path $PSScriptRoot 'gen-step.ps1'))
$tokens = $null
$errors = $null
[System.Management.Automation.Language.Parser]::ParseFile($File, [ref]$tokens, [ref]$errors) | Out-Null
$lines = [System.IO.File]::ReadAllLines($File)
Write-Output ("总行数: " + $lines.Count)
if ($errors.Count -eq 0) {
Write-Output "语法 OK"
} else {
foreach ($e in $errors) {
$ln = $e.Extent.StartLineNumber
Write-Output ("--- 错误 行{0} 列{1}: {2}" -f $ln, $e.Extent.StartColumnNumber, $e.Message)
for ($i = $ln - 3; $i -le ($ln + 1); $i++) {
if ($i -ge 1 -and $i -le $lines.Count) {
Write-Output ("{0,4} | {1}" -f $i, $lines[$i - 1])
}
}
}
}