|
1 | 1 | import * as tstl from "../../src"; |
2 | 2 | import * as util from "../util"; |
| 3 | +import { truthyOnlyConditionalValue } from "../../src/transformation/utils/diagnostics"; |
3 | 4 |
|
4 | 5 | test.each([0, 1])("if (%p)", inp => { |
5 | 6 | util.testFunction` |
@@ -88,7 +89,9 @@ test.each([ |
88 | 89 | { condition: false, lhs: 4, rhs: 5 }, |
89 | 90 | { condition: 3, lhs: 4, rhs: 5 }, |
90 | 91 | ])("Ternary Conditional (%p)", ({ condition, lhs, rhs }) => { |
91 | | - util.testExpressionTemplate`${condition} ? ${lhs} : ${rhs}`.expectToMatchJsResult(); |
| 92 | + util.testExpressionTemplate`${condition} ? ${lhs} : ${rhs}` |
| 93 | + .ignoreDiagnostics([truthyOnlyConditionalValue.code]) |
| 94 | + .expectToMatchJsResult(); |
92 | 95 | }); |
93 | 96 |
|
94 | 97 | test.each(["true", "false", "a < 4", "a == 8"])("Ternary Conditional Delayed (%p)", condition => { |
@@ -138,3 +141,49 @@ test.each([false, true])("Ternary conditional with preceding statements in false |
138 | 141 | }) |
139 | 142 | .expectToMatchJsResult(); |
140 | 143 | }); |
| 144 | + |
| 145 | +test.each(["string", "number", "string | number"])( |
| 146 | + "Warning when using if statement that cannot evaluate to false undefined or null (%p)", |
| 147 | + type => { |
| 148 | + util.testFunction` |
| 149 | + if (condition) {} |
| 150 | + ` |
| 151 | + .setTsHeader(`declare var condition: ${type};`) |
| 152 | + .setOptions({ strict: true }) |
| 153 | + .expectToHaveDiagnostics([truthyOnlyConditionalValue.code]); |
| 154 | + } |
| 155 | +); |
| 156 | + |
| 157 | +test.each(["string", "number", "string | number"])( |
| 158 | + "Warning when using while statement that cannot evaluate to false undefined or null (%p)", |
| 159 | + type => { |
| 160 | + util.testFunction` |
| 161 | + while (condition) {} |
| 162 | + ` |
| 163 | + .setTsHeader(`declare var condition: ${type};`) |
| 164 | + .setOptions({ strict: true }) |
| 165 | + .expectToHaveDiagnostics([truthyOnlyConditionalValue.code]); |
| 166 | + } |
| 167 | +); |
| 168 | + |
| 169 | +test.each(["string", "number", "string | number"])( |
| 170 | + "Warning when using do while statement that cannot evaluate to false undefined or null (%p)", |
| 171 | + type => { |
| 172 | + util.testFunction` |
| 173 | + do {} while (condition) |
| 174 | + ` |
| 175 | + .setTsHeader(`declare var condition: ${type};`) |
| 176 | + .setOptions({ strict: true }) |
| 177 | + .expectToHaveDiagnostics([truthyOnlyConditionalValue.code]); |
| 178 | + } |
| 179 | +); |
| 180 | + |
| 181 | +test.each(["string", "number", "string | number"])( |
| 182 | + "Warning when using ternary that cannot evaluate to false undefined or null (%p)", |
| 183 | + type => { |
| 184 | + util.testExpression`condition ? 1 : 0` |
| 185 | + .setTsHeader(`declare var condition: ${type};`) |
| 186 | + .setOptions({ strict: true }) |
| 187 | + .expectToHaveDiagnostics([truthyOnlyConditionalValue.code]); |
| 188 | + } |
| 189 | +); |
0 commit comments