mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
Add check for reference-compared literals to JS files (#49164)
This commit is contained in:
parent
6a996ac995
commit
e60cf121ae
@ -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");
|
||||
}
|
||||
|
||||
@ -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,
|
||||
]);
|
||||
|
||||
/**
|
||||
|
||||
12
tests/baselines/reference/plainJSTypeErrors.errors.txt
Normal file
12
tests/baselines/reference/plainJSTypeErrors.errors.txt
Normal 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 ({} == {}) {}
|
||||
|
||||
15
tests/baselines/reference/plainJSTypeErrors.js
Normal file
15
tests/baselines/reference/plainJSTypeErrors.js
Normal 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 ({} == {}) { }
|
||||
10
tests/baselines/reference/plainJSTypeErrors.symbols
Normal file
10
tests/baselines/reference/plainJSTypeErrors.symbols
Normal file
@ -0,0 +1,10 @@
|
||||
//// [tests/cases/conformance/salsa/plainJSTypeErrors.ts] ////
|
||||
|
||||
=== plainJSTypeErrors.js ===
|
||||
|
||||
// should error
|
||||
if ({} === {}) {}
|
||||
|
||||
// should not error
|
||||
if ({} == {}) {}
|
||||
|
||||
15
tests/baselines/reference/plainJSTypeErrors.types
Normal file
15
tests/baselines/reference/plainJSTypeErrors.types
Normal file
@ -0,0 +1,15 @@
|
||||
//// [tests/cases/conformance/salsa/plainJSTypeErrors.ts] ////
|
||||
|
||||
=== plainJSTypeErrors.js ===
|
||||
// should error
|
||||
if ({} === {}) {}
|
||||
>{} === {} : boolean
|
||||
>{} : {}
|
||||
>{} : {}
|
||||
|
||||
// should not error
|
||||
if ({} == {}) {}
|
||||
>{} == {} : boolean
|
||||
>{} : {}
|
||||
>{} : {}
|
||||
|
||||
10
tests/cases/conformance/salsa/plainJSTypeErrors.ts
Normal file
10
tests/cases/conformance/salsa/plainJSTypeErrors.ts
Normal file
@ -0,0 +1,10 @@
|
||||
// @outdir: out/
|
||||
// @target: esnext
|
||||
// @allowJS: true
|
||||
// @filename: plainJSTypeErrors.js
|
||||
|
||||
// should error
|
||||
if ({} === {}) {}
|
||||
|
||||
// should not error
|
||||
if ({} == {}) {}
|
||||
Loading…
x
Reference in New Issue
Block a user