Skip to content

Commit add570e

Browse files
Planeshifterkgryte
andauthored
feat: add doctest-annotation-spacing ESLint rule
PR-URL: #8842 Closes: stdlib-js/metr-issue-tracker#127 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent b2b3432 commit add570e

File tree

9 files changed

+934
-0
lines changed

9 files changed

+934
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# doctest-annotation-spacing
22+
23+
> [ESLint rule][eslint-rules] to enforce spacing in return annotations in single-line comments.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' );
37+
```
38+
39+
#### rule
40+
41+
[ESLint rule][eslint-rules] to enforce spacing in return annotations in single-line comments.
42+
43+
**Bad** (too many spaces after `returns`):
44+
45+
<!-- eslint-disable stdlib/doctest-annotation-spacing -->
46+
47+
```javascript
48+
var v = 3.14;
49+
// returns 3.14
50+
```
51+
52+
**Bad** (no space before `=>`):
53+
54+
<!-- eslint-disable stdlib/doctest-annotation-spacing, spaced-comment -->
55+
56+
```javascript
57+
console.log( 'beep' );
58+
//=> 'beep'
59+
```
60+
61+
**Bad** (too many spaces before `returns`):
62+
63+
<!-- eslint-disable stdlib/doctest-annotation-spacing, spaced-comment -->
64+
65+
```javascript
66+
var x = true;
67+
// returns true
68+
```
69+
70+
**Good**:
71+
72+
```javascript
73+
var v = 3.14;
74+
// returns 3.14
75+
76+
console.log( 'beep' );
77+
// => 'beep'
78+
```
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="notes">
85+
86+
## Notes
87+
88+
- This rule only applies to single-line comments (starting with `//`). Multi-line comments (`/* ... */`) are ignored.
89+
90+
- The rule matches the following patterns:
91+
92+
- `// returns`
93+
- `// =>`
94+
- `// e.g., returns`
95+
- `// e.g., =>`
96+
97+
- The rule enforces two spacing requirements:
98+
99+
- Exactly 1 space between `//` and an annotation keyword (`returns`) or identifier (`=>`).
100+
- Exactly 1 space after the annotation keyword.
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var Linter = require( 'eslint' ).Linter;
114+
var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' );
115+
116+
var linter = new Linter();
117+
118+
var code = [
119+
'var v = foo();',
120+
'// returns \'beep\'',
121+
'',
122+
'console.log( bar() );',
123+
'// => \'boop\''
124+
].join( '\n' );
125+
126+
linter.defineRule( 'doctest-annotation-spacing', rule );
127+
128+
var result = linter.verify( code, {
129+
'rules': {
130+
'doctest-annotation-spacing': 'error'
131+
}
132+
});
133+
/* returns
134+
[
135+
{
136+
'ruleId': 'doctest-annotation-spacing',
137+
'severity': 2,
138+
'message': 'Return annotation keyword `returns` should be followed by 1 space. Found 13 space(s).',
139+
'line': 2,
140+
'column': 1,
141+
'nodeType': null,
142+
'endLine': 2,
143+
'endColumn': 30
144+
}
145+
]
146+
*/
147+
```
148+
149+
</section>
150+
151+
<!-- /.examples -->
152+
153+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
154+
155+
<section class="related">
156+
157+
</section>
158+
159+
<!-- /.related -->
160+
161+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
162+
163+
<section class="links">
164+
165+
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules
166+
167+
</section>
168+
169+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var Linter = require( 'eslint' ).Linter;
22+
var rule = require( './../lib' );
23+
24+
var linter = new Linter();
25+
26+
var code = [
27+
'var v = foo();',
28+
'// returns \'beep\'',
29+
'',
30+
'console.log( bar() );',
31+
'// => \'boop\''
32+
].join( '\n' );
33+
34+
linter.defineRule( 'doctest-annotation-spacing', rule );
35+
36+
var result = linter.verify( code, {
37+
'rules': {
38+
'doctest-annotation-spacing': 'error'
39+
}
40+
});
41+
console.log( result );
42+
/* =>
43+
[
44+
{
45+
'ruleId': 'doctest-annotation-spacing',
46+
'severity': 2,
47+
'message': 'Return annotation keyword `returns` should be followed by 1 space(s). Found 13 space(s).',
48+
'line': 2,
49+
'column': 1,
50+
'nodeType': null,
51+
'endLine': 2,
52+
'endColumn': 30
53+
}
54+
]
55+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* ESLint rule to enforce spacing after return annotations in single-line comments.
23+
*
24+
* @module @stdlib/_tools/eslint/rules/doctest-annotation-spacing
25+
*
26+
* @example
27+
* var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' );
28+
*
29+
* console.log( rule );
30+
*/
31+
32+
// MODULES //
33+
34+
var main = require( './main.js' );
35+
36+
37+
// EXPORTS //
38+
39+
module.exports = main;

0 commit comments

Comments
 (0)