<commit_before>var _ = require('lodash');
import * as doc from '../doc';

import eclint = require('../eclint');
import EditorConfigError =  require('../editor-config-error');

var newlines = {
	lf: '\n',
	crlf: '\r\n',
	cr: '\r'
};

function resolve(settings: eclint.Settings) {
	if (_.isBoolean(settings.insert_final_newline)) {
		return settings.insert_final_newline;
	}
	return void(0);
}

function check(settings: eclint.Settings, document: doc.Document) {
	var configSetting = resolve(settings);
	var inferredSetting = infer(document);
	if (configSetting == null || inferredSetting === configSetting) {
		return [];
	}
	var message: string;
	if (configSetting) {
		message = 'expected final newline';
	} else {
		message = 'unexpected final newline';
	}

	var error = new EditorConfigError([message]);
	error.lineNumber = document.lines.length;
	var lastLine: doc.Line = document.lines[document.lines.length - 1];
	error.columnNumber = lastLine.text.length + lastLine.ending.length;
	error.rule = 'insert_final_newline';
	error.source = lastLine.text + lastLine.ending;
	return [error];
}

function fix(settings: eclint.Settings, document: doc.Document) {
	var lastLine: doc.Line;
	var configSetting = resolve(settings);
	if (configSetting && !infer(document)) {
		lastLine = document.lines[document.lines.length - 1];
		var endOfLineSetting = settings.end_of_line || 'lf';
		if (lastLine) {
			lastLine.ending = newlines[endOfLineSetting];
		} else {
			document.lines.push(new doc.Line({
				number: 1,
				text: '',
				ending: newlines[endOfLineSetting],
				offset: 0
			}));
		}
		return document;
	}
	if (!configSetting) {
		while (infer(document)) {
			lastLine = document.lines[document.lines.length - 1];
			if (lastLine.text) {
				lastLine.ending = '';
				break;
			}
			document.lines.pop();
		}
		return document;
	}
	return document;
}

function infer(document: doc.Document) {
	var lastLine = document.lines[document.lines.length - 1];
	if (lastLine && lastLine.ending) {
		return true;
	}
	return false;
}

var InsertFinalNewlineRule: eclint.DocumentRule = {
	type: 'DocumentRule',
	resolve: resolve,
	check: check,
	fix: fix,
	infer: infer<commit_msg>Removes empty final new lines where there is more than one event if insert_final_newline=true<commit_after>var _ = require('lodash');
import * as doc from '../doc';

import eclint = require('../eclint');
import EditorConfigError =  require('../editor-config-error');

var newlines = {
	lf: '\n',
	crlf: '\r\n',
	cr: '\r'
};

function resolve(settings: eclint.Settings) {
	if (_.isBoolean(settings.insert_final_newline)) {
		return settings.insert_final_newline;
	}
	return void(0);
}

function check(settings: eclint.Settings, document: doc.Document) {
	var configSetting = resolve(settings);
	var inferredSetting = infer(document);
	if (configSetting == null || inferredSetting === configSetting) {
		return [];
	}
	var message: string;
	if (configSetting) {
		message = 'expected final newline';
	} else {
		message = 'unexpected final newline';
	}

	var error = new EditorConfigError([message]);
	error.lineNumber = document.lines.length;
	var lastLine: doc.Line = document.lines[document.lines.length - 1];
	error.columnNumber = lastLine.text.length + lastLine.ending.length;
	error.rule = 'insert_final_newline';
	error.source = lastLine.text + lastLine.ending;
	return [error];
}

function fix(settings: eclint.Settings, document: doc.Document) {
	var lastLine: doc.Line;
	var configSetting = resolve(settings);
	while (infer(document)) {
		lastLine = document.lines[document.lines.length - 1];
		if (lastLine.text) {
			lastLine.ending = '';
			break;
		}
		document.lines.pop();
	}
	if (configSetting && !infer(document)) {
		lastLine = document.lines[document.lines.length - 1];
		var endOfLineSetting = settings.end_of_line || 'lf';
		if (lastLine) {
			lastLine.ending = newlines[endOfLineSetting];
		} else {
			document.lines.push(new doc.Line({
				number: 1,
				text: '',
				ending: newlines[endOfLineSetting],
				offset: 0
			}));
		}
		return document;
	}

	return document;
}

function infer(document: doc.Document) {
	var lastLine = document.lines[document.lines.length - 1];
	if (lastLine && lastLine.ending) {
		return true;
	}
	return false;
}

var InsertFinalNewlineRule: eclint.DocumentRule = {
	type: 'DocumentRule',
	resolve: resolve,
	check: check,
	fix: fix,
	infer: infer
{'fraction_non_alphanumeric': 0.14138876199177708, 'fraction_numerical': 0.0031978072179077205, 'mean_word_length': 4.256902761104442, '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': '3725467', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1435, 'n_words': 332}