private (FunctionInstance Function, IFunction FunctionAst)? CheckIfSimpleMapExpression(Engine engine, IFunction function)
        {
            var field = function.TryGetFieldFromSimpleLambdaExpression();

            if (field == null)
            {
                return(null);
            }
            var properties = new List <Expression>
            {
                new Property(PropertyKind.Data, new Identifier(field), false,
                             new StaticMemberExpression(new Identifier("self"), new Identifier(field)), false, false)
            };

            if (MoreArguments != null)
            {
                for (int i = 0; i < MoreArguments.Length; i++)
                {
                    var arg = MoreArguments.Get(i.ToString()).As <FunctionInstance>();

                    if (!(arg is ScriptFunctionInstance sfi))
                    {
                        continue;
                    }
                    var moreFuncAst = sfi.FunctionDeclaration;
                    field = moreFuncAst.TryGetFieldFromSimpleLambdaExpression();
                    if (field != null)
                    {
                        properties.Add(new Property(PropertyKind.Data, new Identifier(field), false,
                                                    new StaticMemberExpression(new Identifier("self"), new Identifier(field)), false, false));
                    }
                }
            }

            var functionExp = new FunctionExpression(
                function.Id,
                NodeList.Create(new List <Expression> {
                new Identifier("self")
            }),
                new BlockStatement(NodeList.Create(new List <Statement>
            {
                new ReturnStatement(new ObjectExpression(NodeList.Create(properties)))
            })),
                generator: false,
                function.Strict,
                async: false);
            var functionObject = new ScriptFunctionInstance(
                engine,
                functionExp,
                LexicalEnvironment.NewDeclarativeEnvironment(engine, engine.ExecutionContext.LexicalEnvironment),
                function.Strict
                );

            return(functionObject, functionExp);
        }
        CreateTestData(int start, int count)
        {
            var array =
                Enumerable
                .Range(start, count)
                .Select(x => new Literal(x, x.ToString(CultureInfo.InvariantCulture)))
                .ToArray();

            return(new TheoryData <int, int, Lazy <NodeList <Literal> > > {
                { start, count, Lazy.Create("Sequence", () => NodeList.Create(array.Select(x => x))) }, { start, count, Lazy.Create("Collection", () => NodeList.Create(new BreakingCollection <Literal>(array))) }, { start, count, Lazy.Create("ReadOnlyList", () => NodeList.Create(new BreakingReadOnlyList <Literal>(array))) }
            });
        }
        public void FolderKindDroppingRewriter_ClearsCorrectList()
        {
            var characteristicType = NodeFactory.CharacteristicType("id", "name");
            var profile            = NodeFactory.ProfileType("id", "name", NodeList.Create(characteristicType));
            var rewriter           = new SourceNodeToGitreeConverter.SeparatableChildrenRemover();
            var result             = (DatablobNode)NodeFactory.Datablob(
                NodeFactory.Metadata(null, null, null),
                characteristicTypes: NodeList.Create(characteristicType),
                profileTypes: NodeList.Create(profile))
                                     .Accept(rewriter);

            Assert.Collection(result.CharacteristicTypes, Assert.NotNull);
            Assert.Empty(result.ProfileTypes);
        }
Exemple #4
0
        public virtual void VisitArrowFunctionExpression(ArrowFunctionExpression arrowFunctionExpression)
        {
            //Here we construct the function so if we iterate only functions we will be able to iterate ArrowFunctions too
            var statement =
                arrowFunctionExpression.Expression
                    ? new BlockStatement(NodeList.Create(new List <IStatementListItem> {
                new ReturnStatement(arrowFunctionExpression.Body.As <Expression>())
            }))
                    : arrowFunctionExpression.Body.As <BlockStatement>();
            var func = new FunctionExpression(new Identifier(null),
                                              arrowFunctionExpression.Params,
                                              statement,
                                              false,
                                              new HoistingScope(),
                                              StrictModeScope.IsStrictModeCode);

            VisitFunctionExpression(func);
        }
Exemple #5
0
        public void CreateFromParams_WithNoArgs_ReturnsDefault()
        {
            var result = NodeList.Create <SourceNode>();

            Assert.Equal(default, result);