예제 #1
0
        private static StyleCopSettings GetStyleCopSettings(AnalyzerOptions options, SyntaxTree tree, ImmutableArray <AdditionalText> additionalFiles, DeserializationFailureBehavior failureBehavior, CancellationToken cancellationToken)
        {
            foreach (var additionalFile in additionalFiles)
            {
                if (IsStyleCopSettingsFile(additionalFile.Path))
                {
                    SourceText additionalTextContent = additionalFile.GetText(cancellationToken);
                    return(GetStyleCopSettings(options, tree, additionalFile.Path, additionalTextContent, failureBehavior));
                }
            }

            if (tree != null)
            {
                var optionsProvider = options.AnalyzerConfigOptionsProvider().GetOptions(tree);
                return(new StyleCopSettings(new JsonObject(), optionsProvider));
            }

            return(new StyleCopSettings());
        }
예제 #2
0
        private static StyleCopSettings GetStyleCopSettings(AnalyzerOptions options, SyntaxTree tree, string path, SourceText text, DeserializationFailureBehavior failureBehavior)
        {
            var optionsProvider = options.AnalyzerConfigOptionsProvider().GetOptions(tree);

            try
            {
                var rootValue = JsonReader.Parse(text.ToString());
                if (!rootValue.IsJsonObject)
                {
                    throw new JsonParseException(
                              $"Settings file at '{path}' was missing or empty.",
                              JsonParseException.ErrorType.InvalidOrUnexpectedCharacter,
                              default);
                }

                var settingsObject = rootValue.AsJsonObject["settings"];
                if (settingsObject.IsJsonObject)
                {
                    return(new StyleCopSettings(settingsObject.AsJsonObject, optionsProvider));
                }
                else if (settingsObject.IsNull)
                {
                    throw new InvalidSettingsException("\"settings\" must be a JSON object.");
                }

                return(new StyleCopSettings());
            }
            catch (InvalidSettingsException) when(failureBehavior == DeserializationFailureBehavior.ReturnDefaultSettings)
            {
                // The settings file is invalid -> return the default settings.
            }
            catch (JsonParseException) when(failureBehavior == DeserializationFailureBehavior.ReturnDefaultSettings)
            {
                // The settings file is invalid -> return the default settings.
            }

            return(new StyleCopSettings());
        }