-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix handling of prologue statements when there are parameter property declarations #48775
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 4 commits into
microsoft:main
from
jlusty:fix-prologue-handling-no-super
Apr 20, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74c337b
Fix handling of prologue statements when there are
jlusty bd2a2ec
Add second test case
jlusty d45098e
Update to match old TS version: parameter properties after prologue
jlusty 891cb6f
Parametert properties should be added after any prologue
jlusty 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
72 changes: 72 additions & 0 deletions
72
tests/baselines/reference/constructorWithParameterPropertiesAndPrivateFields.es2015.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,72 @@ | ||
//// [constructorWithParameterPropertiesAndPrivateFields.es2015.ts] | ||
// https://github.com/microsoft/TypeScript/issues/48771 | ||
|
||
class A { | ||
readonly #privateField: string; | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
({ key: this.#privateField } = arg); | ||
} | ||
|
||
log() { | ||
console.log(this.#privateField); | ||
console.log(this.exposedField); | ||
} | ||
} | ||
|
||
class B { | ||
readonly #privateField: string; | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
"prologue"; | ||
({ key: this.#privateField } = arg); | ||
} | ||
|
||
log() { | ||
console.log(this.#privateField); | ||
console.log(this.exposedField); | ||
} | ||
} | ||
|
||
|
||
//// [constructorWithParameterPropertiesAndPrivateFields.es2015.js] | ||
// https://github.com/microsoft/TypeScript/issues/48771 | ||
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 __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 _A_privateField, _B_privateField; | ||
class A { | ||
constructor(arg, exposedField) { | ||
var _a; | ||
this.exposedField = exposedField; | ||
_A_privateField.set(this, void 0); | ||
(_a = this, { key: ({ set value(_b) { __classPrivateFieldSet(_a, _A_privateField, _b, "f"); } }).value } = arg); | ||
} | ||
log() { | ||
console.log(__classPrivateFieldGet(this, _A_privateField, "f")); | ||
console.log(this.exposedField); | ||
} | ||
} | ||
_A_privateField = new WeakMap(); | ||
class B { | ||
constructor(arg, exposedField) { | ||
"prologue"; | ||
var _a; | ||
this.exposedField = exposedField; | ||
_B_privateField.set(this, void 0); | ||
(_a = this, { key: ({ set value(_b) { __classPrivateFieldSet(_a, _B_privateField, _b, "f"); } }).value } = arg); | ||
} | ||
log() { | ||
console.log(__classPrivateFieldGet(this, _B_privateField, "f")); | ||
console.log(this.exposedField); | ||
} | ||
} | ||
_B_privateField = new WeakMap(); |
80 changes: 80 additions & 0 deletions
80
tests/baselines/reference/constructorWithParameterPropertiesAndPrivateFields.es2015.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,80 @@ | ||
=== tests/cases/compiler/constructorWithParameterPropertiesAndPrivateFields.es2015.ts === | ||
// https://github.com/microsoft/TypeScript/issues/48771 | ||
|
||
class A { | ||
>A : Symbol(A, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 0, 0)) | ||
|
||
readonly #privateField: string; | ||
>#privateField : Symbol(A.#privateField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 2, 9)) | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
>arg : Symbol(arg, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 5, 14)) | ||
>key : Symbol(key, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 5, 20)) | ||
>exposedField : Symbol(A.exposedField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 5, 35)) | ||
|
||
({ key: this.#privateField } = arg); | ||
>key : Symbol(key, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 6, 6)) | ||
>this.#privateField : Symbol(A.#privateField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 2, 9)) | ||
>this : Symbol(A, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 0, 0)) | ||
>arg : Symbol(arg, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 5, 14)) | ||
} | ||
|
||
log() { | ||
>log : Symbol(A.log, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 7, 3)) | ||
|
||
console.log(this.#privateField); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>this.#privateField : Symbol(A.#privateField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 2, 9)) | ||
>this : Symbol(A, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 0, 0)) | ||
|
||
console.log(this.exposedField); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>this.exposedField : Symbol(A.exposedField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 5, 35)) | ||
>this : Symbol(A, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 0, 0)) | ||
>exposedField : Symbol(A.exposedField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 5, 35)) | ||
} | ||
} | ||
|
||
class B { | ||
>B : Symbol(B, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 13, 1)) | ||
|
||
readonly #privateField: string; | ||
>#privateField : Symbol(B.#privateField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 15, 9)) | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
>arg : Symbol(arg, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 18, 14)) | ||
>key : Symbol(key, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 18, 20)) | ||
>exposedField : Symbol(B.exposedField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 18, 35)) | ||
|
||
"prologue"; | ||
({ key: this.#privateField } = arg); | ||
>key : Symbol(key, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 20, 6)) | ||
>this.#privateField : Symbol(B.#privateField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 15, 9)) | ||
>this : Symbol(B, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 13, 1)) | ||
>arg : Symbol(arg, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 18, 14)) | ||
} | ||
|
||
log() { | ||
>log : Symbol(B.log, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 21, 3)) | ||
|
||
console.log(this.#privateField); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>this.#privateField : Symbol(B.#privateField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 15, 9)) | ||
>this : Symbol(B, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 13, 1)) | ||
|
||
console.log(this.exposedField); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>this.exposedField : Symbol(B.exposedField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 18, 35)) | ||
>this : Symbol(B, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 13, 1)) | ||
>exposedField : Symbol(B.exposedField, Decl(constructorWithParameterPropertiesAndPrivateFields.es2015.ts, 18, 35)) | ||
} | ||
} | ||
|
92 changes: 92 additions & 0 deletions
92
tests/baselines/reference/constructorWithParameterPropertiesAndPrivateFields.es2015.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,92 @@ | ||
=== tests/cases/compiler/constructorWithParameterPropertiesAndPrivateFields.es2015.ts === | ||
// https://github.com/microsoft/TypeScript/issues/48771 | ||
|
||
class A { | ||
>A : A | ||
|
||
readonly #privateField: string; | ||
>#privateField : string | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
>arg : { key: string; } | ||
>key : string | ||
>exposedField : number | ||
|
||
({ key: this.#privateField } = arg); | ||
>({ key: this.#privateField } = arg) : { key: string; } | ||
>{ key: this.#privateField } = arg : { key: string; } | ||
>{ key: this.#privateField } : { key: string; } | ||
>key : string | ||
>this.#privateField : string | ||
>this : this | ||
>arg : { key: string; } | ||
} | ||
|
||
log() { | ||
>log : () => void | ||
|
||
console.log(this.#privateField); | ||
>console.log(this.#privateField) : void | ||
>console.log : (...data: any[]) => void | ||
>console : Console | ||
>log : (...data: any[]) => void | ||
>this.#privateField : string | ||
>this : this | ||
|
||
console.log(this.exposedField); | ||
>console.log(this.exposedField) : void | ||
>console.log : (...data: any[]) => void | ||
>console : Console | ||
>log : (...data: any[]) => void | ||
>this.exposedField : number | ||
>this : this | ||
>exposedField : number | ||
} | ||
} | ||
|
||
class B { | ||
>B : B | ||
|
||
readonly #privateField: string; | ||
>#privateField : string | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
>arg : { key: string; } | ||
>key : string | ||
>exposedField : number | ||
|
||
"prologue"; | ||
>"prologue" : "prologue" | ||
|
||
({ key: this.#privateField } = arg); | ||
>({ key: this.#privateField } = arg) : { key: string; } | ||
>{ key: this.#privateField } = arg : { key: string; } | ||
>{ key: this.#privateField } : { key: string; } | ||
>key : string | ||
>this.#privateField : string | ||
>this : this | ||
>arg : { key: string; } | ||
} | ||
|
||
log() { | ||
>log : () => void | ||
|
||
console.log(this.#privateField); | ||
>console.log(this.#privateField) : void | ||
>console.log : (...data: any[]) => void | ||
>console : Console | ||
>log : (...data: any[]) => void | ||
>this.#privateField : string | ||
>this : this | ||
|
||
console.log(this.exposedField); | ||
>console.log(this.exposedField) : void | ||
>console.log : (...data: any[]) => void | ||
>console : Console | ||
>log : (...data: any[]) => void | ||
>this.exposedField : number | ||
>this : this | ||
>exposedField : number | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
tests/cases/compiler/constructorWithParameterPropertiesAndPrivateFields.es2015.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,29 @@ | ||
// @target: es2015 | ||
// https://github.com/microsoft/TypeScript/issues/48771 | ||
|
||
class A { | ||
readonly #privateField: string; | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
({ key: this.#privateField } = arg); | ||
} | ||
|
||
log() { | ||
console.log(this.#privateField); | ||
console.log(this.exposedField); | ||
} | ||
} | ||
|
||
class B { | ||
readonly #privateField: string; | ||
|
||
constructor(arg: { key: string }, public exposedField: number) { | ||
"prologue"; | ||
({ key: this.#privateField } = arg); | ||
} | ||
|
||
log() { | ||
console.log(this.#privateField); | ||
console.log(this.exposedField); | ||
} | ||
} |
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've updated so we include parameter properties after any prologues - I believe this is the correct way of doing it, since that's what the existing TS version does
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.
Yes, see also #48687 and #48765, which fixed this for other transforms.