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 JsEvaluateLiteralVisitor(JsParser parser) { m_parser = parser; }
public JsFunctionObject(JsContext functionContext, JsParser parser) : base(functionContext, parser) { }
public JsConditionalCompilationElseIf(JsContext context, JsParser parser) : base(context, parser) { }
public JsLogicalNot(JsAstNode node, JsParser parser) { m_expression = node; m_parser = parser; }
public JsLookup(JsContext context, JsParser parser) : base(context, parser) { RefType = JsReferenceType.Variable; }
public JsRegExpLiteral(JsContext context, JsParser parser) : base(context, parser) { }
public JsConstantWrapperPP(JsContext context, JsParser parser) : base(context, parser) { }
public JsUnaryOperator(JsContext context, JsParser parser) : base(context, parser) { }
public static JsAstNode CombineWithComma(JsContext context, JsParser parser, JsAstNode operand1, JsAstNode operand2) { var comma = new JsCommaOperator(context, parser); // if the left is a comma-operator already.... var leftBinary = operand1 as JsBinaryOperator; var rightBinary = operand2 as JsBinaryOperator; if (leftBinary != null && leftBinary.OperatorToken == JsToken.Comma) { // the left-hand side is already a comma operator. Instead of nesting these, we're // going to combine them // move the old list's left-hand side to our left-hand side comma.Operand1 = leftBinary.Operand1; JsAstNodeList list; if (rightBinary != null && rightBinary.OperatorToken == JsToken.Comma) { // the right is ALSO a comma operator. Create a new list, append all the rest of the operands // and set our right-hand side to be the list list = new JsAstNodeList(null, parser); list.Append(leftBinary.Operand2).Append(rightBinary.Operand1).Append(rightBinary.Operand2); } else { // the right is not a comma operator. // see if the left-hand side already has a list we can use list = leftBinary.Operand2 as JsAstNodeList; if (list == null) { // it's not a list already // create a new list with the left's right and our right and set it to our right list = new JsAstNodeList(null, parser); list.Append(leftBinary.Operand2); } // and add our right-hand operand to the end of the list list.Append(operand2); } // set the list on the right comma.Operand2 = list; } else if (rightBinary != null && rightBinary.OperatorToken == JsToken.Comma) { // the left hand side is NOT a comma operator. comma.Operand1 = operand1; // the right-hand side is already a comma-operator, but the left is not. // see if it already has a list we can reuse var rightList = rightBinary.Operand2 as JsAstNodeList; if (rightList != null) { // it does. Prepend its right-hand operand and use the list rightList.Insert(0, rightBinary.Operand1); } else { // it's not -- create a new list containing the operands rightList = new JsAstNodeList(rightBinary.Context, parser); rightList.Append(rightBinary.Operand1); rightList.Append(rightBinary.Operand2); } comma.Operand2 = rightList; } else { comma.Operand1 = operand1; comma.Operand2 = operand2; } return comma; }
public JsCommaOperator(JsContext context, JsParser parser) : base(context, parser) { this.OperatorToken = JsToken.Comma; }
public static void Apply(JsAstNode node, JsParser parser) { var visitor = new JsFinalPassVisitor(parser); node.Accept(visitor); }
private JsFinalPassVisitor(JsParser parser) { m_parser = parser; m_statementStart = new JsStatementStartVisitor(); }
public JsLexicalDeclaration(JsContext context, JsParser parser) : base(context, parser) { }
public JsVariableDeclaration(JsContext context, JsParser parser) : base(context, parser) { }
public JsCustomNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsReturnNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsVar(JsContext context, JsParser parser) : base(context, 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 JsBlock(JsContext context, JsParser parser) : base(context, parser) { m_list = new List<JsAstNode>(); }
public static void Apply(JsAstNode node, JsParser parser) { var logicalNot = new JsLogicalNot(node, parser); logicalNot.Apply(); }
public JsGroupingOperator(JsContext context, JsParser parser) : base(context, parser) { }
public JsConditionalCompilationComment(JsContext context, JsParser parser) : base(context, parser) { Statements = new JsBlock(null, parser); }
public JsGetterSetter(String identifier, bool isGetter, JsContext context, JsParser parser) : base(identifier, JsPrimitiveType.String, context, parser) { IsGetter = isGetter; }
public JsTryNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsSwitch(JsContext context, JsParser parser) : base(context, parser) { }
public JsConstStatement(JsContext context, JsParser parser) : base(context, parser) { }
public JsObjectLiteral(JsContext context, JsParser parser) : base(context, parser) { }