-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathprefer-number-properties.js
140 lines (120 loc) · 2.94 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
134
135
136
137
138
139
140
'use strict';
const {GlobalReferenceTracker} = require('./utils/global-reference-tracker.js');
const {replaceReferenceIdentifier} = require('./fix/index.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');
const isLeftHandSide = require('./utils/is-left-hand-side.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.type === 'UnaryExpression' && parent.operator === '-' && parent.argument === node;
};
function checkProperty({node, path: [name]}, sourceCode) {
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);
};
return problem;
}
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,
fix,
},
];
}
return problem;
}
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const {
checkInfinity,
checkNaN,
} = {
checkInfinity: false,
checkNaN: true,
...context.options[0],
};
const {sourceCode} = context;
const objects = Object.keys(globalObjects).filter(name => {
if (!checkInfinity && name === 'Infinity') {
return false;
}
if (!checkNaN && name === 'NaN') {
return false;
}
return true;
});
const tracker = new GlobalReferenceTracker({
objects,
handle: reference => checkProperty(reference, sourceCode),
filter: ({node}) => !isLeftHandSide(node),
});
return tracker.createListeners(context);
};
const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
checkInfinity: {
type: 'boolean',
default: false,
},
checkNaN: {
type: 'boolean',
default: true,
},
},
},
];
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Prefer `Number` static properties over global ones.',
recommended: true,
},
fixable: 'code',
hasSuggestions: true,
schema,
messages,
},
};