PowerShell / PowerShell/PSScriptAnalyzer
Rule request: Warning if `Set`/`Get-Variable` is invoked with only the basic `-Name`/`-Value` parameters
Personne n'a encore pris cette issue.
- Langage dominant
- C#
- Étoiles
- 2.2k
- Forks
- 414
- Merge moyen
- 13 h 1 min
- PR mergées (30 j)
- 2
Description
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner les implémentations et les tests existants des règles de PSScriptAnalyzer, puis suivez la manière dont sont analysés les appels à Get-Variable, Set-Variable et New-Variable. Définissez la condition d’avertissement pour les appels utilisant uniquement des paramètres de base de nom/valeur, ainsi que l’exemption pour un paramètre de portée explicite ; le travail est terminé lorsque la règle et ses tests couvrent ces cas.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- powershell
- Domaine
- tooling
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100