コード例 #1
0
        /// <summary>
        /// Validates configuration file.
        /// </summary>
        public static bool ValidateRootConfiguration(ISourceFile sourceFile, Logger logger, LoggingContext loggingContext)
        {
            if (sourceFile.Statements.Count != 1)
            {
                logger.ReportOnlyASingleFunctionCallInConfigurationFile(loggingContext, sourceFile.LocationForLogging(sourceFile));
                return(false);
            }

            var configurationStatement = sourceFile.Statements[0];

            if (!configurationStatement.IsConfigurationDeclaration())
            {
                logger.ReportUnknownStatementInConfigurationFile(loggingContext, configurationStatement.LocationForLogging(sourceFile));
                return(false);
            }

            var callExpression = configurationStatement.AsCallExpression();

            if (callExpression.Arguments.Count != 1)
            {
                string message = I($"Configuration expression should take 1 argument but got {callExpression.Arguments.Count}");
                logger.ReportInvalidConfigurationFileFormat(loggingContext, callExpression.LocationForLogging(sourceFile), message);
                return(false);
            }

            var objectLiteral = callExpression.Arguments[0].As <IObjectLiteralExpression>();

            if (objectLiteral == null)
            {
                string message = I($"Configuration expression should take object literal but got {callExpression.Arguments[0].Kind}");
                logger.ReportInvalidConfigurationFileFormat(loggingContext, callExpression.LocationForLogging(sourceFile), message);
                return(false);
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Validates <paramref name="sourceFile"/> to follow package configuration structure.
        /// </summary>
        public static bool ValidatePackageConfiguration(ISourceFile sourceFile, Logger logger, LoggingContext loggingContext)
        {
            if (sourceFile.Statements.Count < 1)
            {
                logger.ReportAtLeastSingleFunctionCallInPackageConfigurationFile(loggingContext, sourceFile.LocationForLogging(sourceFile));
                return(false);
            }

            var objectLiteralExpressions = new IObjectLiteralExpression[sourceFile.Statements.Count];

            bool hasErrors = false;

            for (int i = 0; i < sourceFile.Statements.Count; ++i)
            {
                var configurationStatement = sourceFile.Statements[i];
                if (!configurationStatement.IsPackageConfigurationDeclaration())
                {
                    logger.ReportUnknownStatementInPackageConfigurationFile(
                        loggingContext,
                        configurationStatement.LocationForLogging(sourceFile));
                    hasErrors = true;
                    continue;
                }

                var callExpression = configurationStatement.AsCallExpression();

                if (callExpression.Arguments.Count != 1)
                {
                    string message = I($"Package configuration should take 1 argument but got {callExpression.Arguments.Count}");
                    logger.ReportInvalidPackageConfigurationFileFormat(
                        loggingContext,
                        callExpression.LocationForLogging(sourceFile),
                        message);
                    hasErrors = true;
                    continue;
                }

                var objectLiteral = callExpression.Arguments[0].As <IObjectLiteralExpression>();
                if (objectLiteral == null)
                {
                    string message = I($"Package configuration should take object literal but got {callExpression.Arguments[0].Kind}");
                    logger.ReportInvalidPackageConfigurationFileFormat(
                        loggingContext,
                        callExpression.LocationForLogging(sourceFile),
                        message);
                    hasErrors = true;
                    continue;
                }

                objectLiteralExpressions[i] = objectLiteral;
            }

            return(hasErrors);
        }