Example #1
0
        public void Process(LintResults results)
        {
            var file = results.FileName;

            // Start the StyleCop console.
            var console = new StyleCopConsole(Environment.CurrentDirectory, true, null, null, true);

            // Create the StyleCop configuration.
            var configuration = new Configuration(new string[] { "DEBUG" });

            // Add the source files.
            var projects = new List<CodeProject>();
            foreach (var myProject in this.m_ProjectDiscovery.DiscoverProjects(file))
            {
                var project = new CodeProject(myProject.Path.GetHashCode(), myProject.Path, configuration);

                // Add each source file to this project.
                foreach (var sourceFilePath in myProject.DiscoveredFiles)
                {
                    console.Core.Environment.AddSourceCode(project, sourceFilePath.Path, null);
                }

                projects.Add(project);
            }

            // Define event handlers.
            EventHandler<OutputEventArgs> outputGenerated = (sender, e) =>
            {
            };
            EventHandler<ViolationEventArgs> violationEncountered = (sender, e) =>
            {
                if (e.SourceCode.Path != Path.Combine(Environment.CurrentDirectory, file))
                    return;
                var index = new LintIssueIndex
                {
                    Name = e.Violation.Rule.Name,
                    Code = e.Violation.Rule.CheckId,
                    Message = e.Message,
                    Severity = e.SourceCode.Project.ViolationsAsErrors ? LintSeverity.ERROR : LintSeverity.WARNING
                };
                results.Issues.Add(new LintIssue
                {
                    Index = index,
                    LineNumber = e.LineNumber,
                    Column = 0
                });
            };

            // Assign event handlers.
            console.OutputGenerated += outputGenerated;
            console.ViolationEncountered += violationEncountered;

            // Run StyleCop.
            console.Start(projects, false);

            // Finalise.
            console.OutputGenerated -= outputGenerated;
            console.ViolationEncountered -= violationEncountered;
        }
 public ConsoleWriteUsedOutsideOfProgramMainPolicy(LintResults results)
     : base(results)
 {
     this.m_Pattern = new InvocationExpression {
         Target = new MemberReferenceExpression
         {
             Target = new IdentifierExpression("Console"),
             MemberName = Pattern.AnyString,
         },
         Arguments = { new Repeat(new AnyNode()) }
     };
 }
 public ConsoleWriteUsedOutsideOfProgramMainPolicy(LintResults results)
     : base(results)
 {
     this.m_Pattern = new InvocationExpression {
         Target = new MemberReferenceExpression
         {
             Target     = new IdentifierExpression("Console"),
             MemberName = Pattern.AnyString,
         },
         Arguments = { new Repeat(new AnyNode()) }
     };
 }
 public UseImplicitVariableTypeInDeclarationPolicy(LintResults results)
     : base(results)
 {
     this.m_Pattern = new VariableDeclarationStatement {
         Type      = new AnyNode("type"),
         Variables =
         {
             new VariableInitializer {
                 Name        = Pattern.AnyString,
                 Initializer = new AnyNode("initializer")
             }
         }
     };
 }
Example #5
0
        public void Process(LintResults results)
        {
            var parser = new CSharpParser();
            SyntaxTree tree;
            using (var reader = new StreamReader(results.FileName))
            {
                tree = parser.Parse(reader.ReadToEnd(), results.FileName);
            }

            // Apply policies
            tree.AcceptVisitor(new EnsureClassNameMatchesFileNamePolicy(results));
            tree.AcceptVisitor(new EnsureNoNestedPublicClassesPolicy(results));
            tree.AcceptVisitor(new EnsureOnePublicClassPerFilePolicy(results));
            tree.AcceptVisitor(new UseImplicitVariableTypeInDeclarationPolicy(results));
            tree.AcceptVisitor(new ConsoleWriteUsedOutsideOfProgramMainPolicy(results));
        }
Example #6
0
        public void Process(LintResults results)
        {
            var        parser = new CSharpParser();
            SyntaxTree tree;

            using (var reader = new StreamReader(results.FileName))
            {
                tree = parser.Parse(reader.ReadToEnd(), results.FileName);
            }

            // Apply policies
            tree.AcceptVisitor(new EnsureClassNameMatchesFileNamePolicy(results));
            tree.AcceptVisitor(new EnsureNoNestedPublicClassesPolicy(results));
            tree.AcceptVisitor(new EnsureOnePublicClassPerFilePolicy(results));
            tree.AcceptVisitor(new UseImplicitVariableTypeInDeclarationPolicy(results));
            tree.AcceptVisitor(new ConsoleWriteUsedOutsideOfProgramMainPolicy(results));
        }
