node/tools/eslint/lib/rules/no-unneeded-ternary.js
Roman Reiss d91e10b3bd tools: update eslint to 0.24.0
PR-URL: https://github.com/nodejs/io.js/pull/2072
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
Reviewed-by: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alex Kocharin <alex@kocharin.ru>
2015-06-29 19:02:17 +02:00

48 lines
1.4 KiB
JavaScript

/**
* @fileoverview Rule to flag no-unneeded-ternary
* @author Gyandeep Singh
* @copyright 2015 Gyandeep Singh. All rights reserved.
* @copyright 2015 Michael Ficarra. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
/**
* Reports an AST node as a rule violation.
* @param {ASTNode} mainNode The node to report.
* @param {object} culpritNode - The token which has a problem
* @returns {void}
* @private
*/
function report(mainNode, culpritNode) {
context.report(mainNode, culpritNode.loc.start, "Unnecessary use of boolean literals in conditional expression");
}
/**
* Test if the node is a boolean literal
* @param {ASTNode} node - The node to report.
* @returns {boolean} True if the its a boolean literal
* @private
*/
function isBooleanLiteral(node) {
return node.type === "Literal" && typeof node.value === "boolean";
}
return {
"ConditionalExpression": function(node) {
if (isBooleanLiteral(node.alternate) && isBooleanLiteral(node.consequent)) {
report(node, node.consequent);
}
}
};
};
module.exports.schema = [];