ESLint rule to enforce spacing in return annotations in single-line comments.
var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' );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 - The configured number of spaces after the annotation keyword (default: 1)
Bad (too many spaces after returns):
var v = 3.14;
// returns 3.14Bad (no space before =>):
console.log( 'beep' );
//=> 'beep'Bad (too many spaces before returns):
var x = true;
// returns trueGood:
var v = 3.14;
// returns 3.14
console.log( 'beep' );
// => 'beep'The rule accepts an options object with the following property:
- numSpaces: number of spaces required after the annotation keyword. Default:
1.
// With numSpaces: 2, the following would be valid:
var v = 3.14;
// returns 3.14Note: The spacing before the annotation keyword is always enforced to be exactly 1 space and is not configurable.
-
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
//(not configurable) - Configurable spacing after the keyword (default: 1 space)
- Exactly 1 space after
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 `returns` should be followed by 1 space(s). Found 13 space(s).',
'line': 2,
'column': 1,
'nodeType': null,
'endLine': 2,
'endColumn': 31
}
]
*/