Skip to content

Commit 238193a

Browse files
jnyrupkblok
andauthored
Apply analyzer suggestions (#2617)
* Avoid double dictionary lookup * Use const string strings This enable constant string interpolation * Use coalescing * No need to capture `client` * Use string.Concat when the separator is empty * Fix typo * Use await inside try/catch * Bring latest analyzer * Ignore CA1711 AND CA1725 * ignore CA1848 * Fix a rule * Ignore CA2254 * More fixes * Build green * Update lib/.editorconfig * Ignore CA1860 * Fix CA1860 * Update lib/PuppeteerSharp/PuppeteerSharp.csproj * Fix analysis level --------- Co-authored-by: Darío Kondratiuk <[email protected]>
1 parent 28134c7 commit 238193a

18 files changed

+53
-35
lines changed

lib/.editorconfig

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,32 @@ dotnet_diagnostic.CA1056.severity = none
249249
dotnet_diagnostic.CA1040.severity = none
250250
# Use ConfigureAwait
251251
dotnet_diagnostic.CA2007.severity = error
252+
# CA1711: Identifiers should not have incorrect suffix
253+
# We need many suffixes coming from Puppeteer.
254+
dotnet_diagnostic.CA1711.severity = none
255+
# CA1725: Parameter names should match base declaration
256+
# I found that in some places it doesn't detect overloads.
257+
dotnet_diagnostic.CA1725.severity = suggestion
252258
# Do not initialize unnecessarily.
253259
dotnet_diagnostic.CA1805.severity = none
254260
# Properties should not return arrays.
255261
dotnet_diagnostic.CA1819.severity = suggestion
256262
# should override equality and inequality.
257263
dotnet_diagnostic.CA1815.severity = suggestion
264+
# CA1848: Use the LoggerMessage delegates
265+
# TODO: REMOVE
266+
dotnet_diagnostic.CA1848.severity = none
267+
# CA1859: Use concrete types when possible for improved performance
268+
# It collides with https://www.jetbrains.com/help/rider/ReturnTypeCanBeEnumerable.Local.html
269+
dotnet_diagnostic.CA1859.severity = none
270+
# CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
271+
# This rule is not compatible with .netstandard
272+
dotnet_diagnostic.CA1862.severity = none
258273
# Change '...' to be read-only by removing the property setter.
259274
dotnet_diagnostic.CA2227.severity = suggestion
275+
# CA2254: Template should be a static expression
276+
# TODO: REMOVE
277+
dotnet_diagnostic.CA2254.severity = none
260278
# Avoid using cref tags with a prefix
261279
dotnet_diagnostic.CA1200.severity = suggestion
262280
# Marshaling for P/Invoke
@@ -297,6 +315,8 @@ dotnet_diagnostic.CA1806.severity = error
297315
dotnet_diagnostic.CA1816.severity = error
298316
# CA1836: Prefer IsEmpty over Count when available.
299317
dotnet_diagnostic.CA1836.severity = error
318+
# CA1860: Avoid using 'Enumerable.Any()' extension method
319+
dotnet_diagnostic.CA1860.severity = error
300320
# CA2000: Call System.IDisposable.Dispose on object created by 'new Process()' before all references to it are out of scope.
301321
dotnet_diagnostic.CA2000.severity = error
302322
# CA2008: Pass TaskScheduler
@@ -326,9 +346,9 @@ dotnet_diagnostic.IL3000.severity = none
326346

327347
# Default severity for analyzer diagnostics with category 'Usage'
328348
dotnet_analyzer_diagnostic.category-Usage.severity = none
329-
330-
# Default severity for analyzer diagnostics with category 'CodeQuality'
331-
dotnet_analyzer_diagnostic.category-CodeQuality.severity = error
349+
350+
# Default severity for analyzer diagnostics with category 'CodeQuality'
351+
dotnet_analyzer_diagnostic.category-CodeQuality.severity = error
332352

333353
[*.{cpp,h,in}]
334354
curly_bracket_next_line = true

lib/PuppeteerSharp/BrowserData/Firefox.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ internal static async Task<string> ResolveBuildIdAsync(string channel)
3636
.GetAsync<Dictionary<string, string>>("https://product-details.mozilla.org/1.0/firefox_versions.json")
3737
.ConfigureAwait(false);
3838

39-
if (!version.ContainsKey(channel))
39+
if (!version.TryGetValue(channel, out var buildId))
4040
{
4141
throw new PuppeteerException($"Channel {channel} not found.");
4242
}
4343

44-
_cachedBuildIds[channel] = version[channel];
45-
return version[channel];
44+
_cachedBuildIds[channel] = buildId;
45+
return buildId;
4646
}
4747

4848
internal static string RelativeExecutablePath(Platform platform, string buildId)
@@ -90,7 +90,7 @@ private static string GetArchive(Platform platform, string buildId)
9090

