X Tutup
Skip to content

Commit fc508a8

Browse files
authored
Relaxed warning for checking always-true conditions (#1350)
1 parent a221972 commit fc508a8

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/transformation/utils/diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const annotationDeprecated = createWarningDiagnosticFactory(
146146
);
147147

148148
export const truthyOnlyConditionalValue = createWarningDiagnosticFactory(
149-
"Numbers and strings will always evaluate to true in Lua. Explicitly check the value with ===."
149+
"Only false and nil evaluate to 'false' in Lua, everything else is considered 'true'. Explicitly compare the value with ===."
150150
);
151151

152152
export const notAllowedOptionalAssignment = createErrorDiagnosticFactory(

src/transformation/visitors/conditional.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export function transformIfStatement(statement: ts.IfStatement, context: Transfo
113113
}
114114

115115
export function checkOnlyTruthyCondition(condition: ts.Expression, context: TransformationContext) {
116+
if (context.options.strictNullChecks === false) return; // This check is not valid if everything could implicitly be nil
117+
if (ts.isElementAccessExpression(condition)) return; // Array index could always implicitly return nil
118+
116119
if (!canBeFalsy(context, context.checker.getTypeAtLocation(condition))) {
117120
context.diagnostics.push(truthyOnlyConditionalValue(condition));
118121
}

test/unit/conditionals.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ test.each(["string", "number", "string | number"])(
154154
}
155155
);
156156

157+
test.each(["string", "number", "string | number"])("Warning can be disabled when strict is true (%p)", type => {
158+
util.testFunction`
159+
if (condition) {}
160+
`
161+
.setTsHeader(`declare var condition: ${type};`)
162+
.setOptions({ strict: true, strictNullChecks: false })
163+
.expectToHaveNoDiagnostics();
164+
});
165+
157166
test.each(["string", "number", "string | number"])(
158167
"Warning when using while statement that cannot evaluate to false undefined or null (%p)",
159168
type => {
@@ -187,3 +196,10 @@ test.each(["string", "number", "string | number"])(
187196
.expectToHaveDiagnostics([truthyOnlyConditionalValue.code]);
188197
}
189198
);
199+
200+
test.each(["string", "number", "string | number"])("No warning when using element index in condition (%p)", type => {
201+
util.testExpression`condition[0] ? 1 : 0`
202+
.setTsHeader(`declare var condition: ${type}[];`)
203+
.setOptions({ strict: true })
204+
.expectToHaveNoDiagnostics();
205+
});

test/unit/optionalChaining.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ describe("optional chaining function calls", () => {
253253
const result = func?.(i++);
254254
`
255255
.setOptions({
256-
alwaysStrict: strict,
256+
strict,
257257
target: ScriptTarget.ES5,
258258
})
259259
.expectToMatchJsResult();

0 commit comments

Comments
 (0)
X Tutup