Skip to content

Commit

Permalink
Merge pull request eslint#79 from ilyavolodin/no-new
Browse files Browse the repository at this point in the history
Rule: Do not use 'new' for side effects
  • Loading branch information
nzakas committed Jul 18, 2013
2 parents 7f409fb + e4fcf8d commit 2b4a7d5
Show file tree
Hide file tree
Showing 4 changed files with 97 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 @@ -14,6 +14,7 @@
"no-undef-init": 1,
"no-octal": 1,
"no-new-wrappers": 1,
"no-new": 1,

"smarter-eqeqeq": 0,
"camelcase": 1,
Expand Down
1 change: 1 addition & 0 deletions docs/Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ These are rules designed to prevent you from making mistakes. They either prescr
* [no-undef-init] - disallow use of undefined when initializing variables
* [no-floating-decimal] - disallow the use of leading or trailing decimal points in numeric literals
* [no-octal] - disallow use of octal literals
* [no-new] - disallow use of new operator when not part of the assignment or comparison

## Stylistic Issues

Expand Down
23 changes: 23 additions & 0 deletions lib/rules/no-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @fileoverview Rule to flag statements with function invocation preceded by
* "new" and not part of assignment
* @author Ilya Volodin
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

return {

"ExpressionStatement": function(node) {

if (node.expression.type === "NewExpression") {
context.report(node, "Do not use 'new' for side effects");
}
}
};

};
72 changes: 72 additions & 0 deletions tests/lib/rules/no-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @fileoverview Tests for no-new rule.
* @author Ilya Volodin
*/

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var vows = require("vows"),
assert = require("assert"),
eslint = require("../../../lib/eslint");

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------

var RULE_ID = "no-new";

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

vows.describe(RULE_ID).addBatch({

"when evaluating 'new Date()": {

topic: "new Date()",

"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, "Do not use 'new' for side effects");
assert.include(messages[0].node.type, "ExpressionStatement");
}
},

"when evaluating 'var a = new Date()": {

topic: "var a = new Date()",

"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);
}
},

"when evaluating 'var a; if (a === new Date()) { a=false; }": {

topic: "var a; if (a === new Date()) { a = false; }",

"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 2b4a7d5

Please sign in to comment.