Skip to content

Commit

Permalink
Merge pull request #92 from jamesallardice/rule-no-implied-eval
Browse files Browse the repository at this point in the history
Rule: Warn on passing string to setTimeout/Interval
  • Loading branch information
nzakas committed Jul 19, 2013
2 parents d1a229a + bed8e37 commit 4c246d3
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"no-empty": 1,
"no-eval": 1,
"no-floating-decimal": 0,
"no-implied-eval": 1,
"no-with": 1,
"no-fallthrough": 1,
"no-unreachable": 1,
Expand Down
28 changes: 28 additions & 0 deletions lib/rules/no-implied-eval.js
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.");
}
}
}
}
};

};
91 changes: 91 additions & 0 deletions tests/lib/rules/no-implied-eval.js
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);

0 comments on commit 4c246d3

Please sign in to comment.