public void Ctor_NullSourceText_DoesNotThrow()
        {
            // Arrange
            _source.GetText().Returns((SourceText)null);

            // Act
            Action action = () => new ResourceModel(_source);

            // Assert
            action.Should().NotThrow();
        }
Beispiel #2
0
        public void Execute(GeneratorExecutionContext context)
        {
            //Debugger.Launch();
            AdditionalText file     = context.AdditionalFiles.First();
            SourceText     fileText = file.GetText(context.CancellationToken);

            TextLineCollection lines = fileText.Lines;

            Debug.Assert(lines.Count == 4);

            BenchmarkSpecification specification = new();

            specification.NumPocos = int.Parse(lines[0].ToString());
            specification.NumProps = int.Parse(lines[1].ToString());
            specification.UseSerializationAttributes = bool.Parse(lines[2].ToString());
            specification.Process   = (JsonProcess)Enum.Parse(typeof(JsonProcess), lines[3].ToString());
            specification.Processor = (JsonProcessor)Enum.Parse(typeof(JsonProcessor), lines[4].ToString());

            if (!SpecificationIsValid(specification))
            {
                context.AddSource(BenchmarkSerializationLogicFileName, InvalidSepcificationSourceText);
                return;
            }

            GenerateBenchmarkImplementation(context, specification);
        }
        public static AnalyzerSettings LoadSettings([NotNull] AnalyzerOptions options, CancellationToken cancellationToken)
        {
            Guard.NotNull(options, nameof(options));

            AdditionalText settingsFileOrNull = options.AdditionalFiles.FirstOrDefault(file => IsSettingsFile(file.Path));

            if (settingsFileOrNull != null)
            {
                SourceText fileText = settingsFileOrNull.GetText(cancellationToken);

                try
                {
                    return(ReadSourceText(fileText, reader =>
                    {
                        var serializer = new DataContractSerializer(typeof(AnalyzerSettings));
                        return (AnalyzerSettings)serializer.ReadObject(reader);
                    }, cancellationToken));
                }
                catch (Exception ex)
                {
                    Debug.Write("Failed to parse analyser settings file. Using default settings. Exception: " + ex);
                }
            }

            return(AnalyzerSettings.Default);
        }
        /// <summary>
        /// This code works around dotnet/roslyn#6596 by using reflection APIs to bypass the problematic method while
        /// reading the content of an <see cref="AdditionalText"/> file. If the reflection approach fails, the code
        /// falls back to the previous behavior.
        /// </summary>
        /// <param name="additionalText">The additional text to read.</param>
        /// <param name="cancellationToken">The cancellation token that the operation will observe.</param>
        /// <returns>The content of the additional text file.</returns>
        private static SourceText GetText(AdditionalText additionalText, CancellationToken cancellationToken)
        {
            if (AvoidAdditionalTextGetText)
            {
                object document = GetField(additionalText, "_document");
                if (document != null)
                {
                    object textSource = GetField(document, "textSource");
                    if (textSource != null)
                    {
                        object textAndVersion = CallMethod(textSource, "GetValue", new[] { typeof(CancellationToken) }, cancellationToken);
                        if (textAndVersion != null)
                        {
                            SourceText text = GetProperty(textAndVersion, "Text") as SourceText;
                            if (text != null)
                            {
                                return(text);
                            }
                        }
                    }
                }
            }

            return(additionalText.GetText(cancellationToken));
        }
