Skip to content

Commit 6aab926

Browse files
author
Kapil Borle
committed
Add checks to prevent formatter loop from getting stuck
1 parent 2778ba3 commit 6aab926

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
2929
private Settings defaultSettings;
3030
private Settings inputSettings;
3131

32-
3332
[ParameterAttribute(Mandatory = true)]
3433
[ValidateNotNull]
3534
public string ScriptDefinition { get; set; }
3635

37-
// todo make this settings and maybe rename Settings class
3836
[Parameter(Mandatory = false)]
3937
[ValidateNotNull]
4038
public object Settings { get; set; }
4139

4240
#if DEBUG
41+
/// <summary>
42+
/// Attaches to an instance of a .Net debugger
43+
/// </summary>
4344
[Parameter(Mandatory = false)]
4445
public SwitchParameter AttachAndDebug
4546
{
@@ -167,8 +168,8 @@ protected override void ProcessRecord()
167168

168169
var corrections = new List<CorrectionExtent>();
169170
var records = Enumerable.Empty<DiagnosticRecord>();
171+
var numPreviousCorrections = corrections.Count;
170172

171-
// todo add a check for this while loop so that it doesn't go into a black hole
172173
do
173174
{
174175
var correctionApplied = new HashSet<int>();
@@ -186,6 +187,16 @@ protected override void ProcessRecord()
186187

187188
records = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(text.ToString());
188189
corrections = records.Select(r => r.SuggestedCorrections.ElementAt(0)).ToList();
190+
if (numPreviousCorrections > 0 && numPreviousCorrections == corrections.Count)
191+
{
192+
this.ThrowTerminatingError(new ErrorRecord(
193+
new InvalidOperationException(),
194+
"FORMATTER_ERROR",
195+
ErrorCategory.InvalidOperation,
196+
corrections));
197+
}
198+
199+
numPreviousCorrections = corrections.Count;
189200

190201
// get unique correction instances
191202
// sort them by line numbers
@@ -196,7 +207,7 @@ protected override void ProcessRecord()
196207
(x.StartLineNumber == x.StartLineNumber ? 0 : -1);
197208
});
198209

199-
} while (records.Any());
210+
} while (numPreviousCorrections > 0);
200211
}
201212

202213
this.WriteObject(text.ToString());

0 commit comments

Comments
 (0)