Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement rule to flag use of alert (fixes #10)
- Loading branch information
Showing
4 changed files
with
242 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"rules": { | ||
|
||
"no-alert": 1, | ||
"no-arg": 1, | ||
"no-console": 1, | ||
"no-debugger": 1, | ||
|
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,51 @@ | ||
/** | ||
* @fileoverview Rule to flag use of alert, confirm, prompt | ||
* @author Nicholas C. Zakas | ||
*/ | ||
|
||
/*jshint node:true*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
function matchProhibited(name) { | ||
return name.match(/^(alert|confirm|prompt)$/); | ||
} | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
return { | ||
|
||
"CallExpression": function(node) { | ||
|
||
var result; | ||
|
||
// without window. | ||
if (node.callee.type === "Identifier") { | ||
|
||
result = matchProhibited(node.callee.name); | ||
|
||
if (result) { | ||
context.report(node, "Unexpected " + result[1] + "."); | ||
} | ||
|
||
} else if (node.callee.type === "MemberExpression") { | ||
|
||
result = matchProhibited(node.callee.property.name); | ||
|
||
if (result && node.callee.object.name === "window") { | ||
context.report(node, "Unexpected " + result[1] + "."); | ||
} | ||
|
||
} | ||
|
||
} | ||
}; | ||
|
||
}; |
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,179 @@ | ||
/** | ||
* @fileoverview Tests for no-alert rule. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
|
||
/*jshint node:true*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var vows = require("vows"), | ||
assert = require("assert"), | ||
sinon = require("sinon"), | ||
eslint = require("../../../lib/eslint"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Constants | ||
//------------------------------------------------------------------------------ | ||
|
||
var RULE_ID = "no-alert"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
vows.describe(RULE_ID).addBatch({ | ||
|
||
"when evaluating 'alert(foo)'": { | ||
|
||
topic: "alert(foo)", | ||
|
||
"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, "Unexpected alert."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
assert.include(messages[0].node.callee.name, "alert"); | ||
} | ||
}, | ||
|
||
"when evaluating 'window.alert(foo)'": { | ||
|
||
topic: "window.alert(foo)", | ||
|
||
"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, "Unexpected alert."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
assert.include(messages[0].node.callee.property.name, "alert"); | ||
} | ||
}, | ||
|
||
"when evaluating 'confirm(foo)'": { | ||
|
||
topic: "confirm(foo)", | ||
|
||
"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, "Unexpected confirm."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
assert.include(messages[0].node.callee.name, "confirm"); | ||
} | ||
}, | ||
|
||
"when evaluating 'window.confirm(foo)'": { | ||
|
||
topic: "window.confirm(foo)", | ||
|
||
"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, "Unexpected confirm."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
assert.include(messages[0].node.callee.property.name, "confirm"); | ||
} | ||
}, | ||
|
||
"when evaluating 'prompt(foo)'": { | ||
|
||
topic: "prompt(foo)", | ||
|
||
"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, "Unexpected prompt."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
assert.include(messages[0].node.callee.name, "prompt"); | ||
} | ||
}, | ||
|
||
"when evaluating 'window.prompt(foo)'": { | ||
|
||
topic: "window.prompt(foo)", | ||
|
||
"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, "Unexpected prompt."); | ||
assert.include(messages[0].node.type, "CallExpression"); | ||
assert.include(messages[0].node.callee.property.name, "prompt"); | ||
} | ||
}, | ||
|
||
"when evaluating 'foo.alert(foo)'": { | ||
|
||
topic: "foo.alert(foo)", | ||
|
||
"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 'foo.confirm(foo)'": { | ||
|
||
topic: "foo.confirm(foo)", | ||
|
||
"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 'foo.prompt(foo)'": { | ||
|
||
topic: "foo.prompt(foo)", | ||
|
||
"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); |