Beispiel #5
0
        private Dictionary <string, string> ParseAdditionalFile(AdditionalText additionalFile)
        {
            Dictionary <string, string> parsedPinvokes = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            if (additionalFile == null)
            {
                return(parsedPinvokes);
            }

            SourceText fileContents = additionalFile.GetText();

            foreach (TextLine line in fileContents.Lines)
            {
                string lineStr = line.ToString();
                if (!string.IsNullOrWhiteSpace(lineStr) && !lineStr.StartsWith("<!--"))
                {
                    string[] splitCount = lineStr.Split('!');

                    if (splitCount.Length == 2 || splitCount.Length == 3)
                    {
                        parsedPinvokes[splitCount[1]] = splitCount[0];
                        if (splitCount.Length == 3)
                        {
                            _isNotSupportedOnWin7.Add(splitCount[1]);
                        }
                    }
                }
            }

            return(parsedPinvokes);
        }
        private static ImmutableHashSet <string> LoadWhitelist(
            string whitelistFileName,
            ImmutableArray <AdditionalText> additionalFiles
            )
        {
            ImmutableHashSet <string> .Builder whitelist = ImmutableHashSet.CreateBuilder(
                StringComparer.Ordinal
                );

            AdditionalText whitelistFile = additionalFiles.FirstOrDefault(
                file => Path.GetFileName(file.Path) == whitelistFileName
                );

            if (whitelistFile == null)
            {
                return(whitelist.ToImmutableHashSet());
            }

            SourceText whitelistText = whitelistFile.GetText();

            foreach (TextLine line in whitelistText.Lines)
            {
                whitelist.Add(line.ToString().Trim());
            }

            return(whitelist.ToImmutableHashSet());
        }
        private static ImmutableHashSet <string> LoadBannedCategoriesList(
            AnalyzerOptions options
            )
        {
            ImmutableHashSet <string> .Builder bannedList = ImmutableHashSet.CreateBuilder(
                StringComparer.Ordinal
                );

            AdditionalText bannedListFile = options.AdditionalFiles.FirstOrDefault(
                file => Path.GetFileName(file.Path) == "BannedTestCategoriesList.txt"
                );

            if (bannedListFile == null)
            {
                return(bannedList.ToImmutableHashSet());
            }

            SourceText allowedListText = bannedListFile.GetText();

            foreach (TextLine line in allowedListText.Lines)
            {
                bannedList.Add(line.ToString().Trim());
            }

            return(bannedList.ToImmutableHashSet());
        }
 public ResourceModelFixture()
 {
     _source     = Substitute.For <AdditionalText>();
     _sourceText = SourceText.From("<root></root>");
     _source.GetText().Returns(_sourceText);
     _source.Path.Returns("/my/MyClass.resx");
 }
Beispiel #9
0
        public static void AddExternalNotNullMethods(Compilation compilation, AdditionalText config)
        {
            lock (ExternalNotNullMethods)
            {
                var text = config.GetText();
                if (lastConfigLength == text.Length)
                {
                    return;
                }
                ExternalNotNullMethods.Clear();
                lastConfigLength = text.Length;
                // SourceText.Lines sometimes doesn't actually split the text into multiple lines
                foreach (var method in text.ToString().Split('\n').Select(i => i.Trim()))
                {
                    if (!method.Contains("."))
                    {
                        return;
                    }

                    foreach (var match in ParseMethodName(compilation, method))
                    {
                        ExternalNotNullMethods.Add(match);
                    }
                }
            }
        }
        private string GetSourceCodeFromFile(AdditionalText file, GeneratorExecutionContext context)
        {
            var content = file.GetText(context.CancellationToken).ToString();

            return(content
                   .Replace("{0}", Namespace)
                   .Replace("{1}", AttributeName));
        }
Beispiel #11
0
        private void Execute(GeneratorExecutionContext context, AdditionalText file)
        {
            CFG grammar = CFGParser.Parse(file.GetText().ToString());

            grammar = grammar.ApplyOptions(GetOptions(context, file)).ToGreibachNormalForm();
            CFGSubtypingAPIGenerator generator = new CFGSubtypingAPIGenerator(grammar, Path.GetFileNameWithoutExtension(file.Path),
                                                                              GetBoolConfiguration("FluentAPI", context, file));

            context.AddSource(generator.NamespaceName(), SourceText.From(generator.PrintAPI(), Encoding.UTF8));
        }
Beispiel #12
0
        private string GetSourceCodeFromFile(AdditionalText file, GeneratorExecutionContext context)
        {
            var content = file.GetText(context.CancellationToken).ToString();

            return(content
                   .Replace("{0}", Namespace)
                   .Replace("{1}", Environment.UserName)
                   .Replace("{2}", Dns.GetHostName())
                   .Replace("{3}", DateTime.Now.ToString("F")));
        }
Beispiel #13
0
        private string GetFileContent(AdditionalText file)
        {
            StringBuilder str = new StringBuilder();

            foreach (TextLine line in file.GetText().Lines)
            {
                str.Append(line);
            }
            return(str.ToString());
        }
