PowerShell / PowerShell/PSScriptAnalyzer
Rule Proposal: Warn on dot-sourced function
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.2k
- Forks
- 414
- Avg merge
- 13h 1m
- Merged PRs (30d)
- 2
Description
I've inherited a project where everything is in, for lack of a better description, undeclared global scope, spread across several files. Functions don't have always have parameters, and are called with a dot before the function name. Variables are treated as global, though not declared. For example:
c:\Utils.ps1
function Write-MyLog {
$itemToLog | out-file c:\mylog.txt -append
}
c:\Interface.ps1
. c:\Utils.ps1
$itemToLog = "Something happened"
. Write-MyLog
Miraculously, this all works. Of course, I dare not change anything.
I would love it if ". Write-MyLog" came up as a warning, and even more so if there were some indication that "$itemToLog" in C:\Interface.ps1 was being used by the function called from c:\Utils.ps1.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the behavior in the example c:\Utils.ps1 and c:\Interface.ps1 scripts, focusing on the dot-sourced function call and the cross-file $itemToLog variable. Done means the analyzer warns on the requested construct and provides the requested indication of variable usage, with the scope and expected diagnostics agreed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- powershell
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100