Skip to content

Commit 1a83cbc

Browse files
committed
build: add lint rule to enforce spaces in require calls
1 parent 58ee2fe commit 1a83cbc

File tree

10 files changed

+599
-0
lines changed

10 files changed

+599
-0
lines changed

etc/eslint/rules/stdlib.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4454,6 +4454,23 @@ rules[ 'stdlib/require-globals' ] = [ 'error', {
44544454
*/
44554455
rules[ 'stdlib/require-leading-slash' ] = 'error';
44564456

4457+
/**
4458+
* Enforce spaces inside `require()` parentheses.
4459+
*
4460+
* @name require-spaces
4461+
* @memberof rules
4462+
* @type {string}
4463+
*
4464+
* @example
4465+
* // Bad...
4466+
* var foo = require('@stdlib/foo');
4467+
*
4468+
* @example
4469+
* // Good...
4470+
* var foo = require( '@stdlib/foo' );
4471+
*/
4472+
rules[ 'stdlib/require-spaces' ] = 'error';
4473+
44574474
/**
44584475
* Enforce that `require()` calls follow a specified order.
44594476
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,15 @@ setReadOnly( rules, 'require-leading-slash', require( '@stdlib/_tools/eslint/rul
972972
*/
973973
setReadOnly( rules, 'require-order', require( '@stdlib/_tools/eslint/rules/require-order' ) );
974974

975+
/**
976+
* @name require-spaces
977+
* @memberof rules
978+
* @readonly
979+
* @type {Function}
980+
* @see {@link module:@stdlib/_tools/eslint/rules/require-spaces}
981+
*/
982+
setReadOnly( rules, 'require-spaces', require( '@stdlib/_tools/eslint/rules/require-spaces' ) );
983+
975984
/**
976985
* @name section-header-empty-lines
977986
* @memberof rules
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# require-spaces
22+
23+
> [ESLint rule][eslint-rules] enforcing spaces in `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/require-spaces' );
37+
```
38+
39+
#### rule
40+
41+
[ESLint rule][eslint-rules] enforcing spaces in `require()` statements.
42+
43+
**Good**:
44+
45+
```javascript
46+
var betainc = require( '@stdlib/math/base/special/betainc' );
47+
```
48+
49+
**Bad**:
50+
51+
<!-- eslint-disable stdlib/require-spaces -->
52+
53+
```javascript
54+
var betainc = require('@stdlib/math/base/special/betainc');
55+
var zip = require( '@stdlib/utils/zip');
56+
var noop = require('@stdlib/utils/noop' );
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<section class="examples">
64+
65+
## Examples
66+
67+
<!-- eslint no-undef: "error" -->
68+
69+
```javascript
70+
var Linter = require( 'eslint' ).Linter;
71+
var rule = require( '@stdlib/_tools/eslint/rules/require-spaces' );
72+
73+
var linter = new Linter();
74+
var result;
75+
76+
var code = 'var betainc = require(\'@stdlib/math/base/special/betainc\');';
77+
78+
linter.defineRule( 'require-spaces', rule );
79+
80+
result = linter.verify( code, {
81+
'rules': {
82+
'require-spaces': 'error'
83+
}
84+
});
85+
console.log( result );
86+
```
87+
88+
</section>
89+
90+
<!-- /.examples -->
91+
92+
<section class="links">
93+
94+
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules
95+
96+
</section>
97+
98+
<!-- /.links -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
27+
var code = 'var betainc = require(\'@stdlib/math/base/special/betainc\');';
28+
29+
linter.defineRule( 'require-spaces', rule );
30+
31+
result = linter.verify( code, {
32+
'rules': {
33+
'require-spaces': 'error'
34+
}
35+
});
36+
console.log( result );
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) 2024 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 enforcing spaces in `require()` statements.
23+
*
24+
* @module @stdlib/_tools/eslint/rules/require-spaces
25+
*
26+
* @example
27+
* var rule = require( '@stdlib/_tools/eslint/rules/require-spaces' );
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;
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// VARIABLES //
22+
23+
var rule;
24+
25+
26+
// FUNCTIONS //
27+
28+
/**
29+
* Rule for enforcing spaces in `require()` statements.
30+
*
31+
* @param {Object} context - ESLint context
32+
* @returns {Object} validators
33+
*/
34+
function create( context ) {
35+
/**
36+
* Reports the error message.
37+
*
38+
* @private
39+
* @param {ASTNode} node - node to report
40+
* @param {string} message - error message
41+
* @param {Array} range - array containing start and end indices for the fix
42+
*/
43+
function report( node, message, range ) {
44+
context.report({
45+
'node': node,
46+
'message': message,
47+
'fix': fix
48+
});
49+
50+
/**
51+
* Fixes the error by replacing the text in the specified range.
52+
*
53+
* @private
54+
* @param {Object} fixer - object for fixing the error
55+
* @returns {Object} fixer object
56+
*/
57+
function fix( fixer ) {
58+
return fixer.replaceTextRange( range, ' ' );
59+
}
60+
}
61+
62+
/**
63+
* Checks whether require path has correct spacing.
64+
*
65+
* @private
66+
* @param {ASTNode} node - node to examine
67+
*/
68+
function validate( node ) {
69+
var tokens;
70+
var source;
71+
var close;
72+
var open;
73+
var arg;
74+
75+
if ( node.callee.name === 'require' ) {
76+
source = context.getSourceCode();
77+
tokens = source.getTokens( node );
78+
open = tokens[ 1 ];
79+
close = tokens[ tokens.length - 1 ];
80+
arg = node.arguments[ 0 ];
81+
82+
// Check space after opening parenthesis:
83+
if ( source.getText().slice( open.range[1], arg.range[0] ) !== ' ' ) {
84+
report( node, 'There should be exactly one space after the opening parenthesis in require calls.', [ open.range[1], arg.range[0] ] );
85+
}
86+
87+
// Check space before closing parenthesis:
88+
if ( source.getText().slice( arg.range[1], close.range[0] ) !== ' ' ) {
89+
report( node, 'There should be exactly one space before the closing parenthesis in require calls.', [ arg.range[1], close.range[0] ] );
90+
}
91+
}
92+
}
93+
94+
return {
95+
'CallExpression': validate
96+
};
97+
}
98+
99+
100+
// MAIN //
101+
102+
/**
103+
* ESLint rule to enforce spaces in `require()` statements.
104+
*
105+
* @namespace rule
106+
*/
107+
rule = {
108+
'meta': {
109+
'type': 'layout',
110+
'docs': {
111+
'description': 'enforce spaces in require statements'
112+
},
113+
'fixable': 'whitespace',
114+
'schema': []
115+
},
116+
'create': create
117+
};
118+
119+
120+
// EXPORTS //
121+
122+
module.exports = rule;

0 commit comments

Comments
 (0)