DepUtils { static public ParseResult parseDependencies(Node node, Set<String> dependentFeatures) { ParseResult result = new ParseResult(); for (Node cursor = node.getFirstChild(); cursor != null; cursor = cursor .getNext()) { Node defineDeps = null, requireDeps = null, dependencies; String condition; if ((condition = NodeUtil.conditionFromHasNode(cursor)) != null) { dependentFeatures.add(condition); } else { @SuppressWarnings("unchecked") LinkedHashSet<String>[] resultArray = new LinkedHashSet[]{null, null}; defineDeps = NodeUtil.moduleDepsFromDefine(cursor); requireDeps = NodeUtil.moduleDepsFromRequire(cursor); int i; for (i = 0, dependencies = defineDeps; i < 2; i++, dependencies = requireDeps) { if (dependencies == null) { continue; } resultArray[i] = new LinkedHashSet<String>(); Node strNode = dependencies.getFirstChild(); while (strNode != null) { if (strNode.getType() == Token.STRING) { String mid = strNode.getString(); URI uri = URI.create(mid); if (!PathUtil.invalidChars.matcher(mid).find() && !uri.isAbsolute() && !uri.getPath().startsWith("/")) { resultArray[i].add(mid); if (hasPattern.matcher(mid).find()) { int idx = mid.indexOf("!"); HasNode hasNode = new HasNode(mid.substring(idx+1)); hasNode.evaluateAll( mid.substring(0, idx), Features.emptyFeatures, dependentFeatures, BooleanTerm.TRUE, null); } } } strNode = strNode.getNext(); } } result.addAll(resultArray[0], resultArray[1]); } if (cursor.hasChildren()) { result.addAll(parseDependencies(cursor, dependentFeatures)); } } return result; }  static Collection<URI> removeRedundantPaths(Collection<URI> uris); static DepTreeNode getNodeForResource(URI requestUri,
			Map<URI, DepTreeNode> dependencies); static ParseResult parseDependencies(Node node, Set<String> dependentFeatures);  }
@Test public void testParseDependencies() { String js1 = "\ndefine(\"foo\",[\"a\", \"b\", \"c\"], function(a, b, c) {\nalert(\"hellow\");\nreturn null\n});"; String js2 = "\ndefine([\"a\", \"b\", \"c\"], function(a, b, c) {\nalert(\"hellow\");\nreturn null;\n});"; String js3 = "define([], function() {\nalert(\"hello\");\nreturn null;\n});"; String js4 = "(function(){\ndefine([\"a\", \"b\", \"c\"], function(a, b, c) {\nalert(\"hellow\");\nreturn null;\n});})()"; String js5 = "define(deps, function() {\nalert(\"hello\");\nreturn null;\n});"; String js6 = "function foo() {\nalert(\"hello\");\nreturn null;\n};"; String js7 = "define(['dep1', 'dojo/has!hasTest1?hasTest2?dep1:dep2'], function() { require(['has!hasTest3?:dep3'], function(dep3) { if (has('fooTest')) {return true;} else {return false;} }); if (has('barTest')) return bar;});"; Compiler compiler = new Compiler(); Node node = compiler.parse(JSSourceFile.fromCode("js1", js1)); Collection<String> deps = DepUtils.parseDependencies(node, new HashSet<String>()).getDefineDependencies(); assertEquals(deps.toString(), "[a, b, c]"); node = compiler.parse(JSSourceFile.fromCode("js2", js2)); deps = DepUtils.parseDependencies(node, new HashSet<String>()).getDefineDependencies(); assertEquals(deps.toString(), "[a, b, c]"); node = compiler.parse(JSSourceFile.fromCode("js3", js3)); deps = DepUtils.parseDependencies(node, new HashSet<String>()).getDefineDependencies(); assertEquals(deps.size(), 0); node = compiler.parse(JSSourceFile.fromCode("js4", js4)); deps = DepUtils.parseDependencies(node, new HashSet<String>()).getDefineDependencies(); assertEquals(deps.toString(), "[a, b, c]"); node = compiler.parse(JSSourceFile.fromCode("js5", js5)); deps = DepUtils.parseDependencies(node, new HashSet<String>()).getDefineDependencies(); assertNull(deps); node = compiler.parse(JSSourceFile.fromCode("js6", js6)); deps = DepUtils.parseDependencies(node, new HashSet<String>()).getDefineDependencies(); assertNull(deps); node = compiler.parse(JSSourceFile.fromCode("js7", js7)); Set<String> dependentFeatures = new HashSet<String>(); ParseResult parseResult = DepUtils.parseDependencies(node, dependentFeatures); assertEquals("[dep1, dojo/has!hasTest1?hasTest2?dep1:dep2]", parseResult.getDefineDependencies().toString()); assertEquals("[has!hasTest3?:dep3]", parseResult.getRequireDependencies().toString()); assertEquals(new HashSet<String>(Arrays.asList(new String[]{"hasTest1", "hasTest2", "hasTest3", "fooTest", "barTest"})), dependentFeatures); }