Example #7
0
 public EnsureOnePublicClassPerFilePolicy(LintResults results)
     : base(results)
 {
     this.m_TotalClasses   = 0;
     this.m_FirstClassName = null;
 }
 public EnsureOnePublicClassPerFilePolicy(LintResults results)
     : base(results)
 {
     this.m_TotalClasses = 0;
     this.m_FirstClassName = null;
 }
Example #9
0
        public static void Main(string[] args)
        {
            var kernel = new StandardKernel();
            kernel.Load<CSharpLibraryNinjectModule>();

            string settings = null;
            string root = null;
            bool version = false;
            bool help = false;
            var options = new OptionSet
            {
                { "s|settings=", v => settings = v },
                { "settings-base64=", v => settings =
                    Encoding.ASCII.GetString(Convert.FromBase64String(v)) },
                { "r|root=", v => root = v },
                { "h|help", v => help = true },
                { "v|version", v => version = true }
            };

            List<string> files;
            try
            {
                files = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("cslint: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `cslint --help' for more information.");
                return;
            }

            if (version)
            {
                Console.WriteLine("1");
                return;
            }

            if (help)
            {
                ShowHelp(options);
                return;
            }

            if (files.Count == 0)
            {
                Console.Write("cslint: ");
                Console.WriteLine("You must supply at least one file to lint.");
                Console.WriteLine("Try `cslint --help' for more information.");
                return;
            }

            if (!files.All(File.Exists))
            {
                Console.Write("cslint: ");
                Console.WriteLine("Unable to locate one or more files specified.");
                Console.WriteLine("Try `cslint --help' for more information.");
                return;
            }

            var settingsJson = new JsonSettings(settings);
            kernel.Bind<ISettings>().ToMethod(x => settingsJson);

            var allResults = new List<LintResults>();
            foreach (var file in files)
            {
                var results = new LintResults();
                results.FileName = new FileInfo(file).FullName;
                results.BaseName = new FileInfo(file).Name;

                if (root != null && Directory.Exists(root))
                    Directory.SetCurrentDirectory(root);

                if (results.FileName.StartsWith(Environment.CurrentDirectory))
                    results.FileName = results.FileName.Substring(Environment.CurrentDirectory.Length + 1);

                var linters = kernel.GetAll<ILinter>();
                foreach (var linter in linters)
                    linter.Process(results);

                allResults.Add(results);
            }

            Console.Write(JsonConvert.SerializeObject(allResults));
        }
 public EnsureClassNameMatchesFileNamePolicy(LintResults results)
     : base(results)
 {
 }
Example #11
0
        public void Process(LintResults results)
        {
            var file = results.FileName;

            // Start the StyleCop console.
            var console = new StyleCopConsole(Environment.CurrentDirectory, true, null, null, true);

            // Create the StyleCop configuration.
            var configuration = new Configuration(new string[] { "DEBUG" });

            // Add the source files.
            var projects = new List <CodeProject>();

            foreach (var myProject in this.m_ProjectDiscovery.DiscoverProjects(file))
            {
                var project = new CodeProject(myProject.Path.GetHashCode(), myProject.Path, configuration);

                // Add each source file to this project.
                foreach (var sourceFilePath in myProject.DiscoveredFiles)
                {
                    console.Core.Environment.AddSourceCode(project, sourceFilePath.Path, null);
                }

                projects.Add(project);
            }

            // Define event handlers.
            EventHandler <OutputEventArgs> outputGenerated = (sender, e) =>
            {
            };
            EventHandler <ViolationEventArgs> violationEncountered = (sender, e) =>
            {
                if (e.SourceCode.Path != Path.Combine(Environment.CurrentDirectory, file))
                {
                    return;
                }
                var index = new LintIssueIndex
                {
                    Name     = e.Violation.Rule.Name,
                    Code     = e.Violation.Rule.CheckId,
                    Message  = e.Message,
                    Severity = e.Warning ? LintSeverity.WARNING : LintSeverity.ERROR
                };
                results.Issues.Add(new LintIssue
                {
                    Index      = index,
                    LineNumber = e.LineNumber,
                    Column     = 0
                });
            };

            // Assign event handlers.
            console.OutputGenerated      += outputGenerated;
            console.ViolationEncountered += violationEncountered;

            // Run StyleCop.
            console.Start(projects, false);

            // Finalise.
            console.OutputGenerated      -= outputGenerated;
            console.ViolationEncountered -= violationEncountered;
        }
Example #12
0
 protected LintPolicy(LintResults results)
 {
     this.Results = results;
 }
Example #13
0
 protected LintPolicy(LintResults results)
 {
     this.Results = results;
 }
Example #14
0
 public EnsureClassNameMatchesFileNamePolicy(LintResults results)
     : base(results)
 {
 }
 public EnsureNoNestedPublicClassesPolicy(LintResults results)
     : base(results)
 {
 }
Example #16
0
 public EnsureNoNestedPublicClassesPolicy(LintResults results)
     : base(results)
 {
 }