コード例 #1
0
        /// <summary>
        /// Применить правило.
        /// </summary>
        /// <param name="report">Отчет.</param>
        /// <param name="document">Документ.</param>
        /// <param name="context">Контекст.</param>
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            // Отчеты не проверяем. в них переменные могут использоваться для отчета и для шаблона.
            // TODO: Сделать менеджмент правил, перенести эту проверку туда.
            if (document.ComponentType == ComponentType.CommonReport ||
                document.ComponentType == ComponentType.IntegratedReport)
            {
                return;
            }

            var tree     = document.GetSyntaxTree();
            var walker   = new ParseTreeWalker();
            var listener = new NotUsedVarListener(context, document);

            walker.Walk(listener, tree);

            var notUsedVariableDefinitions = listener.variableDefinitions
                                             .Where(d => !d.IsEverUsed);

            foreach (var notUsedVariableDefinition in notUsedVariableDefinitions)
            {
                report.AddWarning(Code, string.Format(Resources.NotUsedVariable, notUsedVariableDefinition.VariableName),
                                  document, notUsedVariableDefinition.VariableContext.Start.ToTextPosition());
            }
        }
コード例 #2
0
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var tree     = document.GetSyntaxTree();
            var walker   = new ParseTreeWalker();
            var listener = new IncorrectFormatStringListener(context);

            walker.Walk(listener, tree);
            foreach (var entry in listener.FormatItemsWithoutArguments)
            {
                var description = string.Format(Resources.ArgumentForTemplateNotFound,
                                                entry.Data.Text, entry.Data.Index, entry.TemplateString);
                var position = entry.Context.GetTextPosition();
                if (entry.TemplateStringSource == IncorrectFormatStringListener.TemplateStringSource.StringConstant)
                {
                    // Подсвечиваем некорректный описатель прямо в строковой константе.
                    var startIndex = position.StartIndex;
                    position.StartIndex = startIndex + entry.Data.Pos.StartIndex;
                    position.EndIndex   = startIndex + entry.Data.Pos.EndIndex;
                    position.Column     = entry.Data.Pos.Line == 0 ? position.Column + entry.Data.Pos.Column : entry.Data.Pos.Column;
                    position.Line      += entry.Data.Pos.Line;
                }
                report.AddError(FormatItemNotFoundRuleCode, description, document, position);
            }
            foreach (var entry in listener.FormatItemsWithEmptyArguments)
            {
                var description = string.Format(Resources.EmptyFormatArgument,
                                                entry.Data.Text, entry.Data.Index, entry.TemplateString);
                var position = entry.Context.GetTextPosition();
                if (entry.TemplateStringSource == IncorrectFormatStringListener.TemplateStringSource.StringConstant)
                {
                    // Подсвечиваем некорректный описатель прямо в строковой константе.
                    var startIndex = position.StartIndex;
                    position.StartIndex = startIndex + entry.Data.Pos.StartIndex;
                    position.EndIndex   = startIndex + entry.Data.Pos.EndIndex;
                    position.Column     = entry.Data.Pos.Line == 0 ? position.Column + entry.Data.Pos.Column : entry.Data.Pos.Column;
                    position.Line      += entry.Data.Pos.Line;
                }
                report.AddWarning(EmptyArgumentForFormatItemRuleCode, description, document, position);
            }
            foreach (var incorrectFormatEntry in listener.ArgumentsWithoutFormatItems)
            {
                var description = string.Format(Resources.RedutantFormatArgument,
                                                incorrectFormatEntry.Data.Text, incorrectFormatEntry.Data.Index, incorrectFormatEntry.TemplateString);
                report.AddWarning(ArgumentForFormatItemNotFound, description, document, incorrectFormatEntry.Context.GetTextPosition());
            }
        }
コード例 #3
0
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var tree     = document.GetSyntaxTree();
            var walker   = new ParseTreeWalker();
            var listener = new UsingTrueFalseKeywordsListener();

            walker.Walk(listener, tree);
            foreach (var entry in listener.Entries)
            {
                report.AddWarning(Code, Resources.UsingTrueFalseKeywords, document, entry.GetTextPosition());
            }
        }
コード例 #4
0
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            if (document.ComponentType != ComponentType.Function)
            {
                return;
            }

            var function = context.Development.Functions
                           .FirstOrDefault(f => string.Equals(f.Name, document.ComponentName, StringComparison.OrdinalIgnoreCase));

            if (function != null && string.IsNullOrWhiteSpace(function.Help) && string.IsNullOrWhiteSpace(function.Comment))
            {
                report.AddWarning(Code, Resources.NoFunctionHelp, document, new TextPosition());
            }
        }
