Skip to content

Commit 1298006

Browse files
committed
build: add lint rule enforcing empty lines between requires and code
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 8514fc9 commit 1298006

File tree

8 files changed

+763
-0
lines changed

8 files changed

+763
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
# jsdoc-example-require-spacing
22+
23+
> [ESLint rule][eslint-rules] to enforce empty lines between requires and code in JSDoc examples.
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/jsdoc-example-require-spacing' );
37+
```
38+
39+
#### rule
40+
41+
[ESLint rule][eslint-rules] to enforce empty lines between requires and code in JSDoc examples.
42+
43+
**Bad**:
44+
45+
<!-- eslint-disable stdlib/jsdoc-example-require-spacing -->
46+
47+
```javascript
48+
/**
49+
* Fréchet distribution constructor.
50+
*
51+
* @module @stdlib/stats/base/dists/frechet/ctor
52+
*
53+
* @example
54+
* var Frechet = require( '@stdlib/stats/base/dists/frechet/ctor' );
55+
* var frechet = new Frechet( 1.0, 1.0, 0.5 );
56+
*
57+
* var y = frechet.cdf( 0.8 );
58+
* // returns ~0.036
59+
*/
60+
```
61+
62+
**Good**:
63+
64+
```javascript
65+
/**
66+
* Fréchet distribution constructor.
67+
*
68+
* @module @stdlib/stats/base/dists/frechet/ctor
69+
*
70+
* @example
71+
* var Frechet = require( '@stdlib/stats/base/dists/frechet/ctor' );
72+
*
73+
* var frechet = new Frechet( 1.0, 1.0, 0.5 );
74+
*
75+
* var y = frechet.cdf( 0.8 );
76+
* // returns ~0.036
77+
*/
78+
```
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var Linter = require( 'eslint' ).Linter;
92+
var rule = require( '@stdlib/_tools/eslint/rules/jsdoc-example-require-spacing' );
93+
94+
var linter = new Linter();
95+
var result;
96+
var code;
97+
98+
code = [
99+
'/**',
100+
'* Fréchet distribution constructor.',
101+
'*',
102+
'* @module @stdlib/stats/base/dists/frechet/ctor',
103+
'*',
104+
'* @example',
105+
'* var Frechet = require( \'@stdlib/stats/base/dists/frechet/ctor\' );',
106+
'* var frechet = new Frechet( 1.0, 1.0, 0.5 );',
107+
'*',
108+
'* var y = frechet.cdf( 0.8 );',
109+
'* // returns ~0.036',
110+
'*/',
111+
'function Frechet() {}'
112+
].join( '\n' );
113+
114+
linter.defineRule( 'jsdoc-example-require-spacing', rule );
115+
116+
result = linter.verify( code, {
117+
'rules': {
118+
'jsdoc-example-require-spacing': 'error'
119+
}
120+
});
121+
/* returns
122+
[
123+
{
124+
'ruleId': 'jsdoc-example-require-spacing',
125+
'severity': 2,
126+
'message': 'Missing empty line between require statement and code',
127+
'line': 13,
128+
'column': 1,
129+
'nodeType': 'FunctionDeclaration',
130+
'endLine': 13,
131+
'endColumn': 21
132+
}
133+
]
134+
*/
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules
154+
155+
</section>
156+
157+
<!-- /.links -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// Valid example with empty line after require:
27+
var valid = [
28+
'/**',
29+
'* Fréchet distribution constructor.',
30+
'*',
31+
'* @example',
32+
'* var Frechet = require( \'@stdlib/stats/base/dists/frechet/ctor\' );',
33+
'*',
34+
'* var frechet = new Frechet( 1.0, 1.0, 0.5 );',
35+
'*',
36+
'* var y = frechet.cdf( 0.8 );',
37+
'* // returns ~0.036',
38+
'*/',
39+
'function Frechet() {}'
40+
].join( '\n' );
41+
42+
// Invalid example without empty line after require:
43+
var invalid = [
44+
'/**',
45+
'* Fréchet distribution constructor.',
46+
'*',
47+
'* @example',
48+
'* var Frechet = require( \'@stdlib/stats/base/dists/frechet/ctor\' );',
49+
'* var frechet = new Frechet( 1.0, 1.0, 0.5 );',
50+
'*',
51+
'* var y = frechet.cdf( 0.8 );',
52+
'* // returns ~0.036',
53+
'*/',
54+
'function Frechet() {}'
55+
].join( '\n' );
56+
57+
// Register the rule:
58+
linter.defineRule( 'jsdoc-example-require-spacing', rule );
59+
60+
// Lint the valid example:
61+
var validResult = linter.verify( valid, {
62+
'rules': {
63+
'jsdoc-example-require-spacing': 'error'
64+
}
65+
});
66+
console.log( 'Valid example - Number of errors: %d', validResult.length );
67+
68+
// Lint the invalid example:
69+
var invalidResult = linter.verify( invalid, {
70+
'rules': {
71+
'jsdoc-example-require-spacing': 'error'
72+
}
73+
});
74+
console.log( 'Invalid example - Number of errors: %d', invalidResult.length );
75+
console.log( 'Error message: %s', invalidResult[ 0 ].message );
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
// MODULES //
22+
23+
var rule = require( './main.js' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* ESLint rule to enforce empty lines between requires and code in JSDoc examples.
30+
*
31+
* @module @stdlib/_tools/eslint/rules/jsdoc-example-require-spacing
32+
*
33+
* @example
34+
* var rule = require( '@stdlib/_tools/eslint/rules/jsdoc-example-require-spacing' );
35+
*
36+
* var config = {
37+
* 'rules': {
38+
* 'stdlib/jsdoc-example-require-spacing': 'error'
39+
* }
40+
* };
41+
*/
42+
43+
// EXPORTS //
44+
45+
module.exports = rule;

0 commit comments

Comments
 (0)