diff --git a/etc/eslint/rules/stdlib.js b/etc/eslint/rules/stdlib.js index ceacbd72d966..58bf584b845a 100644 --- a/etc/eslint/rules/stdlib.js +++ b/etc/eslint/rules/stdlib.js @@ -4310,6 +4310,38 @@ rules[ 'stdlib/no-dynamic-exports' ] = 'error'; */ 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. * diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js b/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js index 7611a92391a2..28356128cb79 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js @@ -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 diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/README.md b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/README.md new file mode 100644 index 000000000000..a16da76bc42a --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/README.md @@ -0,0 +1,129 @@ + + +# no-new-array + +> [ESLint rule][eslint-rules] disallowing the use of the `new Array()` constructor. + +
+ +
+ + + +
+ +## 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**: + + + +```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 ] +``` + +
+ + + +
+ +## 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 + } + ] +*/ +``` + + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/examples/index.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/examples/index.js new file mode 100644 index 000000000000..e816226a9a85 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/examples/index.js @@ -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 + } + ] +*/ diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/lib/index.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/lib/index.js new file mode 100644 index 000000000000..dc731273e816 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/lib/index.js @@ -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; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/lib/main.js new file mode 100644 index 000000000000..f55ea1d4d7cc --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/lib/main.js @@ -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; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/package.json b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/package.json new file mode 100644 index 000000000000..df135693657f --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/package.json @@ -0,0 +1,54 @@ +{ + "name": "@stdlib/_tools/eslint/rules/no-new-array", + "version": "0.0.0", + "description": "ESLint rule disallowing the use of the `new Array()` constructor.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": {}, + "main": "./lib", + "directories": { + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "keywords": [ + "stdlib", + "tools", + "tool", + "eslint", + "lint", + "custom", + "rules", + "rule", + "plugin", + "constructor", + "new", + "literal", + "sparse", + "array" + ] +} diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/invalid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/invalid.js new file mode 100644 index 000000000000..f44bf3c93425 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/invalid.js @@ -0,0 +1,81 @@ +/** +* @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 invalid = []; +var test; + +test = { + 'code': [ + 'var arr = new Array();' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.', + 'type': 'NewExpression' + } + ] +}; +invalid.push( test ); + +test = { + 'code': [ + 'var arr = new Array(10);' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.', + 'type': 'NewExpression' + } + ] +}; +invalid.push( test ); + +test = { + 'code': [ + 'var arr = new Array(1, 2, 3, 4);' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.', + 'type': 'NewExpression' + } + ] +}; +invalid.push( test ); + +test = { + 'code': [ + 'function createArray() {', + ' return new Array(5);', + '}' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the `new Array()` constructor is not allowed; use an array literal with push instead.', + 'type': 'NewExpression' + } + ] +}; +invalid.push( test ); + + +// EXPORTS // + +module.exports = invalid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/unvalidated.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/unvalidated.js new file mode 100644 index 000000000000..17c2f85c5e26 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/unvalidated.js @@ -0,0 +1,51 @@ +/** +* @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 valid = []; +var test; + +test = { + 'code': [ + 'var arr = [];' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'var arr = [1, 2, 3, 4];' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'var arr = [];', + 'for (var i = 0; i < 10; i++) {', + ' arr.push(i);', + '}' + ].join( '\n' ) +}; +valid.push( test ); + + +// EXPORTS // + +module.exports = valid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/valid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/valid.js new file mode 100644 index 000000000000..a4cef75d412f --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/fixtures/valid.js @@ -0,0 +1,159 @@ +/** +* @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 valid = []; +var test; + +test = { + 'code': [ + '// MODULES //', + '', + 'var Float64Array = require( \'@stdlib/array/float64\' );', + '', + 'var arr = new Float64Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Float32Array = require( \'@stdlib/array/float32\' );', + '', + 'var arr = new Float32Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Complex64Array = require( \'@stdlib/array/complex64\' );', + '', + 'var arr = new Complex64Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Complex128Array = require( \'@stdlib/array/complex128\' );', + '', + 'var arr = new Complex128Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var BooleanArray = require( \'@stdlib/array/bool\' );', + '', + 'var arr = new BooleanArray( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Int8Array = require( \'@stdlib/array/int8\' );', + '', + 'var arr = new Int8Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Uint8Array = require( \'@stdlib/array/uint8\' );', + '', + 'var arr = new Uint8Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Uint8ClampedArray = require( \'@stdlib/array/uint8c\' );', + '', + 'var arr = new Uint8ClampedArray( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Int16Array = require( \'@stdlib/array/int16\' );', + '', + 'var arr = new Int16Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Uint16Array = require( \'@stdlib/array/uint16\' );', + '', + 'var arr = new Uint16Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Int32Array = require( \'@stdlib/array/int32\' );', + '', + 'var arr = new Int32Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + '// MODULES //', + '', + 'var Uint32Array = require( \'@stdlib/array/uint32\' );', + '', + 'var arr = new Uint32Array( 5 );' + ].join( '\n' ) +}; +valid.push( test ); + + +// EXPORTS // + +module.exports = valid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/test.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/test.js new file mode 100644 index 000000000000..782627ec4e00 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-new-array/test/test.js @@ -0,0 +1,86 @@ +/** +* @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'; + +// MODULES // + +var tape = require( 'tape' ); +var RuleTester = require( 'eslint' ).RuleTester; +var rule = require( './../lib' ); + + +// FIXTURES // + +var valid = require( './fixtures/valid.js' ); +var invalid = require( './fixtures/invalid.js' ); +var unvalidated = require( './fixtures/unvalidated.js' ); + + +// TESTS // + +tape( 'main export is an object', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof rule, 'object', 'main export is an object' ); + t.end(); +}); + +tape( 'the function positively validates code without `new Array()` constructor', function test( t ) { + var tester = new RuleTester(); + + try { + tester.run( 'no-new-array', rule, { + 'valid': valid, + 'invalid': [] + }); + t.pass( 'passed without errors' ); + } catch ( err ) { + t.fail( 'encountered an error: ' + err.message ); + } + t.end(); +}); + +tape( 'the function negatively validates code with `new Array()` constructor', function test( t ) { + var tester = new RuleTester(); + + try { + tester.run( 'no-new-array', rule, { + 'valid': [], + 'invalid': invalid + }); + t.pass( 'passed without errors' ); + } catch ( err ) { + t.fail( 'encountered an error: ' + err.message ); + } + t.end(); +}); + +tape( 'the function does not validate code with array literals', function test( t ) { + var tester = new RuleTester(); + + try { + tester.run( 'no-new-array', rule, { + 'valid': unvalidated, + 'invalid': [] + }); + t.pass( 'passed without errors' ); + } catch ( err ) { + t.fail( 'encountered an error: ' + err.message ); + } + t.end(); +});