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

Commit

Permalink
Merge pull request #254 from misterdjules/no_empty_functions
Browse files Browse the repository at this point in the history
Add no_empty_functions rule that triggers if an empty function is defined
  • Loading branch information
AsaAyers committed Apr 16, 2014
2 parents 25e1fc8 + 38c5099 commit 2987e3c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/coffeelint.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ coffeelint.registerRule require './rules/no_debugger.coffee'
coffeelint.registerRule(
require './rules/no_interpolation_in_single_quotes.coffee'
)
coffeelint.registerRule require './rules/no_empty_functions.coffee'

hasSyntaxError = (source) ->
try
Expand Down
24 changes: 24 additions & 0 deletions src/rules/no_empty_functions.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
isEmptyCode = (node, astApi) ->
nodeName = astApi.getNodeName node
nodeName is 'Code' and node.body.isEmpty()

module.exports = class NoEmptyFunctions

rule:
name: 'no_empty_functions'
level: 'ignore'
message: 'Empty function'
description: """
Disallows declaring empty functions.
"""

lintAST: (node, astApi) ->
@lintNode node, astApi
undefined

lintNode: (node, astApi) ->
if isEmptyCode node, astApi
error = astApi.createError
lineNumber: node.locationData.first_line + 1
@errors.push error
node.eachChild (child) => @lintNode child, astApi
46 changes: 46 additions & 0 deletions test/test_no_empty_functions.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
path = require 'path'
vows = require 'vows'
assert = require 'assert'
coffeelint = require path.join('..', 'lib', 'coffeelint')

runLint = (source) ->
config = no_empty_functions: level: 'error'
coffeelint.lint source, config

shouldError = (source, numErrors = 1, errorNames = ['no_empty_functions']) ->
topic: source
'errors for empty function': (source) ->
errors = runLint source
assert.lengthOf errors, numErrors, "Expected #{numErrors} errors, got
[#{errors.map( (error) -> error.name).join ', '}] instead"
for errorName in errorNames
assert.notEqual errors.indexOf errorName, -1

shouldPass = (source) ->
topic: source
'does not error for empty function': (source) ->
errors = runLint source
assert.isEmpty errors, "Expected no errors, got
[#{errors.map( (error) -> error.name).join ', '}] instead"

vows.describe('no empty functions').addBatch({
'empty fat-arrow function' : shouldError(
'=>', 2)
'empty function' : shouldError(
'->')
'function with undefined statement' : shouldPass(
'-> undefined')
'function within function with undefined statement' : shouldPass(
'-> -> undefined')
'empty fat arrow function within a function ' : shouldError(
'-> =>', 2)
'empty function within a function ' : shouldError(
'-> ->')
"empty function as param's default value" : shouldError(
'foo = (empty=(->)) -> undefined')
"non-empty function as param's default value" : shouldPass(
'foo = (empty=(-> undefined)) -> undefined')
"empty function with implicit instance member
assignment as param" : shouldError(
'foo = (@_fooMember) ->')
}).export(module)

0 comments on commit 2987e3c

Please sign in to comment.