Skip to content

build: add ESLint rule to disallow new Array(...) #5493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions etc/eslint/rules/stdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@
* // Bad...
*
* /**
* * Fréchet distribution constructor.

Check warning on line 853 in etc/eslint/rules/stdlib.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "échet"
* *
* * @module @stdlib/stats/base/dists/frechet/ctor
* *
Expand All @@ -866,7 +866,7 @@
* // Good...
*
* /**
* * Fréchet distribution constructor.

Check warning on line 869 in etc/eslint/rules/stdlib.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "échet"
* *
* * @module @stdlib/stats/base/dists/frechet/ctor
* *
Expand Down Expand Up @@ -4310,6 +4310,38 @@
*/
rules[ 'stdlib/no-nested-require' ] = 'error';

/**
* Disallow the use of the `new Array()` constructor.
*
* @name no-new-array
* @memberof rules
* @type {string}
* @default 'error'
*
* @example
* // Bad...
* var arr = new Array( 5 );
* var i;
* for ( i = 0; i < 5; i++ ) {
* arr[ i ] = i;
* }
*
* console.log( arr );
* // => [ 0, 1, 2, 3, 4 ]
*
* @example
* // Good...
* var arr = [];
* var i;
* for ( i = 0; i < 5; i++ ) {
* arr.push( i );
* }
*
* console.log( arr );
* // => [ 0, 1, 2, 3, 4 ]
*/
rules[ 'stdlib/no-new-array' ] = 'error';

/**
* Never allow a variable to be declared multiple times within the same scope or for built-in globals to be redeclared.
*
Expand Down
9 changes: 9 additions & 0 deletions lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,15 @@ setReadOnly( rules, 'no-multiple-empty-lines', require( '@stdlib/_tools/eslint/r
*/
setReadOnly( rules, 'no-nested-require', require( '@stdlib/_tools/eslint/rules/no-nested-require' ) );

/**
* @name no-new-array
* @memberof rules
* @readonly
* @type {Function}
* @see {@link module:@stdlib/_tools/eslint/rules/no-new-array}
*/
setReadOnly( rules, 'no-new-array', require( '@stdlib/_tools/eslint/rules/no-new-array' ) );

/**
* @name no-redeclare
* @memberof rules
Expand Down
129 changes: 129 additions & 0 deletions lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# no-new-array

> [ESLint rule][eslint-rules] disallowing the use of the `new Array()` constructor.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var rule = require( '@stdlib/_tools/eslint/rules/no-new-array' );
```

#### rule

[ESLint rule][eslint-rules] disallowing the use of the `new Array()` constructor.

**Bad**:

<!-- eslint-disable stdlib/no-new-array -->

```javascript
var arr = new Array( 5 );
var i;
for ( i = 0; i < 5; i++ ) {
arr[ i ] = i;
}

console.log( arr );
// => [ 0, 1, 2, 3, 4 ]
```

**Good**:

```javascript
var arr = [];
var i;
for ( i = 0; i < 5; i++ ) {
arr.push( i );
}

console.log( arr );
// => [ 0, 1, 2, 3, 4 ]
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

```javascript
var Linter = require( 'eslint' ).Linter;
var rule = require( '@stdlib/_tools/eslint/rules/no-new-array' );

var linter = new Linter();

var code = 'var arr = new Array( 5 );';

linter.defineRule( 'no-new-array', rule );

var result = linter.verify( code, {
'rules': {
'no-new-array': 'error'
}
});
/* returns
[
{
'ruleId': 'no-new-array',
'severity': 2,
'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.',
'line': 1,
'column': 14,
'nodeType': 'CallExpression',
'source': 'var arr = new Array( 5 );',
'endLine': 1,
'endColumn': 25
}
]
*/
```

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var Linter = require( 'eslint' ).Linter;
var rule = require( './../lib' );

var linter = new Linter();

var code = 'var arr = new Array( 5 );';

linter.defineRule( 'no-new-array', rule );

var result = linter.verify( code, {
'rules': {
'no-new-array': 'error'
}
});
console.log( result );
/* =>
[
{
'ruleId': 'no-new-array',
'severity': 2,
'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.',
'line': 1,
'column': 14,
'nodeType': 'CallExpression',
'source': 'var arr = new Array( 5 );',
'endLine': 1,
'endColumn': 25
}
]
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* ESLint rule disallowing the use of the `new Array()` constructor.
*
* @module @stdlib/_tools/eslint/rules/no-new-array
*
* @example
* var rule = require( '@stdlib/_tools/eslint/rules/no-new-array' );
*
* console.log( rule );
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// VARIABLES //

var rule;


// FUNCTIONS //

/**
* Rule for disallowing the use of the `new Array()` constructor.
*
* @param {Object} context - ESLint context
* @returns {Object} validators
*/
function main( context ) {
/**
* Reports the error message.
*
* @private
* @param {ASTNode} node - node to report
*/
function report( node ) {
context.report({
'node': node,
'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.'
});
}

/**
* Checks whether a node is a `new Array()` expression.
*
* @private
* @param {ASTNode} node - node to examine
*/
function validate( node ) {
if (
node.callee &&
node.callee.name === 'Array'
) {
report( node );
}
}

return {
'NewExpression': validate
};
}


// MAIN //

rule = {
'meta': {
'type': 'suggestion',
'docs': {
'description': 'disallow the use of the `new Array()` constructor'
},
'schema': []
},
'create': main
};


// EXPORTS //

module.exports = rule;
Loading