From a766b97b4b8de3f9a2f06827a86c97b18a1a8677 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 23 Dec 2024 02:20:45 +0500 Subject: [PATCH 1/4] feat: add `eslint/rules/eol-open-bracket-spacing` PR-URL: https://github.com/stdlib-js/stdlib/pull/4061 Private-ref: https://github.com/stdlib-js/todo/issues/2324 Reviewed-by: Athan Reines --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: passed - task: run_c_examples status: passed - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: failed --- --- etc/eslint/rules/stdlib.js | 46 ++++ .../rules/eol-open-bracket-spacing/README.md | 163 +++++++++++++ .../examples/index.js | 66 +++++ .../eol-open-bracket-spacing/lib/index.js | 39 +++ .../eol-open-bracket-spacing/lib/main.js | 156 ++++++++++++ .../eol-open-bracket-spacing/package.json | 63 +++++ .../test/fixtures/invalid.js | 230 ++++++++++++++++++ .../test/fixtures/unvalidated.js | 41 ++++ .../test/fixtures/valid.js | 97 ++++++++ .../eol-open-bracket-spacing/test/test.js | 86 +++++++ .../@stdlib/_tools/eslint/rules/lib/index.js | 9 + 11 files changed, 996 insertions(+) create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/README.md create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/examples/index.js create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/index.js create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/main.js create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/package.json create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/invalid.js create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/unvalidated.js create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/valid.js create mode 100644 lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/test.js diff --git a/etc/eslint/rules/stdlib.js b/etc/eslint/rules/stdlib.js index 5304d3895a5c..0de07ac30f90 100644 --- a/etc/eslint/rules/stdlib.js +++ b/etc/eslint/rules/stdlib.js @@ -200,6 +200,52 @@ rules[ 'stdlib/doctest-quote-props' ] = 'error'; */ rules[ 'stdlib/empty-line-before-comment' ] = 'error'; +/** +* Disallow spaces between an opening parenthesis or bracket and a nested object or array expression at the end of a line. +* +* @name eol-open-bracket-spacing +* @memberof rules +* @type {string} +* @default 'error' +* +* @example +* // Bad... +* var log = require( '@stdlib/console/log' ); +* +* log( { +* 'foo': true +* }); +* +* log( [ +* 1, +* 2, +* 3 +* ]); +* +* log( [ { +* 'bar': true +* }]); +* +* @example +* // Good... +* var log = require( '@stdlib/console/log' ); +* +* log({ +* 'foo': true +* }); +* +* log([ +* 1, +* 2, +* 3 +* ]); +* +* log([{ +* 'bar': true +* }]); +*/ +rules[ 'stdlib/eol-open-bracket-spacing' ] = 'error'; + /** * Require blockquotes to have `2` character indentation. * diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/README.md b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/README.md new file mode 100644 index 000000000000..721f820dae72 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/README.md @@ -0,0 +1,163 @@ + + +# eol-open-bracket-spacing + +> [ESLint rule][eslint-rules] to enforce that no spaces are present between an opening parenthesis or bracket and a nested object or array expression at the end of a line. + +
+ +
+ + + +
+ +## Usage + +```javascript +var rule = require( '@stdlib/_tools/eslint/rules/eol-open-bracket-spacing' ); +``` + +#### rule + +[ESLint rule][eslint-rules] to enforce that no spaces are present between an opening parenthesis or bracket and a nested object or array expression at the end of a line. + +**Bad**: + + + +```javascript +var log = require( '@stdlib/console/log' ); + +log( { + 'foo': true +}); + +log( [ + 1, + 2, + 3 +]); + +log( [ { + 'bar': true +}]); +``` + +**Good**: + +```javascript +var log = require( '@stdlib/console/log' ); + +log({ + 'foo': true +}); + +log([ + 1, + 2, + 3 +]); + +log([{ + 'bar': true +}]); +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Linter = require( 'eslint' ).Linter; +var rule = require( '@stdlib/_tools/eslint/rules/eol-open-bracket-spacing' ); + +var linter = new Linter(); + +var code = [ + 'function test() {', + ' var log = require( \'@stdlib/console/log\' );', + ' log( {', + ' "key": "value"', + ' });', + ' var arr = [ {', + ' "nested": true', + ' }];', + ' log( arr );', + '}' +].join( '\n' ); + +linter.defineRule( 'eol-open-bracket-spacing', rule ); + +var result = linter.verify( code, { + 'rules': { + 'eol-open-bracket-spacing': 'error' + } +}); +/* returns + [ + { + 'ruleId': 'eol-open-bracket-spacing', + 'severity': 2, + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'line': 3, + 'column': 3, + 'nodeType': 'CallExpression' + }, + { + 'ruleId': 'eol-open-bracket-spacing', + 'severity': 2, + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'line': 6, + 'column': 13, + 'nodeType': 'ArrayExpression' + } + ] +*/ +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/examples/index.js b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/examples/index.js new file mode 100644 index 000000000000..9645f73f936d --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/examples/index.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 = [ + 'function test() {', + ' var log = require( \'@stdlib/console/log\' );', + ' log( {', + ' "key": "value"', + ' });', + ' var arr = [ {', + ' "nested": true', + ' }];', + ' log( arr );', + '}' +].join( '\n' ); + +linter.defineRule( 'eol-open-bracket-spacing', rule ); + +var result = linter.verify( code, { + 'rules': { + 'eol-open-bracket-spacing': 'error' + } +}); +console.log( result ); +/* => + [ + { + 'ruleId': 'eol-open-bracket-spacing', + 'severity': 2, + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'line': 3, + 'column': 3, + 'nodeType': 'CallExpression' + }, + { + 'ruleId': 'eol-open-bracket-spacing', + 'severity': 2, + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'line': 6, + 'column': 13, + 'nodeType': 'ArrayExpression' + } + ] +*/ diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/index.js b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/index.js new file mode 100644 index 000000000000..2353d04e43c6 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 to enforce that no spaces are present between an opening parenthesis or bracket and a nested object or array expression at the end of a line. +* +* @module @stdlib/_tools/eslint/rules/eol-open-bracket-spacing +* +* @example +* var rule = require( '@stdlib/_tools/eslint/rules/eol-open-bracket-spacing' ); +* +* console.log( rule ); +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/main.js new file mode 100644 index 000000000000..59474cccfef8 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/main.js @@ -0,0 +1,156 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 validating that no spaces are present between an opening parenthesis or bracket and a nested object or array expression at the end of a line. +* +* @private +* @param {Object} context - ESLint context +* @returns {Object} validators +*/ +function main( context ) { + var source = context.getSourceCode(); + + /** + * Reports the error message. + * + * @private + * @param {ASTNode} node - node to report + * @param {Object} prevToken - token before the space + * @param {Object} tokenAfter - token after the space + */ + function report( node, prevToken, tokenAfter ) { + context.report({ + 'node': node, + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'fix': fix + }); + + /** + * Fixes lint the error by removing the space before the object or array expression. + * + * @private + * @param {Object} fixer - ESLint fixer + * @returns {Object} fix + */ + function fix( fixer ) { + var afterIdx; + var prevIdx; + + prevIdx = source.getIndexFromLoc( prevToken.loc.end ); + afterIdx = source.getIndexFromLoc( tokenAfter.loc.start ); + return fixer.replaceTextRange( [ prevIdx, afterIdx ], '' ); + } + } + + /** + * Checks whether there are spaces present between an opening parenthesis or bracket and a nested object or array expression at the end of a line. + * + * @private + * @param {ASTNode} node - node to examine + */ + function validate( node ) { + var tokenAfter; + var nextToken; + var prevToken; + var elem; + var args; + + if ( + node.type === 'CallExpression' && + node.arguments.length > 0 + ) { + args = node.arguments; + if ( args[ 0 ].type === 'ObjectExpression' ) { + prevToken = source.getTokenBefore( args[ 0 ] ); + tokenAfter = source.getFirstToken( args[ 0 ] ); + if ( source.isSpaceBetween( prevToken, tokenAfter ) ) { + report( node, prevToken, tokenAfter ); + } + } else if ( args[ 0 ].type === 'ArrayExpression' ) { + elem = args[ 0 ].elements[ 0 ]; + if ( elem.type === 'ObjectExpression' ) { + prevToken = source.getTokenBefore( args[ 0 ] ); + tokenAfter = source.getFirstToken( args[ 0 ] ); + if ( source.isSpaceBetween( prevToken, tokenAfter ) ) { + report( node, prevToken, tokenAfter ); + } + } else { + prevToken = source.getTokenBefore( args[ 0 ] ); + tokenAfter = source.getFirstToken( args[ 0 ] ); + nextToken = source.getTokenAfter( tokenAfter ); + if ( + tokenAfter.loc.end.line !== nextToken.loc.end.line && + source.isSpaceBetween( prevToken, tokenAfter ) + ) { + report( node, prevToken, tokenAfter ); + } + } + } + } + + if ( + node.type === 'ArrayExpression' && + node.elements.length > 0 + ) { + elem = node.elements[ 0 ]; + if ( elem.type === 'ObjectExpression' ) { + prevToken = source.getFirstToken( node ); + tokenAfter = source.getFirstToken( elem ); + + if ( source.isSpaceBetween( prevToken, tokenAfter ) ) { + report( node, prevToken, tokenAfter ); + } + } + } + } + + return { + 'CallExpression': validate, + 'ArrayExpression': validate + }; +} + + +// MAIN // + +rule = { + 'meta': { + 'type': 'layout', + 'docs': { + 'description': 'disallow spaces between an opening parenthesis or bracket and a nested object or array expression at the end of a line' + }, + 'schema': [], + 'fixable': 'whitespace' + }, + 'create': main +}; + + +// EXPORTS // + +module.exports = rule; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/package.json b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/package.json new file mode 100644 index 000000000000..4773e06bbbbd --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/_tools/eslint/rules/eol-open-bracket-spacing", + "version": "0.0.0", + "description": "ESLint rule to enforce that no spaces are present between an opening parenthesis or bracket and a nested object or array expression at the end of a line.", + "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" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "tools", + "tool", + "eslint", + "lint", + "custom", + "rules", + "rule", + "array", + "object", + "plugin", + "whitespace" + ] +} diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/invalid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/invalid.js new file mode 100644 index 000000000000..30fe652a99aa --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/invalid.js @@ -0,0 +1,230 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 = [ {', + ' key1: "value1",', + ' key2: "value2"', + '}];' + ].join( '\n' ), + 'errors': [{ + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }], + 'output': [ + 'var arr = [{', + ' key1: "value1",', + ' key2: "value2"', + '}];' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'function sum( x ) {', + ' var sum = 0;', + ' for ( var i = 0; i < x.length; i++ ) {', + ' sum += x[ i ];', + ' }', + ' return sum;', + '}', + 'var arrSum = sum( [', + ' 1,', + ' 2,', + ' 3,', + ' 4', + ']);' + ].join( '\n' ), + 'errors': [{ + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }], + 'output': [ + 'function sum( x ) {', + ' var sum = 0;', + ' for ( var i = 0; i < x.length; i++ ) {', + ' sum += x[ i ];', + ' }', + ' return sum;', + '}', + 'var arrSum = sum([', + ' 1,', + ' 2,', + ' 3,', + ' 4', + ']);' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'function log( x ) {', + ' for ( var i = 0; i < x.length; i++ ) {', + ' console.log( x[ i ] );', + ' }', + '}', + 'log( [{', + ' a: 1,', + ' b: 2', + '}]);' + ].join( '\n' ), + 'errors': [{ + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }], + 'output': [ + 'function log( x ) {', + ' for ( var i = 0; i < x.length; i++ ) {', + ' console.log( x[ i ] );', + ' }', + '}', + 'log([{', + ' a: 1,', + ' b: 2', + '}]);' + ].join( '\n' ) +}; +valid.push( test); + +test = { + 'code': [ + 'function log( x ) {', + ' for ( var i = 0; i < x.length; i++ ) {', + ' console.log( x[ i ] );', + ' }', + '}', + 'log([ {', + ' a: 1,', + ' b: 2', + '}]);' + ].join( '\n' ), + 'errors': [{ + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }], + 'output': [ + 'function log( x ) {', + ' for ( var i = 0; i < x.length; i++ ) {', + ' console.log( x[ i ] );', + ' }', + '}', + 'log([{', + ' a: 1,', + ' b: 2', + '}]);' + ].join( '\n' ) +}; +valid.push( test); + +test = { + 'code': [ + 'function log( x ) {', + ' for ( var i = 0; i < x.length; i++ ) {', + ' console.log( x[ i ] );', + ' }', + '}', + 'log( [ {', + ' a: 1,', + ' b: 2', + '}]);' + ].join( '\n' ), + 'errors': [{ + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }, { + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }], + 'output': [ + 'function log( x ) {', + ' for ( var i = 0; i < x.length; i++ ) {', + ' console.log( x[ i ] );', + ' }', + '}', + 'log([{', + ' a: 1,', + ' b: 2', + '}]);' + ].join( '\n' ) +}; +valid.push( test); + +test = { + 'code': [ + 'console.log( [', + ' Math.random(),', + ' Math.random()', + ']);', + '/* e.g., =>', + ' [', + ' 0.2580887012988746,', + ' 0.128454513229588', + ' ]', + '*/' + ].join( '\n' ), + 'errors': [{ + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }], + 'output': [ + 'console.log([', + ' Math.random(),', + ' Math.random()', + ']);', + '/* e.g., =>', + ' [', + ' 0.2580887012988746,', + ' 0.128454513229588', + ' ]', + '*/' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'console.log( {', + ' a: Math.random(),', + ' b: Math.random()', + '});' + ].join( '\n' ), + 'errors': [{ + 'message': 'No spaces allowed between an opening parenthesis or bracket and a nested object or array expression at the end of a line', + 'type': null + }], + 'output': [ + 'console.log({', + ' a: Math.random(),', + ' b: Math.random()', + '});' + ].join( '\n' ) +}; +valid.push( test ); + + +// EXPORTS // + +module.exports = valid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/unvalidated.js b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/unvalidated.js new file mode 100644 index 000000000000..c6c33b1faa84 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/unvalidated.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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': [ + 'function sum( x ) {', + ' var sum = 0;', + ' for ( var i = 0; i < x.length; i++ ) {', + ' sum += x[ i ];', + ' }', + ' return sum;', + '}', + 'var arrSum = sum( [ 1, 2, 3, 4 ] );' + ].join( '\n' ) +}; +valid.push( test); + + +// EXPORTS // + +module.exports = valid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/valid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/valid.js new file mode 100644 index 000000000000..761141fd2b28 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/fixtures/valid.js @@ -0,0 +1,97 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 = [{', + ' key1: "value1",', + ' key2: "value2"', + '}];' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'function sum( x ) {', + ' var sum = 0;', + ' for ( var i = 0; i < x.length; i++ ) {', + ' sum += x[ i ];', + ' }', + ' return sum;', + '}', + 'var arrSum = sum([', + ' 1,', + ' 2,', + ' 3,', + ' 4', + ']);' + ].join( '\n' ) +}; +valid.push( test); + +test = { + 'code': [ + 'function log( x ) {', + ' for ( var i = 0; i < x.length; i++ ) {', + ' console.log( x[ i ] );', + ' }', + '}', + 'log([{', + ' a: 1,', + ' b: 2', + '}]);' + ].join( '\n' ) +}; +valid.push( test); + +test = { + 'code': [ + 'console.log([', + ' Math.random(),', + ' Math.random()', + ']);', + '/* e.g., =>', + ' [', + ' 0.2580887012988746,', + ' 0.128454513229588', + ' ]', + '*/' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'console.log({', + ' a: Math.random(),', + ' b: Math.random()', + '});' + ].join( '\n' ) +}; +valid.push( test ); + + +// EXPORTS // + +module.exports = valid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/test.js b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/test.js new file mode 100644 index 000000000000..43153651efa8 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/test/test.js @@ -0,0 +1,86 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 where no spaces are present between an opening parenthesis or bracket and a nested object or array expression at the end of a line', function test( t ) { + var tester = new RuleTester(); + + try { + tester.run( 'eol-open-bracket-spacing', 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 where spaces are present between an opening parenthesis or bracket and a nested object or array expression at the end of a line', function test( t ) { + var tester = new RuleTester(); + + try { + tester.run( 'eol-open-bracket-spacing', 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 non-object or non-array or inline array expressions', function test( t ) { + var tester = new RuleTester(); + + try { + tester.run( 'eol-open-bracket-spacing', rule, { + 'valid': unvalidated, + 'invalid': [] + }); + t.pass( 'passed without errors' ); + } catch ( err ) { + t.fail( 'encountered an error: ' + err.message ); + } + t.end(); +}); 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 3a8df9a27024..221f82484546 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js @@ -81,6 +81,15 @@ setReadOnly( rules, 'doctest-quote-props', require( '@stdlib/_tools/eslint/rules */ setReadOnly( rules, 'empty-line-before-comment', require( '@stdlib/_tools/eslint/rules/empty-line-before-comment' ) ); +/** +* @name eol-open-bracket-spacing +* @memberof rules +* @readonly +* @type {Function} +* @see {@link module:@stdlib/_tools/eslint/rules/eol-open-bracket-spacing} +*/ +setReadOnly( rules, 'eol-open-bracket-spacing', require( '@stdlib/_tools/eslint/rules/eol-open-bracket-spacing' ) ); + /** * @name first-unit-test * @memberof rules From 87298ff88224ac02c1187d07a24802185d9c8112 Mon Sep 17 00:00:00 2001 From: Vinit Pandit Date: Tue, 24 Dec 2024 21:01:05 +0530 Subject: [PATCH 2/4] refactor: update stats/base/svariance native addon from C++ to C --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/stats/base/svariance/include.gypi | 2 +- .../stats/base/svariance/manifest.json | 43 +++++- .../@stdlib/stats/base/svariance/src/addon.c | 46 +++++++ .../stats/base/svariance/src/addon.cpp | 130 ------------------ 4 files changed, 89 insertions(+), 132 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/svariance/src/addon.c delete mode 100644 lib/node_modules/@stdlib/stats/base/svariance/src/addon.cpp diff --git a/lib/node_modules/@stdlib/stats/base/svariance/include.gypi b/lib/node_modules/@stdlib/stats/base/svariance/include.gypi index 868c5c12e852..26476a8c2655 100644 --- a/lib/node_modules/@stdlib/stats/base/svariance/include.gypi +++ b/lib/node_modules/@stdlib/stats/base/svariance/include.gypi @@ -36,7 +36,7 @@ # Source files: 'src_files': [ - '<(src_dir)/addon.cpp', + '<(src_dir)/addon.c', ' + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 4 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_FLOAT( env, correction, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, stride, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, stride, argv, 2) + STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_svariance( N, correction, X, stride ), v ); + return v; +} + + +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/svariance/src/addon.cpp b/lib/node_modules/@stdlib/stats/base/svariance/src/addon.cpp deleted file mode 100644 index 703559ceb10e..000000000000 --- a/lib/node_modules/@stdlib/stats/base/svariance/src/addon.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 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. -*/ - -#include "stdlib/stats/base/svariance.h" -#include -#include -#include -#include -#include - -/** -* Add-on namespace. -*/ -namespace stdlib_stats_base_svariance { - - /** - * Computes the variance of a single-precision floating-point strided array. - * - * ## Notes - * - * - When called from JavaScript, the function expects four arguments: - * - * - `N`: number of indexed elements - * - `correction`: degrees of freedom adjustment - * - `X`: input array - * - `stride`: stride length - */ - napi_value node_svariance( napi_env env, napi_callback_info info ) { - napi_status status; - - size_t argc = 4; - napi_value argv[ 4 ]; - status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); - assert( status == napi_ok ); - - if ( argc < 4 ) { - napi_throw_error( env, nullptr, "invalid invocation. Must provide 4 arguments." ); - return nullptr; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); - return nullptr; - } - - napi_valuetype vtype1; - status = napi_typeof( env, argv[ 1 ], &vtype1 ); - assert( status == napi_ok ); - if ( vtype1 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a number." ); - return nullptr; - } - - bool res; - status = napi_is_typedarray( env, argv[ 2 ], &res ); - assert( status == napi_ok ); - if ( res == false ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float32Array." ); - return nullptr; - } - - napi_valuetype vtype3; - status = napi_typeof( env, argv[ 3 ], &vtype3 ); - assert( status == napi_ok ); - if ( vtype3 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a number." ); - return nullptr; - } - - int64_t N; - status = napi_get_value_int64( env, argv[ 0 ], &N ); - assert( status == napi_ok ); - - double correction; - status = napi_get_value_double( env, argv[ 1 ], &correction ); - assert( status == napi_ok ); - - int64_t stride; - status = napi_get_value_int64( env, argv[ 3 ], &stride ); - assert( status == napi_ok ); - - napi_typedarray_type vtype2; - size_t xlen; - void *X; - status = napi_get_typedarray_info( env, argv[ 2 ], &vtype2, &xlen, &X, nullptr, nullptr ); - assert( status == napi_ok ); - if ( vtype2 != napi_float32_array ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float32Array." ); - return nullptr; - } - if ( (N-1)*llabs(stride) >= (int64_t)xlen ) { - napi_throw_range_error( env, nullptr, "invalid argument. Third argument has insufficient elements based on the associated stride and the number of indexed elements." ); - return nullptr; - } - - napi_value v; - status = napi_create_double( env, (double)stdlib_strided_svariance( N, (float)correction, (float *)X, stride ), &v ); - assert( status == napi_ok ); - - return v; - } - - napi_value Init( napi_env env, napi_value exports ) { - napi_status status; - napi_value fcn; - status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_svariance, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; - } - - NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) -} // end namespace stdlib_stats_base_svariance From b3544706c274045787ca7e3518ff2139d8fb3e3e Mon Sep 17 00:00:00 2001 From: Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> Date: Wed, 25 Dec 2024 14:04:56 +0530 Subject: [PATCH 3/4] fix: adding suggested changes Co-authored-by: Athan Signed-off-by: Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> --- lib/node_modules/@stdlib/stats/base/svariance/src/addon.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c b/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c index b12c32385dd9..3ae3806529cf 100644 --- a/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c +++ b/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c @@ -38,9 +38,8 @@ static napi_value addon( napi_env env, napi_callback_info info ) { STDLIB_NAPI_ARGV_FLOAT( env, correction, argv, 1 ); STDLIB_NAPI_ARGV_INT64( env, stride, argv, 3 ); STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, stride, argv, 2) - STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_svariance( N, correction, X, stride ), v ); + STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_strided_svariance( N, correction, X, stride ), v ); return v; } - STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) \ No newline at end of file From fefc74edaa4b62384b9e8231b63dc29ac505b455 Mon Sep 17 00:00:00 2001 From: Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> Date: Wed, 25 Dec 2024 14:10:25 +0530 Subject: [PATCH 4/4] refactor: add trailing new line Signed-off-by: Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> --- lib/node_modules/@stdlib/stats/base/svariance/src/addon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c b/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c index 3ae3806529cf..7f8ff92eaa2f 100644 --- a/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c +++ b/lib/node_modules/@stdlib/stats/base/svariance/src/addon.c @@ -42,4 +42,4 @@ static napi_value addon( napi_env env, napi_callback_info info ) { return v; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) \ No newline at end of file +STDLIB_NAPI_MODULE_EXPORT_FCN( addon )