Add check for reference-compared literals to JS files (#49164)

This commit is contained in:
Jack Works
2023-06-13 07:02:25 +08:00
committed by GitHub
parent 6a996ac995
commit e60cf121ae
7 changed files with 69 additions and 1 deletions

View File

@@ -37024,7 +37024,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// control flow analysis it is possible for operands to temporarily have narrower types, and those narrower
// types may cause the operands to not be comparable. We don't want such errors reported (see #46475).
if (!(checkMode && checkMode & CheckMode.TypeOnly)) {
if (isLiteralExpressionOfObject(left) || isLiteralExpressionOfObject(right)) {
if (
(isLiteralExpressionOfObject(left) || isLiteralExpressionOfObject(right)) &&
// only report for === and !== in JS, not == or !=
(!isInJSFile(left) || (operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken))
) {
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
}

View File

@@ -1430,6 +1430,8 @@ export const plainJSErrors: Set<number> = new Set([
Diagnostics.Class_constructor_may_not_be_a_generator.code,
Diagnostics.Class_constructor_may_not_be_an_accessor.code,
Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
// Type errors
Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value.code,
]);
/**