Beispiel #14
0
#pragma warning disable RS1012 // Start action has no registered actions.
        bool LoadOptions(CompilationStartAnalysisContext context, AdditionalText mdkOptions)
#pragma warning restore RS1012 // Start action has no registered actions.
        {
            try
            {
                var content        = mdkOptions.GetText(context.CancellationToken);
                var document       = XDocument.Parse(content.ToString());
                var ignoredFolders = document.Element("mdk")?.Elements("ignore").SelectMany(e => e.Elements("folder"));
                var ignoredFiles   = document.Element("mdk")?.Elements("ignore").SelectMany(e => e.Elements("file")).ToArray();
                var basePath       = Path.GetDirectoryName(mdkOptions.Path).TrimEnd('\\') + "\\..\\";
                if (!basePath.EndsWith("\\"))
                {
                    basePath += "\\";
                }

                _basePath      = new Uri(basePath);
                _namespaceName = (string)document.Element("mdk")?.Element("namespace") ?? DefaultNamespaceName;
                lock (_ignoredFolders)
                    lock (_ignoredFiles)
                    {
                        _ignoredFolders.Clear();
                        _ignoredFiles.Clear();
                        if (ignoredFolders != null)
                        {
                            foreach (var folderElement in ignoredFolders)
                            {
                                var folder = folderElement.Value;
                                if (!folder.EndsWith("\\"))
                                {
                                    _ignoredFolders.Add(new Uri(_basePath, new Uri(folder + "\\", UriKind.RelativeOrAbsolute)));
                                }
                                else
                                {
                                    _ignoredFolders.Add(new Uri(_basePath, new Uri(folder, UriKind.RelativeOrAbsolute)));
                                }
                            }
                        }

                        if (ignoredFiles != null)
                        {
                            foreach (var fileElement in ignoredFiles)
                            {
                                var file = fileElement.Value;
                                _ignoredFiles.Add(new Uri(_basePath, new Uri(file, UriKind.RelativeOrAbsolute)));
                            }
                        }
                    }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #15
0
        public SourceGeneratorProjectItem(string basePath, string filePath, string relativePhysicalPath, string fileKind, AdditionalText additionalText, string cssScope)
        {
            BasePath             = basePath;
            FilePath             = filePath;
            RelativePhysicalPath = relativePhysicalPath;
            _fileKind            = fileKind;
            AdditionalText       = additionalText;
            CssScope             = cssScope;
            var text = AdditionalText.GetText();

            RazorSourceDocument = new SourceTextRazorSourceDocument(AdditionalText.Path, relativePhysicalPath, text);
        }
Beispiel #16
0
        private static SourceText GenerateSourceForAdditionalFile(AdditionalText file, CancellationToken cancellationToken)
        {
            // We're going to "comment" out the contents of the file when generating this
            var sourceText = file.GetText(cancellationToken);

            Contract.ThrowIfNull(sourceText, "Failed to fetch the text of an additional file.");

            var changes       = sourceText.Lines.SelectAsArray(l => new TextChange(new TextSpan(l.Start, length: 0), "// "));
            var generatedText = sourceText.WithChanges(changes);

            return(SourceText.From(generatedText.ToString(), encoding: Encoding.UTF8));
        }
        public void AnalyzeFile(AdditionalText file, CompilationAnalysisContext context)
        {
            var text    = file.GetText();
            var content = text.ToString();

            foreach (Match match in Regex.Matches(content))
            {
                context.ReportDiagnostic(ExternalDiagnostic.Create(RuleValidateRequest,
                                                                   file.Path,
                                                                   text.Lines.GetLinePosition(match.Index).Line + 1,
                                                                   match.Value));
            }
        }
        private static void AddSourceForAdditionalFile(GeneratorExecutionContext context, AdditionalText file)
        {
            // We're going to "comment" out the contents of the file when generating this
            var sourceText = file.GetText(context.CancellationToken);

            Contract.ThrowIfNull(sourceText, "Failed to fetch the text of an additional file.");

            var changes       = sourceText.Lines.SelectAsArray(l => new TextChange(new TextSpan(l.Start, length: 0), "// "));
            var generatedText = sourceText.WithChanges(changes);

            // TODO: remove the generatedText.ToString() when I don't have to specify the encoding
            context.AddSource(GetGeneratedFileName(file.Path), SourceText.From(generatedText.ToString(), encoding: Encoding.UTF8));
        }
Beispiel #19
0
        private static void VerifyAppManifest(AdditionalFileAnalysisContext context, AdditionalText appManifest)
        {
            SourceText?appManifestXml = appManifest.GetText(context.CancellationToken);

            if (appManifestXml is null)
            {
                return;
            }

            // If the manifest file is corrupt - let the build fail
            XmlDocument doc = new();

            try
            {
                doc.LoadXml(appManifestXml.ToString());
            }
            catch
            {
                // Invalid xml, don't care
                return;
            }

            XmlNamespaceManager nsmgr = new(doc.NameTable);

            nsmgr.AddNamespace("v1", "urn:schemas-microsoft-com:asm.v1");
            nsmgr.AddNamespace("v3", "urn:schemas-microsoft-com:asm.v3");
            nsmgr.AddNamespace("v3ws", "http://schemas.microsoft.com/SMI/2005/WindowsSettings");

            if (doc.DocumentElement.SelectSingleNode("//v3:application/v3:windowsSettings/v3ws:dpiAware", nsmgr) is not null)
            {
                switch (context.Compilation.Language)
                {
                case LanguageNames.CSharp:
                    context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.s_migrateHighDpiSettings_CSharp,
                                                               Location.None,
                                                               appManifest.Path,
                                                               ApplicationConfig.PropertyNameCSharp.HighDpiMode));
                    break;

                case LanguageNames.VisualBasic:
                    context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.s_migrateHighDpiSettings_VB,
                                                               Location.None,
                                                               appManifest.Path,
                                                               ApplicationConfig.PropertyNameVisualBasic.HighDpiMode));
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
        }
