Skip to content

Commit

Permalink
(GH-2665) Support blockless using statment
Browse files Browse the repository at this point in the history
* fixes #2665
  • Loading branch information
devlead committed Nov 27, 2021
1 parent 0de2adc commit 16a35ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/Cake.Core/Scripting/Processors/UsingStatementProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ public override bool Process(IScriptAnalyzerContext context, string line, out st
return false;
}

// Using block?
// Using disposable block?
var @namespace = tokens[1].TrimEnd(';');
if (@namespace.StartsWith("("))
{
return false;
}

// Using disposable statement?
const int usingLength = 5;
int openParentheses = line.IndexOf('(', usingLength),
closeParentheses = openParentheses < usingLength ? -1 : line.IndexOf(')', openParentheses);

if (closeParentheses > openParentheses)
{
return false;
}

// Using alias directive?
if (tokens.Any(t => t == "="))
{
Expand Down
12 changes: 11 additions & 1 deletion tests/integration/Cake.Core/Scripting/UsingDirective.cake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ Task("Cake.Core.Scripting.UsingDirective.UsingStatic")
Information(Round(1.1));
});

Task("Cake.Core.Scripting.UsingDirective.UsingDisposable")
.Does(() =>
{
// Intentionally not indented
using var @string = new StringReader("String");
using var reader = new StringReader("Reader");
Information("{0}{1}", @string.ReadLine(), reader.ReadLine());
});

//////////////////////////////////////////////////////////////////////////////

Task("Cake.Core.Scripting.UsingDirective")
.IsDependentOn("Cake.Core.Scripting.UsingDirective.UsingStatic");
.IsDependentOn("Cake.Core.Scripting.UsingDirective.UsingStatic")
.IsDependentOn("Cake.Core.Scripting.UsingDirective.UsingDisposable");

0 comments on commit 16a35ce

Please sign in to comment.