Skip to content

Commit

Permalink
Some code cleanups for readability
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=181216471
  • Loading branch information
tbreisacher authored and brad4d committed Jan 9, 2018
1 parent 679ea8f commit fe990fe
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
1 change: 0 additions & 1 deletion src/com/google/javascript/jscomp/FunctionInjector.java
Expand Up @@ -663,7 +663,6 @@ private CanInlineResult canInlineReferenceAsStatementBlock(
*/
private boolean callMeetsBlockInliningRequirements(
Reference ref, final Node fnNode, ImmutableSet<String> namesToAlias) {
final boolean assumeMinimumCapture = this.assumeMinimumCapture;
// Note: functions that contain function definitions are filtered out
// in isCandidateFunction.

Expand Down
19 changes: 9 additions & 10 deletions src/com/google/javascript/jscomp/InlineFunctions.java
Expand Up @@ -228,9 +228,9 @@ public void findFunctionExpressions(NodeTraversal t, Node n) {
if (n.getFirstChild().isFunction()) {
fnNode = n.getFirstChild();
} else if (NodeUtil.isFunctionObjectCall(n)) {
Node fnIdentifingNode = n.getFirstFirstChild();
if (fnIdentifingNode.isFunction()) {
fnNode = fnIdentifingNode;
Node fnIdentifyingNode = n.getFirstFirstChild();
if (fnIdentifyingNode.isFunction()) {
fnNode = fnIdentifyingNode;
}
}

Expand Down Expand Up @@ -400,19 +400,18 @@ public void visit(NodeTraversal t, Node n, Node parent) {
case CALL:
Node child = n.getFirstChild();
String name = null;
// NOTE: The normalization pass insures that local names do not
// collide with global names.
// NOTE: The normalization pass ensures that local names do not collide with global names.
if (child.isName()) {
name = child.getString();
} else if (child.isFunction()) {
name = anonFunctionMap.get(child);
} else if (NodeUtil.isFunctionObjectCall(n)) {
checkState(NodeUtil.isGet(child));
Node fnIdentifingNode = child.getFirstChild();
if (fnIdentifingNode.isName()) {
name = fnIdentifingNode.getString();
} else if (fnIdentifingNode.isFunction()) {
name = anonFunctionMap.get(fnIdentifingNode);
Node fnIdentifyingNode = child.getFirstChild();
if (fnIdentifyingNode.isName()) {
name = fnIdentifyingNode.getString();
} else if (fnIdentifyingNode.isFunction()) {
name = anonFunctionMap.get(fnIdentifyingNode);
}
}

Expand Down
76 changes: 60 additions & 16 deletions test/com/google/javascript/jscomp/InlineFunctionsTest.java
Expand Up @@ -1199,10 +1199,15 @@ public void testNoInlineOfNonGlobalFunction1() {
}

public void testNoInlineOfNonGlobalFunction2() {
test("var g;function _f(){var g=function(){return 0}}" +
"function _h(){return g()}",
"var g;function _f(){}" +
"function _h(){return g()}");
test(
lines(
"var g;",
"function _f() { var g = function() { return 0; }; }",
"function _h() { return g(); }"),
lines(
"var g;",
"function _f(){}",
"function _h(){ return g(); }"));
}

public void testNoInlineOfNonGlobalFunction3() {
Expand Down Expand Up @@ -1664,27 +1669,56 @@ public void testComplexFunctionWithFunctionDefinition2() {
assumeMinimumCapture = false;

// Don't inline if local names might be captured.
testSame("function f(a){call(function(){return})}f()");
testSame(
lines(
"function f(a) {",
" call(function(){return});",
"}",
"f();"));

assumeMinimumCapture = true;

test("(function(){" +
"var f = function(a){call(function(){return a})};f()})()",
"{{var a$jscomp$inline_0=void 0;call(function(){return a$jscomp$inline_0})}}");
test(
lines(
"(function() {",
" var f = function(a) { call(function(){return a}); };",
" f();",
"})();"),
lines(
"{",
" {",
" var a$jscomp$inline_0 = void 0;",
" call(function() { return a$jscomp$inline_0; });",
" }",
"}"));
}

public void testComplexFunctionWithFunctionDefinition2a() {
assumeMinimumCapture = false;

// Don't inline if local names might be captured.
testSame("(function(){" +
"var f = function(a){call(function(){return a})};f()})()");
testSame(
lines(
"(function() {",
" var f = function(a) { call(function(){return a}); };",
" f();",
"})()"));

assumeMinimumCapture = true;

test("(function(){" +
"var f = function(a){call(function(){return a})};f()})()",
"{{var a$jscomp$inline_0=void 0;call(function(){return a$jscomp$inline_0})}}");
test(
lines(
"(function() {",
" var f = function(a) { call(function(){return a}); };",
" f();",
"})()"),
lines(
"{",
" {",
" var a$jscomp$inline_0 = void 0;",
" call(function() { return a$jscomp$inline_0; });",
" }",
"}"));
}

public void testComplexFunctionWithFunctionDefinition3() {
Expand Down Expand Up @@ -1916,9 +1950,19 @@ public void testFunctionExpressionCallInlining12() {

public void testFunctionExpressionOmega() {
// ... with unused recursive name.
test("(function (f){f(f)})(function(f){f(f)})",
"{var f$jscomp$inline_0=function(f$jscomp$1){f$jscomp$1(f$jscomp$1)};" +
"{{f$jscomp$inline_0(f$jscomp$inline_0)}}}");
test(
"(function (f){f(f)})(function(f){f(f)})",
lines(
"{",
" var f$jscomp$inline_0 = function(f$jscomp$1) {",
" f$jscomp$1(f$jscomp$1);",
" };",
" {",
" {",
" f$jscomp$inline_0(f$jscomp$inline_0);",
" }",
" }",
"}"));
}

public void testLocalFunctionInlining1() {
Expand Down

0 comments on commit fe990fe

Please sign in to comment.