Skip to content

Commit

Permalink
wip: testing the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
shahabganji committed Apr 11, 2024
1 parent c5bd538 commit 66f12b9
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 25 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build and Test

on:
push:
branches:
*
pull_request:
branches:
- main
- dev

jobs:
build_and_test:
name: Build and Test
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Setup .NET SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x' # You can specify the version you need

- name: Restore Dependencies
run: dotnet restore CodeJoyRide.sln

- name: Build Solution
run: dotnet build CodeJoyRide.sln --configuration Release

- name: Run Tests
run: dotnet test CodeJoyRide.sln --configuration Release --no-restore --verbosity normal
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

| Rule ID | Category | Severity | Notes |
|---------|----------|----------|--------------------------------------------------|
| CJR001 | Usage | Warning | The speed must be lower than the Speed of Light. |
| CJR01 | Usage | Warning | The speed must be lower than the Speed of Light. |
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

// Register a code action that will invoke the fix.
context.RegisterCodeFix(CodeAction.Create(
title: string.Format(Resources.CJR001CodeFixTitle, "throw", "Maybe.None"),
title: string.Format(Resources.CJR01CodeFixTitle, "throw", "Maybe.None"),
token => ReplaceThrowWithReturnStatement(context.Document, throwStatementSyntax, token),
equivalenceKey: nameof(Resources.CJR001CodeFixTitle)
equivalenceKey: nameof(Resources.CJR01CodeFixTitle)
), diagnostic);
}

private Task<Document> ReplaceThrowWithReturnStatement(Document document, CSharpSyntaxNode throwSyntaxNode,
CancellationToken token)
private Task<Document> ReplaceThrowWithReturnStatement(
Document document, CSharpSyntaxNode throwSyntaxNode, CancellationToken token)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace CodeJoyRide.Fx.Analyzer;

Expand All @@ -12,28 +16,28 @@ namespace CodeJoyRide.Fx.Analyzer;
public class MaybeSemanticAnalyzer : DiagnosticAnalyzer
{
// Preferred format of DiagnosticId is Your Prefix + Number, e.g. CA1234.
public const string DiagnosticId = "CJR001";
public const string DiagnosticId = "CJR01";

// Feel free to use raw strings if you don't need localization.
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.CJR001Title),
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.CJR01Title),
Resources.ResourceManager, typeof(Resources));

// The message that will be displayed to the user.
private static readonly LocalizableString MessageFormat =
new LocalizableResourceString(nameof(Resources.CJR001MessageFormat), Resources.ResourceManager,
new LocalizableResourceString(nameof(Resources.CJR01MessageFormat), Resources.ResourceManager,
typeof(Resources));

private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.CJR001Description), Resources.ResourceManager,
new LocalizableResourceString(nameof(Resources.CJR01Description), Resources.ResourceManager,
typeof(Resources));

// The category of the diagnostic (Design, Naming etc.).
private const string Category = "Usage";

private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category,
DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description
DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description
, customTags: [WellKnownDiagnosticTags.NotConfigurable]
);
);

// Keep in mind: you have to list your rules here.
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -45,9 +49,40 @@ public override void Initialize(AnalysisContext context)
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

// You must call this method to enable the Concurrent Execution.
context.EnableConcurrentExecution();
context.EnableConcurrentExecution();

// Subscribe to semantic (compile time) action invocation, e.g. throw .
// TODO: register operation action
context.RegisterOperationAction(AnalyzeThrowStatements, OperationKind.Throw);
}

private void AnalyzeThrowStatements(OperationAnalysisContext context)
{
if (context.Operation is not IThrowOperation || context.Operation.Syntax is not ThrowStatementSyntax)
return;

if (context.Operation.SemanticModel is null)
return;

var containingMethodSyntax = GetContainingMethodSyntax(context.Operation.Syntax);
var containingMethodSymbol =
context.Operation.SemanticModel.GetDeclaredSymbol(containingMethodSyntax) as IMethodSymbol;

var returnTypeSymbol = containingMethodSymbol!.ReturnType;
var maybeTypeSymbol = context.Compilation.GetTypeByMetadataName("CodeJoyRide.Fx.Maybe`1");

if (!returnTypeSymbol.OriginalDefinition.Equals(maybeTypeSymbol, SymbolEqualityComparer.Default))
return;

var diagnostic = Diagnostic.Create(Rule, context.Operation.Syntax.GetLocation());
context.ReportDiagnostic(diagnostic);
}

private MethodDeclarationSyntax GetContainingMethodSyntax(SyntaxNode syntaxNode)
{
while (true)
{
if (syntaxNode.Parent is MethodDeclarationSyntax mds) return mds;
syntaxNode = syntaxNode.Parent!;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="CJR001CodeFixTitle" xml:space="preserve">
<data name="CJR01CodeFixTitle" xml:space="preserve">
<value>Replace '{0}' with '{1}'</value><comment>The title of the code fix.</comment>
</data>
<data name="CJR001Description" xml:space="preserve">
<data name="CJR01Description" xml:space="preserve">
<value>It's recommended to return None instead of throw exception.</value><comment>An optional longer localizable description of the diagnostic.</comment>
</data>
<data name="CJR001MessageFormat" xml:space="preserve">
<data name="CJR01MessageFormat" xml:space="preserve">
<value>Use Maybe.None instead of throw exception</value><comment>The format-able message the diagnostic displays.</comment>
</data>
<data name="CJR001Title" xml:space="preserve">
<data name="CJR01Title" xml:space="preserve">
<value>No Throw Exception</value><comment>The title of the diagnostic.</comment>
</data>
</root>
8 changes: 8 additions & 0 deletions CodeJoyRide.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{D3C4A25F-0
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{A6A1F426-676D-4413-93B2-B9DD6A25736A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{F37F6912-F8AE-4B03-8EB0-966358BC757B}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build_and_test.yaml = .github\workflows\build_and_test.yaml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -45,5 +52,6 @@ Global
{3D0FED8B-0509-4407-B66C-AFC7971956C3} = {1B62324C-07F0-45FA-A1F5-C5CAD91A6AC5}
{906D80E7-ED31-4D3E-A47D-7ABF8F6153FB} = {1B62324C-07F0-45FA-A1F5-C5CAD91A6AC5}
{5508C6E9-856E-4B08-8BFD-D05803BAE144} = {85459589-7B8D-43F5-977D-64D13E685F71}
{F37F6912-F8AE-4B03-8EB0-966358BC757B} = {A6A1F426-676D-4413-93B2-B9DD6A25736A}
EndGlobalSection
EndGlobal

0 comments on commit 66f12b9

Please sign in to comment.