-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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 #92 from jamesallardice/rule-no-implied-eval
Rule: Warn on passing string to setTimeout/Interval
- Loading branch information
Showing
3 changed files
with
120 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,28 @@ | ||
/** | ||
* @fileoverview Rule to flag use of implied eval via setTimeout and setInterval | ||
* @author James Allardice | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
return { | ||
"CallExpression": function(node) { | ||
|
||
if (node.callee.type === "Identifier") { | ||
var callee = node.callee.name; | ||
|
||
if (callee === "setTimeout" || callee === "setInterval") { | ||
var argument = node.arguments[0]; | ||
if (argument && argument.type === "Literal" && typeof argument.value === "string") { | ||
context.report(node, "Implied eval. Consider passing a function instead of a string."); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
}; |
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,91 @@ | ||
/** | ||
* @fileoverview Tests for no-implied-eval rule. | ||
* @author James Allardice | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var vows = require("vows"), | ||
assert = require("assert"), | ||
eslint = require("../../../lib/eslint"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Constants | ||
//------------------------------------------------------------------------------ | ||
|
||
var RULE_ID = "no-implied-eval"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
vows.describe(RULE_ID).addBatch({ | ||
|
||
"when evaluating 'setTimeout(\"x = 1;\");'": { | ||
|
||
topic: "setTimeout(\"x = 1;\");", | ||
|
||
"should report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 1); | ||
assert.equal(messages[0].ruleId, RULE_ID); | ||
assert.equal(messages[0].message, "Implied eval. Consider passing a function instead of a string."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
} | ||
}, | ||
|
||
"when evaluating 'setTimeout(\"x = 1;\", 100);'": { | ||
|
||
topic: "setTimeout(\"x = 1;\", 100);", | ||
|
||
"should report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 1); | ||
assert.equal(messages[0].ruleId, RULE_ID); | ||
assert.equal(messages[0].message, "Implied eval. Consider passing a function instead of a string."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
} | ||
}, | ||
|
||
"when evaluating 'setInterval(\"x = 1;\");'": { | ||
|
||
topic: "setInterval(\"x = 1;\");", | ||
|
||
"should report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 1); | ||
assert.equal(messages[0].ruleId, RULE_ID); | ||
assert.equal(messages[0].message, "Implied eval. Consider passing a function instead of a string."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
} | ||
}, | ||
|
||
"when evaluating 'setInterval(function () { x = 1; }, 100);'": { | ||
|
||
topic: "setInterval(function () { x = 1; }, 100);", | ||
|
||
"should not report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 0); | ||
} | ||
} | ||
|
||
}).export(module); |