agoda-com / agodaanalyzers Goto Github PK
View Code? Open in Web Editor NEWA set of opinionated Roslyn analyzers for C#
License: Apache License 2.0
A set of opinionated Roslyn analyzers for C#
License: Apache License 2.0
Now fixer fixes the code by replacing:
return null;
with
return new List<string>();
We can improve it to support following test scenarios: (see ignored test case)
da925f8#diff-86497902145ff2fe49846efc4c54dfe5
When designing our own interfaces/classes, we should not expose both synchronous and asynchronous versions of methods.
For example, if methods named MyMethod
and MyMethodAsync
are declared, then fail.
class Downloader
{
public byte[] DownloadFile() { ... } // <-- disallow this declaration, because async version exists
public Task<byte[]> DownloadFileAsync() { ... }
}
class Downloader
{
public Task<byte[]> DownloadFileAsync() { ... }
}
https://github.agodadev.io/pages/standards-c-sharp/code-standards/async/expose-async-method.html
Could we remove the "Test only public surface for your API" rule?
It is not a good rule.
There is nothing wrong with mocking internal methods or using internal classes and unit test them. You should always try to break down complex behavior and test each part separately. If the parts work fine then you know the whole will probably work.
Allowing the testing of only public endpoints is limiting and complicates the test setup.
This rule also encourages using public which is not good.
private const string SELECTOR = "[data-selenium=hotel=item]";
...
var element = By.CssSelector(SELECTOR);
If public
class/interface method/property return value implements IEnumerable
, then it can only be declared as one of the following:
IEnumerable<T>
ISet<T>
IList<T>
IDictionary<K, V>
IReadOnlyDictionary<K, V>
KeyedCollection<K, V>
byte[]
(special case for raw binary data)string
(which happens to implement IEnumerable<char>
)public List<string> GetStrings() { ... }
public IList<string> GetStrings() { ... }
#60 is giving false positives, eg: https://github.agodadev.io/agoda-front-end/agoda-com-dictator/pull/15738
Seems that any string is being analyzed as a Selenium selector.
If we use this rule on Ctrl then all methods need to be appended with "Async" doesn't make sense
To do:
Task
or ValueTask
HTTP controllers should not access query string parameters directly from HttpRequest.QueryString
, they should instead leverage ASP.NET model binding
Prevent use of OpenQA.Selenium.By....()
except for By.CssSelector()
https://github.agodadev.io/pages/standards-c-sharp/code-standards/gui-testing/css-selectors.html
System.Activator.CreateInstance
- any overload that takes a stringSystem.Type.GetType
https://github.agodadev.io/pages/standards-c-sharp/code-standards/code-style/reflection.html
https://github.agodadev.io/pages/standards-c-sharp/code-standards/static-methods/when-static.html
Task.Result
Task.GetAwaiter
await task
async
modifier to method declaration if it does not existFrom:
Fix unescaped entities and badly formatted code examples.
Seems like rule is picking up all methods marked with attributes that derives from NUnitAttribute
, resulting in false positive reporting for SetUp
methods etc
I would like to prevent people from misusing the parameters as a way to return some value from a method.
For example, for following code:
public static void Method(A a){
a.B = new B();
a.B.C = new C();
}
both assignments are mutating the input parameter and treating it like a return value. The rule would not be applied for ref
and out
type params, as they imply explicitly that the parameter is / might be mutated.
Rule already exists in SonarQube, needs to be activated.
When a webapi controller returns void, aspnet make it return a 204 instead of 200. iOS/Safari treat this differently and prompt the user for a document download blocking off the website the instant the API method is called from ajax.
We should create a rule that forces all webapi controller methods to return data and not a void.
[TestCase(1,2,3,4,5,6)]
// 6 params - must be testing too much
public void My_Test(int a, int b, int c, int d, int e, int f)
{ ... }
[TestCase(1,2,3,4,5)]
// maximum 5 test method params
public void My_Test(int a, int b, int c, int d, int e)
{ ... }
Task.WaitAll
Task.WaitAny
await Task.WhenAll
or await Task.WhenAny
async
modifier to method declaration if it does not existBy the time we will get more async code that will be awesome if we have analyzers that prevent async misuse.
Nice example that we can use:
https://github.com/dotnet/roslyn-analyzers/blob/master/src/Unfactored/AsyncPackage/AsyncPackage/BlockingAsyncAnalyzer.cs
public async Task<byte[]> DownloadFile() { ... }
public async Task<byte[]> DownloadFileAsync() { ... }
Add Async
suffix to method name.
Classes containing test methods should not inherit from a base class.
await Task.Delay
async
modifier to method declaration if it does not existWhen calling external code, if we use method TheirMethod()
, but there also exists a method named TheirMethodAsync()
, then fail.
Say we have an external library that exposes both sync and async versions of the same method:
// external lib
namespace AwesomeSoft
{
public class Downloader
{
public byte[] DownloadFile() { ... }
public Task<byte[]> DownloadFileAsync() { ... }
}
}
// our code
var client = new Downloader();
var bytes = client.DownloadFile(...); // should not use synchronous version when async version available
// our code
var client = new Downloader();
var bytes = await client.DownloadFileAsync(...)
Async
await
async
modifier to method declaration if it does not existhttps://github.agodadev.io/pages/standards-c-sharp/code-standards/async/consume-async-method.html
Look at this example.
public static List<SomeThing> GetList()
{
var object = isSomeCondition ? GetObject() : null;
var someThings = new List<SomeThing>();
somThings.Add(object);
return someThings;
}
GetObject only return Object of SomeThing
but this rules also report line var object = isSomeCondition ? GetObject() : null;
as it violate the rule.
Disallow use of any method on System.Threading.Task
whose name begins with Continue
, eg. Task.ContinueWith
, Task.ContinueWhenAll
, etc.
await
Example: DependencyResolver.Current.GetService
Environment.MachineName
HttpContext.Current.Server.MachineName
Gives 404 page
We want to move away from convention-based to attribute-based routing.
This analyzer should ensure that all public methods in a class that inherit from System.Web.Mvc.Controller
or System.Web.Http.ApiController
have a [Route]
attribute.
https://agoda-com.github.io/standards-c-sharp/routing/attribute.html
This may be NUnit's Assert
or one of Shouldly's. http://docs.shouldly-lib.net/v2.4.0/docs
To enable automatic notification of failing tests, and to collects stats on each team's test performance, we need to ensure that all Selenium tests are assigned to the team who is responsible for its maintenance.
When writing test case for CodeFixer, CodeFixVerifier.cs
helps check if fixer introduce any new diagnostics:
The error report, showing only new diagnostics, is quite misleading.
Example:
Before a fix:
using System;
using System.Collections.Generic;
namespace Agoda.Analyzers.Test
{
public class TestClass
{
public int[] GetValuesForId(int id)
{
return null;
}
}
}
After a fix:
using System;
using System.Collections.Generic;
namespace Agoda.Analyzers.Test
{
public class TestClass
{
public int[] GetValuesForId(int id)
{
return Array.Empty<int>();
}
}
}
Error report:
Message: Fix introduced new compiler diagnostics:
Test0.cs(7,18): warning CS1591: Missing XML comment for publicly visible type or member 'TestClass'
Test0.cs(9,22): warning CS1591: Missing XML comment for publicly visible type or member 'TestClass.GetValuesForId(int)'
Before a fix: using System;
is unused.
After a fix: using System;
is used, and this cause a different number of diagnostics returned.
We should display both before a fix and after a fix diagnostics.
Ensure argument to By.CssSelector
matches the regex ^\[data-selenium=
.
By.CssSelector("h1")
By.CssSelector("[data-selenium=main-heading]")
https://github.agodadev.io/pages/standards-c-sharp/code-standards/gui-testing/data-selenium.html
Task.Run(...);
https://github.agodadev.io/pages/standards-c-sharp/code-standards/async/task-run.html
Replace the existing implementation for AG0002 that just checks that assembly:InternalsVisibleToAttribute
is not used.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.