1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var assert = require('assert');
|
37 |
|
38 | module.exports = function() {};
|
39 |
|
40 | module.exports.prototype = {
|
41 |
|
42 | configure: function(operators) {
|
43 | var isTrue = operators === true;
|
44 |
|
45 | assert(
|
46 | Array.isArray(operators) || isTrue,
|
47 | this.getOptionName() + ' option requires array or true value'
|
48 | );
|
49 |
|
50 | if (isTrue) {
|
51 | operators = ['==', '===', '!=', '!=='];
|
52 | }
|
53 |
|
54 | this._operatorIndex = {};
|
55 | for (var i = 0, l = operators.length; i < l; i++) {
|
56 | this._operatorIndex[operators[i]] = true;
|
57 | }
|
58 | },
|
59 |
|
60 | getOptionName: function() {
|
61 | return 'disallowYodaConditions';
|
62 | },
|
63 |
|
64 | check: function(file, errors) {
|
65 | var operators = this._operatorIndex;
|
66 | file.iterateNodesByType('BinaryExpression', function(node) {
|
67 | if (operators[node.operator]) {
|
68 | if (
|
69 | node.left.type.indexOf('Literal') > -1 ||
|
70 | (node.left.type === 'Identifier' && node.left.name === 'undefined')
|
71 | ) {
|
72 | errors.add('Yoda condition', node.left);
|
73 | }
|
74 | }
|
75 | });
|
76 | }
|
77 |
|
78 | };
|