-
Notifications
You must be signed in to change notification settings - Fork 399
Add Fix switch parameter for 'File' parameter set to auto-correct warnings (#802) #817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
77ff614
ef75719
24732ee
4dc4aeb
792fe30
5a2f47b
023f9e3
5d0b52d
78d9663
6edeef4
f129846
bfa1c54
50af5d2
771ffb6
701c264
a16c232
e64335f
4297ff4
9785993
14197ce
fffcd76
c707a1e
9d4e8ef
c0f881d
c59b6b0
6f6896a
f5ac9eb
8e46750
e184e3b
0df445b
8525e03
e12c4a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
using System.Collections.ObjectModel; | ||
using System.Collections; | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer | ||
{ | ||
|
@@ -1451,11 +1452,12 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn | |
/// </summary> | ||
/// <param name="path">The path of the file or directory to analyze.</param> | ||
/// <param name="searchRecursively"> | ||
/// <param name="fix"> | ||
/// If true, recursively searches the given file path and analyzes any | ||
/// script files that are found. | ||
/// </param> | ||
/// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns> | ||
public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursively = false) | ||
public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursively = false, bool fix = false) | ||
{ | ||
List<string> scriptFilePaths = new List<string>(); | ||
|
||
|
@@ -1475,6 +1477,13 @@ public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursi | |
this.BuildScriptPathList(path, searchRecursively, scriptFilePaths); | ||
foreach (string scriptFilePath in scriptFilePaths) | ||
{ | ||
if (fix) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the right place for this block is in the InvokeScriptAnalyzerCommand.cs file. We do not need to change behavior 'Analyzepath` method, which should only analyze and not fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would require some re-factoring of the project because one needs to call the private method |
||
var fileEncoding = GetFileEncoding(scriptFilePath); | ||
var scriptFileContentWithFixes = Fix(File.ReadAllText(scriptFilePath, fileEncoding)); | ||
File.WriteAllText(scriptFilePath, scriptFileContentWithFixes, fileEncoding); // although this preserves the encoding, it will add a BOM to UTF8 files | ||
} | ||
|
||
// Yield each record in the result so that the | ||
// caller can pull them one at a time | ||
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath)) | ||
|
@@ -1613,6 +1622,18 @@ public EditableText Fix(EditableText text, Range range, out Range updatedRange) | |
return text; | ||
} | ||
|
||
private static Encoding GetFileEncoding(string path) | ||
{ | ||
using (var stream = new FileStream(path, FileMode.Open)) | ||
{ | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
reader.Peek(); // needed in order to populate the CurrentEncoding property | ||
return reader.CurrentEncoding; | ||
} | ||
} | ||
} | ||
|
||
private static Range SnapToEdges(EditableText text, Range range) | ||
{ | ||
// todo add TextLines.Validate(range) and TextLines.Validate(position) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ Evaluates a script or module based on selected best practice rules | |
### UNNAMED_PARAMETER_SET_1 | ||
``` | ||
Invoke-ScriptAnalyzer [-Path] <String> [-CustomRulePath <String>] [-RecurseCustomRulePath] | ||
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] | ||
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] [-Fix] | ||
[-Settings <String>] | ||
``` | ||
|
||
|
@@ -399,6 +399,23 @@ Accept pipeline input: False | |
Accept wildcard characters: False | ||
``` | ||
|
||
### -Fix | ||
Certain warnings contain a suggested fix in their DiagnosticRecord and therefore those warnings will be fixed automatically using this fix. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest a bit of rewording to be like other switch parameters Fixes certain warnings which contain a fix in their DiagnosticRecord. |
||
|
||
When you used Fix, Invoke-ScriptAnalyzer runs as usual but will apply the fixes before running the analysis. Please make sure that you have a backup of your files when using this switch. It tries to pre-server the file encoding but it is possible that a BOM gets added to the fixed files. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pre-server -> preserve. |
||
|
||
```yaml | ||
Type: SwitchParameter | ||
Parameter Sets: UNNAMED_PARAMETER_SET_1 | ||
Aliases: | ||
|
||
Required: False | ||
Position: Named | ||
Default value: False | ||
Accept pipeline input: False | ||
Accept wildcard characters: False | ||
``` | ||
|
||
### -Settings | ||
File path that contains user profile or hash table for ScriptAnalyzer | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can some tests be added to test new code?