<commit_before>'use strict';

const
	Sugar = require('sugar'),
	escaper = require('escaper');

const
	paramsStartReg = /^\s*\$p\s*=\s*(?=\{)/m,
	defineReg = /^([bigp]-[a-z][a-z0-9-]*)(?:\s+extends\s+([bigp]-[a-z][a-z0-9-]*))?$/m;

function collectBlock(str) {
	str = escaper.replace(str);

	let res = '{';

	let balance = 1;

	for (let i = 1; (i < str.length) && balance; ++i) {
		const char = str[i];

		if (char === '{') {
			++balance;

		} else if (char === '}') {
			--balance;
		}

		res += char;
	}

	if (balance) {
		throw new Error(`Can't extract params`);
	}

	return escaper.paste(res);
}

/**
 *
 * @param {string} source
 * @return {string}
 */
module.exports = function (source) {
	const
		define = defineReg.exec(source);

	if (!define) {
		return source;
	}

	const [defineString, block, parent] = define;

	let paramsBlock;

	const paramsStartSearch = paramsStartReg.exec(source);

	if (paramsStartSearch) {
		const [start] = paramsStartSearch;

		paramsBlock = collectBlock(source.substr(paramsStartSearch.index + start.length));
		source = source.replace(start + paramsBlock, '');

	} else {
		paramsBlock = '{}';
	}

	let fullDefineString;

	const
		camelBlock = Sugar.String.camelize(block, false),
		camelParent = Sugar.String.camelize(parent || '', false);

	fullDefineString = `
$${camelBlock} = ()
declare($${camelBlock}, ${block})

$${camelBlock}Params = ${
	parent ? 
		`fork($${camelParent}Params, ${paramsBlock})` :
		paramsBlock
}
if file-exists("${block}_*.styl")
	@import "${block}_*.styl"

${block}($p)
	$p = fork($${camelBlock}Params, $p)

	${ parent ? `extends($${camelParent}, $p)` : ''}
`;

	fullDefineString = fullDefineString
		.split('\n')<commit_msg>:art:<commit_after>'use strict';

const
	Sugar = require('sugar'),
	escaper = require('escaper');

const
	paramsStartReg = /^\s*\$p\s*=\s*(?=\{)/m,
	defineReg = /^([bigp]-[a-z][a-z0-9-]*)(?:\s+extends\s+([bigp]-[a-z][a-z0-9-]*))?$/m;

function collectBlock(str) {
	str = escaper.replace(str);

	let res = '{';

	let balance = 1;

	for (let i = 1; (i < str.length) && balance; ++i) {
		const char = str[i];

		if (char === '{') {
			++balance;

		} else if (char === '}') {
			--balance;
		}

		res += char;
	}

	if (balance) {
		throw new Error(`Can't extract params`);
	}

	return escaper.paste(res);
}

/**
 *
 * @param {string} source
 * @return {string}
 */
module.exports = function (source) {
	const
		define = defineReg.exec(source);

	if (!define) {
		return source;
	}

	const [defineString, block, parent] = define;

	let paramsBlock;

	const paramsStartSearch = paramsStartReg.exec(source);

	if (paramsStartSearch) {
		const [start] = paramsStartSearch;

		paramsBlock = collectBlock(source.substr(paramsStartSearch.index + start.length));
		source = source.replace(start + paramsBlock, '');

	} else {
		paramsBlock = '{}';
	}

	let fullDefineString;

	const
		camelBlock = Sugar.String.camelize(block, false),
		camelParent = Sugar.String.camelize(parent || '', false);

	fullDefineString = `
$${camelBlock} = ()
declare($${camelBlock}, ${block})

$${camelBlock}Params = ${
	parent ? `fork($${camelParent}Params, ${paramsBlock})` : paramsBlock
}
if file-exists("${block}_*.styl")
	@import "${block}_*.styl"

${block}($p)
	$p = fork($${camelBlock}Params, $p)

	${ parent ? `extends($${camelParent}, $p)` : ''}
`;

	fullDefineString = fullDefineString
		.split('\n')
{'fraction_non_alphanumeric': 0.21193232413178986, 'fraction_numerical': 0.003561887800534283, 'mean_word_length': 4.075301204819277, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6448013', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1269, 'n_words': 239}