diff options
author | Mihai Bazon <mihai.bazon@gmail.com> | 2015-01-11 12:10:42 +0200 |
---|---|---|
committer | Mihai Bazon <mihai.bazon@gmail.com> | 2015-01-11 12:10:42 +0200 |
commit | a10f6a96d724319f9bb582dab42392b4ef05a0e0 (patch) | |
tree | 9b7c32f05c6ad6028a5b7fe9f5c13796c6731668 /lib | |
parent | 0d232a142288c26376e434c36d6374c615b3ee62 (diff) | |
parent | 6006dd933d30b5480002e23b5ae77e0c76059233 (diff) | |
download | tracifyjs-a10f6a96d724319f9bb582dab42392b4ef05a0e0.tar.gz tracifyjs-a10f6a96d724319f9bb582dab42392b4ef05a0e0.zip |
Merge pull request #482 from arty-name/inline-ng-inject
added @ngInject support for inline functions
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/lib/compress.js b/lib/compress.js index d503ed17..1f1d4b50 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -226,6 +226,17 @@ merge(Compressor.prototype, { return statements; 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, { @@ -234,37 +245,56 @@ merge(Compressor.prototype, { expression: make_node(AST_SymbolRef, name, name), property: "$inject" }), - right: make_node(AST_Array, func, { - elements: func.argnames.map(function(sym){ - return make_node(AST_String, sym, { value: sym.name }); - }) - }) + 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); - var token = stat.start; - var comments = token.comments_before; - if (comments && comments.length > 0) { - var last = comments.pop(); - if (/@ngInject/.test(last.value)) { - // 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); + + 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; }, []); } |