Beispiel #20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceModel"/> class.
        /// </summary>
        /// <param name="source">The source information.</param>
        public ResourceModel(AdditionalText source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (string.IsNullOrWhiteSpace(source.Path))
            {
                throw new InvalidOperationException(@$ "Cannot derive class name from invalid path " "{source.Path}" "");
            }

            _propertiesCode = ProcessProperties(source.GetText()?.ToString());
            ClassName       = Path.GetFileNameWithoutExtension(source.Path);
        }
Beispiel #21
0
        public override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();
            context.RegisterCompilationStartAction(compilationStartContext =>
            {
                // Find the additional file with the terms.
                ImmutableArray <AdditionalText> additionalFiles = compilationStartContext.Options.AdditionalFiles;
                AdditionalText termsFile = additionalFiles.FirstOrDefault(file => Path.GetFileName(file.Path).Equals("Terms.xml"));

                if (termsFile != null)
                {
                    HashSet <string> terms = new HashSet <string>();
                    SourceText fileText    = termsFile.GetText(compilationStartContext.CancellationToken);

                    // Write the additional file back to a stream.
                    MemoryStream stream = new MemoryStream();
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        fileText.Write(writer);
                    }

                    // Read all the <Term> elements to get the terms.
                    XDocument document = XDocument.Load(stream);
                    foreach (XElement termElement in document.Descendants("Term"))
                    {
                        terms.Add(termElement.Value);
                    }

                    // Check every named type for the invalid terms.
                    compilationStartContext.RegisterSymbolAction(symbolAnalysisContext =>
                    {
                        INamedTypeSymbol namedTypeSymbol = (INamedTypeSymbol)symbolAnalysisContext.Symbol;
                        string symbolName = namedTypeSymbol.Name;

                        foreach (string term in terms)
                        {
                            if (symbolName.Contains(term))
                            {
                                symbolAnalysisContext.ReportDiagnostic(
                                    Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], term));
                            }
                        }
                    },
                                                                 SymbolKind.NamedType);
                }
            });
        }
        private static HashSet <string> ReadPublicSymbols(AdditionalText additionalFile)
        {
            HashSet <string> publicSymbols = new HashSet <string>();

            foreach (var line in additionalFile.GetText().Lines)
            {
                var text = line.ToString();

                if (!string.IsNullOrWhiteSpace(text))
                {
                    publicSymbols.Add(text);
                }
            }

            return(publicSymbols);
        }
        internal static AnalyzerSettingsRegistry LoadSettings([NotNull] AnalyzerOptions options,
                                                              CancellationToken cancellationToken)
        {
            Guard.NotNull(options, nameof(options));

            AdditionalText settingsFileOrNull = options.AdditionalFiles.FirstOrDefault(file => IsSettingsFile(file.Path));

            if (settingsFileOrNull != null)
            {
                SourceText fileText = settingsFileOrNull.GetText(cancellationToken);

                return(SafeReadSourceText(fileText, cancellationToken));
            }

            return(AnalyzerSettingsRegistry.ImmutableEmpty);
        }
        /// <summary>
        /// Gets the settings.
        /// </summary>
        /// <param name="additionalText">The additional text.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The <see cref="AnalyzersSettings"/></returns>
        internal static AnalyzersSettings GetSettings(AdditionalText additionalText, CancellationToken cancellationToken)
        {
            SourceText text = additionalText.GetText(cancellationToken);

            try
            {
                SettingsFile settingsRoot = JsonConvert.DeserializeObject <SettingsFile>(text.ToString());

                return(settingsRoot.Settings);
            }
            catch (JsonException)
            {
                // Error deserializing the settings file, will return the default settings instead.
            }

            return(new AnalyzersSettings());
        }
