Skip to content

Commit e633bc2

Browse files
[dotnet] Annotate JavaScript strings within BiDi (#16657)
1 parent 96a4e90 commit e633bc2

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextScriptModule.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using System.Threading.Tasks;
2120
using OpenQA.Selenium.BiDi.Script;
21+
using OpenQA.Selenium.Internal;
22+
using System.Diagnostics.CodeAnalysis;
23+
using System.Threading.Tasks;
2224

2325
namespace OpenQA.Selenium.BiDi.BrowsingContext;
2426

2527
public sealed class BrowsingContextScriptModule(BrowsingContext context, ScriptModule scriptModule)
2628
{
27-
public async Task<AddPreloadScriptResult> AddPreloadScriptAsync(string functionDeclaration, BrowsingContextAddPreloadScriptOptions? options = null)
29+
public async Task<AddPreloadScriptResult> AddPreloadScriptAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, BrowsingContextAddPreloadScriptOptions? options = null)
2830
{
2931
AddPreloadScriptOptions addPreloadScriptOptions = new(options)
3032
{
@@ -43,7 +45,7 @@ public async Task<GetRealmsResult> GetRealmsAsync(GetRealmsOptions? options = nu
4345
return await scriptModule.GetRealmsAsync(options).ConfigureAwait(false);
4446
}
4547

46-
public Task<EvaluateResult> EvaluateAsync(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
48+
public Task<EvaluateResult> EvaluateAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
4749
{
4850
var contextTarget = new ContextTarget(context);
4951

@@ -55,14 +57,14 @@ public Task<EvaluateResult> EvaluateAsync(string expression, bool awaitPromise,
5557
return scriptModule.EvaluateAsync(expression, awaitPromise, contextTarget, options);
5658
}
5759

58-
public async Task<TResult?> EvaluateAsync<TResult>(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
60+
public async Task<TResult?> EvaluateAsync<TResult>([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
5961
{
6062
var result = await EvaluateAsync(expression, awaitPromise, options, targetOptions).ConfigureAwait(false);
6163

6264
return result.AsSuccessResult().ConvertTo<TResult>();
6365
}
6466

65-
public Task<EvaluateResult> CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null)
67+
public Task<EvaluateResult> CallFunctionAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null)
6668
{
6769
var contextTarget = new ContextTarget(context);
6870

@@ -74,7 +76,7 @@ public Task<EvaluateResult> CallFunctionAsync(string functionDeclaration, bool a
7476
return scriptModule.CallFunctionAsync(functionDeclaration, awaitPromise, contextTarget, options);
7577
}
7678

77-
public async Task<TResult?> CallFunctionAsync<TResult>(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null)
79+
public async Task<TResult?> CallFunctionAsync<TResult>([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null)
7880
{
7981
var result = await CallFunctionAsync(functionDeclaration, awaitPromise, options, targetOptions).ConfigureAwait(false);
8082

dotnet/src/webdriver/BiDi/Script/AddPreloadScriptCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.Internal;
2021
using System.Collections.Generic;
22+
using System.Diagnostics.CodeAnalysis;
2123

2224
namespace OpenQA.Selenium.BiDi.Script;
2325

2426
internal sealed class AddPreloadScriptCommand(AddPreloadScriptParameters @params)
2527
: Command<AddPreloadScriptParameters, AddPreloadScriptResult>(@params, "script.addPreloadScript");
2628

27-
internal sealed record AddPreloadScriptParameters(string FunctionDeclaration, IEnumerable<ChannelLocalValue>? Arguments, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, string? Sandbox) : Parameters;
29+
internal sealed record AddPreloadScriptParameters([StringSyntax(StringSyntaxConstants.JavaScript)] string FunctionDeclaration, IEnumerable<ChannelLocalValue>? Arguments, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, string? Sandbox) : Parameters;
2830

2931
public sealed class AddPreloadScriptOptions : CommandOptions
3032
{

dotnet/src/webdriver/BiDi/Script/CallFunctionCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.Internal;
2021
using System.Collections.Generic;
22+
using System.Diagnostics.CodeAnalysis;
2123

2224
namespace OpenQA.Selenium.BiDi.Script;
2325

2426
internal sealed class CallFunctionCommand(CallFunctionParameters @params)
2527
: Command<CallFunctionParameters, EvaluateResult>(@params, "script.callFunction");
2628

27-
internal sealed record CallFunctionParameters(string FunctionDeclaration, bool AwaitPromise, Target Target, IEnumerable<LocalValue>? Arguments, ResultOwnership? ResultOwnership, SerializationOptions? SerializationOptions, LocalValue? This, bool? UserActivation) : Parameters;
29+
internal sealed record CallFunctionParameters([StringSyntax(StringSyntaxConstants.JavaScript)] string FunctionDeclaration, bool AwaitPromise, Target Target, IEnumerable<LocalValue>? Arguments, ResultOwnership? ResultOwnership, SerializationOptions? SerializationOptions, LocalValue? This, bool? UserActivation) : Parameters;
2830

2931
public sealed class CallFunctionOptions : CommandOptions
3032
{

dotnet/src/webdriver/BiDi/Script/EvaluateCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Json.Converters.Polymorphic;
21+
using OpenQA.Selenium.Internal;
2122
using System;
23+
using System.Diagnostics.CodeAnalysis;
2224
using System.Text.Json.Serialization;
2325

2426
namespace OpenQA.Selenium.BiDi.Script;
2527

2628
internal sealed class EvaluateCommand(EvaluateParameters @params)
2729
: Command<EvaluateParameters, EvaluateResult>(@params, "script.evaluate");
2830

29-
internal sealed record EvaluateParameters(string Expression, Target Target, bool AwaitPromise, ResultOwnership? ResultOwnership, SerializationOptions? SerializationOptions, bool? UserActivation) : Parameters;
31+
internal sealed record EvaluateParameters([StringSyntax(StringSyntaxConstants.JavaScript)] string Expression, Target Target, bool AwaitPromise, ResultOwnership? ResultOwnership, SerializationOptions? SerializationOptions, bool? UserActivation) : Parameters;
3032

3133
public sealed class EvaluateOptions : CommandOptions
3234
{

dotnet/src/webdriver/BiDi/Script/ScriptModule.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
using System;
2121
using System.Collections.Generic;
22+
using System.Diagnostics.CodeAnalysis;
2223
using System.Text.Json;
2324
using System.Text.Json.Serialization;
2425
using System.Threading.Tasks;
@@ -29,28 +30,28 @@ public sealed class ScriptModule : Module
2930
{
3031
private ScriptJsonSerializerContext _jsonContext = null!;
3132

32-
public async Task<EvaluateResult> EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
33+
public async Task<EvaluateResult> EvaluateAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
3334
{
3435
var @params = new EvaluateParameters(expression, target, awaitPromise, options?.ResultOwnership, options?.SerializationOptions, options?.UserActivation);
3536

3637
return await Broker.ExecuteCommandAsync(new EvaluateCommand(@params), options, _jsonContext.EvaluateCommand, _jsonContext.EvaluateResult).ConfigureAwait(false);
3738
}
3839

39-
public async Task<TResult?> EvaluateAsync<TResult>(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
40+
public async Task<TResult?> EvaluateAsync<TResult>([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
4041
{
4142
var result = await EvaluateAsync(expression, awaitPromise, target, options).ConfigureAwait(false);
4243

4344
return result.AsSuccessResult().ConvertTo<TResult>();
4445
}
4546

46-
public async Task<EvaluateResult> CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
47+
public async Task<EvaluateResult> CallFunctionAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
4748
{
4849
var @params = new CallFunctionParameters(functionDeclaration, awaitPromise, target, options?.Arguments, options?.ResultOwnership, options?.SerializationOptions, options?.This, options?.UserActivation);
4950

5051
return await Broker.ExecuteCommandAsync(new CallFunctionCommand(@params), options, _jsonContext.CallFunctionCommand, _jsonContext.EvaluateResult).ConfigureAwait(false);
5152
}
5253

53-
public async Task<TResult?> CallFunctionAsync<TResult>(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
54+
public async Task<TResult?> CallFunctionAsync<TResult>([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
5455
{
5556
var result = await CallFunctionAsync(functionDeclaration, awaitPromise, target, options).ConfigureAwait(false);
5657

@@ -71,7 +72,7 @@ public async Task<GetRealmsResult> GetRealmsAsync(GetRealmsOptions? options = nu
7172
return await Broker.ExecuteCommandAsync(new GetRealmsCommand(@params), options, _jsonContext.GetRealmsCommand, _jsonContext.GetRealmsResult).ConfigureAwait(false);
7273
}
7374

74-
public async Task<AddPreloadScriptResult> AddPreloadScriptAsync(string functionDeclaration, AddPreloadScriptOptions? options = null)
75+
public async Task<AddPreloadScriptResult> AddPreloadScriptAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, AddPreloadScriptOptions? options = null)
7576
{
7677
var @params = new AddPreloadScriptParameters(functionDeclaration, options?.Arguments, options?.Contexts, options?.Sandbox);
7778

0 commit comments

Comments
 (0)