Skip to content

Commit 41fd19a

Browse files
authored
build: add an ESLint rule to disallow global BigInt literal and constructor
PR-URL: #5742 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 406337f commit 41fd19a

File tree

11 files changed

+757
-0
lines changed

11 files changed

+757
-0
lines changed

etc/eslint/rules/stdlib.js

+24
Original file line numberDiff line numberDiff line change
@@ -4229,6 +4229,30 @@ rules[ 'stdlib/no-internal-require' ] = 'error';
42294229
*/
42304230
rules[ 'stdlib/no-multiple-empty-lines' ] = 'error';
42314231

4232+
/**
4233+
* Disallow usage of the built-in global `BigInt` literal syntax and constructor.
4234+
*
4235+
* @name no-builtin-big-int
4236+
* @memberof rules
4237+
* @type {string}
4238+
* @default 'error'
4239+
*
4240+
* @example
4241+
* // Bad...
4242+
* var x = BigInt( 123 );
4243+
* console.log( typeof x );
4244+
* // => 'bigint'
4245+
*
4246+
* @example
4247+
* // Good...
4248+
* var BigInt = require( '@stdlib/bigint/ctor' );
4249+
*
4250+
* var x = BigInt( 123 );
4251+
* console.log( typeof x );
4252+
* // => 'bigint'
4253+
*/
4254+
rules[ 'stdlib/no-builtin-big-int' ] = 'error';
4255+
42324256
/**
42334257
* Disallow usage of the built-in global `Math` object.
42344258
*

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

+9
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,15 @@ setReadOnly( rules, 'new-cap-error', require( '@stdlib/_tools/eslint/rules/new-c
837837
*/
838838
setReadOnly( rules, 'new-cap-regexp', require( '@stdlib/_tools/eslint/rules/new-cap-regexp' ) );
839839

840+
/**
841+
* @name no-builtin-big-int
842+
* @memberof rules
843+
* @readonly
844+
* @type {Function}
845+
* @see {@link module:@stdlib/_tools/eslint/rules/no-builtin-big-int}
846+
*/
847+
setReadOnly( rules, 'no-builtin-big-int', require( '@stdlib/_tools/eslint/rules/no-builtin-big-int' ) );
848+
840849
/**
841850
* @name no-builtin-math
842851
* @memberof rules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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-builtin-big-int
22+
23+
> [ESLint rule][eslint-rules] disallowing the use of the built-in global `BigInt` literal syntax and 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-builtin-big-int' );
37+
```
38+
39+
#### rule
40+
41+
[ESLint rule][eslint-rules] disallowing the use of the built-in global `BigInt` literal syntax and constructor.
42+
43+
**Bad**:
44+
45+
<!-- eslint-disable stdlib/no-builtin-big-int -->
46+
47+
<!-- eslint-disable stdlib/require-globals -->
48+
49+
```javascript
50+
var x = BigInt( 123 );
51+
console.log( typeof x );
52+
// => 'bigint'
53+
```
54+
55+
<!-- eslint-enable stdlib/require-globals -->
56+
57+
**Good**:
58+
59+
```javascript
60+
var BigInt = require( '@stdlib/bigint/ctor' );
61+
62+
var x = BigInt( 123 );
63+
console.log( typeof x );
64+
// => 'bigint'
65+
```
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
```javascript
76+
var Linter = require( 'eslint' ).Linter;
77+
var rule = require( '@stdlib/_tools/eslint/rules/no-builtin-big-int' );
78+
79+
var linter = new Linter();
80+
81+
var code = 'var x = 123n;';
82+
83+
linter.defineRule( 'no-builtin-big-int', rule );
84+
85+
var result = linter.verify( code, {
86+
'rules': {
87+
'no-builtin-big-int': 'error'
88+
}
89+
});
90+
/* returns
91+
[
92+
{
93+
'ruleId': 'no-builtin-big-int',
94+
'severity': 2,
95+
'message': 'Using the built-in global `BigInt` literal syntax is not allowed; use `@stdlib/bigint/ctor` instead.',
96+
'line': 1,
97+
'column': 9,
98+
'nodeType': 'Literal',
99+
'source': 'var x = 123n;',
100+
'endLine': 1,
101+
'endColumn': 13
102+
}
103+
]
104+
*/
105+
```
106+
107+
<!-- /.examples -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules
122+
123+
</section>
124+
125+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
var code = 'var x = 123n;';
26+
27+
linter.defineRule( 'no-builtin-big-int', rule );
28+
29+
var results = linter.verify( code, {
30+
'rules': {
31+
'no-builtin-big-int': 'error'
32+
},
33+
'parserOptions': {
34+
'ecmaVersion': 2020
35+
}
36+
});
37+
console.log( results );
38+
39+
/*
40+
[
41+
{
42+
'ruleId': 'no-builtin-big-int',
43+
'severity': 2,
44+
'message': 'Using the built-in global `BigInt` literal syntax is not allowed; use `@stdlib/bigint/ctor` instead.',
45+
'line': 1,
46+
'column': 9,
47+
'nodeType': 'Literal',
48+
'source': 'var x = 123n;',
49+
'endLine': 1,
50+
'endColumn': 13
51+
}
52+
]
53+
*/
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 built-in global `BigInt` literal syntax and constructor.
23+
*
24+
* @module @stdlib/_tools/eslint/rules/no-builtin-big-int
25+
*
26+
* @example
27+
* var rule = require( '@stdlib/_tools/eslint/rules/no-builtin-big-int' );
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;

0 commit comments

Comments
 (0)