X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transformation/utils/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const annotationDeprecated = createWarningDiagnosticFactory(
);

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

export const notAllowedOptionalAssignment = createErrorDiagnosticFactory(
Expand Down
3 changes: 3 additions & 0 deletions src/transformation/visitors/conditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export function transformIfStatement(statement: ts.IfStatement, context: Transfo
}

export function checkOnlyTruthyCondition(condition: ts.Expression, context: TransformationContext) {
if (context.options.strictNullChecks === false) return; // This check is not valid if everything could implicitly be nil
if (ts.isElementAccessExpression(condition)) return; // Array index could always implicitly return nil

if (!canBeFalsy(context, context.checker.getTypeAtLocation(condition))) {
context.diagnostics.push(truthyOnlyConditionalValue(condition));
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/conditionals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ test.each(["string", "number", "string | number"])(
}
);

test.each(["string", "number", "string | number"])("Warning can be disabled when strict is true (%p)", type => {
util.testFunction`
if (condition) {}
`
.setTsHeader(`declare var condition: ${type};`)
.setOptions({ strict: true, strictNullChecks: false })
.expectToHaveNoDiagnostics();
});

test.each(["string", "number", "string | number"])(
"Warning when using while statement that cannot evaluate to false undefined or null (%p)",
type => {
Expand Down Expand Up @@ -187,3 +196,10 @@ test.each(["string", "number", "string | number"])(
.expectToHaveDiagnostics([truthyOnlyConditionalValue.code]);
}
);

test.each(["string", "number", "string | number"])("No warning when using element index in condition (%p)", type => {
util.testExpression`condition[0] ? 1 : 0`
.setTsHeader(`declare var condition: ${type}[];`)
.setOptions({ strict: true })
.expectToHaveNoDiagnostics();
});
2 changes: 1 addition & 1 deletion test/unit/optionalChaining.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe("optional chaining function calls", () => {
const result = func?.(i++);
`
.setOptions({
alwaysStrict: strict,
strict,
target: ScriptTarget.ES5,
})
.expectToMatchJsResult();
Expand Down
X Tutup