Skip to content

Commit b657ab8

Browse files
authored
chore: introduce string.IsNullOrEmpty helper (#3179)
1 parent c14165c commit b657ab8

10 files changed

Lines changed: 33 additions & 13 deletions

File tree

src/Playwright/Core/APIRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async Task<IAPIRequestContext> IAPIRequest.NewContextAsync(APIRequestNewContextO
6363

6464
storageState = File.ReadAllText(options?.StorageStatePath);
6565
}
66-
if (!string.IsNullOrEmpty(storageState) && storageState != null)
66+
if (!storageState.IsNullOrEmpty())
6767
{
6868
args.Add("storageState", JsonSerializer.Deserialize<object>(storageState, Helpers.JsonExtensions.DefaultJsonSerializerOptions));
6969
}

src/Playwright/Core/APIRequestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ internal async Task<IAPIResponse> InnerFetchAsync(IRequest request, string? urlO
9595
[MethodImpl(MethodImplOptions.NoInlining)]
9696
public async Task<IAPIResponse> FetchAsync(string url, APIRequestContextOptions? options = null)
9797
{
98-
if (!string.IsNullOrEmpty(_closeReason) && _closeReason != null)
98+
if (!_closeReason.IsNullOrEmpty())
9999
{
100100
throw new PlaywrightException(_closeReason);
101101
}

src/Playwright/Core/Browser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public async Task<IBrowserContext> NewContextAsync(BrowserNewContextOptions? opt
154154
storageState = File.ReadAllText(options.StorageStatePath);
155155
}
156156

157-
if (!string.IsNullOrEmpty(storageState) && storageState != null)
157+
if (!storageState.IsNullOrEmpty())
158158
{
159159
args.Add("storageState", JsonSerializer.Deserialize<object>(storageState, Helpers.JsonExtensions.DefaultJsonSerializerOptions));
160160
}

src/Playwright/Core/HarRouter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private HarRouter(LocalUtils localUtils, string harId, HarNotFound notFoundActio
5050
internal static async Task<HarRouter> CreateAsync(LocalUtils localUtils, string file, HarNotFound notFoundAction, HarRouterOptions options)
5151
{
5252
var (harId, error) = await localUtils.HarOpenAsync(file).ConfigureAwait(false);
53-
if (!string.IsNullOrEmpty(error) && error != null)
53+
if (!error.IsNullOrEmpty())
5454
{
5555
throw new PlaywrightException(error);
5656
}

src/Playwright/Core/Route.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private async Task RaceWithTargetCloseAsync(Task task)
196196
isBase64 = true;
197197
length = resultBody.Length;
198198
}
199-
else if (!string.IsNullOrEmpty(body) && body != null)
199+
else if (!body.IsNullOrEmpty())
200200
{
201201
resultBody = body;
202202
isBase64 = false;
@@ -219,15 +219,15 @@ private async Task RaceWithTargetCloseAsync(Task task)
219219
}
220220
}
221221

222-
if (!string.IsNullOrEmpty(contentType) && contentType != null)
222+
if (!contentType.IsNullOrEmpty())
223223
{
224224
resultHeaders["content-type"] = contentType;
225225
}
226226
else if (json != null)
227227
{
228228
resultHeaders["content-type"] = "application/json";
229229
}
230-
else if (!string.IsNullOrEmpty(path) && path != null)
230+
else if (!path.IsNullOrEmpty())
231231
{
232232
resultHeaders["content-type"] = path.GetContentType();
233233
}

src/Playwright/Core/ScriptsHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.Linq;
2929
using System.Reflection;
3030
using System.Text.Json;
31+
using Microsoft.Playwright.Helpers;
3132
using Microsoft.Playwright.Transport.Converters;
3233

3334
namespace Microsoft.Playwright.Core;
@@ -75,11 +76,11 @@ internal static object SerializedArgument(object? arg)
7576

7677
internal static string EvaluationScript(string? content, string? path, bool addSourceUrl)
7778
{
78-
if (!string.IsNullOrEmpty(content) && content != null)
79+
if (!content.IsNullOrEmpty())
7980
{
8081
return content;
8182
}
82-
else if (!string.IsNullOrEmpty(path) && path != null)
83+
else if (!path.IsNullOrEmpty())
8384
{
8485
var source = File.ReadAllText(path);
8586
return addSourceUrl ? AddSourceUrlToScript(source, path) : source;

src/Playwright/Core/Tracing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private async Task DoStopChunkAsync(string? filePath)
108108
{
109109
// Not interested in artifacts.
110110
await _TracingStopChunkAsync("discard").ConfigureAwait(false);
111-
if (!string.IsNullOrEmpty(_stacksId) && _stacksId != null)
111+
if (!_stacksId.IsNullOrEmpty())
112112
{
113113
if (_connection.LocalUtils != null)
114114
{
@@ -135,7 +135,7 @@ private async Task DoStopChunkAsync(string? filePath)
135135
// The artifact may be missing if the browser closed while stopping tracing.
136136
if (artifact == null)
137137
{
138-
if (!string.IsNullOrEmpty(_stacksId) && _stacksId != null)
138+
if (!_stacksId.IsNullOrEmpty())
139139
{
140140
if (_connection.LocalUtils != null)
141141
{

src/Playwright/Core/Waiter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void Dispose()
7878
["waitId"] = _waitId,
7979
["phase"] = "after",
8080
};
81-
if (!string.IsNullOrEmpty(_error) && _error != null)
81+
if (!_error.IsNullOrEmpty())
8282
{
8383
info["error"] = _error;
8484
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace System.Diagnostics.CodeAnalysis;
2+
3+
// This can be dropped when we drop netstandard2.0
4+
[AttributeUsage(AttributeTargets.Parameter)]
5+
internal sealed class NotNullWhenAttribute(bool returnValue) : Attribute
6+
{
7+
public bool ReturnValue { get; } = returnValue;
8+
}

src/Playwright/Helpers/StringExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
using System;
2626
using System.Collections.Generic;
27+
using System.Diagnostics.CodeAnalysis;
2728
using System.IO;
2829
using System.Linq;
2930

@@ -603,7 +604,7 @@ internal static class StringExtensions
603604
/// </summary>
604605
/// <param name="query">Query string.</param>
605606
/// <returns>A <see cref="Dictionary{TKey, TValue}"/> containing the parsed QueryString.</returns>
606-
public static Dictionary<string, string> ParseQueryString(this string query)
607+
internal static Dictionary<string, string> ParseQueryString(this string query)
607608
{
608609
if (query is null)
609610
{
@@ -656,4 +657,14 @@ internal static string MimeType(this string file)
656657

657658
return path.Substring(index);
658659
}
660+
661+
/// <summary>
662+
/// Checks if the string is null or empty. This should be used instead of string.IsNullOrEmpty to avoid nullability warnings.
663+
/// If we drop netstandard2.0 support, we can migrate to string.IsNullOrEmpty().
664+
/// Relates https://stackoverflow.com/a/64066801.
665+
/// </summary>
666+
internal static bool IsNullOrEmpty([NotNullWhen(false)] this string? data)
667+
{
668+
return string.IsNullOrEmpty(data);
669+
}
659670
}

0 commit comments

Comments
 (0)