public IEnumerable <ICodeGeneratable> GetRepositories() { var isAsync = IsFunctionAsync(); var arrowClause = Statements.Count > 1 ? null : Statements.First(); if (arrowClause != null && arrowClause.StartsWith("return ")) { arrowClause = arrowClause.Replace("return ", string.Empty); } var block = arrowClause != null ? null : Statements; var names = GetTypeAndMethodNames(); var methods = new List <Method> { new Method { ArrowClauseExpression = arrowClause, Block = block, Comment = Description, Modifiers = isAsync ? "public static async" : "public static", Name = names.Last(), Parameters = Parameters, Type = ReturnType, TypeParameters = TypeParameters } }; yield return(new StaticFunction( Name, names.First(), Description, Version, EnvironmentVariables, PackageReferences, SameAccountDependencies, fields: Fields, methods: methods, usingDirectives: UsingDirectives, usingStaticDirectives: UsingStaticDirectives)); }
public string TokenLiteral() { if (Statements.Any()) { return(Statements.First().TokenLiteral()); } return(string.Empty); }
public void CastExpressionNullAnnotator_DoesNotAnnotateNonNullableCast(string castExpression) { var source = string.Format(c_castTemplate, castExpression); var(semanticModel, method) = CompiledSourceFileProvider.CompileMethod(source); var stmt = (method.Body !.Statements.First() as LocalDeclarationStatementSyntax) !; var castExpressionSyntax = (stmt.Declaration.Variables.First().Initializer !.Value as CastExpressionSyntax) !; var rewriter = new CastExpressionNullAnnotator(semanticModel); var rewritten = rewriter.Visit(castExpressionSyntax); Assert.That(rewritten, Is.InstanceOf <CastExpressionSyntax>()); Assert.That(rewritten, Is.EqualTo(castExpressionSyntax)); }
public void LocalDeclarationNullAnnotator_DoesAnnotateDeclarationsCallingNullableMethods(string declarationSource) { var classContentTemplate = @"public string? returnNull() { return null; } public void TestMethod() {" + $"\r\n {declarationSource}\r\n" + "}\r\n"; var(semanticModel, syntaxNode) = CompiledSourceFileProvider.CompileInClass("TestClass", classContentTemplate); var testMethod = syntaxNode.DescendantNodes().OfType <MethodDeclarationSyntax>().Single(m => m.Identifier.ToString() == "TestMethod"); var declaration = testMethod.Body !.Statements.First(); var rewriter = new LocalDeclarationNullAnnotator(semanticModel); var rewritten = rewriter.Visit(declaration) as LocalDeclarationStatementSyntax; Assert.That(rewritten, Is.Not.Null); Assert.That(rewritten !.Declaration.Type, Is.InstanceOf <NullableTypeSyntax>()); }
private static void TestConditional( string conditionalExpression, string targetType, string?naturalType, CSharpParseOptions?parseOptions, params DiagnosticDescription[] expectedDiagnostics) { string source = $@" class Program {{ unsafe void Test<T, U, V>(bool b) where T : class where U : class, T where V : class, T {{ {targetType} t = {conditionalExpression}; Use(t); }} A GetA() {{ return null; }} B GetB() {{ return null; }} C GetC() {{ return null; }} D GetD() {{ return null; }} int GetInt() {{ return 1; }} uint GetUInt() {{ return 1; }} T Get<T>() where T : class {{ return null; }} void Use(object t) {{ }} unsafe void Use(void* t) {{ }} unsafe int* GetIntp() {{ return null; }} unsafe long* GetLongp() {{ return null; }} static int M1(int x) => x; static int M2(int x) => x; static int M3(int x, int y) => x; }} public enum color {{ Red, Blue, Green }}; class A {{ }} class B : A {{ public static implicit operator X(B self) => new X(); }} class C : A {{ public static implicit operator X(C self) => new X(); }} class D : A {{ [System.Obsolete(""D"", true)] public static implicit operator X(D self) => new X(); }} class X {{ }} interface IOut<out T> {{ }} interface IIn<in T> {{ }} delegate int Del(int x); "; parseOptions ??= TestOptions.Regular; parseOptions = parseOptions.WithLanguageVersion(MessageID.IDS_FeatureTargetTypedConditional.RequiredVersion()); var tree = Parse(source, options: parseOptions); var comp = CreateCompilation(tree, options: TestOptions.DebugDll.WithAllowUnsafe(true)); comp.VerifyDiagnostics(expectedDiagnostics); var compUnit = tree.GetCompilationUnitRoot(); var classC = (TypeDeclarationSyntax)compUnit.Members.First(); var methodTest = (MethodDeclarationSyntax)classC.Members.First(); var stmt = (LocalDeclarationStatementSyntax)methodTest.Body !.Statements.First(); var conditionalExpr = (ConditionalExpressionSyntax)stmt.Declaration.Variables[0].Initializer !.Value; var model = comp.GetSemanticModel(tree); if (naturalType is null) { var actualType = model.GetTypeInfo(conditionalExpr).Type; if (actualType is { })
/// <summary> /// The first statement in the list. /// </summary> /// <returns></returns> public StatementSyntax First() { return(Statements.First()); }