Пример #1
0
        public void SyntaxFacts_Keyword_GetKeywords_EqualsReservedPlusContextual()
        {
            var getKeywords            = SyntaxFacts.GetKeywordKinds().OrderBy(t => t);
            var reserved               = SyntaxFacts.GetReservedKeywordKinds();
            var contextual             = SyntaxFacts.GetContextualKeywordKinds();
            var reservedPlusContextual = reserved.Concat(contextual).OrderBy(t => t);

            Assert.Equal(getKeywords, reservedPlusContextual);
        }
Пример #2
0
        public void SyntaxFacts_Keyword_GetKeywordKind_DetectsAllReservedKeywords()
        {
            foreach (var kind in SyntaxFacts.GetReservedKeywordKinds())
            {
                var text        = kind.GetText();
                var keywordKind = SyntaxFacts.GetKeywordKind(text);

                Assert.Equal(kind, keywordKind);
            }
        }
Пример #3
0
        public void SyntaxFacts_Identifier_IsValidIdentifier_ReturnsFalse_ForReservedKeywords()
        {
            foreach (var keywordKind in SyntaxFacts.GetReservedKeywordKinds())
            {
                var text    = keywordKind.GetText();
                var isValid = SyntaxFacts.IsValidIdentifier(text);

                Assert.False(isValid);
            }
        }
Пример #4
0
        public void SyntaxFacts_Keyword_ReservedAndContextual_AreMutuallyExclusive()
        {
            var reservedKinds   = new HashSet <SyntaxKind>(SyntaxFacts.GetReservedKeywordKinds());
            var contextualKinds = new HashSet <SyntaxKind>(SyntaxFacts.GetContextualKeywordKinds());

            foreach (var kind in SyntaxFacts.GetKeywordKinds())
            {
                Assert.True(reservedKinds.Contains(kind) ^ contextualKinds.Contains(kind));
            }
        }
Пример #5
0
        public void SyntaxFacts_Keyword_GetKeywordKind_DetectsAllReservedKeywords_RegardlessOfCase()
        {
            foreach (var kind in SyntaxFacts.GetReservedKeywordKinds())
            {
                var text = kind.GetText();
                var textWithMixedCast = GetMixedCase(text);
                var keywordKind       = SyntaxFacts.GetKeywordKind(textWithMixedCast);

                Assert.Equal(kind, keywordKind);
            }
        }
Пример #6
0
        public void SyntaxFacts_Identifier_GetValidIdentifier_ReturnsParenthesized_ForReservedKeywords()
        {
            foreach (var keywordKind in SyntaxFacts.GetReservedKeywordKinds())
            {
                var text       = keywordKind.GetText();
                var expected   = "[" + text + "]";
                var identifier = SyntaxFacts.GetValidIdentifier(text);

                Assert.Equal(expected, identifier);
            }
        }
Пример #7
0
        public void TestReservedKeywords()
        {
            // https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Syntax/SyntaxKindFacts.cs

            // Check if all items in CodeGenUtil.Instance.ReservedKeywords are keywords
            foreach (string keyword in CodeGenUtil.Instance.ReservedKeywords)
            {
                Assert.That(SyntaxFacts.GetKeywordKind(keyword) != SyntaxKind.None, Is.True);
            }

            // Check if all Roslyn defined keywords are in CodeGenUtil.Instance.ReservedKeywords
            foreach (SyntaxKind keywordKind in SyntaxFacts.GetReservedKeywordKinds())
            {
                Assert.That(CodeGenUtil.Instance.ReservedKeywords, Does.Contain(SyntaxFacts.GetText(keywordKind)));
            }

            // If this test fails, CodeGenUtil.ReservedKeywords list must be updated.
            // This might happen if newer version of C# language defines new reserved keywords.
        }
        public void Keywords()
        {
            var builder = ImmutableHashSet.CreateBuilder <SyntaxKind>();

            // SyntaxFacts.GetReservedKeywordKinds() contains ReferenceKeyword
            // although "Reference" should not be considered a keyword.
            // (see https://github.com/dotnet/roslyn/issues/15242).
            builder.Add(SyntaxKind.ReferenceKeyword);
            foreach (var text in MemberSignatureParser.Keywords)
            {
                var kind = SyntaxFacts.GetKeywordKind(text);
                Assert.NotEqual(SyntaxKind.None, kind);
                bool added = builder.Add(kind);
                Assert.True(added);
            }

            var actualKeywordKinds   = builder.ToImmutable();
            var expectedKeywordKinds = ImmutableHashSet.CreateRange(SyntaxFacts.GetReservedKeywordKinds());

            AssertEx.SetEqual(actualKeywordKinds, expectedKeywordKinds);
        }
Пример #9
0
 public static IEnumerable <object[]> GetReservedKeywordKinds()
 {
     return(SyntaxFacts.GetReservedKeywordKinds()
            .Select(t => new object[] { t }));
 }
        /// <summary>
        /// Get the arguments which are unnamed and not "params"
        /// </summary>
        private static IEnumerable <ArgParamBinding> GetUnnamedArgs(
            SemanticModel model,
            ArgumentListSyntax args
            )
        {
            for (int idx = 0; idx < args.Arguments.Count; idx++)
            {
                ArgumentSyntax arg = args.Arguments[idx];

                // Ignore args that already have names
                if (arg.NameColon != null)
                {
                    continue;
                }

                IParameterSymbol param = arg.DetermineParameter(
                    model,

                    // Don't map params arguments. It's okay that they are
                    // unnamed. Some things like ImmutableArray.Create() could
                    // take a large number of args and we don't want anything to
                    // be named. Named params really suck so we may still
                    // encourage it but APIs that take params and many other
                    // args would suck anyway.
                    allowParams: false
                    );

                // Not sure if this can happen but it'd be hard to name this
                // param so ignore it.
                if (param == null)
                {
                    continue;
                }

                // IParameterSymbol.Name is documented to be possibly empty in
                // which case it is "unnamed", so ignore it.
                if (param.Name == "")
                {
                    continue;
                }

                // C# allows us to create variables with the same names as reserved keywords,
                // as long as we prefix with @ (e.g. @int is a valid identifier)
                // So any parameters which are reserved must have the @ prefix
                string     paramName;
                SyntaxKind paramNameKind = SyntaxFacts.GetKeywordKind(param.Name);
                if (SyntaxFacts.GetReservedKeywordKinds().Any(reservedKind => reservedKind == paramNameKind))
                {
                    paramName = "@" + param.Name;
                }
                else
                {
                    paramName = param.Name;
                }

                string psuedoName = GetPsuedoName(arg);
                if (psuedoName != null)
                {
                    bool matchesParamName = string.Equals(
                        psuedoName,
                        param.Name,
                        StringComparison.OrdinalIgnoreCase
                        );

                    if (matchesParamName)
                    {
                        continue;
                    }
                }

                yield return(new ArgParamBinding(
                                 position: idx,
                                 paramName: paramName,        // Use the verbatim parameter name if applicable
                                 syntax: arg
                                 ));
            }
        }