-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Report error for function declarations as statement children in strict mode #62899
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
thromel
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
thromel:fix/function-declaration-statement-error
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.
+364
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f55c6a2
Report error for function declarations as statement children in stric…
thromel 6a1ff4b
Report error unconditionally since TypeScript assumes strict mode
thromel e1078dd
Update baselines for labeled function declaration errors
thromel 3b6c859
Only report error in strict mode as per review feedback
thromel 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
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/functionDeclarationAsStatementInStrictMode.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,53 @@ | ||
| functionDeclarationAsStatementInStrictMode.ts(2,20): error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| functionDeclarationAsStatementInStrictMode.ts(3,23): error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| functionDeclarationAsStatementInStrictMode.ts(4,13): error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| functionDeclarationAsStatementInStrictMode.ts(5,19): error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| functionDeclarationAsStatementInStrictMode.ts(6,28): error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| functionDeclarationAsStatementInStrictMode.ts(7,28): error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| functionDeclarationAsStatementInStrictMode.ts(8,1): error TS1344: 'A label is not allowed here. | ||
| functionDeclarationAsStatementInStrictMode.ts(8,17): error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
|
|
||
|
|
||
| ==== functionDeclarationAsStatementInStrictMode.ts (8 errors) ==== | ||
| // Error cases - function declarations as direct children of statements in strict mode | ||
| if (true) function f1() {} | ||
| ~~ | ||
| !!! error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| while (true) function f2() {} | ||
| ~~ | ||
| !!! error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| do function f3() {} while (false); | ||
| ~~ | ||
| !!! error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| for (;;) function f4() {} | ||
| ~~ | ||
| !!! error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| for (let x in {}) function f5() {} | ||
| ~~ | ||
| !!! error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| for (let x of []) function f6() {} | ||
| ~~ | ||
| !!! error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
| label: function f7() {} | ||
| ~~~~~ | ||
| !!! error TS1344: 'A label is not allowed here. | ||
| ~~ | ||
| !!! error TS1256: In strict mode code, functions can only be declared at top level or inside a block. | ||
|
|
||
| // Valid cases - function declarations inside blocks | ||
| if (true) { function g1() {} } | ||
| while (true) { function g2() {} } | ||
| do { function g3() {} } while (false); | ||
| for (;;) { function g4() {} } | ||
| for (let x in {}) { function g5() {} } | ||
| for (let x of []) { function g6() {} } | ||
| label: { function g7() {} } | ||
|
|
||
| // Valid - top level | ||
| function topLevel() {} | ||
|
|
||
| // Valid - inside function body | ||
| function outer() { | ||
| function inner() {} | ||
| } | ||
|
|
75 changes: 75 additions & 0 deletions
75
tests/baselines/reference/functionDeclarationAsStatementInStrictMode.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,75 @@ | ||
| //// [tests/cases/compiler/functionDeclarationAsStatementInStrictMode.ts] //// | ||
|
|
||
| //// [functionDeclarationAsStatementInStrictMode.ts] | ||
| // Error cases - function declarations as direct children of statements in strict mode | ||
| if (true) function f1() {} | ||
| while (true) function f2() {} | ||
| do function f3() {} while (false); | ||
| for (;;) function f4() {} | ||
| for (let x in {}) function f5() {} | ||
| for (let x of []) function f6() {} | ||
| label: function f7() {} | ||
|
|
||
| // Valid cases - function declarations inside blocks | ||
| if (true) { function g1() {} } | ||
| while (true) { function g2() {} } | ||
| do { function g3() {} } while (false); | ||
| for (;;) { function g4() {} } | ||
| for (let x in {}) { function g5() {} } | ||
| for (let x of []) { function g6() {} } | ||
| label: { function g7() {} } | ||
|
|
||
| // Valid - top level | ||
| function topLevel() {} | ||
|
|
||
| // Valid - inside function body | ||
| function outer() { | ||
| function inner() {} | ||
| } | ||
|
|
||
|
|
||
| //// [functionDeclarationAsStatementInStrictMode.js] | ||
| "use strict"; | ||
| // Error cases - function declarations as direct children of statements in strict mode | ||
| if (true) | ||
| function f1() { } | ||
| while (true) | ||
| function f2() { } | ||
| do | ||
| function f3() { } | ||
| while (false); | ||
| for (;;) | ||
| function f4() { } | ||
| for (let x in {}) | ||
| function f5() { } | ||
| for (let x of []) | ||
| function f6() { } | ||
| label: function f7() { } | ||
| // Valid cases - function declarations inside blocks | ||
| if (true) { | ||
| function g1() { } | ||
| } | ||
| while (true) { | ||
| function g2() { } | ||
| } | ||
| do { | ||
| function g3() { } | ||
| } while (false); | ||
| for (;;) { | ||
| function g4() { } | ||
| } | ||
| for (let x in {}) { | ||
| function g5() { } | ||
| } | ||
| for (let x of []) { | ||
| function g6() { } | ||
| } | ||
| label: { | ||
| function g7() { } | ||
| } | ||
| // Valid - top level | ||
| function topLevel() { } | ||
| // Valid - inside function body | ||
| function outer() { | ||
| function inner() { } | ||
| } |
63 changes: 63 additions & 0 deletions
63
tests/baselines/reference/functionDeclarationAsStatementInStrictMode.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,63 @@ | ||
| //// [tests/cases/compiler/functionDeclarationAsStatementInStrictMode.ts] //// | ||
|
|
||
| === functionDeclarationAsStatementInStrictMode.ts === | ||
| // Error cases - function declarations as direct children of statements in strict mode | ||
| if (true) function f1() {} | ||
| >f1 : Symbol(f1, Decl(functionDeclarationAsStatementInStrictMode.ts, 1, 9)) | ||
|
|
||
| while (true) function f2() {} | ||
| >f2 : Symbol(f2, Decl(functionDeclarationAsStatementInStrictMode.ts, 2, 12)) | ||
|
|
||
| do function f3() {} while (false); | ||
| >f3 : Symbol(f3, Decl(functionDeclarationAsStatementInStrictMode.ts, 3, 2)) | ||
|
|
||
| for (;;) function f4() {} | ||
| >f4 : Symbol(f4, Decl(functionDeclarationAsStatementInStrictMode.ts, 4, 8)) | ||
|
|
||
| for (let x in {}) function f5() {} | ||
| >x : Symbol(x, Decl(functionDeclarationAsStatementInStrictMode.ts, 5, 8)) | ||
| >f5 : Symbol(f5, Decl(functionDeclarationAsStatementInStrictMode.ts, 5, 17)) | ||
|
|
||
| for (let x of []) function f6() {} | ||
| >x : Symbol(x, Decl(functionDeclarationAsStatementInStrictMode.ts, 6, 8)) | ||
| >f6 : Symbol(f6, Decl(functionDeclarationAsStatementInStrictMode.ts, 6, 17)) | ||
|
|
||
| label: function f7() {} | ||
| >f7 : Symbol(f7, Decl(functionDeclarationAsStatementInStrictMode.ts, 7, 6)) | ||
|
|
||
| // Valid cases - function declarations inside blocks | ||
| if (true) { function g1() {} } | ||
| >g1 : Symbol(g1, Decl(functionDeclarationAsStatementInStrictMode.ts, 10, 11)) | ||
|
|
||
| while (true) { function g2() {} } | ||
| >g2 : Symbol(g2, Decl(functionDeclarationAsStatementInStrictMode.ts, 11, 14)) | ||
|
|
||
| do { function g3() {} } while (false); | ||
| >g3 : Symbol(g3, Decl(functionDeclarationAsStatementInStrictMode.ts, 12, 4)) | ||
|
|
||
| for (;;) { function g4() {} } | ||
| >g4 : Symbol(g4, Decl(functionDeclarationAsStatementInStrictMode.ts, 13, 10)) | ||
|
|
||
| for (let x in {}) { function g5() {} } | ||
| >x : Symbol(x, Decl(functionDeclarationAsStatementInStrictMode.ts, 14, 8)) | ||
| >g5 : Symbol(g5, Decl(functionDeclarationAsStatementInStrictMode.ts, 14, 19)) | ||
|
|
||
| for (let x of []) { function g6() {} } | ||
| >x : Symbol(x, Decl(functionDeclarationAsStatementInStrictMode.ts, 15, 8)) | ||
| >g6 : Symbol(g6, Decl(functionDeclarationAsStatementInStrictMode.ts, 15, 19)) | ||
|
|
||
| label: { function g7() {} } | ||
| >g7 : Symbol(g7, Decl(functionDeclarationAsStatementInStrictMode.ts, 16, 8)) | ||
|
|
||
| // Valid - top level | ||
| function topLevel() {} | ||
| >topLevel : Symbol(topLevel, Decl(functionDeclarationAsStatementInStrictMode.ts, 16, 27)) | ||
|
|
||
| // Valid - inside function body | ||
| function outer() { | ||
| >outer : Symbol(outer, Decl(functionDeclarationAsStatementInStrictMode.ts, 19, 22)) | ||
|
|
||
| function inner() {} | ||
| >inner : Symbol(inner, Decl(functionDeclarationAsStatementInStrictMode.ts, 22, 18)) | ||
| } | ||
|
|
108 changes: 108 additions & 0 deletions
108
tests/baselines/reference/functionDeclarationAsStatementInStrictMode.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,108 @@ | ||
| //// [tests/cases/compiler/functionDeclarationAsStatementInStrictMode.ts] //// | ||
|
|
||
| === functionDeclarationAsStatementInStrictMode.ts === | ||
| // Error cases - function declarations as direct children of statements in strict mode | ||
| if (true) function f1() {} | ||
| >true : true | ||
| > : ^^^^ | ||
| >f1 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| while (true) function f2() {} | ||
| >true : true | ||
| > : ^^^^ | ||
| >f2 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| do function f3() {} while (false); | ||
| >f3 : () => void | ||
| > : ^^^^^^^^^^ | ||
| >false : false | ||
| > : ^^^^^ | ||
|
|
||
| for (;;) function f4() {} | ||
| >f4 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| for (let x in {}) function f5() {} | ||
| >x : string | ||
| > : ^^^^^^ | ||
| >{} : {} | ||
| > : ^^ | ||
| >f5 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| for (let x of []) function f6() {} | ||
| >x : never | ||
| > : ^^^^^ | ||
| >[] : never[] | ||
| > : ^^^^^^^ | ||
| >f6 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| label: function f7() {} | ||
| >label : any | ||
| > : ^^^ | ||
| >f7 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| // Valid cases - function declarations inside blocks | ||
| if (true) { function g1() {} } | ||
| >true : true | ||
| > : ^^^^ | ||
| >g1 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| while (true) { function g2() {} } | ||
| >true : true | ||
| > : ^^^^ | ||
| >g2 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| do { function g3() {} } while (false); | ||
| >g3 : () => void | ||
| > : ^^^^^^^^^^ | ||
| >false : false | ||
| > : ^^^^^ | ||
|
|
||
| for (;;) { function g4() {} } | ||
| >g4 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| for (let x in {}) { function g5() {} } | ||
| >x : string | ||
| > : ^^^^^^ | ||
| >{} : {} | ||
| > : ^^ | ||
| >g5 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| for (let x of []) { function g6() {} } | ||
| >x : never | ||
| > : ^^^^^ | ||
| >[] : never[] | ||
| > : ^^^^^^^ | ||
| >g6 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| label: { function g7() {} } | ||
| >label : any | ||
| > : ^^^ | ||
| >g7 : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| // Valid - top level | ||
| function topLevel() {} | ||
| >topLevel : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| // Valid - inside function body | ||
| function outer() { | ||
| >outer : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| function inner() {} | ||
| >inner : () => 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
Oops, something went wrong.
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.
Paging @DanielRosenwasser for phrasing