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 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | var assert = require('assert');
|
58 |
|
59 | module.exports = function() {};
|
60 |
|
61 | module.exports.prototype = {
|
62 |
|
63 | configure: function(options) {
|
64 | assert(
|
65 | options === true,
|
66 | this.getOptionName() + ' option requires a true value or should be removed'
|
67 | );
|
68 | },
|
69 |
|
70 | getOptionName: function() {
|
71 | return 'disallowNotOperatorsInConditionals';
|
72 | },
|
73 |
|
74 | check: function(file, errors) {
|
75 | function hasNotOperator(test) {
|
76 | return test.type === 'UnaryExpression' && test.operator === '!';
|
77 | }
|
78 |
|
79 | function hasNotEqualOperator(test) {
|
80 | return test.type === 'BinaryExpression' && test.operator === '!=';
|
81 | }
|
82 |
|
83 | function hasStrictNotEqualOperator(test) {
|
84 | return test.type === 'BinaryExpression' && test.operator === '!==';
|
85 | }
|
86 |
|
87 | file.iterateNodesByType(['IfStatement', 'ConditionalExpression'], function(node) {
|
88 | var alternate = node.alternate;
|
89 |
|
90 |
|
91 | if (node.type === 'IfStatement' && (!alternate || alternate.type !== 'BlockStatement')) {
|
92 | return;
|
93 | }
|
94 | var test = node.test;
|
95 | if (hasNotOperator(test)) {
|
96 | errors.add('Illegal use of not operator in if statement', test);
|
97 | }
|
98 | if (hasNotEqualOperator(test)) {
|
99 | errors.add('Illegal use of not equal operator in if statement', test);
|
100 | }
|
101 | if (hasStrictNotEqualOperator(test)) {
|
102 | errors.add('Illegal use of strict not equal operator in if statement', test);
|
103 | }
|
104 | });
|
105 | }
|
106 | };
|