This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from misterdjules/no_empty_functions
Add no_empty_functions rule that triggers if an empty function is defined
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |