<commit_before>/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author <NAME> @sokra
*/
var ConstDependency = require("./dependencies/ConstDependency");
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");

var NullFactory = require("./NullFactory");

function DefinePlugin(definitions) {
	this.definitions = definitions;
}
module.exports = DefinePlugin;
DefinePlugin.prototype.apply = function(compiler) {
	var definitions = this.definitions;
	compiler.plugin("compilation", function(compilation, params) {
		compilation.dependencyFactories.set(ConstDependency, new NullFactory());
		compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());

		params.normalModuleFactory.plugin("parser", function(parser) {
			(function walkDefinitions(definitions, prefix) {
				Object.keys(definitions).forEach(function(key) {
					var code = definitions[key];
					if(code && typeof code === "object" && !(code instanceof RegExp)) {
						walkDefinitions(code, prefix + key + ".");
						applyObjectDefine(prefix + key, code);
						return;
					}
					applyDefineKey(prefix, key);
					applyDefine(prefix + key, code);
				});
			}(definitions, ""));

			function stringifyObj(obj) {
				return "{" + Object.keys(obj).map(function(key) {
					var code = obj[key];
					return JSON.stringify(key) + ":" + toCode(code);
				}).join(",") + "}";
			}

			function toCode(code) {
				if(code === null) return "null";
				else if(code === undefined) return "undefined";
				else if(code instanceof RegExp && code.toString) return code.toString();
				else if(typeof code === "function" && code.toString) return "(" + code.toString() + ")";
				else if(typeof code === "object") return stringifyObj(code);
				else return code + "";
			}

			function applyDefineKey(prefix, key) {
				var splittedKey = key.split(".");
				splittedKey.slice(1).forEach(function(_, i) {
					var fullKey = prefix + splittedKey.slice(0, i + 1).join(".");
					parser.plugin("can-rename " + fullKey, function() {
						return true;
					});
				});
			}

			function applyDefine(key, code) {
				var isTypeof = /^typeof\s+/.test(key);
				if(isTypeof) key = key.replace(/^typeof\s+/, "");
				var recurse = false;
				var recurseTypeof = false;
				code = toCode(code);
				if(!isTypeof) {
					parser.plugin("can-rename " + key, function() {
						return true;
					});
					parser.plugin("evaluate Identifier " + key, function(expr) {
						if(recurse) return;
						recurse = true;
						var res = this.evaluate(code);
						recurse = false;
						res.setRange(expr.range);
						return res;
					});
					parser.plugin("expression " + key, function(expr) {
						var dep = new ConstDependency(code, expr.range);
						dep.loc = expr.loc;
						this.state.current.addDependency(dep);
						return true;
					});
				}
				var typeofCode = isTypeof ? code : "typeof (" + code + ")";
				parser.plugin("evaluate typeof " + key, function(expr) {
					if(recurseTypeof) return;
					recurseTypeof = true;
					var res = this.evaluate(typeofCode);
					recurseTypeof = false;
					res.setRange(expr.range);
					return res;
				});
				parser.plugin("typeof " + key, function(expr) {
					var res = this.evaluate(typeofCode);
					if(!res.isString()) return;
					var dep = new ConstDependency(JSON.stringify(res.string), expr.range);
					dep.loc = expr.loc;
					this.state.current.addDependency(dep);
					return true;
				});
			}

			function applyObjectDefine(key, obj) {
				var code = stringifyObj(obj);
				parser.plugin("can-rename " + key, function() {
					return true;
				});
				parser.plugin("evaluate Identifier " + key, function(expr) {
					return new BasicEvaluatedExpression().setRange(expr.range);
				});
				parser.plugin("evaluate typeof " + key, function(expr) {
					return new BasicEvaluatedExpression().setString("object").setRange(expr.range);
				});
				parser.plugin("expression " + key, function(expr) {
					var dep = new ConstDependency(code, expr.range);
					dep.loc = expr.loc;
					this.state.current.addDependency(dep);
					return true;
				});
				parser.plugin("typeof " + key, function(expr) {
					var dep = new ConstDependency("\"object\"", expr.range);
					dep.loc = expr.loc;
					this.state.current.addDependency(dep);
					return true;
				});
			}
		});
	});
};<commit_msg>refactor(es6): Update DefinePlugin to es2015 (#3632)<commit_after>/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author <NAME> @sokra
*/
"use strict";

const ConstDependency = require("./dependencies/ConstDependency");
const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
const NullFactory = require("./NullFactory");

class DefinePlugin {
	constructor(definitions) {
		this.definitions = definitions;
	}

	apply(compiler) {
		let definitions = this.definitions;
		compiler.plugin("compilation", (compilation, params) => {
			compilation.dependencyFactories.set(ConstDependency, new NullFactory());
			compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());

			params.normalModuleFactory.plugin("parser", (parser) => {
				(function walkDefinitions(definitions, prefix) {
					Object.keys(definitions).forEach((key) => {
						let code = definitions[key];
						if(code && typeof code === "object" && !(code instanceof RegExp)) {
							walkDefinitions(code, prefix + key + ".");
							applyObjectDefine(prefix + key, code);
							return;
						}
						applyDefineKey(prefix, key);
						applyDefine(prefix + key, code);
					});
				}(definitions, ""));

				function stringifyObj(obj) {
					return "{" + Object.keys(obj).map((key) => {
						let code = obj[key];
						return JSON.stringify(key) + ":" + toCode(code);
					}).join(",") + "}";
				}

				function toCode(code) {
					if(code === null) return "null";
					else if(code === undefined) return "undefined";
					else if(code instanceof RegExp && code.toString) return code.toString();
					else if(typeof code === "function" && code.toString) return "(" + code.toString() + ")";
					else if(typeof code === "object") return stringifyObj(code);
					else return code + "";
				}

				function applyDefineKey(prefix, key) {
					const splittedKey = key.split(".");
					splittedKey.slice(1).forEach((_, i) => {
						const fullKey = prefix + splittedKey.slice(0, i + 1).join(".");
						parser.plugin("can-rename " + fullKey, () => true);
					});
				}

				function applyDefine(key, code) {
					let isTypeof = /^typeof\s+/.test(key);
					if(isTypeof) key = key.replace(/^typeof\s+/, "");
					let recurse = false;
					let recurseTypeof = false;
					code = toCode(code);
					if(!isTypeof) {
						parser.plugin("can-rename " + key, () => true);
						parser.plugin("evaluate Identifier " + key, (expr) => {
							if(recurse) return;
							let res = parser.evaluate(code);
							recurse = false;
							res.setRange(expr.range);
							return res;
						});
						parser.plugin("expression " + key, (expr) => {
							let dep = new ConstDependency(code, expr.range);
							dep.loc = expr.loc;
							parser.state.current.addDependency(dep);
							return true;
						});
					}
					let typeofCode = isTypeof ? code : "typeof (" + code + ")";
					parser.plugin("evaluate typeof " + key, (expr) => {
						if(recurseTypeof) return;
						let res = parser.evaluate(typeofCode);
						recurseTypeof = false;
						res.setRange(expr.range);
						return res;
					});
					parser.plugin("typeof " + key, (expr) => {
						let res = parser.evaluate(typeofCode);
						if(!res.isString()) return;
						let dep = new ConstDependency(JSON.stringify(res.string), expr.range);
						dep.loc = expr.loc;
						parser.state.current.addDependency(dep);
						return true;
					});
				}

				function applyObjectDefine(key, obj) {
					let code = stringifyObj(obj);
					parser.plugin("can-rename " + key, () => true);
					parser.plugin("evaluate Identifier " + key, (expr) => new BasicEvaluatedExpression().setRange(expr.range));
					parser.plugin("evaluate typeof " + key, expr => new BasicEvaluatedExpression().setString("object").setRange(expr.range));
					parser.plugin("expression " + key, (expr) => {
						let dep = new ConstDependency(code, expr.range);
						dep.loc = expr.loc;
						parser.state.current.addDependency(dep);
						return true;
					});
					parser.plugin("typeof " + key, (expr) => {
						let dep = new ConstDependency("\"object\"", expr.range);
						dep.loc = expr.loc;
						parser.state.current.addDependency(dep);
						return true;
					});
				}
			});
		});
	}
}
module.exports = DefinePlugin;
{'fraction_non_alphanumeric': 0.1573450916686006, 'fraction_numerical': 0.0017405430494314227, 'mean_word_length': 3.165780570323828, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 21, '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': '7418591', 'n_tokens_mistral': 3417, 'n_tokens_neox': 2585, 'n_words': 539}