1 | /**
|
2 | * @fileoverview Rule to disallow `parseInt()` in favor of binary, octal, and hexadecimal literals
|
3 | * @author Annie Zhang, Henry Zhu
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Rule Definition
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | module.exports = {
|
13 | meta: {
|
14 | docs: {
|
15 | description: "disallow `parseInt()` in favor of binary, octal, and hexadecimal literals",
|
16 | category: "ECMAScript 6",
|
17 | recommended: false
|
18 | },
|
19 |
|
20 | schema: []
|
21 | },
|
22 |
|
23 | create(context) {
|
24 | const radixMap = {
|
25 | 2: "binary",
|
26 | 8: "octal",
|
27 | 16: "hexadecimal"
|
28 | };
|
29 |
|
30 | //--------------------------------------------------------------------------
|
31 | // Public
|
32 | //--------------------------------------------------------------------------
|
33 |
|
34 | return {
|
35 |
|
36 | CallExpression(node) {
|
37 |
|
38 | // doesn't check parseInt() if it doesn't have a radix argument
|
39 | if (node.arguments.length !== 2) {
|
40 | return;
|
41 | }
|
42 |
|
43 | // only error if the radix is 2, 8, or 16
|
44 | const radixName = radixMap[node.arguments[1].value];
|
45 |
|
46 | if (node.callee.type === "Identifier" &&
|
47 | node.callee.name === "parseInt" &&
|
48 | radixName &&
|
49 | node.arguments[0].type === "Literal"
|
50 | ) {
|
51 | context.report({
|
52 | node,
|
53 | message: "Use {{radixName}} literals instead of parseInt().",
|
54 | data: {
|
55 | radixName
|
56 | }
|
57 | });
|
58 | }
|
59 | }
|
60 | };
|
61 | }
|
62 | };
|