Skip to content

Commit d4f153a

Browse files
committed
build: add ESLint rule to enforce no empty lines between module-level require statements
--- 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: passed - task: lint_package_json status: passed - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 88af63a commit d4f153a

11 files changed

Lines changed: 1228 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: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
# 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+
var result;
104+
var code;
105+
106+
var opts = {
107+
'rules': {
108+
'no-empty-lines-between-requires': 'error'
109+
}
110+
};
111+
linter.defineRule( 'no-empty-lines-between-requires', rule );
112+
113+
code = [
114+
'\'use strict\';',
115+
'',
116+
'// MODULES //',
117+
'',
118+
'var tape = require( \'tape\' );',
119+
'',
120+
'var isnan = require( \'@stdlib/math/base/assert/is-nan\' );'
121+
].join( '\n' );
122+
123+
result = linter.verify( code, opts );
124+
console.log( result );
125+
/* =>
126+
[
127+
{
128+
'ruleId': 'no-empty-lines-between-requires',
129+
'severity': 2,
130+
'message': 'Unexpected empty line between require statements.',
131+
'line': 6,
132+
'column': 0,
133+
'nodeType': null,
134+
'endLine': 7,
135+
'endColumn': 0
136+
}
137+
]
138+
*/
139+
```
140+
141+
</section>
142+
143+
<!-- /.examples -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules
158+
159+
</section>
160+
161+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
var result;
26+
var code;
27+
28+
var opts = {
29+
'rules': {
30+
'no-empty-lines-between-requires': 'error'
31+
}
32+
};
33+
linter.defineRule( 'no-empty-lines-between-requires', rule );
34+
35+
code = [
36+
'\'use strict\';',
37+
'',
38+
'// MODULES //',
39+
'',
40+
'var tape = require( \'tape\' );',
41+
'',
42+
'var isnan = require( \'@stdlib/math/base/assert/is-nan\' );',
43+
'',
44+
'',
45+
'// TESTS //',
46+
'',
47+
'tape( \'test\', function() {} );'
48+
].join( '\n' );
49+
50+
result = linter.verifyAndFix( code, opts );
51+
console.log( result );
52+
/* =>
53+
{
54+
'fixed': true,
55+
'messages': [],
56+
'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() {} );'
57+
}
58+
*/
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 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)