<commit_before>/*******************************************************************************
 * @license
 * Copyright (c) 2012 VMware, Inc. All Rights Reserved.
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Contributors:
 *     <NAME> - initial API and implementation
 ******************************************************************************/

/*global resolve require define esprima console module*/
if (typeof define !== 'function') {
    var define = require('amdefine')(module);
}
define(function(require, exports, module) {

////////////////////////////////////////
// amd-support
//
//   Support for resolving amd references.
/////////////////////////////////////////

function configure(conf) {

	var parser = require("./parser");
	var treeMatcher = require('./tree-matcher');
	//Note: 
	//   conf = the 'global' configuration for the api, provides file system type operations
	//   resolverConf = configuration information for the resolver, varies based on the context
	//                  of where a reference came from.

	var andPat = treeMatcher.andPat;
	var orPat = treeMatcher.orPat;
	
	var getContents = conf.getContents;
	var getDirectory = require('./utils').getDirectory;
	var orMap = require('./utils').orMap;
	var ork = require('./utils').ork;
	var deref = require('./utils').deref;
	var listFiles = conf.listFiles;
	var pathResolve = require('./utils').pathResolve;
	var getScriptTags = require('./script-tag-finder').getScriptTags;
	var getScriptCode = require('./script-tag-finder').getScriptCode;
	var htmlExtensions = ['.html', '.htm', '.HTML', '.HTM' ];
	var objectPat = treeMatcher.objectPat;
	var successPat = treeMatcher.successPat;
	var containsPat = treeMatcher.containsPat;
//	var successMatcher = treeMatcher.successMatcher;
	var variablePat = treeMatcher.variablePat;
	var arrayWithElementPat = treeMatcher.arrayWithElementPat;
	var mapPaths = require('./amd-path-mapper').mapPaths;
	
	function endsWith(str, suffix) {
		return str.indexOf(suffix, str.length - suffix.length) !== -1;
	}
	
	//isHtml :: String -> (String | false)
	// if given name is the name of a html file then returns the name.
	// otherwise returns false.
	function isHtml(name) {
		return orMap(htmlExtensions, function (ext) {
			return endsWith(name, ext) && name;
		});
	}
	
	function getBaseDirFromDataMain(scriptTag) {
		var datamain = scriptTag.attribs && scriptTag.attribs['data-main'];
		return datamain && getDirectory(datamain);
	}
	
	function objectWithProperty(propName) {
		return objectPat({
			"type": "ObjectExpression",
			"properties": arrayWithElementPat(objectPat({
				"type": "Property",
				"key": {
					"type": "Identifier",
					"name": propName
				},
				"value": successPat
			}))
		});
	}

	var configBlockPat = objectWithProperty(orPat(["baseUrl", "paths"]));
	
	function findCurlConfigBlock(tree) {
	
		//TODO: this pattern, where the configblock is assigned to
		// a variable and then the variable passed to curl call
		// should also be supported for requirejs, not just curl.
		
		var configIdNameVar = variablePat('string');
		
		var curlCallWithIdentifier = containsPat(objectPat({
			"type": "CallExpression",
			"callee": {
				"type": "Identifier",
				"name": "curl"
			},
			"arguments": [ {
                      "type": "Identifier",
                      "name": configIdNameVar
            }]
		}));
		var configBlock = null;
		curlCallWithIdentifier(tree)(
			//Success
			function () {
				var configIdName = configIdNameVar.value;
				var configDecl = objectPat({
                    "type": "VariableDeclarator",
                    "id": {
                      "type": "Identifier",
                      "name": configIdName
                    }
				});
				var pattern = containsPat(
					andPat([
							configDecl,
							containsPat(configBlockPat)
					])
				);
				pattern(tree)(
					//success
					function (found) {
						configBlock = found;
					},
					//fail
					function () {
					}
				);
			},
			//Fail
			function () {
			}
		);
		return configBlock;
	}
	
	function findRequireConfigBlock(tree) {
		//configBlockPat.debug = 'configBlockPat';
		var requireCall = objectPat({
			"type": "CallExpression",
			"callee": containsPat(objectPat({
				"type": "Identifier",
				"name": orPat(["require", "requirejs", "curl"])
			}))
		});
		var requireAssignment = objectPat({
			"type": "AssignmentExpression",
			"operator": "=",
			"left": {
				"type": "Identifier",
				"name": "require"
			}
		});
		var pattern = containsPat(
				andPat([
					orPat([requireCall, requireAssignment]),
					containsPat(configBlockPat)
				])
		);

		var configBlock = null;
		pattern(tree)(
			//success
			function (found) {
				configBlock = found;
			},
			//fail
			function () {
			}
		);
		return configBlock;
	}
	
	//tries to determine the value of an expression.
	//if the value can not be statically determined then 'undefined' is returned.
	//Note: this function is essentially a dispatcher that delegates to different
	//analyzer functions depending on the expression type.
	function analyzeExp(exp) {
		var type = exp && exp.type;
		if (type) {
			var analyzerForType = analyzeExp[type];
			if (typeof(analyzerForType)==='function') {
				return analyzerForType(exp);
			} else {
				console.log("No analyzer for exp type: " + type);
				console.log(JSON.stringify(exp, null, "  "));
			}
		}
	}

	var keyVar = variablePat("string");
	var valueVar = variablePat();
	var propPat = objectPat({<commit_msg>Remove dead code, disable some verbose logging.<commit_after>/*******************************************************************************
 * @license
 * Copyright (c) 2012 VMware, Inc. All Rights Reserved.
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Contributors:
 *     <NAME> - initial API and implementation
 ******************************************************************************/

/*global resolve require define esprima console module*/
if (typeof define !== 'function') {
    var define = require('amdefine')(module);
}
define(function(require, exports, module) {

////////////////////////////////////////
// amd-support
//
//   Support for resolving amd references.
/////////////////////////////////////////

function configure(conf) {

	var parser = require("./parser");
	var treeMatcher = require('./tree-matcher');
	//Note: 
	//   conf = the 'global' configuration for the api, provides file system type operations
	//   resolverConf = configuration information for the resolver, varies based on the context
	//                  of where a reference came from.

	var andPat = treeMatcher.andPat;
	var orPat = treeMatcher.orPat;
	
	var getContents = conf.getContents;
	var getDirectory = require('./utils').getDirectory;
	var orMap = require('./utils').orMap;
	var ork = require('./utils').ork;
	var deref = require('./utils').deref;
	var listFiles = conf.listFiles;
	var pathResolve = require('./utils').pathResolve;
	var getScriptTags = require('./script-tag-finder').getScriptTags;
	var getScriptCode = require('./script-tag-finder').getScriptCode;
	var htmlExtensions = ['.html', '.htm', '.HTML', '.HTM' ];
	var objectPat = treeMatcher.objectPat;
	var successPat = treeMatcher.successPat;
	var containsPat = treeMatcher.containsPat;
//	var successMatcher = treeMatcher.successMatcher;
	var variablePat = treeMatcher.variablePat;
	var arrayWithElementPat = treeMatcher.arrayWithElementPat;
	var mapPaths = require('./amd-path-mapper').mapPaths;
	
	function endsWith(str, suffix) {
		return str.indexOf(suffix, str.length - suffix.length) !== -1;
	}
	
	//isHtml :: String -> (String | false)
	// if given name is the name of a html file then returns the name.
	// otherwise returns false.
	function isHtml(name) {
		return orMap(htmlExtensions, function (ext) {
			return endsWith(name, ext) && name;
		});
	}
	
	function objectWithProperty(propName) {
		return objectPat({
			"type": "ObjectExpression",
			"properties": arrayWithElementPat(objectPat({
				"type": "Property",
				"key": {
					"type": "Identifier",
					"name": propName
				},
				"value": successPat
			}))
		});
	}

	var configBlockPat = objectWithProperty(orPat(["baseUrl", "paths"]));
	
	function findCurlConfigBlock(tree) {
	
		//TODO: this pattern, where the configblock is assigned to
		// a variable and then the variable passed to curl call
		// should also be supported for requirejs, not just curl.
		
		var configIdNameVar = variablePat('string');
		
		var curlCallWithIdentifier = containsPat(objectPat({
			"type": "CallExpression",
			"callee": {
				"type": "Identifier",
				"name": "curl"
			},
			"arguments": [ {
                      "type": "Identifier",
                      "name": configIdNameVar
            }]
		}));
		var configBlock = null;
		curlCallWithIdentifier(tree)(
			//Success
			function () {
				var configIdName = configIdNameVar.value;
				var configDecl = objectPat({
                    "type": "VariableDeclarator",
                    "id": {
                      "type": "Identifier",
                      "name": configIdName
                    }
				});
				var pattern = containsPat(
					andPat([
							configDecl,
							containsPat(configBlockPat)
					])
				);
				pattern(tree)(
					//success
					function (found) {
						configBlock = found;
					},
					//fail
					function () {
					}
				);
			},
			//Fail
			function () {
			}
		);
		return configBlock;
	}
	
	function findRequireConfigBlock(tree) {
		//configBlockPat.debug = 'configBlockPat';
		var requireCall = objectPat({
			"type": "CallExpression",
			"callee": containsPat(objectPat({
				"type": "Identifier",
				"name": orPat(["require", "requirejs", "curl"])
			}))
		});
		var requireAssignment = objectPat({
			"type": "AssignmentExpression",
			"operator": "=",
			"left": {
				"type": "Identifier",
				"name": "require"
			}
		});
		var pattern = containsPat(
				andPat([
					orPat([requireCall, requireAssignment]),
					containsPat(configBlockPat)
				])
		);

		var configBlock = null;
		pattern(tree)(
			//success
			function (found) {
				configBlock = found;
			},
			//fail
			function () {
			}
		);
		return configBlock;
	}
	
	//tries to determine the value of an expression.
	//if the value can not be statically determined then 'undefined' is returned.
	//Note: this function is essentially a dispatcher that delegates to different
	//analyzer functions depending on the expression type.
	function analyzeExp(exp) {
		var type = exp && exp.type;
		if (type) {
			var analyzerForType = analyzeExp[type];
			if (typeof(analyzerForType)==='function') {
				return analyzerForType(exp);
//			} else {
//				console.log("No analyzer for exp type: " + type);
//				console.log(JSON.stringify(exp, null, "  "));
			}
		}
	}

	var keyVar = variablePat("string");
	var valueVar = variablePat();
	var propPat = objectPat({
{'fraction_non_alphanumeric': 0.16827513598876995, 'fraction_numerical': 0.0012282856641516056, 'mean_word_length': 2.9870584120321793, 'pattern_counts': {'":': 54, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5877055', 'n_tokens_mistral': 3800, 'n_tokens_neox': 3227, 'n_words': 896}