Skip to content

Commit 23c0797

Browse files
committed
chore: add TS2839 to JS files
chore: add test files chore: update tests
1 parent 63d0574 commit 23c0797

File tree

7 files changed

+65
-2
lines changed

7 files changed

+65
-2
lines changed

src/compiler/checker.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -34083,8 +34083,13 @@ namespace ts {
3408334083
case SyntaxKind.EqualsEqualsEqualsToken:
3408434084
case SyntaxKind.ExclamationEqualsEqualsToken:
3408534085
if (isLiteralExpressionOfObject(left) || isLiteralExpressionOfObject(right)) {
34086-
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
34087-
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
34086+
if (isInJSFile(left) && (operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken)) {
34087+
// we skip the emit for {} == {} in JS files
34088+
}
34089+
else {
34090+
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
34091+
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
34092+
}
3408834093
}
3408934094
reportOperatorErrorUnless((left, right) => isTypeEqualityComparableTo(left, right) || isTypeEqualityComparableTo(right, left));
3409034095
return booleanType;

src/compiler/program.ts

+2
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ namespace ts {
955955
Diagnostics.Class_constructor_may_not_be_a_generator.code,
956956
Diagnostics.Class_constructor_may_not_be_an_accessor.code,
957957
Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
958+
// Type errors
959+
Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value.code,
958960
]);
959961

960962
/**
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+
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 ({} == {}) { }
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.
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+
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)