Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Add missing_parseint_radix rule #487

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/coffeelint.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/rules/missing_parseint_radix.coffee
Original file line number Diff line number Diff line change
@@ -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: <q>Always specify this parameter to eliminate
reader confusion and to guarantee predictable behavior.</q>
<pre>
<code># 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
</code>
</pre>
"""


tokens: ['CALL_START']

lintToken : (token, tokenApi) ->
[prevToken, functionName] = tokenApi.peek(-1)

if functionName is 'parseInt'
[callEnd] = tokenApi.peek(2)
return callEnd is 'CALL_END'
58 changes: 58 additions & 0 deletions test/test_missing_parseint_radix.coffee
Original file line number Diff line number Diff line change
@@ -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)