This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rules: (disallow|require)FunctionDeclarations
Closes gh-569
- Loading branch information
1 parent
bce7eda
commit d735899
Showing
6 changed files
with
229 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,22 @@ | ||
var assert = require('assert'); | ||
|
||
module.exports = function() {}; | ||
|
||
module.exports.prototype = { | ||
configure: function(disallowFunctionDeclarations) { | ||
assert( | ||
disallowFunctionDeclarations === true, | ||
'disallowFunctionDeclarations option requires true value or should be removed' | ||
); | ||
}, | ||
|
||
getOptionName: function() { | ||
return 'disallowFunctionDeclarations'; | ||
}, | ||
|
||
check: function(file, errors) { | ||
file.iterateNodesByType('FunctionDeclaration', function(node) { | ||
errors.add('Illegal function declaration', node.loc.start); | ||
}); | ||
} | ||
}; |
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,37 @@ | ||
var assert = require('assert'); | ||
|
||
module.exports = function() {}; | ||
|
||
module.exports.prototype = { | ||
configure: function(requireFunctionDeclarations) { | ||
assert( | ||
requireFunctionDeclarations === true, | ||
'requireFunctionDeclarations option requires true value or should be removed' | ||
); | ||
}, | ||
|
||
getOptionName: function() { | ||
return 'requireFunctionDeclarations'; | ||
}, | ||
|
||
check: function(file, errors) { | ||
file.iterateNodesByType( | ||
'VariableDeclarator', | ||
function(node) { | ||
if (node.init && node.init.type === 'FunctionExpression') { | ||
errors.add('Use a function declaration instead', node.loc.start); | ||
} | ||
} | ||
); | ||
|
||
file.iterateNodesByType( | ||
'AssignmentExpression', | ||
function(node) { | ||
if (node.left.type !== 'MemberExpression' && | ||
node.right.type === 'FunctionExpression') { | ||
errors.add('Use a function declaration instead', node.loc.start); | ||
} | ||
} | ||
); | ||
} | ||
}; |
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,29 @@ | ||
var Checker = require('../../lib/checker'); | ||
var assert = require('assert'); | ||
|
||
describe('rules/disallow-function-declarations', function() { | ||
var checker; | ||
|
||
beforeEach(function() { | ||
checker = new Checker(); | ||
checker.registerDefaultRules(); | ||
|
||
checker.configure({ | ||
disallowFunctionDeclarations: true | ||
}); | ||
}); | ||
|
||
it('should report on function declarations', function() { | ||
assert(checker.checkString('function declared() { }').getErrorCount() === 1); | ||
}); | ||
|
||
it('should not report on anonymous function expressions', function() { | ||
assert(checker.checkString('var expressed = function (){};').isEmpty()); | ||
assert(checker.checkString('var foo = {bar: function() {}};').isEmpty()); | ||
}); | ||
|
||
it('should not report on named function expressions', function() { | ||
assert(checker.checkString('$("hi").click(function named(){});').isEmpty()); | ||
assert(checker.checkString('var x = function named(){};').isEmpty()); | ||
}); | ||
}); |
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,47 @@ | ||
var Checker = require('../../lib/checker'); | ||
var assert = require('assert'); | ||
|
||
describe('rules/require-function-declarations', function() { | ||
var checker; | ||
|
||
beforeEach(function() { | ||
checker = new Checker(); | ||
checker.registerDefaultRules(); | ||
|
||
checker.configure({ | ||
requireFunctionDeclarations: true | ||
}); | ||
}); | ||
|
||
it('should report on anonymous function expression declarations', function() { | ||
assert(checker.checkString('var anon = function() {};').getErrorCount() === 1); | ||
}); | ||
|
||
it('should report on named function expression declarations', function() { | ||
assert(checker.checkString('var named = function named() {};').getErrorCount() === 1); | ||
}); | ||
|
||
it('should report on anonymous function expression assignments', function() { | ||
assert(checker.checkString('var anon; anon = function() {};').getErrorCount() === 1); | ||
}); | ||
|
||
it('should report on named function expression assignments', function() { | ||
assert(checker.checkString('var named; named = function named() {};').getErrorCount() === 1); | ||
}); | ||
|
||
it('should ignore member expression assignments', function() { | ||
assert(checker.checkString('obj.a = function() {};').isEmpty()); | ||
}); | ||
|
||
it('should ignore IIFEs', function() { | ||
assert(checker.checkString('(function() { void 0; })();').isEmpty()); | ||
}); | ||
|
||
it('should ignore function expressions in object literals', function() { | ||
assert(checker.checkString('var foo = {bar: function() {}};').isEmpty()); | ||
}); | ||
|
||
it('should ignore function expressions in function calls', function() { | ||
assert(checker.checkString('onclick(function() {})').isEmpty()); | ||
}); | ||
}); |