return new Compressor(options, false_by_default);
TreeTransformer.call(this, this.before, this.after);
this.options = defaults(options, {
- angular : false,
booleans : !false_by_default,
cascade : !false_by_default,
collapse_vars : !false_by_default,
var CHANGED, max_iter = 10;
do {
CHANGED = false;
- if (compressor.option("angular")) {
- statements = process_for_angular(statements);
- }
statements = eliminate_spurious_blocks(statements);
if (compressor.option("dead_code")) {
statements = eliminate_dead_code(statements, compressor);
}
}
- function process_for_angular(statements) {
- function has_inject(comment) {
- return /@ngInject/.test(comment.value);
- }
- function make_arguments_names_list(func) {
- return func.argnames.map(function(sym){
- return make_node(AST_String, sym, { value: sym.name });
- });
- }
- function make_array(orig, elements) {
- return make_node(AST_Array, orig, { elements: elements });
- }
- function make_injector(func, name) {
- return make_node(AST_SimpleStatement, func, {
- body: make_node(AST_Assign, func, {
- operator: "=",
- left: make_node(AST_Dot, name, {
- expression: make_node(AST_SymbolRef, name, name),
- property: "$inject"
- }),
- right: make_array(func, make_arguments_names_list(func))
- })
- });
- }
- function check_expression(body) {
- if (body && body.args) {
- // if this is a function call check all of arguments passed
- body.args.forEach(function(argument, index, array) {
- var comments = argument.start.comments_before;
- // if the argument is function preceded by @ngInject
- if (argument instanceof AST_Lambda && comments.length && has_inject(comments[0])) {
- // replace the function with an array of names of its parameters and function at the end
- array[index] = make_array(argument, make_arguments_names_list(argument).concat(argument));
- }
- });
- // if this is chained call check previous one recursively
- if (body.expression && body.expression.expression) {
- check_expression(body.expression.expression);
- }
- }
- }
- return statements.reduce(function(a, stat){
- a.push(stat);
-
- if (stat.body && stat.body.args) {
- check_expression(stat.body);
- } else {
- var token = stat.start;
- var comments = token.comments_before;
- if (comments && comments.length > 0) {
- var last = comments.pop();
- if (has_inject(last)) {
- // case 1: defun
- if (stat instanceof AST_Defun) {
- a.push(make_injector(stat, stat.name));
- }
- else if (stat instanceof AST_Definitions) {
- stat.definitions.forEach(function(def) {
- if (def.value && def.value instanceof AST_Lambda) {
- a.push(make_injector(def.value, def.name));
- }
- });
- }
- else {
- compressor.warn("Unknown statement marked with @ngInject [{file}:{line},{col}]", token);
- }
- }
- }
- }
-
- return a;
- }, []);
- }
-
function eliminate_spurious_blocks(statements) {
var seen_dirs = [];
return statements.reduce(function(a, stat){