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; }
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); }
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); }
public JsContext(JsParser parser) : this(new JsDocumentContext(parser)) { }
public JsMember(JsContext context, JsParser parser) : base(context, parser) { }
public JsConditionalCompilationIf(JsContext context, JsParser parser) : base(context, parser) { }
public JsBlock(JsContext context, JsParser parser) : base(context, parser) { m_list = new List<JsAstNode>(); }
public JsForIn(JsContext context, JsParser parser) : base(context, parser) { }
public JsLabeledStatement(JsContext context, JsParser parser) : base(context, parser) { }
public JsFunctionObject(JsContext functionContext, JsParser parser) : base(functionContext, parser) { }
/// <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; }
public JsObjectLiteralProperty(JsContext context, JsParser parser) : base(context, parser) { }
public JsReturnNode(JsContext context, JsParser parser) : base(context, parser) { }
public static void Apply(JsAstNode node, JsParser parser) { var logicalNot = new JsLogicalNot(node, parser); logicalNot.Apply(); }
public JsLogicalNot(JsAstNode node, JsParser parser) { m_expression = node; m_parser = parser; }
public JsEvaluateLiteralVisitor(JsParser parser) { m_parser = parser; }
public JsCustomNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsGroupingOperator(JsContext context, JsParser parser) : base(context, parser) { }
public JsWhileNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsVariableDeclaration(JsContext context, JsParser parser) : base(context, parser) { }
public JsSwitchCase(JsContext context, JsParser parser) : base(context, parser) { }
protected JsIterationStatement(JsContext context, JsParser parser) : base(context, parser) { }
public JsObjectLiteralField(Object value, JsPrimitiveType primitiveType, JsContext context, JsParser parser) : base(value, primitiveType, context, parser) { }
protected JsExpression(JsContext context, JsParser parser) : base(context, parser) { }
public JsUnaryOperator(JsContext context, JsParser parser) : base(context, parser) { }
public JsBreak(JsContext context, JsParser parser) : base(context, parser) { }
public JsVar(JsContext context, JsParser parser) : base(context, parser) { }
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); } } }
public JsTryNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsEmptyStatement(JsContext context, JsParser parser) : base(context, parser) { }