9191
private static Dictionary<string, object> GetDefaultPreferences(Dictionary<string, object> preferences)
9292
{
93-
var server = "dummy.test";
93+
const string server = "dummy.test";
9494
var prefs = new Dictionary<string, object>()
9595
{
9696
// Make sure Shield doesn't hit the network.

lib/PuppeteerSharp/BrowserFetcher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private async Task<InstalledBrowser> DownloadAsync(SupportedBrowser browser, str
247247
return new InstalledBrowser(cache, browser, buildId, Platform);
248248
}
249249

250-
private Task InstallDmgAsync(string dmgPath, string folderPath)
250+
private async Task InstallDmgAsync(string dmgPath, string folderPath)
251251
{
252252
try
253253
{
@@ -301,7 +301,7 @@ private Task InstallDmgAsync(string dmgPath, string folderPath)
301301
process.BeginOutputReadLine();
302302
process.WaitForExit();
303303

304-
return mountAndCopyTcs.Task.WithTimeout(Puppeteer.DefaultTimeout);
304+
await mountAndCopyTcs.Task.WithTimeout(Puppeteer.DefaultTimeout).ConfigureAwait(false);
305305
}
306306
finally
307307
{
@@ -400,7 +400,7 @@ private async Task UnpackArchiveAsync(string archivePath, string outputPath, str
400400

401401
if (code != 0)
402402
{
403-
throw new Exception("Chmod operation failed");
403+
throw new PuppeteerException("Chmod operation failed");
404404
}
405405
}
406406
}

lib/PuppeteerSharp/Cdp/CdpElementHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public override Task UploadFileAsync(bool resolveFilePaths, params string[] file
107107
// The zero-length array is a special case, it seems that
108108
// DOM.setFileInputFiles does not actually update the files in that case, so
109109
// the solution is to eval the element value to a new FileList directly.
110-
if (!filePaths.Any())
110+
if (filePaths.Length == 0)
111111
{
112112
await handle.EvaluateFunctionAsync(@"(element) => {
113113
element.files = new DataTransfer().files;

lib/PuppeteerSharp/Cdp/CdpJSHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override async Task<T> JsonValueAsync<T>()
5858

5959
var value = await EvaluateFunctionAsync<T>("object => object").ConfigureAwait(false);
6060

61-
return value == null ? throw new PuppeteerException("Could not serialize referenced object") : value;
61+
return value ?? throw new PuppeteerException("Could not serialize referenced object");
6262
}
6363

6464
/// <inheritdoc/>

lib/PuppeteerSharp/Cdp/CdpPage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ async void ResponseEventListener(object sender, ResponseCreatedEventArgs e)
615615
}
616616
catch (Exception ex)
617617
{
618-
responseTcs.TrySetException(new Exception("Predicated failed", ex));
618+
responseTcs.TrySetException(new PuppeteerException("Predicated failed", ex));
619619
}
620620
}
621621

lib/PuppeteerSharp/Cdp/FrameManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ internal ExecutionContext GetExecutionContextById(int contextId, CDPSession sess
107107
}
108108

109109
internal DeviceRequestPromptManager GetDeviceRequestPromptManager(CDPSession client)
110-
=> _deviceRequestPromptManagerMap.GetOrAdd(client, _ => new DeviceRequestPromptManager(client, TimeoutSettings));
110+
=> _deviceRequestPromptManagerMap.GetOrAdd(client, client => new DeviceRequestPromptManager(client, TimeoutSettings));
111111

112112
internal Frame[] GetFrames() => FrameTree.Frames;
113113

@@ -425,7 +425,7 @@ private void OnFrameNavigatedWithinDocument(NavigatedWithinDocumentResponse e)
425425

426426
private void RemoveFramesRecursively(Frame frame)
427427
{
428-
while (frame.ChildFrames.Any())
428+
while (frame.ChildFrames.Count != 0)
429429
{
430430
RemoveFramesRecursively(frame.ChildFrames.First() as Frame);
431431
}

lib/PuppeteerSharp/Cdp/Messaging/FetchFulfillRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public string ResponsePhrase
7979
{
8080
get
8181
{
82-
if (StatusTexts.ContainsKey(ResponseCode))
82+
if (StatusTexts.TryGetValue(ResponseCode, out var statusText))
8383
{
84-
return StatusTexts[ResponseCode];
84+
return statusText;
8585
}
8686

8787
return "Unknown Status Code";

lib/PuppeteerSharp/Helpers/AsyncMessageQueue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Enqueue(MessageTask callback, ConnectionResponse obj)
5151
task.ContinueWith(
5252
t =>
5353
{
54-
_logger.LogError(t.Exception, "Failed to complete async handling of SendAsync for {callback}", callback.Method);
54+
_logger.LogError(t.Exception, "Failed to complete async handling of SendAsync for {Method}", callback.Method);
5555
callback.TaskWrapper.TrySetException(t.Exception!); // t.Exception is available since this runs only on faulted
5656
},
5757
CancellationToken.None,

lib/PuppeteerSharp/Helpers/RemoteObjectHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private static object ValueFromUnserializableValue(RemoteObject remoteObject, st
126126
case "-Infinity":
127127
return double.NegativeInfinity;
128128
default:
129-
throw new Exception("Unsupported unserializable value: " + unserializableValue);
129+
throw new PuppeteerException("Unsupported unserializable value: " + unserializableValue);
130130
}
131131
}
132132
}

0 commit comments

Comments
 (0)