diff --git a/etc/eslint/rules/stdlib.js b/etc/eslint/rules/stdlib.js index 58bf584b845a..a817c6297b8d 100644 --- a/etc/eslint/rules/stdlib.js +++ b/etc/eslint/rules/stdlib.js @@ -4229,6 +4229,30 @@ rules[ 'stdlib/no-internal-require' ] = 'error'; */ rules[ 'stdlib/no-multiple-empty-lines' ] = 'error'; +/** +* Disallow usage of the built-in global `BigInt` literal syntax and constructor. +* +* @name no-builtin-big-int +* @memberof rules +* @type {string} +* @default 'error' +* +* @example +* // Bad... +* var x = BigInt( 123 ); +* console.log( typeof x ); +* // => 'bigint' +* +* @example +* // Good... +* var BigInt = require( '@stdlib/bigint/ctor' ); +* +* var x = BigInt( 123 ); +* console.log( typeof x ); +* // => 'bigint' +*/ +rules[ 'stdlib/no-builtin-big-int' ] = 'error'; + /** * Disallow usage of the built-in global `Math` object. * 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 28356128cb79..e7a2c4f7ce7d 100644 --- a/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js @@ -837,6 +837,15 @@ setReadOnly( rules, 'new-cap-error', require( '@stdlib/_tools/eslint/rules/new-c */ setReadOnly( rules, 'new-cap-regexp', require( '@stdlib/_tools/eslint/rules/new-cap-regexp' ) ); +/** +* @name no-builtin-big-int +* @memberof rules +* @readonly +* @type {Function} +* @see {@link module:@stdlib/_tools/eslint/rules/no-builtin-big-int} +*/ +setReadOnly( rules, 'no-builtin-big-int', require( '@stdlib/_tools/eslint/rules/no-builtin-big-int' ) ); + /** * @name no-builtin-math * @memberof rules diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/README.md b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/README.md new file mode 100644 index 000000000000..1f9b54febdd0 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/README.md @@ -0,0 +1,125 @@ + + +# no-builtin-big-int + +> [ESLint rule][eslint-rules] disallowing the use of the built-in global `BigInt` literal syntax and constructor. + +
+ +
+ + + +
+ +## Usage + +```javascript +var rule = require( '@stdlib/_tools/eslint/rules/no-builtin-big-int' ); +``` + +#### rule + +[ESLint rule][eslint-rules] disallowing the use of the built-in global `BigInt` literal syntax and constructor. + +**Bad**: + + + + + +```javascript +var x = BigInt( 123 ); +console.log( typeof x ); +// => 'bigint' +``` + + + +**Good**: + +```javascript +var BigInt = require( '@stdlib/bigint/ctor' ); + +var x = BigInt( 123 ); +console.log( typeof x ); +// => 'bigint' +``` + +
+ + + +
+ +## Examples + +```javascript +var Linter = require( 'eslint' ).Linter; +var rule = require( '@stdlib/_tools/eslint/rules/no-builtin-big-int' ); + +var linter = new Linter(); + +var code = 'var x = 123n;'; + +linter.defineRule( 'no-builtin-big-int', rule ); + +var result = linter.verify( code, { + 'rules': { + 'no-builtin-big-int': 'error' + } +}); +/* returns + [ + { + 'ruleId': 'no-builtin-big-int', + 'severity': 2, + 'message': 'Using the built-in global `BigInt` literal syntax is not allowed; use `@stdlib/bigint/ctor` instead.', + 'line': 1, + 'column': 9, + 'nodeType': 'Literal', + 'source': 'var x = 123n;', + 'endLine': 1, + 'endColumn': 13 + } + ] +*/ +``` + + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/examples/index.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/examples/index.js new file mode 100644 index 000000000000..89b37213bd5f --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/examples/index.js @@ -0,0 +1,53 @@ +/** +* @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 x = 123n;'; + +linter.defineRule( 'no-builtin-big-int', rule ); + +var results = linter.verify( code, { + 'rules': { + 'no-builtin-big-int': 'error' + }, + 'parserOptions': { + 'ecmaVersion': 2020 + } +}); +console.log( results ); + +/* + [ + { + 'ruleId': 'no-builtin-big-int', + 'severity': 2, + 'message': 'Using the built-in global `BigInt` literal syntax is not allowed; use `@stdlib/bigint/ctor` instead.', + 'line': 1, + 'column': 9, + 'nodeType': 'Literal', + 'source': 'var x = 123n;', + 'endLine': 1, + 'endColumn': 13 + } + ] +*/ diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/lib/index.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/lib/index.js new file mode 100644 index 000000000000..27e32298dc42 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/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 built-in global `BigInt` literal syntax and constructor. +* +* @module @stdlib/_tools/eslint/rules/no-builtin-big-int +* +* @example +* var rule = require( '@stdlib/_tools/eslint/rules/no-builtin-big-int' ); +* +* console.log( rule ); +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/lib/main.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/lib/main.js new file mode 100644 index 000000000000..92aed553e2cd --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/lib/main.js @@ -0,0 +1,140 @@ +/** +* @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 // + +/** +* Checks whether using imported constructor from `@stdlib/bigint/ctor`. +* +* @private +* @param {Object} scope - scope +* @returns {boolean} boolean indicating if using imported constructor from `@stdlib/bigint/ctor` +*/ +function isImportedBigInt( scope ) { + var isNotBuiltIn; + var v; + + isNotBuiltIn = scope.set.has( 'BigInt' ); + if ( isNotBuiltIn ) { + v = scope.set.get( 'BigInt' ); + if ( + v.defs.length > 0 && + v.defs[ 0 ].node.type === 'VariableDeclarator' && + v.defs[ 0 ].node.init && + v.defs[ 0 ].node.init.type === 'CallExpression' && + v.defs[ 0 ].node.init.callee.name === 'require' && + v.defs[ 0 ].node.init.arguments[ 0 ].value === '@stdlib/bigint/ctor' + ) { + return true; + } + } + return false; +} + +/** +* Rule for disallowing the use of the built-in global `BigInt` literal syntax and constructor. +* +* @param {Object} context - ESLint context +* @returns {Object} validators +*/ +function main( context ) { + var scope = context.getScope(); + + /** + * Reports the error message. + * + * @private + * @param {ASTNode} node - node to report + * @param {string} type - node type + */ + function report( node, type ) { + var message; + + if ( type === 'Literal' ) { + message = 'Using the built-in global `BigInt` literal syntax is not allowed; use `@stdlib/bigint/ctor` instead.'; + } else if ( type === 'CallExpression' ) { + message = 'Using the built-in global `BigInt` constructor is not allowed; use `@stdlib/bigint/ctor` instead.'; + } + context.report({ + 'node': node, + 'message': message + }); + } + + /** + * Validates a BigInt literal. + * + * @private + * @param {ASTNode} node - node to examine + */ + function validateBigIntLiteral( node ) { + if ( node.bigint ) { + report( node, 'Literal' ); + } + } + + /** + * Validates a BigInt constructor. + * + * @private + * @param {ASTNode} node - node to examine + */ + function validateBigIntConstructor( node ) { + if ( + node.callee && + node.callee.type === 'Identifier' && + node.callee.name === 'BigInt' + ) { + if ( isImportedBigInt( scope ) ) { + return; + } + report( node, 'CallExpression' ); + } + } + + return { + 'Literal': validateBigIntLiteral, + 'CallExpression': validateBigIntConstructor + }; +} + + +// MAIN // + +rule = { + 'meta': { + 'type': 'problem', + 'docs': { + 'description': 'disallow the use of the built-in global `BigInt` literal syntax and constructor' + }, + 'schema': [] + }, + 'create': main +}; + + +// EXPORTS // + +module.exports = rule; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/package.json b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/package.json new file mode 100644 index 000000000000..a9fd413d2f93 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/package.json @@ -0,0 +1,55 @@ +{ + "name": "@stdlib/_tools/eslint/rules/no-builtin-big-int", + "version": "0.0.0", + "description": "ESLint rule disallowing the use of the built-in global `BigInt` literal syntax and 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", + "bigint", + "big", + "int", + "integer", + "literal", + "constructor" + ] +} diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/invalid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/invalid.js new file mode 100644 index 000000000000..4f9c737e2a85 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/invalid.js @@ -0,0 +1,94 @@ +/** +* @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 x = 123n;' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the built-in global `BigInt` literal syntax is not allowed; use `@stdlib/bigint/ctor` instead.', + 'type': 'Literal' + } + ] +}; +invalid.push( test ); + +test = { + 'code': [ + 'var x = { a: 123n }' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the built-in global `BigInt` literal syntax is not allowed; use `@stdlib/bigint/ctor` instead.', + 'type': 'Literal' + } + ] +}; +invalid.push( test ); + +test = { + 'code': [ + 'var x = { a: BigInt( 123 ) }' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the built-in global `BigInt` constructor is not allowed; use `@stdlib/bigint/ctor` instead.', + 'type': 'CallExpression' + } + ] +}; +invalid.push( test ); + +test = { + 'code': [ + 'var x = BigInt( 123 );' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the built-in global `BigInt` constructor is not allowed; use `@stdlib/bigint/ctor` instead.', + 'type': 'CallExpression' + } + ] +}; +invalid.push( test ); + +test = { + 'code': [ + 'function createBigInt() {', + ' return BigInt( 123 );', + '}' + ].join( '\n' ), + 'errors': [ + { + 'message': 'Using the built-in global `BigInt` constructor is not allowed; use `@stdlib/bigint/ctor` instead.', + 'type': 'CallExpression' + } + ] +}; +invalid.push( test ); + + +// EXPORTS // + +module.exports = invalid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/unvalidated.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/unvalidated.js new file mode 100644 index 000000000000..19a46227f162 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/unvalidated.js @@ -0,0 +1,55 @@ +/** +* @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 unvalidated = []; +var test; + +test = { + 'code': [ + 'var x = 123;' + ].join( '\n' ) +}; +unvalidated.push( test ); + +test = { + 'code': [ + 'var x = "123";' + ].join( '\n' ) +}; +unvalidated.push( test ); + +test = { + 'code': [ + 'var x = [];' + ].join( '\n' ) +}; +unvalidated.push( test ); + +test = { + 'code': [ + 'var x = {};' + ].join( '\n' ) +}; +unvalidated.push( test ); + + +// EXPORTS // + +module.exports = unvalidated; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/valid.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/valid.js new file mode 100644 index 000000000000..303e6013c81d --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/fixtures/valid.js @@ -0,0 +1,65 @@ +/** +* @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 BigInt = require( \'@stdlib/bigint/ctor\' );', + '', + 'var x = BigInt( 123 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'var BigInt = require( \'@stdlib/bigint/ctor\' );', + '', + 'function createBigInt() {', + ' return BigInt( 123 );', + '}' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'var BigInt = require( \'@stdlib/bigint/ctor\' );', + '', + 'var x = BigInt( 123 );' + ].join( '\n' ) +}; +valid.push( test ); + +test = { + 'code': [ + 'var BigInt = require( \'@stdlib/bigint/ctor\' );', + '', + 'var x = { a: BigInt( 123 ) };' + ].join( '\n' ) +}; +valid.push( test ); + + +// EXPORTS // + +module.exports = valid; diff --git a/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/test.js b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/test.js new file mode 100644 index 000000000000..bc3305141a44 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/eslint/rules/no-builtin-big-int/test/test.js @@ -0,0 +1,98 @@ +/** +* @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 the built-in global `BigInt` constructor or the BigInt literal syntax', function test( t ) { + var tester = new RuleTester({ + 'parserOptions': { + 'ecmaVersion': 2020 + } + }); + + try { + tester.run( 'no-builtin-big-int', 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 the built-in global `BigInt` constructor or the BigInt literal syntax', function test( t ) { + var tester = new RuleTester({ + 'parserOptions': { + 'ecmaVersion': 2020 + } + }); + + try { + tester.run( 'no-builtin-big-int', 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 without global `BigInt`s', function test( t ) { + var tester = new RuleTester({ + 'parserOptions': { + 'ecmaVersion': 2020 + } + }); + + try { + tester.run( 'no-builtin-big-int', rule, { + 'valid': unvalidated, + 'invalid': [] + }); + t.pass( 'passed without errors' ); + } catch ( err ) { + t.fail( 'encountered an error: ' + err.message ); + } + t.end(); +});