Beispiel #25
0
#pragma warning disable RS1012 // Start action has no registered actions.
        bool LoadOptions(CompilationStartAnalysisContext context, AdditionalText mdkOptions)
#pragma warning restore RS1012 // Start action has no registered actions.
        {
            try
            {
                var content    = mdkOptions.GetText(context.CancellationToken);
                var properties = MDKProjectOptions.Parse(content.ToString(), mdkOptions.Path);
                var basePath   = Path.GetFullPath(Path.GetDirectoryName(mdkOptions.Path) ?? ".").TrimEnd('\\') + "\\..\\";
                if (!basePath.EndsWith("\\"))
                {
                    basePath += "\\";
                }

                _basePath      = new Uri(basePath);
                _namespaceName = properties.Namespace ?? DefaultNamespaceName;
                lock (_ignoredFolders)
                    lock (_ignoredFiles)
                    {
                        _ignoredFolders.Clear();
                        _ignoredFiles.Clear();
                        foreach (var folder in properties.IgnoredFolders)
                        {
                            if (!folder.EndsWith("\\"))
                            {
                                _ignoredFolders.Add(new Uri(_basePath, new Uri(folder + "\\", UriKind.RelativeOrAbsolute)));
                            }
                            else
                            {
                                _ignoredFolders.Add(new Uri(_basePath, new Uri(folder, UriKind.RelativeOrAbsolute)));
                            }
                        }

                        foreach (var file in properties.IgnoredFiles)
                        {
                            _ignoredFiles.Add(new Uri(_basePath, new Uri(file, UriKind.RelativeOrAbsolute)));
                        }
                    }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #26
0
#pragma warning disable RS1012 // Start action has no registered actions.
        public static Settings Cache(CompilationStartAnalysisContext context)
#pragma warning restore RS1012 // Start action has no registered actions.
        {
            // See docs for using AdditionalFiles:
            // https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Using%20Additional%20Files.md
            // Using OrdinalIgnoreCase to compare file names per MSDN:
            // https://msdn.microsoft.com/en-us/library/dd465121.aspx#choosing_a_stringcomparison_member_for_your_method_call
            AdditionalText additionalText = context.Options?.AdditionalFiles
                                            .FirstOrDefault(file => string.Equals(Path.GetFileName(file.Path), SettingsFileName, StringComparison.OrdinalIgnoreCase));

            SourceText sourceText = additionalText?.GetText(context.CancellationToken);

            if (sourceText == null || !context.TryGetValue(sourceText, ValueProvider, out Settings result))
            {
                result = DefaultSettings;
            }

            return(result);
        }
Beispiel #27
0
            static ImmutableDictionary <string, (string value, Location location)> CreateResourceMapForFile(AdditionalText file, CancellationToken cancellationToken)
            {
                const string valueTagPrefix = @"<value>";
                const string valueTagSuffix = @"</value>";
                var          builder        = ImmutableDictionary.CreateBuilder <string, (string value, Location location)>();
                var          sourceText     = file.GetText(cancellationToken);
                var          sourceTextStr  = sourceText.ToString();
                var          parsedDocument = XDocument.Parse(sourceTextStr, LoadOptions.PreserveWhitespace);

                foreach (var dataElement in parsedDocument.Descendants("data"))
                {
                    if (!(dataElement.Attribute("name")?.Value is { } name) ||
                        !(dataElement.Elements("value").FirstOrDefault() is { } valueElement))
                    {
                        continue;
                    }

                    var dataElementStr  = dataElement.ToString();
                    var valueElementStr = valueElement.ToString();

                    var indexOfDataElement = sourceTextStr.IndexOf(dataElementStr, StringComparison.Ordinal);
                    if (indexOfDataElement < 0 ||
                        !valueElementStr.StartsWith(valueTagPrefix, StringComparison.OrdinalIgnoreCase) ||
                        !valueElementStr.EndsWith(valueTagSuffix, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var indexOfValue = indexOfDataElement +
                                       dataElementStr.IndexOf(valueElementStr, StringComparison.Ordinal) +
                                       valueTagPrefix.Length;
                    var valueLength      = valueElementStr.Length - (valueTagPrefix.Length + valueTagSuffix.Length);
                    var span             = new TextSpan(indexOfValue, valueLength);
                    var linePositionSpan = sourceText.Lines.GetLinePositionSpan(span);
                    var location         = Location.Create(file.Path, span, linePositionSpan);

                    var value = valueElementStr.Substring(valueTagPrefix.Length, valueLength);
                    builder[name] = (value, location);
                }

                return(builder.ToImmutable());
            }
        public SourceGeneratorProjectItem(string basePath, string filePath, string relativePhysicalPath, string fileKind, AdditionalText additionalText, string?cssScope, GeneratorExecutionContext context)
        {
            BasePath             = basePath;
            FilePath             = filePath;
            RelativePhysicalPath = relativePhysicalPath;
            _fileKind            = fileKind;
            AdditionalText       = additionalText;
            CssScope             = cssScope;
            _context             = context;
            var text = AdditionalText.GetText();

            if (text is null)
            {
                _context.ReportDiagnostic(Diagnostic.Create(RazorDiagnostics.SourceTextNotFoundDescriptor, Location.None, filePath));
            }
            else
            {
                RazorSourceDocument = new SourceTextRazorSourceDocument(AdditionalText.Path, relativePhysicalPath, text);
            }
        }
Beispiel #29
0
        public override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();
            context.RegisterCompilationStartAction(compilationStartContext =>
            {
                // Find the additional file with the terms.
                ImmutableArray <AdditionalText> additionalFiles = compilationStartContext.Options.AdditionalFiles;
                AdditionalText termsFile = additionalFiles.FirstOrDefault(file => Path.GetFileName(file.Path).Equals("Terms.txt"));

                if (termsFile != null)
                {
                    HashSet <string> terms = new HashSet <string>();

                    // Read the file line-by-line to get the terms.
                    SourceText fileText = termsFile.GetText(compilationStartContext.CancellationToken);
                    foreach (TextLine line in fileText.Lines)
                    {
                        terms.Add(line.ToString());
                    }

                    // Check every named type for the invalid terms.
                    compilationStartContext.RegisterSymbolAction(symbolAnalysisContext =>
                    {
                        INamedTypeSymbol namedTypeSymbol = (INamedTypeSymbol)symbolAnalysisContext.Symbol;
                        string symbolName = namedTypeSymbol.Name;

                        foreach (string term in terms)
                        {
                            if (symbolName.Contains(term))
                            {
                                symbolAnalysisContext.ReportDiagnostic(
                                    Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], term));
                            }
                        }
                    },
                                                                 SymbolKind.NamedType);
                }
            });
        }
Beispiel #30
0
        /// <inheritdoc />
        public XElement ParseXmlFile(AdditionalText xmlFile, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var xml = string.Empty;

                if (!cancellationToken.IsCancellationRequested)
                {
                    xml = xmlFile.GetText(cancellationToken).ToString();
                }

                if (!cancellationToken.IsCancellationRequested)
                {
                    return(XElement.Parse(xml));
                }
            }
            catch (XmlException ex)
            {
                ParsingDiagnostics.Add(Diagnostic.Create(_additionalFileParseRule, Location.None, ex.Message));
            }

            return(null);
        }