Skip to content

Commit 8525e03

Browse files
committed
Optimize -Fix switch to only write the fixed file to disk if any fixes were actually applied.
This helps especially because sometimes the encoding cannot be preserved (which is probably due to the EditableText implementation)
1 parent 701c264 commit 8525e03

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Engine/Formatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public static string Format<TCmdlet>(
5353
ScriptAnalyzer.Instance.Initialize(cmdlet, null, null, null, null, true, false);
5454

5555
Range updatedRange;
56-
text = ScriptAnalyzer.Instance.Fix(text, range, out updatedRange);
56+
bool fixesWereApplied;
57+
text = ScriptAnalyzer.Instance.Fix(text, range, out updatedRange, out fixesWereApplied);
5758
range = updatedRange;
5859
}
5960

Engine/ScriptAnalyzer.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,8 +1493,12 @@ public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, Func<string,
14931493
if (shouldProcess(scriptFilePath, $"Analyzing and fixing file {scriptFilePath}"))
14941494
{
14951495
var fileEncoding = GetFileEncoding(scriptFilePath);
1496-
var scriptFileContentWithFixes = Fix(File.ReadAllText(scriptFilePath, fileEncoding));
1497-
File.WriteAllText(scriptFilePath, scriptFileContentWithFixes, fileEncoding);
1496+
bool fixesWereApplied;
1497+
var scriptFileContentWithFixes = Fix(File.ReadAllText(scriptFilePath, fileEncoding), out fixesWereApplied);
1498+
if (fixesWereApplied)
1499+
{
1500+
File.WriteAllText(scriptFilePath, scriptFileContentWithFixes, fileEncoding);
1501+
}
14981502

14991503
// Yield each record in the result so that the caller can pull them one at a time
15001504
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
@@ -1552,16 +1556,17 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
15521556
/// Fix the violations in the given script text.
15531557
/// </summary>
15541558
/// <param name="scriptDefinition">The script text to be fixed.</param>
1559+
/// <param name="updatedRange">Whether any warnings were fixed.</param>
15551560
/// <returns>The fixed script text.</returns>
1556-
public string Fix(string scriptDefinition)
1561+
public string Fix(string scriptDefinition, out bool fixesWereApplied)
15571562
{
15581563
if (scriptDefinition == null)
15591564
{
15601565
throw new ArgumentNullException(nameof(scriptDefinition));
15611566
}
15621567

15631568
Range updatedRange;
1564-
return Fix(new EditableText(scriptDefinition), null, out updatedRange).ToString();
1569+
return Fix(new EditableText(scriptDefinition), null, out updatedRange, out fixesWereApplied).ToString();
15651570
}
15661571

15671572
/// <summary>
@@ -1570,8 +1575,9 @@ public string Fix(string scriptDefinition)
15701575
/// <param name="text">An object of type `EditableText` that encapsulates the script text to be fixed.</param>
15711576
/// <param name="range">The range in which the fixes are allowed.</param>
15721577
/// <param name="updatedRange">The updated range after the fixes have been applied.</param>
1578+
/// <param name="updatedRange">Whether any warnings were fixed.</param>
15731579
/// <returns>The same instance of `EditableText` that was passed to the method, but the instance encapsulates the fixed script text. This helps in chaining the Fix method.</returns>
1574-
public EditableText Fix(EditableText text, Range range, out Range updatedRange)
1580+
public EditableText Fix(EditableText text, Range range, out Range updatedRange, out bool fixesWereApplied)
15751581
{
15761582
if (text == null)
15771583
{
@@ -1604,7 +1610,9 @@ public EditableText Fix(EditableText text, Range range, out Range updatedRange)
16041610
this.outputWriter.WriteVerbose($"Found {corrections.Count} violations.");
16051611
int unusedCorrections;
16061612
Fix(text, corrections, out unusedCorrections);
1607-
this.outputWriter.WriteVerbose($"Fixed {corrections.Count - unusedCorrections} violations.");
1613+
var numberOfFixedViolatons = corrections.Count - unusedCorrections;
1614+
fixesWereApplied = numberOfFixedViolatons > 0;
1615+
this.outputWriter.WriteVerbose($"Fixed {numberOfFixedViolatons} violations.");
16081616

16091617
// This is an indication of an infinite loop. There is a small chance of this.
16101618
// It is better to abort the fixing operation at this point.

0 commit comments

Comments
 (0)