コード例 #1
0
 public JsDirectivePrologue(string value, JsContext context, JsParser parser)
     : base(value, JsPrimitiveType.String, context, parser)
 {
     // this is a "use strict" directive if the source context is EXACTLY "use strict"
     // don't consider the quotes so it can be " or ' delimiters
     UseStrict = string.CompareOrdinal(Context.Code, 1, "use strict", 0, 10) == 0;
 }
コード例 #2
0
 private JsReorderScopeVisitor(JsParser parser)
 {
     // save the mods we care about
     m_moveVarStatements = parser.Settings.ReorderScopeDeclarations && parser.Settings.IsModificationAllowed(JsTreeModifications.CombineVarStatementsToTopOfScope);
     m_moveFunctionDecls = parser.Settings.ReorderScopeDeclarations && parser.Settings.IsModificationAllowed(JsTreeModifications.MoveFunctionToTopOfScope);
     m_combineAdjacentVars = parser.Settings.IsModificationAllowed(JsTreeModifications.CombineVarStatements);
 }
コード例 #3
0
        public JsConstantWrapper(Object value, JsPrimitiveType primitiveType, JsContext context, JsParser parser)
            : base(context, parser)
        {
            PrimitiveType = primitiveType;

            // force numerics to be of type double
            Value = (primitiveType == JsPrimitiveType.Number ? System.Convert.ToDouble(value, CultureInfo.InvariantCulture) : value);
        }
コード例 #4
0
 public JsContext(JsParser parser)
     : this(new JsDocumentContext(parser))
 {
 }
