summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/radix.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/radix.js')
-rw-r--r--tools/eslint/lib/rules/radix.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/radix.js b/tools/eslint/lib/rules/radix.js
new file mode 100644
index 0000000000..a0c7acfb2e
--- /dev/null
+++ b/tools/eslint/lib/rules/radix.js
@@ -0,0 +1,39 @@
+/**
+ * @fileoverview Rule to flag use of parseInt without a radix argument
+ * @author James Allardice
+ */
+
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ return {
+ "CallExpression": function(node) {
+
+ var radix;
+
+ if (node.callee.name === "parseInt") {
+
+ if (node.arguments.length < 2) {
+ context.report(node, "Missing radix parameter.");
+ } else {
+
+ radix = node.arguments[1];
+
+ // don't allow non-numeric literals or undefined
+ if ((radix.type === "Literal" && typeof radix.value !== "number") ||
+ (radix.type === "Identifier" && radix.name === "undefined")
+ ) {
+ context.report(node, "Invalid radix parameter.");
+ }
+ }
+
+ }
+ }
+ };
+
+};