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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,
]);
/**

View File

@ -0,0 +1,12 @@
plainJSTypeErrors.js(2,5): error TS2839: This condition will always return 'false' since JavaScript compares objects by reference, not value.
==== plainJSTypeErrors.js (1 errors) ====
// should error
if ({} === {}) {}
~~~~~~~~~
!!! error TS2839: This condition will always return 'false' since JavaScript compares objects by reference, not value.
// should not error
if ({} == {}) {}

View File

@ -0,0 +1,15 @@
//// [tests/cases/conformance/salsa/plainJSTypeErrors.ts] ////
//// [plainJSTypeErrors.js]
// should error
if ({} === {}) {}
// should not error
if ({} == {}) {}
//// [plainJSTypeErrors.js]
// should error
if ({} === {}) { }
// should not error
if ({} == {}) { }

View File

@ -0,0 +1,10 @@
//// [tests/cases/conformance/salsa/plainJSTypeErrors.ts] ////
=== plainJSTypeErrors.js ===
// should error
if ({} === {}) {}
// should not error
if ({} == {}) {}

View File

@ -0,0 +1,15 @@
//// [tests/cases/conformance/salsa/plainJSTypeErrors.ts] ////
=== plainJSTypeErrors.js ===
// should error
if ({} === {}) {}
>{} === {} : boolean
>{} : {}
>{} : {}
// should not error
if ({} == {}) {}
>{} == {} : boolean
>{} : {}
>{} : {}

View File

@ -0,0 +1,10 @@
// @outdir: out/
// @target: esnext
// @allowJS: true
// @filename: plainJSTypeErrors.js
// should error
if ({} === {}) {}
// should not error
if ({} == {}) {}