Exemplo n.º 1
0
 public static bool IsEnabled(
     this SyntaxNodeAnalysisContext context,
     ConfigOptionDescriptor option,
     bool?defaultValue = null)
 {
     return(IsEnabled(context.Options, option, context.Node.SyntaxTree, defaultValue));
 }
Exemplo n.º 2
0
 public static bool TryGetOptionAsBool(
     this SyntaxNodeAnalysisContext context,
     ConfigOptionDescriptor option,
     out bool result)
 {
     return(TryGetOptionAsBool(context.Options, option, context.Node.SyntaxTree, out result));
 }
Exemplo n.º 3
0
 public static int GetOptionAsInt(
     this AnalyzerOptions analyzerOptions,
     ConfigOptionDescriptor option,
     SyntaxTree syntaxTree,
     int defaultValue)
 {
     return((TryGetOptionAsInt(analyzerOptions, option, syntaxTree, out int result))
         ? result
         : defaultValue);
 }
Exemplo n.º 4
0
        public static string GetValue(AnalyzerConfigOptions configOptions, ConfigOptionDescriptor option, string defaultValue = null)
        {
            if (configOptions.TryGetValue(option.Key, out string value))
            {
                return(value);
            }

            return(defaultValue
                   ?? CodeAnalysisConfig.Instance.EditorConfig.Options.GetValueOrDefault(option.Key)
                   ?? option.DefaultValue);
        }
Exemplo n.º 5
0
        public static bool?GetValueAsBool(AnalyzerConfigOptions configOptions, ConfigOptionDescriptor option, bool?defaultValue = null)
        {
            if (configOptions.TryGetValue(option.Key, out string rawValue) &&
                bool.TryParse(rawValue, out bool boolValue))
            {
                return(boolValue);
            }

            return(defaultValue
                   ?? CodeAnalysisConfig.Instance.GetOptionAsBool(option.Key)
                   ?? option.DefaultValueAsBool);
        }
Exemplo n.º 6
0
        public static bool TryGetValue(AnalyzerConfigOptions configOptions, ConfigOptionDescriptor option, out string value, string defaultValue = null)
        {
            if (configOptions.TryGetValue(option.Key, out string rawValue))
            {
                value = rawValue;
                return(true);
            }

            value = defaultValue
                    ?? CodeAnalysisConfig.Instance.EditorConfig.Options.GetValueOrDefault(option.Key)
                    ?? option.DefaultValue;

            return(value != null);
        }
        protected static bool TryReportRequiredOptionNotSet(
            SyntaxTreeAnalysisContext context,
            AnalyzerConfigOptions configOptions,
            DiagnosticDescriptor descriptor,
            ConfigOptionDescriptor option)
        {
            if (!IsOptionSet(configOptions, option))
            {
                ReportDiagnostic(context, descriptor);
                return(true);
            }

            return(false);
        }
Exemplo n.º 8
0
        public static bool TryGetOptionAsInt(
            this AnalyzerOptions analyzerOptions,
            ConfigOptionDescriptor option,
            SyntaxTree syntaxTree,
            out int result)
        {
            if (analyzerOptions
                .AnalyzerConfigOptionsProvider
                .GetOptions(syntaxTree)
                .TryGetValue(option.Key, out string rawValue) &&
                int.TryParse(rawValue, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.CurrentCulture, out int value))
            {
                result = value;
                return(true);
            }

            result = default;
            return(false);
        }
Exemplo n.º 9
0
        public static bool TryGetOptionAsBool(
            this AnalyzerOptions analyzerOptions,
            ConfigOptionDescriptor option,
            SyntaxTree syntaxTree,
            out bool result)
        {
            if (analyzerOptions
                .AnalyzerConfigOptionsProvider
                .GetOptions(syntaxTree)
                .TryGetValue(option.Key, out string rawValue) &&
                bool.TryParse(rawValue, out bool value))
            {
                result = value;
                return(true);
            }

            result = default;
            return(false);
        }
Exemplo n.º 10
0
        public static bool IsEnabled(
            this AnalyzerOptions analyzerOptions,
            ConfigOptionDescriptor option,
            SyntaxTree syntaxTree,
            bool?defaultValue = null)
        {
            if (analyzerOptions
                .AnalyzerConfigOptionsProvider
                .GetOptions(syntaxTree)
                .TryGetValue(option.Key, out string value) &&
                bool.TryParse(value, out bool result))
            {
                return(result);
            }

            return(defaultValue
                   ?? CodeAnalysisConfig.Instance.GetOptionAsBool(option.Key)
                   ?? option.DefaultValueAsBool
                   ?? false);
        }
        protected static bool TryReportObsoleteOption(
            SyntaxTreeAnalysisContext context,
            AnalyzerConfigOptions configOptions,
            LegacyConfigOptionDescriptor legacyOption,
            ConfigOptionDescriptor newOption,
            string newValue)
        {
            if (configOptions.IsEnabled(legacyOption))
            {
                DiagnosticHelpers.ReportDiagnostic(
                    context,
                    CommonDiagnosticRules.AnalyzerOptionIsObsolete,
                    Location.None,
                    legacyOption.Key,
                    $", use option '{newOption.Key} = {newValue}' instead");

                return(true);
            }

            return(false);
        }
Exemplo n.º 12
0
        public static bool TryGetValueAsBool(AnalyzerConfigOptions configOptions, ConfigOptionDescriptor option, out bool value, bool?defaultValue = null)
        {
            if (configOptions.TryGetValue(option.Key, out string rawValue) &&
                bool.TryParse(rawValue, out bool boolValue))
            {
                value = boolValue;
                return(true);
            }

            bool?maybeValue = defaultValue
                              ?? CodeAnalysisConfig.Instance.GetOptionAsBool(option.Key)
                              ?? option.DefaultValueAsBool;

            if (maybeValue != null)
            {
                value = maybeValue.Value;
                return(true);
            }

            value = false;
            return(false);
        }
 private static bool IsOptionSet(AnalyzerConfigOptions configOptions, ConfigOptionDescriptor option)
 {
     return(configOptions.TryGetValue(option.Key, out string _) ||
            CodeAnalysisConfig.Instance.EditorConfig.Options.ContainsKey(option.Key));
 }
Exemplo n.º 14
0
 internal static bool TryGetValueAsBool(this AnalyzerConfigOptions analyzerConfigOptions, ConfigOptionDescriptor option, out bool value)
 {
     return(TryGetValueAsBool(analyzerConfigOptions, option.Key, out value));
 }
Exemplo n.º 15
0
 internal static bool ContainsKey(this AnalyzerConfigOptions analyzerConfigOptions, ConfigOptionDescriptor option)
 {
     return(analyzerConfigOptions.TryGetValue(option.Key, out string _));
 }
Exemplo n.º 16
0
 internal static bool IsEnabled(this AnalyzerConfigOptions analyzerConfigOptions, ConfigOptionDescriptor option)
 {
     return(analyzerConfigOptions.TryGetValue(option.Key, out string rawValue) &&
            bool.TryParse(rawValue, out bool value) &&
            value);
 }