示例#1
0
            private async Task <Document> GetOrCreateSuppressionsDocumentAsync(Document document, CancellationToken c)
            {
                int index = 1;
                var suppressionsFileName = s_globalSuppressionsFileName + _fixer.DefaultFileExtension;

                if (document.Name == suppressionsFileName)
                {
                    index++;
                    suppressionsFileName = s_globalSuppressionsFileName + index.ToString() + _fixer.DefaultFileExtension;
                }

                Document suppressionsDoc = null;

                while (suppressionsDoc == null)
                {
                    var hasDocWithSuppressionsName = false;
                    foreach (var d in document.Project.Documents)
                    {
                        if (d.Name == suppressionsFileName)
                        {
                            // Existing global suppressions file, see if this file only has global assembly attributes.
                            hasDocWithSuppressionsName = true;

                            var t = await d.GetSyntaxTreeAsync(c).ConfigureAwait(false);

                            var r = await t.GetRootAsync(c).ConfigureAwait(false);

                            if (r.ChildNodes().All(n => _fixer.IsAttributeListWithAssemblyAttributes(n)))
                            {
                                suppressionsDoc = d;
                                break;
                            }
                        }
                    }

                    if (suppressionsDoc == null)
                    {
                        if (hasDocWithSuppressionsName)
                        {
                            index++;
                            suppressionsFileName = s_globalSuppressionsFileName + index.ToString() + _fixer.DefaultFileExtension;
                        }
                        else
                        {
                            // Create an empty global suppressions file.
                            suppressionsDoc = document.Project.AddDocument(suppressionsFileName, string.Empty);
                        }
                    }
                }

                return(suppressionsDoc);
            }