<commit_before>/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author <NAME> @sokra
*/
var fs = require("fs");
var parse = require("./parse");
var execLoaders = require("enhanced-require/lib/execLoaders");

function buildModule(context, filenameWithLoaders,
					preLoaders, loaders, postLoaders,
					filename,
					options, callback) {

	if(options.profile) var profile = {
		start: new Date().getTime()
	}

	var dependencyInfo = {
		cacheable: true,
		files: [filename]
	};

	var extraResults = {
		dependencyInfo: dependencyInfo,
		profile: profile,
		warnings: [],
		errors: []
	}

	fs.readFile(filename, function(err, content) {
		if(err) return callback(err, extraResults);

		profile && (profile.readEnd = new Date().getTime());

		var loaderContext = {
			loaders: loaders,
			preLoaders: preLoaders,
			postLoaders: postLoaders,
			loaderType: null,
			emitWarning: function(warning) {
				extraResults.warnings.push(warning);
			},
			emitError: function(error) {
				extraResults.errors.push(error);
			}
		};

		loaderContext.loaderType = "preLoader";
		execLoaders(context, filenameWithLoaders, preLoaders, [filename], [content], loaderContext, dependencyInfo, options,
		  function(err, result) {
			if(err) return callback(err, extraResults);
			profile && (profile.preLoadersEnd = new Date().getTime());
			loaderContext.loaderType = "loader";
			execLoaders(context, filenameWithLoaders, loaders, [filename], result, loaderContext, dependencyInfo, options,
			  function(err, result) {
				if(err) return callback(err, extraResults);
				profile && (profile.loadersEnd = new Date().getTime());
				loaderContext.loaderType = "postLoader";
				execLoaders(context, filenameWithLoaders, postLoaders, [filename], result, loaderContext, dependencyInfo, options,
				  function(err, result) {
					if(err) return callback(err, extraResults);
					profile && (profile.postLoadersEnd = new Date().getTime());
					return processJs(result)
				});
			});
		});
	});

	// process the result delivered from loaders or direct from file
	// for inclusion into the result
	// (static code analysis for requires and other stuff)
	// [this step is cached]
	function processJs(resultBuffers) {
		var source = resultBuffers[0].toString("utf-8");
		var deps;
		try {
			deps = parse(source, options.parse);
		} catch(e) {
			callback(new Error("File \"" + filenameWithLoaders + "\" parsing failed: " + e), extraResults);
			return;
		}
		profile && (profile.end = new Date().getTime());
		return callback(null, extraResults, source, deps);
	}
}
module.exports = buildModule;<commit_msg>better error reporting of parse errors<commit_after>/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author <NAME> @sokra
*/
var fs = require("fs");
var parse = require("./parse");
var execLoaders = require("enhanced-require/lib/execLoaders");

function buildModule(context, filenameWithLoaders,
					preLoaders, loaders, postLoaders,
					filename,
					options, callback) {

	if(options.profile) var profile = {
		start: new Date().getTime()
	}

	var dependencyInfo = {
		cacheable: true,
		files: [filename]
	};

	var extraResults = {
		dependencyInfo: dependencyInfo,
		profile: profile,
		warnings: [],
		errors: []
	}

	fs.readFile(filename, function(err, content) {
		if(err) return callback(err, extraResults);

		profile && (profile.readEnd = new Date().getTime());

		var loaderContext = {
			loaders: loaders,
			preLoaders: preLoaders,
			postLoaders: postLoaders,
			loaderType: null,
			emitWarning: function(warning) {
				extraResults.warnings.push(warning);
			},
			emitError: function(error) {
				extraResults.errors.push(error);
			}
		};

		loaderContext.loaderType = "preLoader";
		execLoaders(context, filenameWithLoaders, preLoaders, [filename], [content], loaderContext, dependencyInfo, options,
		  function(err, result) {
			if(err) return callback(err, extraResults);
			profile && (profile.preLoadersEnd = new Date().getTime());
			loaderContext.loaderType = "loader";
			execLoaders(context, filenameWithLoaders, loaders, [filename], result, loaderContext, dependencyInfo, options,
			  function(err, result) {
				if(err) return callback(err, extraResults);
				profile && (profile.loadersEnd = new Date().getTime());
				loaderContext.loaderType = "postLoader";
				execLoaders(context, filenameWithLoaders, postLoaders, [filename], result, loaderContext, dependencyInfo, options,
				  function(err, result) {
					if(err) return callback(err, extraResults);
					profile && (profile.postLoadersEnd = new Date().getTime());
					return processJs(result)
				});
			});
		});
	});

	// process the result delivered from loaders or direct from file
	// for inclusion into the result
	// (static code analysis for requires and other stuff)
	// [this step is cached]
	function processJs(resultBuffers) {
		var source = resultBuffers[0].toString("utf-8");
		var deps;
		try {
			deps = parse(source, options.parse);
		} catch(e) {
			callback(new Error("File \"" + filenameWithLoaders + "\" parsing failed: " + e.stack), extraResults);
			return;
		}
		profile && (profile.end = new Date().getTime());
		return callback(null, extraResults, source, deps);
	}
}
module.exports = buildModule;
{'fraction_non_alphanumeric': 0.13560938394049207, 'fraction_numerical': 0.0007629219912263971, 'mean_word_length': 4.291624621594349, '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': '7513479', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1576, 'n_words': 388}