Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit 4904bec

Browse files
author
Nathan Friend
committed
fix for GitHub issue #61
1 parent 91a6fd6 commit 4904bec

File tree

2 files changed

+85
-16
lines changed

2 files changed

+85
-16
lines changed

src/rules/noConstantConditionRule.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,24 @@ class NoConstantConditionWalker extends Lint.RuleWalker {
6767
case ts.SyntaxKind.ArrayLiteralExpression:
6868
return true;
6969
// ESLint UnaryExpression
70-
case ts.SyntaxKind.PrefixUnaryExpression:
7170
case ts.SyntaxKind.PostfixUnaryExpression:
7271
return true;
7372
// ESLint BinaryExpression / LogicalExpression
7473
case ts.SyntaxKind.BinaryExpression:
7574
// ESLint AssignmentExpression
7675
if (this.isAssignmentToken((node as ts.BinaryExpression).operatorToken)) {
77-
return this.isConstant(node.getLastToken());
76+
return this.isConstant((node as ts.BinaryExpression).right);
7877
}
79-
return this.isConstant(node.getFirstToken()) && this.isConstant(node.getLastToken());
78+
return this.isConstant((node as ts.BinaryExpression).left) && this.isConstant((node as ts.BinaryExpression).right);
8079
case ts.SyntaxKind.ConditionalExpression:
8180
return this.isConstant((node as ts.ConditionalExpression).condition);
81+
case ts.SyntaxKind.PrefixUnaryExpression:
82+
if (node.getFirstToken().kind === ts.SyntaxKind.ExclamationToken) {
83+
return this.isConstant((node as ts.PrefixUnaryExpression).operand);
84+
}
85+
return true;
86+
case ts.SyntaxKind.ParenthesizedExpression:
87+
return this.isConstant((node as ts.ParenthesizedExpression).expression);
8288
}
8389

8490
return false;

