-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Fixed nullish coalesce operator's precedence #61372
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
jakebailey
merged 7 commits into
microsoft:main
from
Andarist:fix/nullish-coalesce-precedence
Jun 30, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b77d0e
Fixed nullish coalesce operator's precedence
Andarist 6f5d4a1
fmt
Andarist d99057d
add extra test case
Andarist 3f8cff6
Merge remote-tracking branch 'origin/main' into fix/nullish-coalesce-…
Andarist 85937ab
Merge remote-tracking branch 'origin/main' into fix/nullish-coalesce-…
Andarist 538ffd8
fix missing error
Andarist 955d8e0
unify style of the cases
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
tests/baselines/reference/nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//// [tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts] //// | ||
|
||
//// [nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts] | ||
// https://github.com/microsoft/TypeScript/issues/61109 | ||
|
||
class Cls { | ||
#privateProp: number | undefined; | ||
|
||
problem() { | ||
this.#privateProp ??= false ? neverThis() : 20; | ||
} | ||
} | ||
|
||
function neverThis(): never { | ||
throw new Error("This should really really never happen!"); | ||
} | ||
|
||
|
||
//// [nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.js] | ||
"use strict"; | ||
// https://github.com/microsoft/TypeScript/issues/61109 | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
if (kind === "m") throw new TypeError("Private method is not writable"); | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); | ||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; | ||
}; | ||
var _Cls_privateProp; | ||
class Cls { | ||
constructor() { | ||
_Cls_privateProp.set(this, void 0); | ||
} | ||
problem() { | ||
__classPrivateFieldSet(this, _Cls_privateProp, __classPrivateFieldGet(this, _Cls_privateProp, "f") ?? (false ? neverThis() : 20), "f"); | ||
} | ||
} | ||
_Cls_privateProp = new WeakMap(); | ||
function neverThis() { | ||
throw new Error("This should really really never happen!"); | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/baselines/reference/nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//// [tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts] //// | ||
|
||
=== nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/61109 | ||
|
||
class Cls { | ||
>Cls : Symbol(Cls, Decl(nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts, 0, 0)) | ||
|
||
#privateProp: number | undefined; | ||
>#privateProp : Symbol(Cls.#privateProp, Decl(nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts, 2, 11)) | ||
|
||
problem() { | ||
>problem : Symbol(Cls.problem, Decl(nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts, 3, 35)) | ||
|
||
this.#privateProp ??= false ? neverThis() : 20; | ||
>this.#privateProp : Symbol(Cls.#privateProp, Decl(nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts, 2, 11)) | ||
>this : Symbol(Cls, Decl(nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts, 0, 0)) | ||
>neverThis : Symbol(neverThis, Decl(nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts, 8, 1)) | ||
} | ||
} | ||
|
||
function neverThis(): never { | ||
>neverThis : Symbol(neverThis, Decl(nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts, 8, 1)) | ||
|
||
throw new Error("This should really really never happen!"); | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
tests/baselines/reference/nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//// [tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts] //// | ||
|
||
=== nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/61109 | ||
|
||
class Cls { | ||
>Cls : Cls | ||
> : ^^^ | ||
|
||
#privateProp: number | undefined; | ||
>#privateProp : number | undefined | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
|
||
problem() { | ||
>problem : () => void | ||
> : ^^^^^^^^^^ | ||
|
||
this.#privateProp ??= false ? neverThis() : 20; | ||
>this.#privateProp ??= false ? neverThis() : 20 : number | ||
> : ^^^^^^ | ||
>this.#privateProp : number | undefined | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
>this : this | ||
> : ^^^^ | ||
>false ? neverThis() : 20 : 20 | ||
> : ^^ | ||
>false : false | ||
> : ^^^^^ | ||
>neverThis() : never | ||
> : ^^^^^ | ||
>neverThis : () => never | ||
> : ^^^^^^ | ||
>20 : 20 | ||
> : ^^ | ||
} | ||
} | ||
|
||
function neverThis(): never { | ||
>neverThis : () => never | ||
> : ^^^^^^ | ||
|
||
throw new Error("This should really really never happen!"); | ||
>new Error("This should really really never happen!") : Error | ||
> : ^^^^^ | ||
>Error : ErrorConstructor | ||
> : ^^^^^^^^^^^^^^^^ | ||
>"This should really really never happen!" : "This should really really never happen!" | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...pi/printsNodeCorrectly.binaryAmpersandAmpersandExpressionWithLeftConditionalExpression.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(a ? b : c) && d; |
1 change: 1 addition & 0 deletions
1
...nce/printerApi/printsNodeCorrectly.binaryBarBarExpressionWithLeftConditionalExpression.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(a ? b : c) || d; |
1 change: 1 addition & 0 deletions
1
...rApi/printsNodeCorrectly.binaryQuestionQuestionExpressionWithLeftConditionalExpression.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(a ? b : c) ?? d; |
16 changes: 16 additions & 0 deletions
16
...xpressions/nullishCoalescingOperator/nullishCoalescingAssignmentVsPrivateFieldsJsEmit1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @strict: true | ||
// @target: es2021 | ||
|
||
// https://github.com/microsoft/TypeScript/issues/61109 | ||
|
||
class Cls { | ||
#privateProp: number | undefined; | ||
|
||
problem() { | ||
this.#privateProp ??= false ? neverThis() : 20; | ||
} | ||
} | ||
|
||
function neverThis(): never { | ||
throw new Error("This should really really never happen!"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder how old this comment is...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 years, it was added in #35282