Skip to content

Commit f129846

Browse files
committed
Refactor 'fix' switch out of AnalyzePath method as requested in PR
1 parent 6edeef4 commit f129846

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,14 @@ private void ProcessInput()
388388
{
389389
foreach (var p in processedPaths)
390390
{
391-
diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(p, this.recurse, this.fix);
391+
if (fix)
392+
{
393+
diagnosticsList = ScriptAnalyzer.Instance.AnalyzeAndFixPath(p, this.recurse);
394+
}
395+
else
396+
{
397+
diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(p, this.recurse);
398+
}
392399
WriteToOutput(diagnosticsList);
393400
}
394401
}

Engine/ScriptAnalyzer.cs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,40 +1452,44 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
14521452
/// </summary>
14531453
/// <param name="path">The path of the file or directory to analyze.</param>
14541454
/// <param name="searchRecursively">
1455-
/// <param name="fix">
14561455
/// If true, recursively searches the given file path and analyzes any
14571456
/// script files that are found.
14581457
/// </param>
14591458
/// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns>
1460-
public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursively = false, bool fix = false)
1459+
public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursively = false)
14611460
{
1462-
List<string> scriptFilePaths = new List<string>();
1461+
List<string> scriptFilePaths = ScriptPathList(path, searchRecursively);
14631462

1464-
if (path == null)
1463+
foreach (string scriptFilePath in scriptFilePaths)
14651464
{
1466-
this.outputWriter.ThrowTerminatingError(
1467-
new ErrorRecord(
1468-
new FileNotFoundException(),
1469-
string.Format(CultureInfo.CurrentCulture, Strings.FileNotFound, path),
1470-
ErrorCategory.InvalidArgument,
1471-
this));
1465+
// Yield each record in the result so that the caller can pull them one at a time
1466+
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
1467+
{
1468+
yield return diagnosticRecord;
1469+
}
14721470
}
1471+
}
1472+
1473+
/// <summary>
1474+
/// Analyzes a script file or a directory containing script files and fixes warning where possible.
1475+
/// </summary>
1476+
/// <param name="path">The path of the file or directory to analyze.</param>
1477+
/// <param name="searchRecursively">
1478+
/// If true, recursively searches the given file path and analyzes any
1479+
/// script files that are found.
1480+
/// </param>
1481+
/// <returns>An enumeration of DiagnosticRecords that were found by rules and could not be fixed automatically.</returns>
1482+
public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, bool searchRecursively = false)
1483+
{
1484+
List<string> scriptFilePaths = ScriptPathList(path, searchRecursively);
14731485

1474-
// Create in advance the list of script file paths to analyze. This
1475-
// is an optimization over doing the whole operation at once
1476-
// and calling .Concat on IEnumerables to join results.
1477-
this.BuildScriptPathList(path, searchRecursively, scriptFilePaths);
14781486
foreach (string scriptFilePath in scriptFilePaths)
14791487
{
1480-
if (fix)
1481-
{
1482-
var fileEncoding = GetFileEncoding(scriptFilePath);
1483-
var scriptFileContentWithFixes = Fix(File.ReadAllText(scriptFilePath, fileEncoding));
1484-
File.WriteAllText(scriptFilePath, scriptFileContentWithFixes, fileEncoding); // although this preserves the encoding, it will add a BOM to UTF8 files
1485-
}
1488+
var fileEncoding = GetFileEncoding(scriptFilePath);
1489+
var scriptFileContentWithFixes = Fix(File.ReadAllText(scriptFilePath, fileEncoding));
1490+
File.WriteAllText(scriptFilePath, scriptFileContentWithFixes, fileEncoding);
14861491

1487-
// Yield each record in the result so that the
1488-
// caller can pull them one at a time
1492+
// Yield each record in the result so that the caller can pull them one at a time
14891493
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
14901494
{
14911495
yield return diagnosticRecord;
@@ -1676,6 +1680,28 @@ private static EditableText Fix(
16761680
return text;
16771681
}
16781682

1683+
private List<string> ScriptPathList(string path, bool searchRecursively)
1684+
{
1685+
List<string> scriptFilePaths = new List<string>();
1686+
1687+
if (path == null)
1688+
{
1689+
this.outputWriter.ThrowTerminatingError(
1690+
new ErrorRecord(
1691+
new FileNotFoundException(),
1692+
string.Format(CultureInfo.CurrentCulture, Strings.FileNotFound, path),
1693+
ErrorCategory.InvalidArgument,
1694+
this));
1695+
}
1696+
1697+
// Create in advance the list of script file paths to analyze. This
1698+
// is an optimization over doing the whole operation at once
1699+
// and calling .Concat on IEnumerables to join results.
1700+
this.BuildScriptPathList(path, searchRecursively, scriptFilePaths);
1701+
1702+
return scriptFilePaths;
1703+
}
1704+
16791705
private void BuildScriptPathList(
16801706
string path,
16811707
bool searchRecursively,

0 commit comments

Comments
 (0)