section-headers

ESLint rule to enforce formatting of section header comments.

Usage

var rule = require( '@stdlib/_tools/eslint/rules/section-headers' );

rule

ESLint rule to enforce formatting of section header comments. Section header comments must be in uppercase letters and end with //. The following headers are allowed:

  • MODULES
  • VARIABLES
  • FUNCTIONS
  • MAIN
  • EXPORTS
  • NOTES
  • FIXTURES
  • TESTS

Bad:

// FUNCTIOns //

function add( x, y ) {
    return x + y;
}


// CONSTANTS //

var TWO_PI = 2.0 * Math.PI;


// EXPORTS

module.exports = {};

Good:

// VARIABLES //

var TWO_PI = 2.0 * Math.PI;


// EXPORTS //

module.exports = TWO_PI;

Examples

var Linter = require( 'eslint' ).Linter;
var rule = require( '@stdlib/_tools/eslint/rules/section-headers' );

var linter = new Linter();
var result;
var code;

var opts = {
    'rules': {
        'section-headers': 'error'
    }
};
linter.defineRule( 'section-headers', rule );

code = '// EXPORTS ';
result = linter.verify( code, opts );
console.log( result );
/* returns
    [
        {
            ruleId: 'section-headers',
            severity: 2,
            message: 'Header comment must end with two trailing slashes',
            line: 1,
            column: 1,
            nodeType: null,
            source: '// EXPORTS ',
            endLine: 1,
            endColumn: 12
        }
    ]
*/

code = '//VARIABLES //';
result = linter.verify( code, opts );
console.log( result );
/* returns
    [
        {
            ruleId: 'section-headers',
            severity: 2,
            message: 'Header title must be padded by exactly one space after and before comment slashes',
            line: 1,
            column: 1,
            nodeType: null,
            source: '//VARIABLES //',
            endLine: 1,
            endColumn: 15
        }
    ]
*/

code = '// FUNCTIOns //';
result = linter.verify( code, opts );
console.log( result );
/* returns
    [
        {
            ruleId: 'section-headers',
            severity: 2,
            message: 'Header comment must be in capital letters',
            line: 1,
            column: 1,
            nodeType: null,
            source: '// FUNCTIOns //',
            endLine: 1,
            endColumn: 16
        }
    ]
*/

code = '// CONSTANTS //';
result = linter.verify( code, opts );
console.log( result );
/* returns
    [
        {
            ruleId: 'section-headers',
            severity: 2,
            message: 'Invalid header comment type (must be any one of MODULES, VARIABLES, FUNCTIONS, METHODS, MAIN, EXPORTS, NOTES, FIXTURES, TESTS). Value: "CONSTANTS"',
            line: 1,
            column: 1,
            nodeType: null,
            source: '// CONSTANTS //',
            endLine: 1,
            endColumn: 16
        }
    ]
*/