All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.shapesecurity.shift.minifier.ComposedRule Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*
 * Copyright 2014 Shape Security, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shapesecurity.shift.minifier;

import com.shapesecurity.shift.ast.Block;
import com.shapesecurity.shift.ast.CatchClause;
import com.shapesecurity.shift.ast.Directive;
import com.shapesecurity.shift.ast.Expression;
import com.shapesecurity.shift.ast.FunctionBody;
import com.shapesecurity.shift.ast.Identifier;
import com.shapesecurity.shift.ast.Script;
import com.shapesecurity.shift.ast.Statement;
import com.shapesecurity.shift.ast.SwitchCase;
import com.shapesecurity.shift.ast.SwitchDefault;
import com.shapesecurity.shift.ast.VariableDeclaration;
import com.shapesecurity.shift.ast.VariableDeclarator;
import com.shapesecurity.shift.ast.directive.UnknownDirective;
import com.shapesecurity.shift.ast.directive.UseStrictDirective;
import com.shapesecurity.shift.ast.expression.ArrayExpression;
import com.shapesecurity.shift.ast.expression.BinaryExpression;
import com.shapesecurity.shift.ast.expression.CallExpression;
import com.shapesecurity.shift.ast.expression.ComputedMemberExpression;
import com.shapesecurity.shift.ast.expression.ConditionalExpression;
import com.shapesecurity.shift.ast.expression.FunctionExpression;
import com.shapesecurity.shift.ast.expression.IdentifierExpression;
import com.shapesecurity.shift.ast.expression.LiteralBooleanExpression;
import com.shapesecurity.shift.ast.expression.LiteralNullExpression;
import com.shapesecurity.shift.ast.expression.LiteralNumericExpression;
import com.shapesecurity.shift.ast.expression.LiteralRegExpExpression;
import com.shapesecurity.shift.ast.expression.LiteralStringExpression;
import com.shapesecurity.shift.ast.expression.NewExpression;
import com.shapesecurity.shift.ast.expression.ObjectExpression;
import com.shapesecurity.shift.ast.expression.PostfixExpression;
import com.shapesecurity.shift.ast.expression.PrefixExpression;
import com.shapesecurity.shift.ast.expression.StaticMemberExpression;
import com.shapesecurity.shift.ast.expression.ThisExpression;
import com.shapesecurity.shift.ast.property.DataProperty;
import com.shapesecurity.shift.ast.property.Getter;
import com.shapesecurity.shift.ast.property.ObjectProperty;
import com.shapesecurity.shift.ast.property.PropertyName;
import com.shapesecurity.shift.ast.property.Setter;
import com.shapesecurity.shift.ast.statement.BlockStatement;
import com.shapesecurity.shift.ast.statement.BreakStatement;
import com.shapesecurity.shift.ast.statement.ContinueStatement;
import com.shapesecurity.shift.ast.statement.DebuggerStatement;
import com.shapesecurity.shift.ast.statement.DoWhileStatement;
import com.shapesecurity.shift.ast.statement.EmptyStatement;
import com.shapesecurity.shift.ast.statement.ExpressionStatement;
import com.shapesecurity.shift.ast.statement.ForInStatement;
import com.shapesecurity.shift.ast.statement.ForStatement;
import com.shapesecurity.shift.ast.statement.FunctionDeclaration;
import com.shapesecurity.shift.ast.statement.IfStatement;
import com.shapesecurity.shift.ast.statement.LabeledStatement;
import com.shapesecurity.shift.ast.statement.ReturnStatement;
import com.shapesecurity.shift.ast.statement.SwitchStatement;
import com.shapesecurity.shift.ast.statement.SwitchStatementWithDefault;
import com.shapesecurity.shift.ast.statement.ThrowStatement;
import com.shapesecurity.shift.ast.statement.TryCatchStatement;
import com.shapesecurity.shift.ast.statement.TryFinallyStatement;
import com.shapesecurity.shift.ast.statement.VariableDeclarationStatement;
import com.shapesecurity.shift.ast.statement.WhileStatement;
import com.shapesecurity.shift.ast.statement.WithStatement;
import com.shapesecurity.shift.visitor.DirtyState;

import org.jetbrains.annotations.NotNull;

public class ComposedRule extends MinificationRule {
  @NotNull
  private final T[] rules;

  public ComposedRule(@NotNull T[] rules) {
    super();
    this.rules = rules;
  }

  @NotNull
  private DirtyState t(@NotNull Statement node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  private DirtyState t(@NotNull Directive node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  private DirtyState t(@NotNull Expression node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  private DirtyState t(@NotNull PropertyName node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  private DirtyState t(@NotNull ObjectProperty node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  private DirtyState t(@NotNull SwitchCase node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  private DirtyState t(@NotNull SwitchDefault node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull CatchClause node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull Block node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull VariableDeclarator node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull UnknownDirective node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull UseStrictDirective node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull ArrayExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull BinaryExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull CallExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull ComputedMemberExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull ConditionalExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull FunctionExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull IdentifierExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull LiteralBooleanExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull LiteralNullExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull LiteralNumericExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull LiteralRegExpExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull LiteralStringExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull NewExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull ObjectExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull PostfixExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull PrefixExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull StaticMemberExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull ThisExpression node) {
    return t(node);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull Identifier node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  @Override
  public DirtyState transform(@NotNull FunctionBody node) {
    boolean dirty = false;
    for (T rule : this.rules) {
      DirtyState transform = rule.transform(node);
      dirty = dirty || transform.dirty;
      node = transform.node;
    }
    return new DirtyState<>(node, dirty);
  }

  @NotNull
  @Override
  public DirtyState