Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37028,7 +37028,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");
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/plainJSTypeErrors.errors.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/baselines/reference/plainJSTypeErrors.js
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions tests/baselines/reference/plainJSTypeErrors.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [tests/cases/conformance/salsa/plainJSTypeErrors.ts] ////

=== plainJSTypeErrors.js ===

// should error
if ({} === {}) {}

// should not error
if ({} == {}) {}

15 changes: 15 additions & 0 deletions tests/baselines/reference/plainJSTypeErrors.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/conformance/salsa/plainJSTypeErrors.ts] ////

=== plainJSTypeErrors.js ===
// should error
if ({} === {}) {}
>{} === {} : boolean
>{} : {}
>{} : {}

// should not error
if ({} == {}) {}
>{} == {} : boolean
>{} : {}
>{} : {}

10 changes: 10 additions & 0 deletions tests/cases/conformance/salsa/plainJSTypeErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @outdir: out/
// @target: esnext
// @allowJS: true
// @filename: plainJSTypeErrors.js

// should error
if ({} === {}) {}

// should not error
if ({} == {}) {}