forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefer-number-properties.js
133 lines (114 loc) · 2.9 KB
/
prefer-number-properties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
const {ReferenceTracker} = require('eslint-utils');
const {replaceReferenceIdentifier} = require('./fix/index.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');
const MESSAGE_ID_ERROR = 'error';
const MESSAGE_ID_SUGGESTION = 'suggestion';
const messages = {
[MESSAGE_ID_ERROR]: 'Prefer `Number.{{property}}` over `{{description}}`.',
[MESSAGE_ID_SUGGESTION]: 'Replace `{{description}}` with `Number.{{property}}`.',
};
const globalObjects = {
// Safe to replace with `Number` properties
parseInt: true,
parseFloat: true,
NaN: true,
Infinity: true,
// Unsafe to replace with `Number` properties
isNaN: false,
isFinite: false,
};
const isNegative = node => {
const {parent} = node;
return parent && parent.type === 'UnaryExpression' && parent.operator === '-' && parent.argument === node;
};
function * checkProperties({sourceCode, tracker, checkInfinity}) {
let names = Object.keys(globalObjects);
if (!checkInfinity) {
names = names.filter(name => name !== 'Infinity');
}
const traceMap = Object.fromEntries(
names.map(name => [name, {[ReferenceTracker.READ]: true}]),
);
for (const {node, path: [name]} of tracker.iterateGlobalReferences(traceMap)) {
const {parent} = node;
let property = name;
if (name === 'Infinity') {
property = isNegative(node) ? 'NEGATIVE_INFINITY' : 'POSITIVE_INFINITY';
}
const problem = {
node,
messageId: MESSAGE_ID_ERROR,
data: {
description: name,
property,
},
};
if (property === 'NEGATIVE_INFINITY') {
problem.node = parent;
problem.data.description = '-Infinity';
problem.fix = function * (fixer) {
yield fixer.replaceText(parent, 'Number.NEGATIVE_INFINITY');
yield * fixSpaceAroundKeyword(fixer, parent, sourceCode);
};
yield problem;
continue;
}
const fix = fixer => replaceReferenceIdentifier(node, `Number.${property}`, fixer, sourceCode);
const isSafeToFix = globalObjects[name];
if (isSafeToFix) {
problem.fix = fix;
} else {
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
data: problem.data,
fix,
},
];
}
yield problem;
}
}
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const {
checkInfinity,
} = {
checkInfinity: true,
...context.options[0],
};
return {
* 'Program:exit'() {
const sourceCode = context.getSourceCode();
const tracker = new ReferenceTracker(context.getScope());
yield * checkProperties({sourceCode, tracker, checkInfinity});
},
};
};
const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
checkInfinity: {
type: 'boolean',
default: true,
},
},
},
];
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Prefer `Number` static properties over global ones.',
},
fixable: 'code',
hasSuggestions: true,
schema,
messages,
},
};