HasFilteringCompilerPass implements CompilerPass { @Override public void process(Node externs, Node root) { if (features != null && !features.featureNames().isEmpty() || discoveredHasConditions != null || coerceUndefinedToFalse) { processChildren(root); } } HasFilteringCompilerPass(Features features, Set<String> discoveredHasConditions, boolean coerceUndefinedToFalse);  HasFilteringCompilerPass(Features features, boolean coerceUndefinedToFalse);  HasFilteringCompilerPass(Features features, Set<String> discoveredHasConditions);  HasFilteringCompilerPass(Features features); Iterable<String> getDiscoveredHasConditions(); @Override void process(Node externs, Node root);  }
@Test public void testProcess() throws Exception { String code, output; Features features = new Features(); Set<String> discoveredHasConditionals = new HashSet<String>(); HasFilteringCompilerPass pass = new HasFilteringCompilerPass(features, discoveredHasConditionals, false); features.put("foo", true); code = "define([], function() { if (has(\"foo\") === true) { say('hello'); } else {say('bye');} });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 0); Assert.assertEquals("define([],function(){if(has(\"foo\")===true)say(\"hello\");else say(\"bye\")});", output); discoveredHasConditionals.clear(); code = "define([], function() { if (has(\"foo\") == true) { say('hello'); } });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 0); Assert.assertEquals("define([],function(){if(has(\"foo\")==true)say(\"hello\")});", output); discoveredHasConditionals.clear(); features.put("foo", false); output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 0); Assert.assertEquals("define([],function(){if(has(\"foo\")==true)say(\"hello\")});", output); discoveredHasConditionals.clear(); code = "define([], function() { if (has(\"foo\") != true) { say('hello'); } });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 0); Assert.assertEquals("define([],function(){if(has(\"foo\")!=true)say(\"hello\")});", output); discoveredHasConditionals.clear(); code = "define([], function() {if (has(\"foo\")) { say('hello'); } else { say('bye');} });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){if(false)say(\"hello\");else say(\"bye\")});", output); discoveredHasConditionals.clear(); code = "define([], function() {if (!has(\"foo\")) { say('hello'); } else { say('bye');} });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){if(!false)say(\"hello\");else say(\"bye\")});", output); features.put("foo", true); discoveredHasConditionals.clear(); code = "define([], function() {if (has(\"foo\")) { say('hello'); } else { say('bye');} });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){if(true)say(\"hello\");else say(\"bye\")});", output); discoveredHasConditionals.clear(); code = "define([], function() {if (!has(\"foo\")) { say('hello'); } else { say('bye');} });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){if(!true)say(\"hello\");else say(\"bye\")});", output); discoveredHasConditionals.clear(); features.put("foo", true); code = "define([], function() {say(has(\"foo\") ? 'hello' : 'bye')});"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){say(true?\"hello\":\"bye\")});", output); discoveredHasConditionals.clear(); features.put("foo", false); code = "define([], function() {say(has(\"foo\") ? 'hello' : 'bye')});"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){say(false?\"hello\":\"bye\")});", output); discoveredHasConditionals.clear(); features.put("foo", true); code = "define([], function() {var v; if (v = has(\"foo\")) { say('hello'); } });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 0); Assert.assertEquals("define([],function(){var v;if(v=has(\"foo\"))say(\"hello\")});", output); discoveredHasConditionals.clear(); features.put("foo", true); code = "define([], function() {has(\"foo\") && say('hello'); });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){true&&say(\"hello\")});", output); discoveredHasConditionals.clear(); features.put("foo", false); code = "define([], function() {has(\"foo\") && say('hello'); });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){false&&say(\"hello\")});", output); discoveredHasConditionals.clear(); features.put("foo", true); code = "define([], function() {has(\"foo\") || say('hello'); });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){true||say(\"hello\")});", output); discoveredHasConditionals.clear(); features.put("foo", false); code = "define([], function() {has(\"foo\") || say('hello'); });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){false||say(\"hello\")});", output); pass = new HasFilteringCompilerPass(new Features(), discoveredHasConditionals, true); discoveredHasConditionals.clear(); code = "define([], function() {if (has(\"foo\")) { say('hello'); } else { say('bye');} });"; output = runPass(pass, code); System.out.println(output); Assert.assertTrue(discoveredHasConditionals.size() == 1); Assert.assertTrue(discoveredHasConditionals.contains("foo")); Assert.assertEquals("define([],function(){if(false)say(\"hello\");else say(\"bye\")});", output); }