@@ -15,6 +15,12 @@ const astUtils = require("./utils/ast-utils");
1515// Helpers
1616//------------------------------------------------------------------------------
1717
18+ const radixMap = new Map ( [
19+ [ 2 , { system : "binary" , literalPrefix : "0b" } ] ,
20+ [ 8 , { system : "octal" , literalPrefix : "0o" } ] ,
21+ [ 16 , { system : "hexadecimal" , literalPrefix : "0x" } ]
22+ ] ) ;
23+
1824/**
1925 * Checks to see if a CallExpression's callee node is `parseInt` or
2026 * `Number.parseInt`.
@@ -54,59 +60,54 @@ module.exports = {
5460 } ,
5561
5662 schema : [ ] ,
63+
64+ messages : {
65+ useLiteral : "Use {{system}} literals instead of {{functionName}}()."
66+ } ,
67+
5768 fixable : "code"
5869 } ,
5970
6071 create ( context ) {
6172 const sourceCode = context . getSourceCode ( ) ;
6273
63- const radixMap = {
64- 2 : "binary" ,
65- 8 : "octal" ,
66- 16 : "hexadecimal"
67- } ;
68-
69- const prefixMap = {
70- 2 : "0b" ,
71- 8 : "0o" ,
72- 16 : "0x"
73- } ;
74-
7574 //----------------------------------------------------------------------
7675 // Public
7776 //----------------------------------------------------------------------
7877
7978 return {
8079
81- CallExpression ( node ) {
82-
83- // doesn't check parseInt() if it doesn't have a radix argument
84- if ( node . arguments . length !== 2 ) {
85- return ;
86- }
80+ "CallExpression[arguments.length=2]" ( node ) {
81+ const [ strNode , radixNode ] = node . arguments ,
82+ str = strNode . value ,
83+ radix = radixNode . value ;
84+
85+ if (
86+ strNode . type === "Literal" &&
87+ radixNode . type === "Literal" &&
88+ typeof str === "string" &&
89+ typeof radix === "number" &&
90+ radixMap . has ( radix ) &&
91+ isParseInt ( node . callee )
92+ ) {
8793
88- // only error if the radix is 2, 8, or 16
89- const radixName = radixMap [ node . arguments [ 1 ] . value ] ;
94+ const { system, literalPrefix } = radixMap . get ( radix ) ;
9095
91- if ( isParseInt ( node . callee ) &&
92- radixName &&
93- node . arguments [ 0 ] . type === "Literal"
94- ) {
9596 context . report ( {
9697 node,
97- message : "Use {{radixName}} literals instead of {{functionName}}(). " ,
98+ messageId : "useLiteral " ,
9899 data : {
99- radixName ,
100+ system ,
100101 functionName : sourceCode . getText ( node . callee )
101102 } ,
102103 fix ( fixer ) {
103104 if ( sourceCode . getCommentsInside ( node ) . length ) {
104105 return null ;
105106 }
106107
107- const replacement = `${ prefixMap [ node . arguments [ 1 ] . value ] } ${ node . arguments [ 0 ] . value } ` ;
108+ const replacement = `${ literalPrefix } ${ str } ` ;
108109
109- if ( + replacement !== parseInt ( node . arguments [ 0 ] . value , node . arguments [ 1 ] . value ) ) {
110+ if ( + replacement !== parseInt ( str , radix ) ) {
110111
111112 /*
112113 * If the newly-produced literal would be invalid, (e.g. 0b1234),
0 commit comments