private static void AnalyzeCall(SyntaxNodeAnalysisContext context, ISymbol migratableAttributeType) { var ct = context.CancellationToken; var typeDeclaration = (TypeDeclarationSyntax)context.Node; var attribute = MigrationHashHelper.GetAttribute(typeDeclaration, migratableAttributeType, context.SemanticModel, ct); if (attribute == null) { return; } var typeSymbol = context.SemanticModel.GetDeclaredSymbol(typeDeclaration, ct); var verifier = new MigrationMethodVerifier(CanAssign(context)); var migrationMethods = MigrationHashHelper.GetMigrationMethods(typeSymbol); var invalidMethods = verifier.VerifyMigrationMethods(migrationMethods) .Where(x => x.Result != VerificationResultEnum.Ok); foreach (var x in invalidMethods) { var method = typeSymbol.GetMembers() .First(sy => sy.Name == x.Method.Name); Debug.Assert(method.Locations.Length == 1, "Method has multiple locations."); var diagnostic = Diagnostic.Create(Rule, method.Locations[0], method.Name, typeSymbol.Name, x.Result); context.ReportDiagnostic(diagnostic); } }
private static async Task <Document> AddMigrationMethod(Document document, TypeDeclarationSyntax typeDecl, CancellationToken ct) { var semanticModel = await document.GetSemanticModelAsync(ct); var typeSymbol = semanticModel.GetDeclaredSymbol(typeDecl, ct); var latestMigrationMethod = MigrationHashHelper.GetMigrationMethods(typeSymbol).LastOrDefault(); var latestVersion = latestMigrationMethod?.ToVersion ?? 0; var dataArgumentTypeName = latestMigrationMethod?.ReturnType.Name ?? "JToken"; var method = GetMigrationMethod(latestVersion + 1, dataArgumentTypeName, ct); var typeDeclWithUpdatedMigrationHash = MigrationHashHelper.UpdateMigrationHash(typeDecl, ct, semanticModel); var typeDeclWithAddedMigrationMethod = AddMember(typeDeclWithUpdatedMigrationHash, method); var root = (CompilationUnitSyntax)await document.GetSyntaxRootAsync(ct); var rootWithNewTypeDecl = root.ReplaceNode(typeDecl, typeDeclWithAddedMigrationMethod); return(document.WithSyntaxRoot(rootWithNewTypeDecl)); }