-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Improve control flow analysis in function expressions #8849
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b5c72c
Include outer function expressions in control flow analysis
ahejlsberg d20664a
Fix test
ahejlsberg e5e21f9
Add new tests
ahejlsberg 1647d20
Fix linting errors
ahejlsberg e8ecf0e
Track return statements in IIFE using a flow label
ahejlsberg 4d730a5
Update test
ahejlsberg 66e9f7d
Optimize critical code paths in binder
ahejlsberg 5b4dffc
More critical path optimization in binder
ahejlsberg 41446fe
Address CR feedback
ahejlsberg 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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
73 changes: 73 additions & 0 deletions
73
tests/baselines/reference/constLocalsInFunctionExpressions.js
This file contains 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,73 @@ | ||
//// [constLocalsInFunctionExpressions.ts] | ||
declare function getStringOrNumber(): string | number; | ||
|
||
function f1() { | ||
const x = getStringOrNumber(); | ||
if (typeof x === "string") { | ||
const f = () => x.length; | ||
} | ||
} | ||
|
||
function f2() { | ||
const x = getStringOrNumber(); | ||
if (typeof x !== "string") { | ||
return; | ||
} | ||
const f = () => x.length; | ||
} | ||
|
||
function f3() { | ||
const x = getStringOrNumber(); | ||
if (typeof x === "string") { | ||
const f = function() { return x.length; }; | ||
} | ||
} | ||
|
||
function f4() { | ||
const x = getStringOrNumber(); | ||
if (typeof x !== "string") { | ||
return; | ||
} | ||
const f = function() { return x.length; }; | ||
} | ||
|
||
function f5() { | ||
const x = getStringOrNumber(); | ||
if (typeof x === "string") { | ||
const f = () => () => x.length; | ||
} | ||
} | ||
|
||
//// [constLocalsInFunctionExpressions.js] | ||
function f1() { | ||
var x = getStringOrNumber(); | ||
if (typeof x === "string") { | ||
var f = function () { return x.length; }; | ||
} | ||
} | ||
function f2() { | ||
var x = getStringOrNumber(); | ||
if (typeof x !== "string") { | ||
return; | ||
} | ||
var f = function () { return x.length; }; | ||
} | ||
function f3() { | ||
var x = getStringOrNumber(); | ||
if (typeof x === "string") { | ||
var f = function () { return x.length; }; | ||
} | ||
} | ||
function f4() { | ||
var x = getStringOrNumber(); | ||
if (typeof x !== "string") { | ||
return; | ||
} | ||
var f = function () { return x.length; }; | ||
} | ||
function f5() { | ||
var x = getStringOrNumber(); | ||
if (typeof x === "string") { | ||
var f = function () { return function () { return x.length; }; }; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/baselines/reference/constLocalsInFunctionExpressions.symbols
This file contains 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,95 @@ | ||
=== tests/cases/conformance/controlFlow/constLocalsInFunctionExpressions.ts === | ||
declare function getStringOrNumber(): string | number; | ||
>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) | ||
|
||
function f1() { | ||
>f1 : Symbol(f1, Decl(constLocalsInFunctionExpressions.ts, 0, 54)) | ||
|
||
const x = getStringOrNumber(); | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 3, 9)) | ||
>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) | ||
|
||
if (typeof x === "string") { | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 3, 9)) | ||
|
||
const f = () => x.length; | ||
>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 5, 13)) | ||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 3, 9)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
} | ||
} | ||
|
||
function f2() { | ||
>f2 : Symbol(f2, Decl(constLocalsInFunctionExpressions.ts, 7, 1)) | ||
|
||
const x = getStringOrNumber(); | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 10, 9)) | ||
>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) | ||
|
||
if (typeof x !== "string") { | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 10, 9)) | ||
|
||
return; | ||
} | ||
const f = () => x.length; | ||
>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 14, 9)) | ||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 10, 9)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
} | ||
|
||
function f3() { | ||
>f3 : Symbol(f3, Decl(constLocalsInFunctionExpressions.ts, 15, 1)) | ||
|
||
const x = getStringOrNumber(); | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 18, 9)) | ||
>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) | ||
|
||
if (typeof x === "string") { | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 18, 9)) | ||
|
||
const f = function() { return x.length; }; | ||
>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 20, 13)) | ||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 18, 9)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
} | ||
} | ||
|
||
function f4() { | ||
>f4 : Symbol(f4, Decl(constLocalsInFunctionExpressions.ts, 22, 1)) | ||
|
||
const x = getStringOrNumber(); | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 25, 9)) | ||
>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) | ||
|
||
if (typeof x !== "string") { | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 25, 9)) | ||
|
||
return; | ||
} | ||
const f = function() { return x.length; }; | ||
>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 29, 9)) | ||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 25, 9)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
} | ||
|
||
function f5() { | ||
>f5 : Symbol(f5, Decl(constLocalsInFunctionExpressions.ts, 30, 1)) | ||
|
||
const x = getStringOrNumber(); | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 33, 9)) | ||
>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) | ||
|
||
if (typeof x === "string") { | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 33, 9)) | ||
|
||
const f = () => () => x.length; | ||
>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 35, 13)) | ||
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 33, 9)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
} | ||
} |
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.
we already check this in getImmediatelyInvokedFunctionExpression
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.
Yeah, I'll remove it.