src/test/rules/noConstantConditionRuleTests.ts

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,152 @@ const rule = 'no-constant-condition';
55
const scripts = {
66
variables: [
77
'if (foo === true) {}',
8+
'if (!foo === true) {}',
89
'if (bar === false) {}',
10+
'if (!bar === false) {}',
911
'if (baz) {}',
12+
'if (!baz) {}',
1013
'if (qux == true) {}',
14+
'if (!(qux == true)) {}',
1115
'if (true == x) {}',
16+
'if (!(true == x)) {}',
1217
'if (false === y) {}',
18+
'if (!(false === y)) {}',
1319
'if (y === x) {}',
20+
'if (!(y === x)) {}',
1421
'if (x > 0) {}',
22+
'if (!(x > 0)) {}',
1523
'if (100 > x) {}',
16-
'if (x === -y) {}'
24+
'if (!(100 > x)) {}',
25+
'if (x === -y) {}',
26+
'if (!(x === -y)) {}'
1727
],
1828
booleans: [
1929
'if (true) {}',
20-
'if (false) {}'
30+
'if (!true) {}',
31+
'if (false) {}',
32+
'if (!false) {}'
2133
],
2234
numbers: [
2335
'if (0) {}',
36+
'if (!0) {}',
2437
'if (1) {}',
38+
'if (!1) {}',
2539
'if (100) {}',
40+
'if (!100) {}',
2641
'if (30.33) {}',
42+
'if (!30.33) {}',
2743
'if (-1) {}',
28-
'if (x = 1) {}'
44+
'if (!-1) {}',
45+
'if (x = 1) {}',
46+
'if (!(x = 1)) {}'
2947
],
3048
objects: [
3149
'if ({}) {}',
32-
'if ({ foo: "bar" }) {}'
50+
'if (!{}) {}',
51+
'if ({ foo: "bar" }) {}',
52+
'if (!{ foo: "bar" }) {}'
3353
],
3454
arrays: [
3555
'if ([]) {}',
36-
'if ([1, 2, 3]) {}'
56+
'if (![]) {}',
57+
'if ([1, 2, 3]) {}',
58+
'if (![1, 2, 3]) {}'
3759
],
3860
binary: [
3961
'if (true === true) {}',
62+
'if (!(true === true)) {}',
4063
'if (100 > -5) {}',
64+
'if (!(100 > -5)) {}',
4165
'if (false != true) {}',
42-
'if (false !== true && true === true) {}'
66+
'if (!(false != true)) {}',
67+
'if (false !== true && true === true) {}',
68+
'if (!(false !== true && true === true)) {}',
69+
'if (!(false !== true) && true === true) {}',
70+
'if (false !== true && !(true === true)) {}',
71+
'if (!(false !== true) && !(true === true)) {}'
4372
],
4473
ternary: [
4574
'let foo = true ? 1 : 0;',
75+
'let foo = !true ? 1 : 0;',
4676
'let bar = false ? "a" : "b";',
77+
'let bar = !false ? "a" : "b";',
4778
'let baz = 100 ? "x" : "z";',
48-
'let qux = true === true ? "p": "w";'
79+
'let baz = !100 ? "x" : "z";',
80+
'let qux = true === true ? "p": "w";',
81+
'let qux = !(true === true) ? "p": "w";'
4982
],
5083
whileVars: [
5184
'while (y === x) {}',
85+
'while (!(y === x)) {}',
5286
'while (x > -5) {}',
87+
'while (!(x > -5)) {}',
5388
'while (100 > x) {}',
54-
'while (foo) {}'
89+
'while (!(100 > x)) {}',
90+
'while (foo) {}',
91+
'while (!foo) {}'
5592
],
5693
whileLiterals: [
5794
'while (true) {}',
95+
'while (!true) {}',
5896
'while (false) {}',
97+
'while (!false) {}',
5998
'while (-5) {}',
99+
'while (!-5) {}',
60100
'while (1) {}',
101+
'while (!1) {}',
61102
'while ({}) {}',
62-
'while ([]) {}'
103+
'while (!{}) {}',
104+
'while ([]) {}',
105+
'while (![]) {}'
63106
],
64107
doWhileVars: [
65108
'do {} while (y === x);',
109+
'do {} while (!(y === x);',
66110
'do {} while (x > -5);',
111+
'do {} while (!(x > -5));',
67112
'do {} while (100 > x);',
68-
'do {} while (foo);'
113+
'do {} while (!(100 > x));',
114+
'do {} while (foo);',
115+
'do {} while (!foo);'
69116
],
70117
doWhileLiterals: [
71118
'do {} while (true);',
119+
'do {} while (!true);',
72120
'do {} while (false);',
121+
'do {} while (!false);',
73122
'do {} while (-5);',
123+
'do {} while (!-5);',
74124
'do {} while (1);',
125+
'do {} while (!1);',
75126
'do {} while ({});',
76-
'do {} while ([]);'
127+
'do {} while (!{});',
128+
'do {} while ([]);',
129+
'do {} while (![]);'
77130
],
78131
forVars: [
79132
'for (;y === x;) {}',
133+
'for (;(!y === x);) {}',
80134
'for (;x > -5;) {}',
135+
'for (;!(x > -5);) {}',
81136
'for (;100 > x;) {}',
82-
'for (;foo;) {}'
137+
'for (;!(100 > x);) {}',
138+
'for (;foo;) {}',
139+
'for (;!foo;) {}'
83140
],
84141
forLiterals: [
85142
'for (;true;) {}',
143+
'for (;!true;) {}',
86144
'for (;false;) {}',
145+
'for (;!false;) {}',
87146
'for (;-5;) {}',
147+
'for (;!-5;) {}',
88148
'for (;1;) {}',
149+
'for (;!1;) {}',
89150
'for (;{};) {}',
90-
'for (;[];) {}'
151+
'for (;!{};) {}',
152+
'for (;[];) {}',
153+
'for (;![];) {}'
91154
]
92155
};
93156

0 commit comments

Comments
 (0)