PowerShell / PowerShell/PSScriptAnalyzer
Rule request: Warning if `Set`/`Get-Variable` is invoked with only the basic `-Name`/`-Value` parameters
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- C#
- Estrellas
- 2.2k
- Forks
- 414
- Merge medio
- 13 h 1 min
- PR fusionados (30 d)
- 2
Descripción
Referring to StackOverflow questions along with: How do I automaticaly create and use variable names?.
The cmdlets:
Could completely mislead a novice PowerShell scripter in a wrong programming direction.
The general learning process of a scripter/programmer is playing with variables. Than it gets extended to list of variables like $Var1 = 'One', $Var2 = 'Two', .... The list extends further and they understand they need to automate this somehow but finding guidance for this is quiet a leap and if they ask for guidance at e.g. StackOverflow they often get a direct answer/comment in the direction of using one of these <verb>-Variable cmdlets which is not a good practice for these users and what they atempt to do or learn.
Afaik, there is no good reason to use one of these <verb>-Variable cmdlets which just -Name and -Value parameters.
e.g. Get-Variable MyVar can be replaced by $MyVar and Set-Variable MyVar 'Test' can be replaced by $MyVar = 'Test'
The only other reason to use just the -Name and -Value parameters is to create dynamic variable names (variable indirection) like:
Set-Variable "Var$i" 'Test'
Which is to my opinion a bad practice as they will be added to the same dictionary as the rest of the parameters and one might easily overwrite other general variables and lose the oversight.
Summary of the new feature
When it comes to creating a dynamic variables, a separated dictionary (in the form of a hashtable) should be created and a warning/hint could be given in that direction.
Visa verse, advanced users that know what they are doing, might simply avoid the warning by adding a specific (e.g. -Scope Script) parameter.
What is the latest version of PSScriptAnalyzer at the point of writing
1.19.1
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza revisando las implementaciones y pruebas existentes de reglas de PSScriptAnalyzer y, después, sigue cómo se analizan las llamadas a Get-Variable, Set-Variable y New-Variable. Define la condición de advertencia para las llamadas que usan únicamente parámetros básicos de nombre/valor y la exención para un parámetro de ámbito explícito; el trabajo estará completo cuando la regla y sus pruebas cubran esos casos.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- powershell
- Área
- tooling
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100