Skip to content

Latest commit

 

History

History
172 lines (113 loc) · 3.64 KB

File metadata and controls

172 lines (113 loc) · 3.64 KB

doctest-annotation-spacing

ESLint rule to enforce spacing in return annotations in single-line comments.

Usage

var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' );

rule

ESLint rule to enforce spacing in return annotations in single-line comments. The rule checks // returns and // => annotations (including // e.g., returns and // e.g., => variants) and ensures:

  • Exactly 1 space between // and the annotation keyword
  • Exactly 1 space after the annotation keyword

Bad (too many spaces after returns):

var v = 3.14;
// returns             3.14

Bad (no space before =>):

console.log( 'beep' );
//=> 'beep'

Bad (too many spaces before returns):

var x = true;
//  returns true

Good:

var v = 3.14;
// returns 3.14

console.log( 'beep' );
// => 'beep'

Notes

  • This rule only applies to single-line comments (starting with //). Multi-line comments (/* ... */) are ignored.

  • The rule matches the following patterns:

    • // returns
    • // =>
    • // e.g., returns
    • // e.g., =>
  • The rule enforces two spacing requirements:

    • Exactly 1 space after //
    • Exactly 1 space after the keyword

Examples

var Linter = require( 'eslint' ).Linter;
var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' );

var linter = new Linter();

var code = [
    'var v = foo();',
    '// returns             \'beep\'',
    '',
    'console.log( bar() );',
    '// => \'boop\''
].join( '\n' );

linter.defineRule( 'doctest-annotation-spacing', rule );

var result = linter.verify( code, {
    'rules': {
        'doctest-annotation-spacing': 'error'
    }
});
/* returns
    [
        {
            'ruleId': 'doctest-annotation-spacing',
            'severity': 2,
            'message': 'Return annotation keyword `returns` should be followed by 1 space. Found 13 space(s).',
            'line': 2,
            'column': 1,
            'nodeType': null,
            'endLine': 2,
            'endColumn': 30
        }
    ]
*/