<commit_before>var eselector = require('esprima-selector');

// decorates the given esprima node with tag-specific helpers.
// statements and the like get:
//   node.before(src) - inserts src before the node
//   node.after(src) - inserts src after the node
//   node.wrap(beforeSrc, afterSrc) - wraps the node
//
// expressions just get:
//   node.wrap(beforeSrc, afterSrc) - wraps the node (be sure to match parentheses)
//
// blocks get:
//   node.before(src) - inserts src before everything in the block
//   node.after(src, useFinally) - inserts src after everything in the block; if useFinally is true, the block is wrapped with try-block with src in the finally clause
module.exports = function (node, options) {
	options = (options || {});
	var primitives = options.falafelMap ? falafelMapPrimitives : falafelPrimitives;

	var w = eselector.nodeTag(node);
	if (w) {
		node.update = newUpdate(node);
		if (['statement', 'declaration', 'program', 'block'].indexOf(w.name) !== -1) {
			node.before = before;
			node.after = after;
			node.wrap = wrap;
		} else if (w.name === "expression") {
			node.wrap = parensWrap;
		} else if (['switch-case'].indexOf(w.name) !== -1) {
			// TODO
		} else if (['declarator', 'property', 'clause'].indexOf(w.name) !== -1) {
			// skip
		} else {
			throw new Error('unrecognized node ' + w.name + ' (' + node.type + ')');
		}
	}

	return node;

	function before(src) {
		rawWrap.call(this, primitives.sequence('{', src), '}');
	}
	function after(src, catchSrc) {
		if (catchSrc === true) {
			rawWrap.call(this, '{ try {', primitives.sequence('} finally {', src, '} }'));
		} else if (catchSrc) {
			rawWrap.call(this, '{ try {', primitives.sequence('} catch (__e) {', catchSrc, '} finally {', src, '} }'));
		} else {
			rawWrap.call(this, '{', primitives.sequence(src, '}'));
		}
	}
	function wrap(beforeSrc, afterSrc, useFinally) {
		this.before(beforeSrc);
		this.after(afterSrc, useFinally);
	}
	function parensWrap(beforeSrc, afterSrc) {
		this.update('(', beforeSrc, primitives.source(this), afterSrc, ')');
	}
	function rawWrap(beforeSrc, afterSrc) {
		this.update(beforeSrc, primitives.source(this), afterSrc);
	}
	function newUpdate(node) {
		var oldUpdate = node.update;
		return function () {
			var seq = primitives.sequence.apply(this, arguments);
			return oldUpdate.call(this, seq);
		};
	}
}

// returns a version of f where the node argument has been wrapped with the function above
module.exports.wrap = function (f, options) {<commit_msg>better name for one of the wrap() functions<commit_after>var eselector = require('esprima-selector');

// decorates the given esprima node with tag-specific helpers.
// statements and the like get:
//   node.before(src) - inserts src before the node
//   node.after(src) - inserts src after the node
//   node.wrap(beforeSrc, afterSrc) - wraps the node
//
// expressions just get:
//   node.wrap(beforeSrc, afterSrc) - wraps the node (be sure to match parentheses)
//
// blocks get:
//   node.before(src) - inserts src before everything in the block
//   node.after(src, useFinally) - inserts src after everything in the block; if useFinally is true, the block is wrapped with try-block with src in the finally clause
module.exports = function (node, options) {
	options = (options || {});
	var primitives = options.falafelMap ? falafelMapPrimitives : falafelPrimitives;

	var w = eselector.nodeTag(node);
	if (w) {
		node.update = newUpdate(node);
		if (['statement', 'declaration', 'program', 'block'].indexOf(w.name) !== -1) {
			node.before = before;
			node.after = after;
			node.wrap = beforeAfterWrap;
		} else if (w.name === "expression") {
			node.wrap = parensWrap;
		} else if (['switch-case'].indexOf(w.name) !== -1) {
			// TODO
		} else if (['declarator', 'property', 'clause'].indexOf(w.name) !== -1) {
			// skip
		} else {
			throw new Error('unrecognized node ' + w.name + ' (' + node.type + ')');
		}
	}

	return node;

	function before(src) {
		rawWrap.call(this, primitives.sequence('{', src), '}');
	}
	function after(src, catchSrc) {
		if (catchSrc === true) {
			rawWrap.call(this, '{ try {', primitives.sequence('} finally {', src, '} }'));
		} else if (catchSrc) {
			rawWrap.call(this, '{ try {', primitives.sequence('} catch (__e) {', catchSrc, '} finally {', src, '} }'));
		} else {
			rawWrap.call(this, '{', primitives.sequence(src, '}'));
		}
	}
	function beforeAfterWrap(beforeSrc, afterSrc, useFinally) {
		this.before(beforeSrc);
		this.after(afterSrc, useFinally);
	}
	function parensWrap(beforeSrc, afterSrc) {
		this.update('(', beforeSrc, primitives.source(this), afterSrc, ')');
	}
	function rawWrap(beforeSrc, afterSrc) {
		this.update(beforeSrc, primitives.source(this), afterSrc);
	}
	function newUpdate(node) {
		var oldUpdate = node.update;
		return function () {
			var seq = primitives.sequence.apply(this, arguments);
			return oldUpdate.call(this, seq);
		};
	}
}

// returns a version of f where the node argument has been wrapped with the function above
module.exports.wrap = function (f, options) {
{'fraction_non_alphanumeric': 0.16152019002375298, 'fraction_numerical': 0.0011876484560570072, 'mean_word_length': 3.821564885496183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '543680', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1642, 'n_words': 451}