Esempio n. 1
0
        private void CriticizeInternal()
        {
            // Read file and create rule context
            var sourceCodePath = SettingsBuilder.RulePath;
            var sourceCode = TryReadSourceCode(sourceCodePath);

            if (sourceCode != null)
            {
                var ruleContext = new RuleContext(sourceCode);

                // Evaluate the code with skeptic
                Critic critic = TryCreateCritic(ruleContext);
                if (critic != null)
                {
                    critic.Criticize();

                    // Output the result
                    var skepticWriter = new SkepticWriter(critic, System.Console.Out);
                    skepticWriter.Write();
                }
            }
            else // The file was not found
            {
                "Cannot load file to evaluate".ConsoleWriteLine();
            }
        }
Esempio n. 2
0
 private Critic TryCreateCritic(RuleContext ruleContext)
 {
     Critic critic = null;
     try
     {
         critic = new Critic(ruleContext, SettingsBuilder.Settings);
     }
     catch (FileNotFoundException ex)
     {
         "Cannot load rules file - {0}".ConsoleWriteLine(ex.FileName);
     }
     return critic;
 }
Esempio n. 3
0
        public void Apply(RuleContext context)
        {
            Violations = new RuleViolationCollection();
            var lines = context.SourceCode.ToLines();

            lines.ForEachWithIndex((i, line) =>
            {
                if (line.Length > MaxLineLength)
                {
                    var violationText = "Line {0} has {1} symbols".Formatted(i + 1, line.Length);
                    var violation = new RuleViolation(violationText);
                    Violations.Add(violation);
                }
            });
        }
Esempio n. 4
0
        public void Apply(RuleContext context)
        {
            Violations = new RuleViolationCollection();
            var lines = context.SourceCodeCleaned
                .ToLines();

            lines.ForEachWithIndex((i, line) =>
            {
                if (line.Contains("goto"))
                {
                    var violationText = "Line {0} with use of goto".Formatted(i + 1);
                    var violation = new RuleViolation(violationText);
                    Violations.Add(violation);
                }
            });
        }
        public void Apply(RuleContext context)
        {
            Violations = new RuleViolationCollection();
            var lines = context.SourceCodeCleaned
                .ToLines();

            lines.ForEachWithIndex((i, line) =>
            {
                if (Regex.Match(line, "\\s+$").Captures.Count > 0)
                {
                    var violationText = "Line {0} has trailing white spaces".Formatted(i + 1);
                    var violation = new RuleViolation(violationText);
                    Violations.Add(violation);
                }
            });
        }
Esempio n. 6
0
        public void Apply(RuleContext context)
        {
            Violations = new RuleViolationCollection();

            var lines = context.SourceCodeCleaned.ToLines();
            var nesting = 0;

            // Remove brackets but preserve lines
            lines.ForEachWithIndex((i, line) =>
            {
                foreach (var ch in line)
                {
                    if (ch == '{') nesting++;
                    if (ch == '}') nesting--;
                    if (MaxNumberOfScopesValue < nesting)
                    {
                        var violationText = "Too deep nesting in line {0} - {1} scopes".Formatted(i+1,nesting);
                        var violation = new RuleViolation(violationText);
                        Violations.Add(violation);
                    }
                    else if (nesting < 0)
                    {
                        var violationText = "} with no matching { on line {0}".Formatted(i + 1);
                        var violation = new RuleViolation(violationText);
                        Violations.Add(violation);
                    }
                }
            });
            

            if (nesting > 0)
            {
                var violationText = "{ with no matching } on line {0}".Formatted(lines.Count);
                var violation = new RuleViolation(violationText);
                Violations.Add(violation);
            }
        }
Esempio n. 7
0
 public Critic(RuleContext context, Settings settings = null, IRuleProvider ruleProvider = null)
 {
     Context = context;
     Settings = settings ?? new Settings();
     RuleProvider = ruleProvider ?? DefaultRuleProvider.Create(Settings);
 }