コード例 #5
0
 public JsMember(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #6
0
 public JsConditionalCompilationIf(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #7
0
 public JsBlock(JsContext context, JsParser parser)
     : base(context, parser)
 {
     m_list = new List<JsAstNode>();
 }
コード例 #8
0
 public JsForIn(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #9
0
 public JsLabeledStatement(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #10
0
 public JsFunctionObject(JsContext functionContext, JsParser parser)
     : base(functionContext, parser)
 {
 }
コード例 #11
0
        /// <summary>
        /// Crunched JS string passed to it, returning crunched string.
        /// The ErrorList property will be set with any errors found during the minification process.
        /// </summary>
        /// <param name="source">source Javascript</param>
        /// <param name="codeSettings">code minification settings</param>
        /// <returns>minified Javascript</returns>
        public string MinifyJavaScript(string source, JsSettings codeSettings)
        {
            // default is an empty string
            var crunched = string.Empty;

            // reset the errors builder
            m_errorList = new List<MinifierError>();

            // create the parser from the source string.
            // pass null for the assumed globals array
            var parser = new JsParser(source);

            // file context is a property on the parser
            parser.FileContext = FileName;

            // hook the engine error event
            parser.CompilerError += OnJavaScriptError;

            try
            {
                var preprocessOnly = codeSettings != null && codeSettings.PreprocessOnly;
                var sb = new StringBuilder();
                using (var stringWriter = new StringWriter(sb, CultureInfo.InvariantCulture))
                {
                    if (preprocessOnly)
                    {
                        parser.EchoWriter = stringWriter;
                    }

                    // parse the input
                    var scriptBlock = parser.Parse(codeSettings);
                    if (scriptBlock != null && !preprocessOnly)
                    {
                        // we'll return the crunched code
                        if (codeSettings != null && codeSettings.Format == JsFormat.JSON)
                        {
                            // we're going to use a different output visitor -- one
                            // that specifically returns valid JSON.
                            if (!JsonOutputVisitor.Apply(stringWriter, scriptBlock))
                            {
                                m_errorList.Add(new MinifierError(
                                    true,
                                    0,
                                    null,
                                    null,
                                    null,
                                    this.FileName,
                                    0,
                                    0,
                                    0,
                                    0,
                                    JScript.InvalidJSONOutput));
                            }
                        }
                        else
                        {
                            // just use the normal output visitor
                            JsOutputVisitor.Apply(stringWriter, scriptBlock, codeSettings);
                        }
                    }
                }

                crunched = sb.ToString();
            }
            catch (Exception e)
            {
                m_errorList.Add(new MinifierError(
                    true,
                    0,
                    null,
                    null,
                    null,
                    this.FileName,
                    0,
                    0,
                    0,
                    0,
                    e.Message));
                throw;
            }

            return crunched;
        }
コード例 #12
0
 public JsObjectLiteralProperty(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #13
0
 public JsReturnNode(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #14
0
 public static void Apply(JsAstNode node, JsParser parser)
 {
     var logicalNot = new JsLogicalNot(node, parser);
     logicalNot.Apply();
 }
コード例 #15
0
 public JsLogicalNot(JsAstNode node, JsParser parser)
 {
     m_expression = node;
     m_parser = parser;
 }
コード例 #16
0
 public JsEvaluateLiteralVisitor(JsParser parser)
 {
     m_parser = parser;
 }
コード例 #17
0
 public JsCustomNode(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #18
0
 public JsGroupingOperator(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #19
0
 public JsWhileNode(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #20
0
 public JsVariableDeclaration(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #21
0
 public JsSwitchCase(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #22
0
 protected JsIterationStatement(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #23
0
 public JsObjectLiteralField(Object value, JsPrimitiveType primitiveType, JsContext context, JsParser parser)
     : base(value, primitiveType, context, parser)
 {
 }
コード例 #24
0
 protected JsExpression(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #25
0
 public JsUnaryOperator(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #26
0
 public JsBreak(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #27
0
ファイル: JsVar.cs プロジェクト: MatthewNichols/misakai-baker
 public JsVar(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #28
0
        public static void Apply(JsBlock block, JsParser parser)
        {
            // create a new instance of the visitor and apply it to the block
            var visitor = new JsReorderScopeVisitor(parser);
            block.Accept(visitor);

            // if there were any module directive prologues we need to promote, do them first
            var insertAt = 0;
            if (visitor.m_moduleDirectives != null)
            {
                foreach (var directivePrologue in visitor.m_moduleDirectives)
                {
                    insertAt = RelocateDirectivePrologue(block, insertAt, directivePrologue);
                }
            }

            // Make sure that we skip over any remaining comments and directive prologues.
            // we do NOT want to insert anything between the start of the scope and any directive prologues.
            while (insertAt < block.Count
                && (block[insertAt] is JsDirectivePrologue || block[insertAt] is JsImportantComment))
            {
                ++insertAt;
            }

            // first, we want to move all function declarations to the top of this block
            if (visitor.m_functionDeclarations != null)
            {
                foreach (var funcDecl in visitor.m_functionDeclarations)
                {
                    insertAt = RelocateFunction(block, insertAt, funcDecl);
                }
            }

            // special case: if there is only one var statement in the entire scope,
            // then just leave it alone because we will only add bytes by moving it around,
            // or be byte-neutral at best (no initializers and not in a for-statement).
            if (visitor.m_varStatements != null && visitor.m_varStatements.Count > 1)
            {
                // then we want to move all variable declarations after to the top (after the functions)
                foreach (var varStatement in visitor.m_varStatements)
                {
                    insertAt = RelocateVar(block, insertAt, varStatement);
                }
            }

            // then we want to do the same thing for all child functions (declarations AND other)
            if (visitor.m_functionDeclarations != null)
            {
                foreach (var funcDecl in visitor.m_functionDeclarations)
                {
                    Apply(funcDecl.Body, parser);
                }
            }

            if (visitor.m_functionExpressions != null)
            {
                foreach (var funcExpr in visitor.m_functionExpressions)
                {
                    Apply(funcExpr.Body, parser);
                }
            }
        }
コード例 #29
0
 public JsTryNode(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }
コード例 #30
0
 public JsEmptyStatement(JsContext context, JsParser parser)
     : base(context, parser)
 {
 }