private CommandResult ExecuteFile(string path) { string extension = Path.GetExtension(path); //if (string.Equals(extension, ".csproj", StringComparison.OrdinalIgnoreCase) // || string.Equals(extension, ".props", StringComparison.OrdinalIgnoreCase)) //{ // if (!GeneratedCodeUtility.IsGeneratedCodeFile(path)) // return ExecuteProject(path); //} if (string.Equals(extension, ".ruleset", StringComparison.OrdinalIgnoreCase)) { if (!GeneratedCodeUtility.IsGeneratedCodeFile(path)) { return(ExecuteRuleSet(path)); } } else { string fileName = Path.GetFileName(path); if (string.Equals(fileName, ".editorconfig", StringComparison.OrdinalIgnoreCase) && !GeneratedCodeUtility.IsGeneratedCodeFile(path)) { return(ExecuteEditorConfig(path)); } } WriteLine(path, Verbosity.Diagnostic); return(CommandResult.NotSuccess); }
public static async Task <Project> FormatProjectAsync( Project project, ISyntaxFactsService syntaxFacts, CodeFormatterOptions options, CancellationToken cancellationToken = default) { if (options == null) { options = CodeFormatterOptions.Default; } foreach (DocumentId documentId in project.DocumentIds) { Document document = project.GetDocument(documentId); if (options.IncludeGeneratedCode || !GeneratedCodeUtility.IsGeneratedCodeFile(document.FilePath)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); if (options.IncludeGeneratedCode || !syntaxFacts.BeginsWithAutoGeneratedComment(root)) { DocumentOptionSet optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); Document newDocument = await Formatter.FormatAsync(document, optionSet, cancellationToken).ConfigureAwait(false); project = newDocument.Project; } } } return(project); }
private IEnumerable <Diagnostic> FilterDiagnostics(IEnumerable <Diagnostic> diagnostics, Project project, CancellationToken cancellationToken = default) { foreach (Diagnostic diagnostic in diagnostics) { if (diagnostic.IsEffective(Options, project.CompilationOptions) && (Options.ReportNotConfigurable || !diagnostic.Descriptor.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable))) { if (diagnostic.Descriptor.CustomTags.Contains(WellKnownDiagnosticTags.Compiler)) { Debug.Assert(diagnostic.Id.StartsWith("CS", "VB", StringComparison.Ordinal), diagnostic.Id); SyntaxTree tree = diagnostic.Location.SourceTree; if (tree == null || !GeneratedCodeUtility.IsGeneratedCode(tree, f => MefWorkspaceServices.Default.GetService <ISyntaxFactsService>(tree.Options.Language).IsComment(f), cancellationToken)) { yield return(diagnostic); } } else { yield return(diagnostic); } } } }
private async Task <ImmutableArray <UnusedSymbolInfo> > AnalyzeProject(Project project, CancellationToken cancellationToken) { WriteLine($"Analyze '{project.Name}'", Verbosity.Minimal); Compilation compilation = await project.GetCompilationAsync(cancellationToken); INamedTypeSymbol generatedCodeAttribute = compilation.GetTypeByMetadataName("System.CodeDom.Compiler.GeneratedCodeAttribute"); ImmutableHashSet <ISymbol> ignoredSymbols = Options.IgnoredSymbols .Select(f => DocumentationCommentId.GetFirstSymbolForDeclarationId(f, compilation)) .Where(f => f != null) .ToImmutableHashSet(); return(await UnusedSymbolFinder.FindUnusedSymbolsAsync(project, compilation, Predicate, ignoredSymbols, cancellationToken : cancellationToken).ConfigureAwait(false)); bool Predicate(ISymbol symbol) { return((UnusedSymbolKinds & GetUnusedSymbolKinds(symbol)) != 0 && IsVisible(symbol) && (!Options.IgnoreObsolete || !symbol.HasAttribute(MetadataNames.System_ObsoleteAttribute)) && (Options.IncludeGeneratedCode || !GeneratedCodeUtility.IsGeneratedCode(symbol, generatedCodeAttribute, MefWorkspaceServices.Default.GetService <ISyntaxFactsService>(project.Language).IsComment, cancellationToken))); } }
public static void TestIsGeneratedCodeFile() { Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("TemporaryGeneratedFile_")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("TemporaryGeneratedFile_Foo")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("TemporaryGeneratedFile_.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("TemporaryGeneratedFile_Foo.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\TemporaryGeneratedFile_.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\TemporaryGeneratedFile_Foo.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(".designer.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(".generated.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(".g.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(".g.i.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(".AssemblyAttributes.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.designer.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.generated.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.g.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.g.i.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.AssemblyAttributes.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\.designer.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\.generated.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\.g.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\.g.i.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\.AssemblyAttributes.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\Foo.designer.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\Foo.generated.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\Foo.g.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\Foo.g.i.cs")); Assert.True(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\Foo.AssemblyAttributes.cs")); }
private IEnumerable <string> GetFilePaths(IEnumerable <ReferencedSymbol> referencedSymbols) { return(referencedSymbols .SelectMany(f => f.Locations) .Where(f => !f.IsCandidateLocation && !f.IsImplicit && !GeneratedCodeUtility.IsGeneratedCodeFile(f.Document.FilePath)) .Select(f => f.Document.FilePath.Replace(SolutionDirectory, "")) .Distinct() .OrderBy(f => f)); }
public static void TestIsNotGeneratedCodeFile() { Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(null)); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile("")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(" ")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(".")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(@"\")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile("foo")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile("foo.")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(@"foo\")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(@"foo\.")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\foo")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile(@"c:\foo\")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.designer")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.generated")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.g")); Assert.False(GeneratedCodeUtility.IsGeneratedCodeFile("Foo.AssemblyAttributes")); }
private static void AnalyzeCompilationUnit(SyntaxNodeAnalysisContext context) { var compilationUnit = (CompilationUnitSyntax)context.Node; SyntaxToken token = compilationUnit.EndOfFileToken; if (compilationUnit.Span == token.Span && !token.HasTrailingTrivia && token.LeadingTrivia.All(f => !f.IsDirective)) { SyntaxTree syntaxTree = compilationUnit.SyntaxTree; if (!GeneratedCodeUtility.IsGeneratedCodeFile(syntaxTree.FilePath)) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.RemoveFileWithNoCode, Location.Create(syntaxTree, default(TextSpan))); } } }
public static async Task <ImmutableArray <Diagnostic> > AnalyzeSpellingAsync( ISpellingService service, Document document, SpellingData spellingData, SpellingFixerOptions options = null, CancellationToken cancellationToken = default) { SyntaxTree tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (tree == null) { return(ImmutableArray <Diagnostic> .Empty); } if (!options.IncludeGeneratedCode && GeneratedCodeUtility.IsGeneratedCode(tree, f => service.SyntaxFacts.IsComment(f), cancellationToken)) { return(ImmutableArray <Diagnostic> .Empty); } SyntaxNode root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false); return(service.AnalyzeSpelling(root, spellingData, options, cancellationToken)); }
public bool BeginsWithAutoGeneratedComment(SyntaxNode root) { return(GeneratedCodeUtility.BeginsWithAutoGeneratedComment( root, f => f.IsKind(SyntaxKind.SingleLineCommentTrivia, SyntaxKind.MultiLineCommentTrivia))); }
internal static async Task <ImmutableArray <ISymbol> > FindSymbolsAsync( Project project, SymbolFinderOptions options = null, IFindSymbolsProgress progress = null, CancellationToken cancellationToken = default) { Compilation compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); INamedTypeSymbol generatedCodeAttribute = compilation.GetTypeByMetadataName("System.CodeDom.Compiler.GeneratedCodeAttribute"); ImmutableArray <ISymbol> .Builder symbols = null; var namespaceOrTypeSymbols = new Stack <INamespaceOrTypeSymbol>(); namespaceOrTypeSymbols.Push(compilation.Assembly.GlobalNamespace); while (namespaceOrTypeSymbols.Count > 0) { INamespaceOrTypeSymbol namespaceOrTypeSymbol = namespaceOrTypeSymbols.Pop(); foreach (ISymbol symbol in namespaceOrTypeSymbol.GetMembers()) { SymbolKind kind = symbol.Kind; if (kind == SymbolKind.Namespace) { var namespaceSymbol = (INamespaceSymbol)symbol; SymbolFilterReason reason = options.GetReason(namespaceSymbol); if (reason == SymbolFilterReason.None) { namespaceOrTypeSymbols.Push(namespaceSymbol); } continue; } var isUnused = false; if (!options.UnusedOnly || UnusedSymbolUtility.CanBeUnusedSymbol(symbol)) { SymbolFilterReason reason = options.GetReason(symbol); switch (reason) { case SymbolFilterReason.None: { if (options.IgnoreGeneratedCode && GeneratedCodeUtility.IsGeneratedCode(symbol, generatedCodeAttribute, f => MefWorkspaceServices.Default.GetService <ISyntaxFactsService>(compilation.Language).IsComment(f), cancellationToken)) { continue; } if (options.UnusedOnly && !symbol.IsImplicitlyDeclared) { isUnused = await UnusedSymbolUtility.IsUnusedSymbolAsync(symbol, project.Solution, cancellationToken).ConfigureAwait(false); } if (!options.UnusedOnly || isUnused) { progress?.OnSymbolFound(symbol); (symbols ??= ImmutableArray.CreateBuilder <ISymbol>()).Add(symbol); } break; } case SymbolFilterReason.Visibility: case SymbolFilterReason.WithoutAttribute: case SymbolFilterReason.ImplicitlyDeclared: { continue; } case SymbolFilterReason.SymbolGroup: case SymbolFilterReason.Ignored: case SymbolFilterReason.WithAttribute: case SymbolFilterReason.Other: { break; } default: { Debug.Fail(reason.ToString()); break; } } } if (!isUnused && kind == SymbolKind.NamedType) { namespaceOrTypeSymbols.Push((INamedTypeSymbol)symbol); } } } return(symbols?.ToImmutableArray() ?? ImmutableArray <ISymbol> .Empty); }
public override bool BeginsWithAutoGeneratedComment(SyntaxNode root) { return(GeneratedCodeUtility.BeginsWithAutoGeneratedComment( root, f => f.IsKind(SyntaxKind.CommentTrivia))); }