-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Avoid binding readonly properties as special properties on a function symbol #49146
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
Open
Andarist
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/fn-readonly-length
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6046ffb
Avoid binding `length` property as special property on a function symbol
Andarist 9438d2b
Avoid binding `name` property as special property on a function symbol
Andarist 608b28e
Merge remote-tracking branch 'origin/main' into fix/fn-readonly-length
Andarist 261ed86
Merge remote-tracking branch 'origin/main' into fix/fn-readonly-length
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
27 changes: 27 additions & 0 deletions
27
tests/baselines/reference/functionReadonlyLength.errors.txt
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,27 @@ | ||
| functionReadonlyLength.ts(2,4): error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
| functionReadonlyLength.ts(5,4): error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
| functionReadonlyLength.ts(8,4): error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
| functionReadonlyLength.ts(11,4): error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
|
|
||
|
|
||
| ==== functionReadonlyLength.ts (4 errors) ==== | ||
| const f0 = new Function() | ||
| f0.length = 1; | ||
| ~~~~~~ | ||
| !!! error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
|
|
||
| function f1() {}; | ||
| f1.length = 1; | ||
| ~~~~~~ | ||
| !!! error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
|
|
||
| const f2 = function () {}; | ||
| f2.length = 1 | ||
| ~~~~~~ | ||
| !!! error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
|
|
||
| const f3 = () => {} | ||
| f3.length = 1 | ||
| ~~~~~~ | ||
| !!! error TS2540: Cannot assign to 'length' because it is a read-only property. | ||
|
|
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,36 @@ | ||
| //// [tests/cases/compiler/functionReadonlyLength.ts] //// | ||
|
|
||
| === functionReadonlyLength.ts === | ||
| const f0 = new Function() | ||
| >f0 : Symbol(f0, Decl(functionReadonlyLength.ts, 0, 5)) | ||
| >Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
|
||
| f0.length = 1; | ||
| >f0.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) | ||
| >f0 : Symbol(f0, Decl(functionReadonlyLength.ts, 0, 5)) | ||
| >length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) | ||
|
|
||
| function f1() {}; | ||
| >f1 : Symbol(f1, Decl(functionReadonlyLength.ts, 1, 14)) | ||
|
|
||
| f1.length = 1; | ||
| >f1.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) | ||
| >f1 : Symbol(f1, Decl(functionReadonlyLength.ts, 1, 14)) | ||
| >length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) | ||
|
|
||
| const f2 = function () {}; | ||
| >f2 : Symbol(f2, Decl(functionReadonlyLength.ts, 6, 5)) | ||
|
|
||
| f2.length = 1 | ||
| >f2.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) | ||
| >f2 : Symbol(f2, Decl(functionReadonlyLength.ts, 6, 5)) | ||
| >length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) | ||
|
|
||
| const f3 = () => {} | ||
| >f3 : Symbol(f3, Decl(functionReadonlyLength.ts, 9, 5)) | ||
|
|
||
| f3.length = 1 | ||
| >f3.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) | ||
| >f3 : Symbol(f3, Decl(functionReadonlyLength.ts, 9, 5)) | ||
| >length : Symbol(Function.length, Decl(lib.es5.d.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,47 @@ | ||
| //// [tests/cases/compiler/functionReadonlyLength.ts] //// | ||
|
|
||
| === functionReadonlyLength.ts === | ||
| const f0 = new Function() | ||
| >f0 : Function | ||
| >new Function() : Function | ||
| >Function : FunctionConstructor | ||
|
|
||
| f0.length = 1; | ||
| >f0.length = 1 : 1 | ||
| >f0.length : any | ||
| >f0 : Function | ||
| >length : any | ||
| >1 : 1 | ||
|
|
||
| function f1() {}; | ||
| >f1 : () => void | ||
|
|
||
| f1.length = 1; | ||
| >f1.length = 1 : 1 | ||
| >f1.length : any | ||
| >f1 : () => void | ||
| >length : any | ||
| >1 : 1 | ||
|
|
||
| const f2 = function () {}; | ||
| >f2 : () => void | ||
| >function () {} : () => void | ||
|
|
||
| f2.length = 1 | ||
| >f2.length = 1 : 1 | ||
| >f2.length : any | ||
| >f2 : () => void | ||
| >length : any | ||
| >1 : 1 | ||
|
|
||
| const f3 = () => {} | ||
| >f3 : () => void | ||
| >() => {} : () => void | ||
|
|
||
| f3.length = 1 | ||
| >f3.length = 1 : 1 | ||
| >f3.length : any | ||
| >f3 : () => void | ||
| >length : any | ||
| >1 : 1 | ||
|
|
||
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,27 @@ | ||
| functionReadonlyName.ts(2,4): error TS2339: Property 'name' does not exist on type 'Function'. | ||
| functionReadonlyName.ts(5,4): error TS2339: Property 'name' does not exist on type '() => void'. | ||
| functionReadonlyName.ts(8,4): error TS2339: Property 'name' does not exist on type '() => void'. | ||
| functionReadonlyName.ts(11,4): error TS2339: Property 'name' does not exist on type '() => void'. | ||
|
|
||
|
|
||
| ==== functionReadonlyName.ts (4 errors) ==== | ||
| const f0 = new Function() | ||
| f0.name = 'foo'; | ||
| ~~~~ | ||
| !!! error TS2339: Property 'name' does not exist on type 'Function'. | ||
|
|
||
| function f1() {}; | ||
| f1.name = 'foo'; | ||
| ~~~~ | ||
| !!! error TS2339: Property 'name' does not exist on type '() => void'. | ||
|
|
||
| const f2 = function () {}; | ||
| f2.name = 'foo' | ||
| ~~~~ | ||
| !!! error TS2339: Property 'name' does not exist on type '() => void'. | ||
|
|
||
| const f3 = () => {} | ||
| f3.name = 'foo' | ||
| ~~~~ | ||
| !!! error TS2339: Property 'name' does not exist on type '() => void'. | ||
|
|
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/compiler/functionReadonlyName.ts] //// | ||
|
|
||
| === functionReadonlyName.ts === | ||
| const f0 = new Function() | ||
| >f0 : Symbol(f0, Decl(functionReadonlyName.ts, 0, 5)) | ||
| >Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
|
||
| f0.name = 'foo'; | ||
| >f0 : Symbol(f0, Decl(functionReadonlyName.ts, 0, 5)) | ||
|
|
||
| function f1() {}; | ||
| >f1 : Symbol(f1, Decl(functionReadonlyName.ts, 1, 16)) | ||
|
|
||
| f1.name = 'foo'; | ||
| >f1 : Symbol(f1, Decl(functionReadonlyName.ts, 1, 16)) | ||
|
|
||
| const f2 = function () {}; | ||
| >f2 : Symbol(f2, Decl(functionReadonlyName.ts, 6, 5)) | ||
|
|
||
| f2.name = 'foo' | ||
| >f2 : Symbol(f2, Decl(functionReadonlyName.ts, 6, 5)) | ||
|
|
||
| const f3 = () => {} | ||
| >f3 : Symbol(f3, Decl(functionReadonlyName.ts, 9, 5)) | ||
|
|
||
| f3.name = 'foo' | ||
| >f3 : Symbol(f3, Decl(functionReadonlyName.ts, 9, 5)) | ||
|
|
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,47 @@ | ||
| //// [tests/cases/compiler/functionReadonlyName.ts] //// | ||
|
|
||
| === functionReadonlyName.ts === | ||
| const f0 = new Function() | ||
| >f0 : Function | ||
| >new Function() : Function | ||
| >Function : FunctionConstructor | ||
|
|
||
| f0.name = 'foo'; | ||
| >f0.name = 'foo' : "foo" | ||
| >f0.name : any | ||
| >f0 : Function | ||
| >name : any | ||
| >'foo' : "foo" | ||
|
|
||
| function f1() {}; | ||
| >f1 : () => void | ||
|
|
||
| f1.name = 'foo'; | ||
| >f1.name = 'foo' : "foo" | ||
| >f1.name : any | ||
| >f1 : () => void | ||
| >name : any | ||
| >'foo' : "foo" | ||
|
|
||
| const f2 = function () {}; | ||
| >f2 : () => void | ||
| >function () {} : () => void | ||
|
|
||
| f2.name = 'foo' | ||
| >f2.name = 'foo' : "foo" | ||
| >f2.name : any | ||
| >f2 : () => void | ||
| >name : any | ||
| >'foo' : "foo" | ||
|
|
||
| const f3 = () => {} | ||
| >f3 : () => void | ||
| >() => {} : () => void | ||
|
|
||
| f3.name = 'foo' | ||
| >f3.name = 'foo' : "foo" | ||
| >f3.name : any | ||
| >f3 : () => void | ||
| >name : any | ||
| >'foo' : "foo" | ||
|
|
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,13 @@ | ||
| // @noEmit: true | ||
|
|
||
| const f0 = new Function() | ||
| f0.length = 1; | ||
|
|
||
| function f1() {}; | ||
| f1.length = 1; | ||
|
|
||
| const f2 = function () {}; | ||
| f2.length = 1 | ||
|
|
||
| const f3 = () => {} | ||
| f3.length = 1 |
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,13 @@ | ||
| // @noEmit: true | ||
|
|
||
| const f0 = new Function() | ||
| f0.name = 'foo'; | ||
|
|
||
| function f1() {}; | ||
| f1.name = 'foo'; | ||
|
|
||
| const f2 = function () {}; | ||
| f2.name = 'foo' | ||
|
|
||
| const f3 = () => {} | ||
| f3.name = 'foo' |
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.
this feels wrong - perhaps the
.typesprinter tries to only lookup the "target" symbol of this assignment and doesn't try to look intoFunctionhere etc. Should I try to fix this as part of this PR?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.
This may be fine. What the type writer is saying is that the type of the expression
f0.length = 1is1, which is correct: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 was specifically referring to the 9th line that has
f0.length : anyand it seems that thisanyis a little bit off here as I would expect this to be of typenumber🤔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.
Ah, but actually this is already the case for
new Functioncase so perhaps this is how it's printed for the readonly LHS. It's still a little bit confusing to me - but at least my PR doesn't really change anything in this regard 🤷♂️