/
prefer-number-properties.js
152 lines (132 loc) · 3.27 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
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
const isShadowed = require('./utils/is-shadowed.js');
const {
referenceIdentifierSelector,
callExpressionSelector,
} = require('./selectors/index.js');
const {replaceReferenceIdentifier} = require('./fix/index.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');
const METHOD_ERROR_MESSAGE_ID = 'method-error';
const METHOD_SUGGESTION_MESSAGE_ID = 'method-suggestion';
const PROPERTY_ERROR_MESSAGE_ID = 'property-error';
const messages = {
[METHOD_ERROR_MESSAGE_ID]: 'Prefer `Number.{{name}}()` over `{{name}}()`.',
[METHOD_SUGGESTION_MESSAGE_ID]: 'Replace `{{name}}()` with `Number.{{name}}()`.',
[PROPERTY_ERROR_MESSAGE_ID]: 'Prefer `Number.{{property}}` over `{{identifier}}`.',
};
const methods = {
// Safe
parseInt: true,
parseFloat: true,
// Unsafe
isNaN: false,
isFinite: false,
};
const methodsSelector = [
callExpressionSelector(Object.keys(methods)),
' > ',
'.callee',
].join('');
const propertiesSelector = referenceIdentifierSelector(['NaN', 'Infinity']);
const isNegative = node => {
const {parent} = node;
return parent && parent.type === 'UnaryExpression' && parent.operator === '-' && parent.argument === node;
};
const create = context => {
const sourceCode = context.getSourceCode();
const options = {
checkInfinity: true,
...context.options[0],
};
// Cache `NaN` and `Infinity` in `foo = {NaN, Infinity}`
const reported = new WeakSet();
return {
[methodsSelector]: node => {
if (isShadowed(context.getScope(), node)) {
return;
}
const {name} = node;
const isSafe = methods[name];
const problem = {
node,
messageId: METHOD_ERROR_MESSAGE_ID,
data: {
name,
},
};
const fix = fixer => replaceReferenceIdentifier(node, `Number.${name}`, fixer, sourceCode);
if (isSafe) {
problem.fix = fix;
} else {
problem.suggest = [
{
messageId: METHOD_SUGGESTION_MESSAGE_ID,
data: {
name,
},
fix,
},
];
}
return problem;
},
[propertiesSelector]: node => {
if (reported.has(node) || isShadowed(context.getScope(), node)) {
return;
}
const {name, parent} = node;
if (name === 'Infinity' && !options.checkInfinity) {
return;
}
let property = name;
if (name === 'Infinity') {
property = isNegative(node) ? 'NEGATIVE_INFINITY' : 'POSITIVE_INFINITY';
}
const problem = {
node,
messageId: PROPERTY_ERROR_MESSAGE_ID,
data: {
identifier: name,
property,
},
};
if (property === 'NEGATIVE_INFINITY') {
problem.node = parent;
problem.data.identifier = '-Infinity';
problem.fix = function * (fixer) {
yield fixer.replaceText(parent, 'Number.NEGATIVE_INFINITY');
yield * fixSpaceAroundKeyword(fixer, parent, sourceCode);
};
} else {
problem.fix = fixer => replaceReferenceIdentifier(node, `Number.${property}`, fixer, sourceCode);
}
reported.add(node);
return problem;
},
};
};
const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
checkInfinity: {
type: 'boolean',
default: true,
},
},
},
];
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Prefer `Number` static properties over global ones.',
},
fixable: 'code',
hasSuggestions: true,
schema,
messages,
},
};