forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
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 eslint#79 from ilyavolodin/no-new
Rule: Do not use 'new' for side effects
- Loading branch information
Showing
4 changed files
with
97 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
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,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"); | ||
} | ||
} | ||
}; | ||
|
||
}; |
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,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); |