1 | /**
|
2 | * @fileoverview Rule to disallow yoda comparisons
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Rule Definition
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | module.exports = function(context) {
|
12 |
|
13 | //--------------------------------------------------------------------------
|
14 | // Helpers
|
15 | //--------------------------------------------------------------------------
|
16 |
|
17 | function isComparisonOperator(operator) {
|
18 | return (/^(==|===|!=|!==|<|>|<=|>=)$/).test(operator);
|
19 | }
|
20 |
|
21 | //--------------------------------------------------------------------------
|
22 | // Public
|
23 | //--------------------------------------------------------------------------
|
24 |
|
25 | return {
|
26 |
|
27 | "BinaryExpression": function(node) {
|
28 |
|
29 | if (node.left.type === "Literal" && isComparisonOperator(node.operator)) {
|
30 | context.report(node, "Expected literal to be on the right side of " + node.operator + ".");
|
31 | }
|
32 |
|
33 | }
|
34 | };
|
35 |
|
36 | };
|