Skip to content

Commit c6c2de6

Browse files
Planeshifterkgrytestdlib-bot
authored
build: add ESLint rule to enforce no empty lines between module-level require statements
PR-URL: #9488 Closes: stdlib-js/metr-issue-tracker#132 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 3ae6e96 commit c6c2de6

11 files changed

Lines changed: 1343 additions & 0 deletions

File tree

etc/eslint/rules/stdlib.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,6 +4186,27 @@ rules[ 'stdlib/no-dynamic-require' ] = 'error';
41864186
*/
41874187
rules[ 'stdlib/no-empty-comments' ] = 'error';
41884188

4189+
/**
4190+
* Enforce no empty lines between module-level require statements.
4191+
*
4192+
* @name no-empty-lines-between-requires
4193+
* @memberof rules
4194+
* @type {string}
4195+
* @default 'error'
4196+
*
4197+
* @example
4198+
* // Bad...
4199+
* var foo = require( 'foo' );
4200+
*
4201+
* var bar = require( 'bar' );
4202+
*
4203+
* @example
4204+
* // Good...
4205+
* var foo = require( 'foo' );
4206+
* var bar = require( 'bar' );
4207+
*/
4208+
rules[ 'stdlib/no-empty-lines-between-requires' ] = 'error';
4209+
41894210
/**
41904211
* Disallow string concatenation in error messages.
41914212
*

lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,15 @@ setReadOnly( rules, 'no-dynamic-require', require( '@stdlib/_tools/eslint/rules/
900900
*/
901901
setReadOnly( rules, 'no-empty-comments', require( '@stdlib/_tools/eslint/rules/no-empty-comments' ) );
902902

903+
/**
904+
* @name no-empty-lines-between-requires
905+
* @memberof rules
906+
* @readonly
907+
* @type {Function}
908+
* @see {@link module:@stdlib/_tools/eslint/rules/no-empty-lines-between-requires}
909+
*/
910+
setReadOnly( rules, 'no-empty-lines-between-requires', require( '@stdlib/_tools/eslint/rules/no-empty-lines-between-requires' ) );
911+
903912
/**
904913
* @name no-error-string-concat
905914
* @memberof rules
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# no-empty-lines-between-requires
22+
23+
> [ESLint rule][eslint-rules] to enforce no empty lines between module-level require statements.
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/no-empty-lines-between-requires' );
37+
```
38+
39+
#### rule
40+
41+
[ESLint rule][eslint-rules] to enforce no empty lines between module-level require statements. Section header comments (such as `// FIXTURES //`) are allowed to separate groups of require statements.
42+
43+
This rule only applies to top-level (module-level) require statements. Require statements inside functions or blocks are not checked.
44+
45+
**Bad**:
46+
47+
<!-- run-disable -->
48+
49+
<!-- eslint-disable stdlib/no-empty-lines-between-requires -->
50+
51+
```javascript
52+
var tape = require( 'tape' );
53+
54+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
55+
56+
var abs = require( '@stdlib/math/base/special/abs' );
57+
```
58+
59+
**Good**:
60+
61+
<!-- run-disable -->
62+
63+
<!-- eslint-disable stdlib/no-empty-lines-between-requires -->
64+
65+
```javascript
66+
var tape = require( 'tape' );
67+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
68+
var abs = require( '@stdlib/math/base/special/abs' );
69+
```
70+
71+
**Good** (section headers separate groups):
72+
73+
<!-- run-disable -->
74+
75+
<!-- eslint-disable stdlib/no-empty-lines-between-requires, stdlib/require-file-extensions -->
76+
77+
```javascript
78+
var tape = require( 'tape' );
79+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
80+
81+
82+
// FIXTURES //
83+
84+
var data = require( './fixtures/data.json' );
85+
var expected = require( './fixtures/expected.json' );
86+
```
87+
88+
</section>
89+
90+
<!-- /.usage -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var Linter = require( 'eslint' ).Linter;
100+
var rule = require( '@stdlib/_tools/eslint/rules/no-empty-lines-between-requires' );
101+
102+
var linter = new Linter();
103+
104+
var opts = {
105+
'rules': {
106+
'no-empty-lines-between-requires': 'error'
107+
}
108+
};
109+
linter.defineRule( 'no-empty-lines-between-requires', rule );
110+
111+
var code = [
112+
'\'use strict\';',
113+
'',
114+
'// MODULES //',
115+
'',
116+
'var tape = require( \'tape\' );',
117+
'',
118+
'var isnan = require( \'@stdlib/math/base/assert/is-nan\' );'
119+
].join( '\n' );
120+
121+
var result = linter.verify( code, opts );
122+
console.log( result );
123+
/* =>
124+
[
125+
{
126+
'ruleId': 'no-empty-lines-between-requires',
127+
'severity': 2,
128+
'message': 'Unexpected empty line between require statements.',
129+
'line': 6,
130+
'column': 0,
131+
'nodeType': null,
132+
'endLine': 7,
133+
'endColumn': 0
134+
}
135+
]
136+
*/
137+
```
138+
139+
</section>
140+
141+
<!-- /.examples -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules
156+
157+
</section>
158+
159+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 opts = {
27+
'rules': {
28+
'no-empty-lines-between-requires': 'error'
29+
}
30+
};
31+
linter.defineRule( 'no-empty-lines-between-requires', rule );
32+
33+
var code = [
34+
'\'use strict\';',
35+
'',
36+
'// MODULES //',
37+
'',
38+
'var tape = require( \'tape\' );',
39+
'',
40+
'var isnan = require( \'@stdlib/math/base/assert/is-nan\' );',
41+
'',
42+
'',
43+
'// TESTS //',
44+
'',
45+
'tape( \'test\', function() {} );'
46+
].join( '\n' );
47+
48+
var result = linter.verifyAndFix( code, opts );
49+
console.log( result );
50+
/* =>
51+
{
52+
'fixed': true,
53+
'messages': [],
54+
'output': '\'use strict\';\n\n// MODULES //\n\nvar tape = require( \'tape\' );\nvar isnan = require( \'@stdlib/math/base/assert/is-nan\' );\n\n\n// TESTS //\n\ntape( \'test\', function() {} );'
55+
}
56+
*/
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) 2026 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 no empty lines between module-level require statements.
23+
*
24+
* @module @stdlib/_tools/eslint/rules/no-empty-lines-between-requires
25+
*
26+
* @example
27+
* var rule = require( '@stdlib/_tools/eslint/rules/no-empty-lines-between-requires' );
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)