Пример #1
0
        public static bool IsRequireJs(string JsBlock)
        {
            Jint.Parser.JavaScriptParser parser = new Jint.Parser.JavaScriptParser();

            var prog = parser.Parse(JsBlock);

            foreach (var item in prog.Body)
            {
                if (item is Jint.Parser.Ast.ExpressionStatement)
                {
                    var expres = item as Jint.Parser.Ast.ExpressionStatement;

                    if (expres.Expression is Jint.Parser.Ast.CallExpression)
                    {
                        var call = expres.Expression as Jint.Parser.Ast.CallExpression;

                        if (call.Callee is Jint.Parser.Ast.Identifier)
                        {
                            var id = call.Callee as Jint.Parser.Ast.Identifier;
                            if (id.Name.ToLower() == "require" || id.Name.ToLower() == "define")
                            {
                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }
Пример #2
0
        public static HashSet <string> ListFunctionNames(string js)
        {
            HashSet <string> result = new HashSet <string>();

            if (IsRequireJs(js))
            {
                var functions = ListRequireJsFuncs(js);

                foreach (var item in functions)
                {
                    result.Add(item.Id.Name);
                }
            }
            else
            {
                Jint.Parser.JavaScriptParser parser = new Jint.Parser.JavaScriptParser();

                var jsprogram = parser.Parse(js);

                foreach (var item in jsprogram.FunctionDeclarations)
                {
                    result.Add(item.Id.Name);
                }
            }
            return(result);
        }
        public JSParserResult Parse(string code)
        {
            var jsp            = new Jint.Parser.JavaScriptParser();
            var returnedResult = new JSParserResult();

            Program program = null;

            try
            {
                program = jsp.Parse(code, new ParserOptions()
                {
                    Tokens  = true,
                    Comment = true,
                });
            }
            catch (ParserException pex)
            {
                returnedResult.Errors.Add(new ErrorMessage()
                {
                    Message     = pex.Message,
                    StartColumn = pex.Column,
                    StartLine   = pex.LineNumber
                });
            }
            catch (Exception ex)
            {
                returnedResult.InternalErrors.Add(new ErrorMessage()
                {
                    Message     = ex.Message,
                    StartColumn = 1,
                    StartLine   = 1
                });
            }

            if (program == null)
            {
                return(returnedResult);
            }

            var comments = (program.Comments ?? Enumerable.Empty <Comment>())
                           .Select(c => new CommentWrapper(c));

            _comments = new CommentsAgregator();
            _comments.ProcessComments(comments);

            ProcessStatements(program.Body, new ParserContext(returnedResult.Nodes));

            returnedResult.TaskList = TaskListAggregator.GetTaskList(_comments.Comments, _settings.ToDoKeyWords).ToList();

            return(returnedResult);
        }
Пример #4
0
        public JSParserResult Parse(string code)
        {
            var jsp = new Jint.Parser.JavaScriptParser();
            var returnedResult = new JSParserResult();

            Program program = null;
            try
            {
                program = jsp.Parse(code, new ParserOptions()
                {
                    Tokens = true,
                    Comment = true,
                });
            }
            catch (ParserException pex)
            {
                returnedResult.Errors.Add(new ErrorMessage()
                {
                    Message = pex.Message,
                    StartColumn = pex.Column,
                    StartLine = pex.LineNumber
                });
            }
            catch (Exception ex)
            {
                returnedResult.InternalErrors.Add(new ErrorMessage()
                {
                    Message = ex.Message,
                    StartColumn = 1,
                    StartLine = 1
                });
            }

            if (program == null)
            {
                return returnedResult;
            }

            var comments = (program.Comments ?? Enumerable.Empty<Comment>())
                .Select(c => new CommentWrapper(c));

            _comments = new CommentsAgregator();
            _comments.ProcessComments(comments);

            ProcessStatements(program.Body, new ParserContext(returnedResult.Nodes));

            returnedResult.TaskList = TaskListAggregator.GetTaskList(_comments.Comments, _settings.ToDoKeyWords).ToList();

            return returnedResult;
        }
Пример #5
0
        public static string GetFuncBody(string js, string functionName)
        {
            Jint.Parser.JavaScriptParser parser = new Jint.Parser.JavaScriptParser();

            var jsprogram = parser.Parse(js);

            if (jsprogram != null)
            {
                var func = GetFuncByName(jsprogram, functionName);
                if (func != null)
                {
                    return(GetFuncBody(js, func));
                }
            }
            return(null);
        }
Пример #6
0
        public static Dictionary <string, List <string> > ListFunctions(string js)
        {
            Dictionary <string, List <string> > result = new Dictionary <string, List <string> >();

            if (IsRequireJs(js))
            {
                var functions = ListRequireJsFuncs(js);

                foreach (var item in functions)
                {
                    string        name  = item.Id.Name;
                    List <string> paras = new List <string>();
                    if (item.Parameters != null)
                    {
                        foreach (var p in item.Parameters)
                        {
                            paras.Add(p.Name);
                        }
                    }

                    result[name] = paras;
                }
            }
            else
            {
                Jint.Parser.JavaScriptParser parser = new Jint.Parser.JavaScriptParser();

                var jsprogram = parser.Parse(js);

                foreach (var item in jsprogram.FunctionDeclarations)
                {
                    string        name  = item.Id.Name;
                    List <string> paras = new List <string>();
                    if (item.Parameters != null)
                    {
                        foreach (var p in item.Parameters)
                        {
                            paras.Add(p.Name);
                        }
                    }

                    result[name] = paras;
                }
            }
            return(result);
        }
Пример #7
0
        public static List <Jint.Parser.Ast.FunctionDeclaration> ListRequireJsFuncs(string requireJsBlock)
        {
            Jint.Parser.JavaScriptParser parser = new Jint.Parser.JavaScriptParser();

            var prog = parser.Parse(requireJsBlock);

            if (prog != null && prog.Body.Count() > 0)
            {
                var item = prog.Body.First();

                if (item is Jint.Parser.Ast.ExpressionStatement)
                {
                    var expres = item as Jint.Parser.Ast.ExpressionStatement;

                    if (expres.Expression is Jint.Parser.Ast.CallExpression)
                    {
                        var call = expres.Expression as Jint.Parser.Ast.CallExpression;
                        if (call != null && call.Arguments.Count() == 2)
                        {
                            var requireargu = call.Arguments[1];

                            if (requireargu != null && requireargu is Jint.Parser.Ast.FunctionExpression)
                            {
                                var requireFunc = requireargu as Jint.Parser.Ast.FunctionExpression;

                                if (requireFunc != null)
                                {
                                    return(requireFunc.FunctionDeclarations.ToList());
                                }
                            }
                        }
                    }
                }
            }


            return(new List <Jint.Parser.Ast.FunctionDeclaration>());
        }