Skip to content

Commit 1d4dd02

Browse files
committed
chore: add TS2839 to JS files
chore: add test files chore: update tests
1 parent 33eac28 commit 1d4dd02

File tree

7 files changed

+65
-2
lines changed

7 files changed

+65
-2
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37029,8 +37029,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3702937029
// types may cause the operands to not be comparable. We don't want such errors reported (see #46475).
3703037030
if (!(checkMode && checkMode & CheckMode.TypeOnly)) {
3703137031
if (isLiteralExpressionOfObject(left) || isLiteralExpressionOfObject(right)) {
37032-
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
37033-
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
37032+
if (isInJSFile(left) && (operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken)) {
37033+
// we skip the emit for {} == {} in JS files
37034+
}
37035+
else {
37036+
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
37037+
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
37038+
}
3703437039
}
3703537040
checkNaNEquality(errorNode, operator, left, right);
3703637041
reportOperatorErrorUnless((left, right) => isTypeEqualityComparableTo(left, right) || isTypeEqualityComparableTo(right, left));

src/compiler/program.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,8 @@ export const plainJSErrors: Set<number> = new Set([
14301430
Diagnostics.Class_constructor_may_not_be_a_generator.code,
14311431
Diagnostics.Class_constructor_may_not_be_an_accessor.code,
14321432
Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
1433+
// Type errors
1434+
Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value.code,
14331435
]);
14341436

14351437
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/salsa/plainJSTypeErrors.js(2,5): error TS2839: This condition will always return 'false' since JavaScript compares objects by reference, not value.
2+
3+
4+
==== tests/cases/conformance/salsa/plainJSTypeErrors.js (1 errors) ====
5+
// should error
6+
if ({} === {}) {}
7+
~~~~~~~~~
8+
!!! error TS2839: This condition will always return 'false' since JavaScript compares objects by reference, not value.
9+
10+
// should not error
11+
if ({} == {}) {}
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [plainJSTypeErrors.js]
2+
// should error
3+
if ({} === {}) {}
4+
5+
// should not error
6+
if ({} == {}) {}
7+
8+
9+
//// [plainJSTypeErrors.js]
10+
// should error
11+
if ({} === {}) { }
12+
// should not error
13+
if ({} == {}) { }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/salsa/plainJSTypeErrors.js ===
2+
// should error
3+
No type information for this code.if ({} === {}) {}
4+
No type information for this code.
5+
No type information for this code.// should not error
6+
No type information for this code.if ({} == {}) {}
7+
No type information for this code.
8+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/salsa/plainJSTypeErrors.js ===
2+
// should error
3+
if ({} === {}) {}
4+
>{} === {} : boolean
5+
>{} : {}
6+
>{} : {}
7+
8+
// should not error
9+
if ({} == {}) {}
10+
>{} == {} : boolean
11+
>{} : {}
12+
>{} : {}
13+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @outdir: out/
2+
// @target: esnext
3+
// @allowJS: true
4+
// @filename: plainJSTypeErrors.js
5+
6+
// should error
7+
if ({} === {}) {}
8+
9+
// should not error
10+
if ({} == {}) {}

0 commit comments

Comments
 (0)