Esempio n. 1
0
        public static void SetParameterValues(ParameterLoadingDiagnosticAnalyzer parameteredAnalyzer,
                                              AnalyzerOptions options)
        {
            if (ProcessedAnalyzers.ContainsKey(parameteredAnalyzer))
            {
                return;
            }

            var additionalFile = options.AdditionalFiles
                                 .FirstOrDefault(f => ConfigurationFilePathMatchesExpected(f.Path));

            if (additionalFile == null)
            {
                return;
            }

            ImmutableList <RuleParameterValues> parameters;

            using (XmlTextReader xtr = new XmlTextReader(additionalFile.Path))
            {
                xtr.Normalization = false;
                var xml = XDocument.Load(xtr);
                parameters = ParseParameters(xml);
            }

            var propertyParameterPairs = parameteredAnalyzer.GetType()
                                         .GetProperties()
                                         .Select(p => new { Property = p, Descriptor = p.GetCustomAttributes <RuleParameterAttribute>().SingleOrDefault() })
                                         .Where(p => p.Descriptor != null);

            var ids = new HashSet <string>(parameteredAnalyzer.SupportedDiagnostics.Select(diagnostic => diagnostic.Id));

            foreach (var propertyParameterPair in propertyParameterPairs)
            {
                var parameter = parameters
                                .FirstOrDefault(p => ids.Contains(p.RuleId));

                var parameterValue = parameter?.ParameterValues
                                     .FirstOrDefault(pv => pv.ParameterKey == propertyParameterPair.Descriptor.Key);

                if (parameterValue == null)
                {
                    return;
                }

                var value          = parameterValue.ParameterValue;
                var convertedValue = ChangeParameterType(value, propertyParameterPair.Descriptor.Type);
                propertyParameterPair.Property.SetValue(parameteredAnalyzer, convertedValue);
            }

            ProcessedAnalyzers.AddOrUpdate(parameteredAnalyzer, 0, (a, b) => b);
        }
Esempio n. 2
0
        public static void SetParameterValues(ParameterLoadingDiagnosticAnalyzer parameteredAnalyzer,
                                              AnalyzerOptions options)
        {
            if (ProcessedAnalyzers.ContainsKey(parameteredAnalyzer))
            {
                return;
            }

            var additionalFile = options.AdditionalFiles
                                 .FirstOrDefault(f => ConfigurationFilePathMatchesExpected(f.Path));

            if (additionalFile == null)
            {
                return;
            }

            var filePath   = additionalFile.Path;
            var xml        = XDocument.Load(filePath);
            var parameters = ParseParameters(xml);

            var propertyParameterPairs = parameteredAnalyzer.GetType()
                                         .GetProperties()
                                         .Select(p => new { Property = p, Descriptor = p.GetCustomAttributes <RuleParameterAttribute>().SingleOrDefault() })
                                         .Where(p => p.Descriptor != null);

            foreach (var propertyParameterPair in propertyParameterPairs)
            {
                var parameter = parameters
                                .FirstOrDefault(p => p.RuleId == parameteredAnalyzer.SupportedDiagnostics.Single().Id);

                var parameterValue = parameter?.ParameterValues
                                     .FirstOrDefault(pv => pv.ParameterKey == propertyParameterPair.Descriptor.Key);

                if (parameterValue == null)
                {
                    return;
                }

                var value          = parameterValue.ParameterValue;
                var convertedValue = ChangeParameterType(value, propertyParameterPair.Descriptor.Type);
                propertyParameterPair.Property.SetValue(parameteredAnalyzer, convertedValue);
            }

            ProcessedAnalyzers.AddOrUpdate(parameteredAnalyzer, 0, (a, b) => b);
        }
Esempio n. 3
0
        /**
         * At each compilation, parse the configuration file and set the rule parameters on
         *
         * There is no caching mechanism because inside the IDE, the configuration file can change when the user:
         * - changes something inside the configuration file
         * - loads a different solution in the IDE
         *
         * If caching needs to be done in the future, it should take into account:
         * - diffing the contents of the configuration file
         * - associating the file with a unique identifier for the build project
         */
        internal static void SetParameterValues(ParameterLoadingDiagnosticAnalyzer parameteredAnalyzer,
                                                AnalyzerOptions options)
        {
            var additionalFile = options.AdditionalFiles.FirstOrDefault(f => IsSonarLintXml(f.Path));

            if (additionalFile == null)
            {
                return;
            }

            var parameters = ParseParameters(additionalFile.Path);

            if (parameters.IsEmpty)
            {
                return;
            }

            var propertyParameterPairs = parameteredAnalyzer.GetType()
                                         .GetRuntimeProperties()
                                         .Select(p => new { Property = p, Descriptor = p.GetCustomAttributes <RuleParameterAttribute>().SingleOrDefault() })
                                         .Where(p => p.Descriptor != null);

            var ids = new HashSet <string>(parameteredAnalyzer.SupportedDiagnostics.Select(diagnostic => diagnostic.Id));

            foreach (var propertyParameterPair in propertyParameterPairs)
            {
                var parameter = parameters
                                .FirstOrDefault(p => ids.Contains(p.RuleId));

                var parameterValue = parameter?.ParameterValues
                                     .FirstOrDefault(pv => pv.ParameterKey == propertyParameterPair.Descriptor.Key);

                if (TryConvertToParameterType(parameterValue?.ParameterValue, propertyParameterPair.Descriptor.Type, out var value))
                {
                    propertyParameterPair.Property.SetValue(parameteredAnalyzer, value);
                }
            }
        }