<commit_before>/**
 * @module compiler
 * @author <EMAIL>
 * @date 2014-07-13
 */

'use strict';

var uglify = require('uglify-js');

module.exports = {
	/**
	 * process compile the codes
	 * @param code
	 * @param opts
	 * @returns {*}
	 */
	process: function (code, options) {
		//get Variables
		var Variables = [
			// Global object properties
			// (http://www.ecma-international.org/publications/standards/Ecma-262.htm) 15.1
			'NaN', 'Infinity', 'undefined',
			'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI',
			'decodeURIComponent', 'encodeURI', 'encodeURIComponent',
			'Object', 'Function', 'Array', 'String', 'Boolean', 'Number',
			'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
			'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
			'Math', 'JSON',
			//UserAgent global properties
			'Events', 'Navigator', 'Screen', 'History', 'Location', 'window', 'arguments', 'console',
			//common module argument
			'require', 'modue', 'exports',
			//try catch
			'e'
		];

		// Figure out if we're getting a template, or if we need to
		// load the template - and be sure to cache the result.
		// Generate a reusable function that will serve as a template
		// generator (and which will be cached).
		var tmp = "function anonymous(data){var p = ''" + (options.lineNumber && ", line = 0, filepath = '" + options.filepath + "'" || "") + ";" +
			// Convert the template into pure JavaScript
			this.parse(code, options)
			+ "return p;}";
		var ast = uglify.parse(tmp);

		var walker = new uglify.TreeWalker(function (node) {
			//Variables
			if (node instanceof uglify.AST_Var) {
				// string_template is a cute little function that UglifyJS uses for warnings
				for (var i = 0, item; item = node.definitions[i]; i++) {
					Variables.push(item.name.name);
				}
			}

			//function name
			if (node instanceof uglify.AST_Defun) {
				Variables.push(node.name.name);
			}

			//function arguments
			if (node instanceof uglify.AST_SymbolFunarg) {
				Variables.push(node.name);
			}
		});

		ast.walk(walker);

		var symbolMap = {};

		for (var i = 0, symbol; symbol = Variables[i]; i++) {
			symbolMap[symbol] = true;
		}

		// transform and print
		var withExpression = 'data';
		var transformer = new uglify.TreeTransformer(null, function (node) {
			//clear function name
			if (node instanceof uglify.AST_Defun) {
				if (node.name.name == 'anonymous') {
					node.name.name = '';
				}
			}

			//add data scope
			if (node instanceof uglify.AST_Symbol) {
				if (!symbolMap[node.name]) {
					node.name = withExpression + '.' + node.name
				}
			}
		});

		var transformedAst = ast.transform(transformer);

		//clear with
		// var withBody = ast2.body[0].body[1].body.body;
		// [].splice.apply(ast2.body[0].body, [1, 1].concat(withBody));

		return transformedAst.print_to_string({
			beautify: true
		}) || '';
	},

	/**
	 * parse tpl to pure js
	 * @param code
	 * @param options
	 * @returns {string}
	 */
	parse: function (code, options) {
		var self = this,
			parsedCode = '',
			openTagCount = 0,
			closeTagCount = 0;<commit_msg>del no used comments<commit_after>/**
 * @module compiler
 * @author <EMAIL>
 * @date 2014-07-13
 */

'use strict';

var uglify = require('uglify-js');

module.exports = {
	/**
	 * process compile the codes
	 * @param code
	 * @param opts
	 * @returns {*}
	 */
	process: function (code, options) {
		//get Variables
		var Variables = [
			// Global object properties
			// (http://www.ecma-international.org/publications/standards/Ecma-262.htm) 15.1
			'NaN', 'Infinity', 'undefined',
			'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI',
			'decodeURIComponent', 'encodeURI', 'encodeURIComponent',
			'Object', 'Function', 'Array', 'String', 'Boolean', 'Number',
			'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
			'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
			'Math', 'JSON',
			//UserAgent global properties
			'Events', 'Navigator', 'Screen', 'History', 'Location', 'window', 'arguments', 'console',
			//common module argument
			'require', 'modue', 'exports',
			//try catch
			'e'
		];

		// Figure out if we're getting a template, or if we need to
		// load the template - and be sure to cache the result.
		// Generate a reusable function that will serve as a template
		// generator (and which will be cached).
		var tmp = "function anonymous(data){var p = ''" + (options.lineNumber && ", line = 0, filepath = '" + options.filepath + "'" || "") + ";" +
			// Convert the template into pure JavaScript
			this.parse(code, options)
			+ "return p;}";
		var ast = uglify.parse(tmp);

		var walker = new uglify.TreeWalker(function (node) {
			//Variables
			if (node instanceof uglify.AST_Var) {
				// string_template is a cute little function that UglifyJS uses for warnings
				for (var i = 0, item; item = node.definitions[i]; i++) {
					Variables.push(item.name.name);
				}
			}

			//function name
			if (node instanceof uglify.AST_Defun) {
				Variables.push(node.name.name);
			}

			//function arguments
			if (node instanceof uglify.AST_SymbolFunarg) {
				Variables.push(node.name);
			}
		});

		ast.walk(walker);

		var symbolMap = {};

		for (var i = 0, symbol; symbol = Variables[i]; i++) {
			symbolMap[symbol] = true;
		}

		// transform and print
		var withExpression = 'data';
		var transformer = new uglify.TreeTransformer(null, function (node) {
			//clear function name
			if (node instanceof uglify.AST_Defun) {
				if (node.name.name == 'anonymous') {
					node.name.name = '';
				}
			}

			//add data scope
			if (node instanceof uglify.AST_Symbol) {
				if (!symbolMap[node.name]) {
					node.name = withExpression + '.' + node.name
				}
			}
		});

		var transformedAst = ast.transform(transformer);

		return transformedAst.print_to_string({
			beautify: true
		}) || '';
	},

	/**
	 * parse tpl to pure js
	 * @param code
	 * @param options
	 * @returns {string}
	 */
	parse: function (code, options) {
		var self = this,
			parsedCode = '',
			openTagCount = 0,
			closeTagCount = 0;
{'fraction_non_alphanumeric': 0.16052848885218826, 'fraction_numerical': 0.007431874483897605, 'mean_word_length': 3.1281526925698704, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6162503', 'n_tokens_mistral': 2339, 'n_tokens_neox': 1970, 'n_words': 544}