[guide] [eslint config] [base] [breaking] Rules prohibiting global isNaN, isFinite.

- Update README with new Standard Library section.
This commit is contained in:
David Walsh
2017-07-04 12:40:53 +10:00
committed by Jordan Harband
parent 5dec8272e0
commit f5cd2869d3
3 changed files with 68 additions and 4 deletions

View File

@@ -44,6 +44,7 @@ Other Style Guides
1. [jQuery](#jquery)
1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility)
1. [ECMAScript 6+ (ES 2015+) Styles](#ecmascript-6-es-2015-styles)
1. [Standard Library](#standard-library)
1. [Testing](#testing)
1. [Performance](#performance)
1. [Resources](#resources)
@@ -3148,10 +3149,50 @@ Other Style Guides
**[⬆ back to top](#table-of-contents)**
## Standard Library
The [Standard Library](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects)
contains utilities that are functionally broken but remain for legacy reasons.
<a name="standard-library--isnan"></a>
- [29.1](#standard-library--isnan) Use `Number.isNaN` instead of global `isNaN`.
eslint: [`no-restricted-globals`](http://eslint.org/docs/rules/no-restricted-globals)
> Why? The global `isNaN` coerces non-numbers to numbers, returning true for anything that coerces to NaN.
If this behavior is desired, make it explicit.
```javascript
// bad
isNaN('1.2'); // false
isNaN('1.2.3'); // true
// good
Number.isNaN('1.2.3'); // false
Number.isNaN(Number('1.2.3')); // true
```
<a name="standard-library--isfinite"></a>
- [29.2](#standard-library--isfinite) Use `Number.isFinite` instead of global `isFinite`.
eslint: [`no-restricted-globals`](http://eslint.org/docs/rules/no-restricted-globals)
> Why? The global `isFinite` coerces non-numbers to numbers, returning true for anything that coerces to a finite number.
If this behavior is desired, make it explicit.
```javascript
// bad
isFinite('2e3'); // true
// good
Number.isFinite('2e3'); // false
Number.isFinite(parseInt('2e3', 10)); // true
```
**[⬆ back to top](#table-of-contents)**
## Testing
<a name="testing--yup"></a><a name="28.1"></a>
- [29.1](#testing--yup) **Yup.**
- [30.1](#testing--yup) **Yup.**
```javascript
function foo() {
@@ -3160,7 +3201,7 @@ Other Style Guides
```
<a name="testing--for-real"></a><a name="28.2"></a>
- [29.2](#testing--for-real) **No, but seriously**:
- [30.2](#testing--for-real) **No, but seriously**:
- Whichever testing framework you use, you should be writing tests!
- Strive to write many small pure functions, and minimize where mutations occur.
- Be cautious about stubs and mocks - they can make your tests more brittle.

View File

@@ -194,6 +194,30 @@ module.exports = {
object: 'arguments',
property: 'callee',
message: 'arguments.callee is deprecated',
}, {
object: 'global',
property: 'isFinite',
message: 'Please use Number.isFinite instead',
}, {
object: 'self',
property: 'isFinite',
message: 'Please use Number.isFinite instead',
}, {
object: 'window',
property: 'isFinite',
message: 'Please use Number.isFinite instead',
}, {
object: 'global',
property: 'isNaN',
message: 'Please use Number.isNaN instead',
}, {
object: 'self',
property: 'isNaN',
message: 'Please use Number.isNaN instead',
}, {
object: 'window',
property: 'isNaN',
message: 'Please use Number.isNaN instead',
}, {
property: '__defineGetter__',
message: 'Please use Object.defineProperty instead.',

View File

@@ -16,8 +16,7 @@ module.exports = {
'no-label-var': 'error',
// disallow specific globals
// TODO: enable, semver-major
'no-restricted-globals': ['off'].concat(restrictedGlobals),
'no-restricted-globals': ['error', 'isFinite', 'isNaN'].concat(restrictedGlobals),
// disallow declaration of variables already declared in the outer scope
'no-shadow': 'error',