diff --git a/src/coffeelint.coffee b/src/coffeelint.coffee index 26e48089..2cdd3cad 100644 --- a/src/coffeelint.coffee +++ b/src/coffeelint.coffee @@ -220,6 +220,7 @@ coffeelint.registerRule require './rules/ensure_comprehensions.coffee' coffeelint.registerRule require './rules/no_this.coffee' coffeelint.registerRule require './rules/eol_last.coffee' coffeelint.registerRule require './rules/no_private_function_fat_arrows.coffee' +coffeelint.registerRule require './rules/missing_parseint_radix.coffee' hasSyntaxError = (source) -> try diff --git a/src/rules/missing_parseint_radix.coffee b/src/rules/missing_parseint_radix.coffee new file mode 100644 index 00000000..efbd1610 --- /dev/null +++ b/src/rules/missing_parseint_radix.coffee @@ -0,0 +1,30 @@ +module.exports = class ParseintRadix + + rule: + name: 'missing_parseint_radix' + level : 'warn' + message : 'parseInt is missing the radix argument' + description: """ + This rule warns about using parseInt without a radix. From the MDN + developers reference: Always specify this parameter to eliminate + reader confusion and to guarantee predictable behavior. +
+              # You would expect this to result in 8, but
+              # it might result in 0 (parsed as octal).
+              parseInt '08'
+
+              # To be safe, specify the radix argument:
+              parseInt '08', 10
+              
+            
+ """ + + + tokens: ['CALL_START'] + + lintToken : (token, tokenApi) -> + [prevToken, functionName] = tokenApi.peek(-1) + + if functionName is 'parseInt' + [callEnd] = tokenApi.peek(2) + return callEnd is 'CALL_END' diff --git a/test/test_missing_parseint_radix.coffee b/test/test_missing_parseint_radix.coffee new file mode 100644 index 00000000..4bcae56b --- /dev/null +++ b/test/test_missing_parseint_radix.coffee @@ -0,0 +1,58 @@ +path = require 'path' +vows = require 'vows' +assert = require 'assert' +coffeelint = require path.join('..', 'lib', 'coffeelint') + +vows.describe('missing_parseint_radix').addBatch({ + + 'parseInt without radix': + topic : "parseInt '08'" + + 'should warn by default' : (source) -> + errors = coffeelint.lint(source) + {lineNumber, message, rule, level} = errors[0] + + assert.isArray(errors) + assert.lengthOf(errors, 1) + assert.equal(lineNumber, 1) + assert.equal(message, 'parseInt is missing the radix argument') + assert.equal(rule, 'missing_parseint_radix') + assert.equal(level, 'warn') + + 'can be forbidden' : (source) -> + config = {missing_parseint_radix : {level:'error'}} + errors = coffeelint.lint(source, config) + {lineNumber, message, rule, level} = errors[0] + + assert.isArray(errors) + assert.lengthOf(errors, 1) + assert.equal(lineNumber, 1) + assert.equal(message, 'parseInt is missing the radix argument') + assert.equal(rule, 'missing_parseint_radix') + assert.equal(level, 'error') + + 'can be permitted' : (source) -> + config = {missing_parseint_radix : {level:'ignore'}} + errors = coffeelint.lint(source, config) + + assert.isArray(errors) + assert.isEmpty(errors) + + 'parseInt with radix': + topic : "parseInt '08', 10" + + 'should not cause a warning' : (source) -> + errors = coffeelint.lint(source) + + assert.isArray(errors) + assert.isEmpty(errors) + + 'should not cause an error' : (source) -> + config = {missing_parseint_radix : {level:'error'}} + errors = coffeelint.lint(source, config) + + assert.isArray(errors) + assert.isEmpty(errors) + +}).export(module) +