コード例 #5
0
        /// <summary>
        /// Применить правило.
        /// </summary>
        /// <param name="report">Отчет.</param>
        /// <param name="document">Документ.</param>
        /// <param name="context">Контекст.</param>
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var tree            = document.GetSyntaxTree();
            var walker          = new ParseTreeWalker();
            var paramListener   = new CallMethodsWithSaveStateListener();
            var noParamListener = new CallMethodsWithoutSaveStateListener();

            walker.Walk(paramListener, tree);

            foreach (var notRestoredObjectDefinition in paramListener.NotRestoredObjectDefinitions)
            {
                report.AddWarning(Code, string.Format(Resources.ObjectDoesNotRestoreSelfStatus, notRestoredObjectDefinition.InvokerName),
                                  document, notRestoredObjectDefinition.StatementContext.GetTextPosition());
            }


            walker.Walk(noParamListener, tree);

            foreach (var notRestoredObjectDefinition in noParamListener.NotRestoredObjectDefinitions)
            {
                report.AddWarning(Code, string.Format(Resources.ObjectDoesNotRestoreSelfStatus, notRestoredObjectDefinition.InvokerName),
                                  document, notRestoredObjectDefinition.StatementContext.GetTextPosition());
            }
        }
コード例 #6
0
        /// <summary>
        /// Применить правило.
        /// </summary>
        /// <param name="report">Отчет.</param>
        /// <param name="document">Документ.</param>
        /// <param name="context">Контекст.</param>
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var tree     = document.GetSyntaxTree();
            var walker   = new ParseTreeWalker();
            var listener = new UsingInfoReferenceListener();

            walker.Walk(listener, tree);
            var listContextsWithError = listener.ContextWithInvocationErrors;

            foreach (var contextWithError in listContextsWithError)
            {
                report.AddWarning(Code,
                                  string.Format(Resources.UsingInfoReferenceRule, contextWithError.identifier().GetText()),
                                  document, contextWithError.GetTextPosition());
            }
        }
コード例 #7
0
        /// <summary>
        /// Применить правило.
        /// </summary>
        /// <param name="report">Отчет.</param>
        /// <param name="document">Документ.</param>
        /// <param name="context">Контекст.</param>
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var tree     = document.GetSyntaxTree();
            var walker   = new ParseTreeWalker();
            var listener = new SelfAssignmentVarListener(context);

            walker.Walk(listener, tree);
            var listContextesWithError = listener.ContextsWithError;

            foreach (var contextWithError in listContextesWithError)
            {
                report.AddWarning(Code,
                                  string.Format(Resources.SelfAssignmentVarRuleDescription, contextWithError.expression().GetText()),
                                  document, contextWithError.GetTextPosition());
            }
        }
コード例 #8
0
        /// <summary>
        /// Применить правило.
        /// </summary>
        /// <param name="report">Отчет.</param>
        /// <param name="document">Документ.</param>
        /// <param name="context">Контекст.</param>
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var allCommentsMatches = oneLineCommentRegex.Matches(document.Text).OfType <Match>()
                                     .Concat(multiLineCommentRegex.Matches(document.Text).OfType <Match>());

            foreach (var match in allCommentsMatches)
            {
                var commentBody = match.Groups[1].Value;
                if (todoDoneCommentBodyRegex.IsMatch(commentBody))
                {
                    var comment     = match.Value.Trim();
                    var description = string.Format(Resources.TodoDoneComments,
                                                    comment.Substring(0, comment.Length > DisplayedCommentMaxLength ? DisplayedCommentMaxLength : comment.Length));
                    report.AddWarning(Code, description, document, GetTextPosition(document.Text, match));
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// Применить правило.
        /// </summary>
        /// <param name="report">Отчет.</param>
        /// <param name="document">Документ.</param>
        /// <param name="context">Контекст.</param>
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var tree     = document.GetSyntaxTree();
            var walker   = new ParseTreeWalker();
            var listener = new UsingRedefinedVariabeleListener(context, document);

            walker.Walk(listener, tree);

            var definitions = listener.variableDefinitions
                              .Where(d => d.IsRedefined && !d.IsUsed &&
                                     !d.AssignExpression.Equals("null", StringComparison.OrdinalIgnoreCase) &&
                                     !d.AssignExpression.Equals("nil", StringComparison.OrdinalIgnoreCase));

            foreach (var definition in definitions)
            {
                report.AddWarning(Code, string.Format(Resources.VariableIsRedefinedBeforeUsing, definition.VariableName),
                                  document, definition.VariableContext.Start.ToTextPosition());
            }
        }
コード例 #10
0
        /// <summary>
        /// Применить правило.
        /// </summary>
        /// <param name="report">Отчет.</param>
        /// <param name="document">Документ.</param>
        /// <param name="context">Контекст.</param>
        public override void Apply(IReport report, IDocument document, IContext context)
        {
            var tree     = document.GetSyntaxTree();
            var walker   = new ParseTreeWalker();
            var listener = new NotAssignedVariablesListener(context, document);

            walker.Walk(listener, tree);

            foreach (var notAssignedVariable in listener.NotAssignedVariables)
            {
                report.AddError(Code, string.Format(Resources.UsingNotAssignedVariable, notAssignedVariable.GetText()),
                                document, notAssignedVariable.Start.ToTextPosition());
            }

            foreach (var uncertainVariable in listener.UncertainVariables)
            {
                report.AddWarning(Code, string.Format(Resources.VariableCanHasUncertainedValue, uncertainVariable.GetText()),
                                  document, uncertainVariable.Start.ToTextPosition());
            }
        }