Skip to content

Commit 8b19217

Browse files
headlessNodekgryte
andauthored
build: add ESLint rule to disallow new Array(...)
PR-URL: #5493 Closes: stdlib-js/metr-issue-tracker#28 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent a4d7692 commit 8b19217

File tree

11 files changed

+775
-0
lines changed

11 files changed

+775
-0
lines changed

etc/eslint/rules/stdlib.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,6 +4310,38 @@ rules[ 'stdlib/no-dynamic-exports' ] = 'error';
43104310
*/
43114311
rules[ 'stdlib/no-nested-require' ] = 'error';
43124312

4313+
/**
4314+
* Disallow the use of the `new Array()` constructor.
4315+
*
4316+
* @name no-new-array
4317+
* @memberof rules
4318+
* @type {string}
4319+
* @default 'error'
4320+
*
4321+
* @example
4322+
* // Bad...
4323+
* var arr = new Array( 5 );
4324+
* var i;
4325+
* for ( i = 0; i < 5; i++ ) {
4326+
* arr[ i ] = i;
4327+
* }
4328+
*
4329+
* console.log( arr );
4330+
* // => [ 0, 1, 2, 3, 4 ]
4331+
*
4332+
* @example
4333+
* // Good...
4334+
* var arr = [];
4335+
* var i;
4336+
* for ( i = 0; i < 5; i++ ) {
4337+
* arr.push( i );
4338+
* }
4339+
*
4340+
* console.log( arr );
4341+
* // => [ 0, 1, 2, 3, 4 ]
4342+
*/
4343+
rules[ 'stdlib/no-new-array' ] = 'error';
4344+
43134345
/**
43144346
* Never allow a variable to be declared multiple times within the same scope or for built-in globals to be redeclared.
43154347
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,15 @@ setReadOnly( rules, 'no-multiple-empty-lines', require( '@stdlib/_tools/eslint/r
909909
*/
910910
setReadOnly( rules, 'no-nested-require', require( '@stdlib/_tools/eslint/rules/no-nested-require' ) );
911911

912+
/**
913+
* @name no-new-array
914+
* @memberof rules
915+
* @readonly
916+
* @type {Function}
917+
* @see {@link module:@stdlib/_tools/eslint/rules/no-new-array}
918+
*/
919+
setReadOnly( rules, 'no-new-array', require( '@stdlib/_tools/eslint/rules/no-new-array' ) );
920+
912921
/**
913922
* @name no-redeclare
914923
* @memberof rules
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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-new-array
22+
23+
> [ESLint rule][eslint-rules] disallowing the use of the `new Array()` constructor.
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-new-array' );
37+
```
38+
39+
#### rule
40+
41+
[ESLint rule][eslint-rules] disallowing the use of the `new Array()` constructor.
42+
43+
**Bad**:
44+
45+
<!-- eslint-disable stdlib/no-new-array -->
46+
47+
```javascript
48+
var arr = new Array( 5 );
49+
var i;
50+
for ( i = 0; i < 5; i++ ) {
51+
arr[ i ] = i;
52+
}
53+
54+
console.log( arr );
55+
// => [ 0, 1, 2, 3, 4 ]
56+
```
57+
58+
**Good**:
59+
60+
```javascript
61+
var arr = [];
62+
var i;
63+
for ( i = 0; i < 5; i++ ) {
64+
arr.push( i );
65+
}
66+
67+
console.log( arr );
68+
// => [ 0, 1, 2, 3, 4 ]
69+
```
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
```javascript
80+
var Linter = require( 'eslint' ).Linter;
81+
var rule = require( '@stdlib/_tools/eslint/rules/no-new-array' );
82+
83+
var linter = new Linter();
84+
85+
var code = 'var arr = new Array( 5 );';
86+
87+
linter.defineRule( 'no-new-array', rule );
88+
89+
var result = linter.verify( code, {
90+
'rules': {
91+
'no-new-array': 'error'
92+
}
93+
});
94+
/* returns
95+
[
96+
{
97+
'ruleId': 'no-new-array',
98+
'severity': 2,
99+
'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.',
100+
'line': 1,
101+
'column': 14,
102+
'nodeType': 'CallExpression',
103+
'source': 'var arr = new Array( 5 );',
104+
'endLine': 1,
105+
'endColumn': 25
106+
}
107+
]
108+
*/
109+
```
110+
111+
<!-- /.examples -->
112+
113+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
114+
115+
<section class="related">
116+
117+
</section>
118+
119+
<!-- /.related -->
120+
121+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
122+
123+
<section class="links">
124+
125+
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules
126+
127+
</section>
128+
129+
<!-- /.links -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 = 'var arr = new Array( 5 );';
27+
28+
linter.defineRule( 'no-new-array', rule );
29+
30+
var result = linter.verify( code, {
31+
'rules': {
32+
'no-new-array': 'error'
33+
}
34+
});
35+
console.log( result );
36+
/* =>
37+
[
38+
{
39+
'ruleId': 'no-new-array',
40+
'severity': 2,
41+
'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.',
42+
'line': 1,
43+
'column': 14,
44+
'nodeType': 'CallExpression',
45+
'source': 'var arr = new Array( 5 );',
46+
'endLine': 1,
47+
'endColumn': 25
48+
}
49+
]
50+
*/
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 disallowing the use of the `new Array()` constructor.
23+
*
24+
* @module @stdlib/_tools/eslint/rules/no-new-array
25+
*
26+
* @example
27+
* var rule = require( '@stdlib/_tools/eslint/rules/no-new-array' );
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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
// VARIABLES //
22+
23+
var rule;
24+
25+
26+
// FUNCTIONS //
27+
28+
/**
29+
* Rule for disallowing the use of the `new Array()` constructor.
30+
*
31+
* @param {Object} context - ESLint context
32+
* @returns {Object} validators
33+
*/
34+
function main( context ) {
35+
/**
36+
* Reports the error message.
37+
*
38+
* @private
39+
* @param {ASTNode} node - node to report
40+
*/
41+
function report( node ) {
42+
context.report({
43+
'node': node,
44+
'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.'
45+
});
46+
}
47+
48+
/**
49+
* Checks whether a node is a `new Array()` expression.
50+
*
51+
* @private
52+
* @param {ASTNode} node - node to examine
53+
*/
54+
function validate( node ) {
55+
if (
56+
node.callee &&
57+
node.callee.name === 'Array'
58+
) {
59+
report( node );
60+
}
61+
}
62+
63+
return {
64+
'NewExpression': validate
65+
};
66+
}
67+
68+
69+
// MAIN //
70+
71+
rule = {
72+
'meta': {
73+
'type': 'suggestion',
74+
'docs': {
75+
'description': 'disallow the use of the `new Array()` constructor'
76+
},
77+
'schema': []
78+
},
79+
'create': main
80+
};
81+
82+
83+
// EXPORTS //
84+
85+
module.exports = rule;

0 commit comments

Comments
 (0)