/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var startsWith = require( '@stdlib/string/starts-with' );
var endsWith = require( '@stdlib/string/ends-with' );


// VARIABLES //

var NUMBER_LITERALS = [
	'zero',
	'one',
	'two'
];
var REGEXP_HEADER = /^( )*[A-Z][A-Z0-9 ]+( )*(\/\/)?$/;
var rule;


// FUNCTIONS //

/**
* Checks whether a comment is a section header.
*
* @private
* @param {Object} comment - comment object
* @returns {boolean} boolean indicating if a line comment denotes a section header
*/
function isSectionHeader( comment ) {
	var val;
	if (
		comment.type !== 'Line' ||
		comment.loc.start.column !== 0
	) {
		return false;
	}
	val = comment.value;
	return REGEXP_HEADER.test( val ) || endsWith( val, '//' );
}

/**
* Rule for validating that section header comments are padded by empty lines.
*
* @param {Object} context - ESLint context
* @returns {Object} validators
*/
function main( context ) {
	var source = context.getSourceCode();

	/**
	* Reports the error message.
	*
	* @private
	* @param {string} msg - message to report
	* @param {Object} loc - lines of code (object with `start` and `end` properties)
	*/
	function report( msg, loc ) {
		context.report({
			'node': null,
			'message': msg,
			'loc': loc
		});
	}

	/**
	* Checks section header comments.
	*
	* @private
	*/
	function validate() {
		var comments;
		var current;
		var i;

		comments = source.getAllComments();
		for ( i = 0; i < comments.length; i++ ) {
			current = comments[ i ];
			if ( isSectionHeader( current ) ) {
				validateHeader( current );
			}
		}
	}

	/**
	* Checks whether a section header comment is padded by empty lines.
	*
	* @private
	* @param {string} comment - literal value of comment
	* @returns {void}
	*/
	function validateHeader( comment ) {
		var missing;
		var prev;
		var next;
		var line;
		var msg;

		line = comment.loc.start.line;
		missing = 0;

		// Always require preceding empty line except at beginning of file:
		prev = source.lines[ line-2 ];
		if ( prev && prev !== '' ) {
			missing += 1;
		}
		prev = source.lines[ line-3 ];
		if (
			// Do not require empty lines at the beginning of files:
			prev &&
			prev !== '' &&

			// Allow one empty line after strict directive:
			prev !== '\'use strict\';' &&

			// Allow one empty line after multi-line comment
			!endsWith( prev, '*/' ) &&

			// Allow one empty line after single-line comment
			!startsWith( prev, '//' )
		) {
			missing += 1;
		}
		if ( !missing ) {
			// Ensure that there are not too many empty lines:
			prev = source.lines[ line-4 ];
			if (
				prev &&
				( prev === '\'use strict\';' ||
				endsWith( prev, '*/' ) ||
				startsWith( prev, '//' ) )
			) {
				msg = 'Header comments  must be preceded by only one empty line if they follow comments or the `use strict` directive';
				return report( msg, comment.loc );
			}
		}

		if ( missing ) {
			msg = 'Header comment must be preceded by '+NUMBER_LITERALS[ missing ]+' additional empty line';
			if ( missing === 2 ) {
				msg += 's';
			}
			return report( msg, comment.loc );
		}
		next = source.lines[ line ];
		if ( next !== '' ) {
			return report( 'Header comment must be followed by an empty line', comment.loc );
		}
	}

	return {
		'Program': validate
	};
}


// MAIN //

rule = {
	'meta': {
		'docs': {
			'description': 'enforce that section header comments are padded by empty lines'
		},
		'schema': []
	},
	'create': main
};


// EXPORTS //

module.exports = rule;

@@ -3206,38 +3206,8 @@
  %7C%7C%0A
-%09%09%09%09endsWith( prev, '*/' ) %7C%7C%0A
 %09%09%09%09
@@ -3323,16 +3323,28 @@
  follow 
+single-line 
 comments

Update rule