-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Retain TDZ violations in common scenarios and fix similar var
bugs
#4124
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
description: 'preserve common TDZ violations' | ||
}; |
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,40 @@ | ||
console.log(function() { | ||
if (x) return "HELLO"; // TDZ | ||
const x = 1; // keep | ||
}()); | ||
|
||
const C = 1 + C + 2; // TDZ | ||
let L = L; // TDZ | ||
var V = V; // TODO: uncommon scenario, but should be dropped | ||
console.log("X+" ); // optimize | ||
|
||
console.log(Y ? "Y+" : "Y-"); // TDZ | ||
const Y = 2; // keep | ||
|
||
console.log(Z ? "Z+" : "Z-"); // TDZ | ||
const Z = 3; // keep | ||
console.log("Z+" ); // keep | ||
|
||
console.log(obj.x.y ? 1 : 2); // TDZ | ||
const obj = { // keep | ||
x: { | ||
y: true | ||
} | ||
}; | ||
console.log(3 ); // keep | ||
|
||
L2; // TDZ for L2 | ||
L3 = 20; // TDZ for L3 | ||
let L2, L3; // keep L2, L3 | ||
L3 = 30; // keep | ||
|
||
// Note that typical var/const/let use is still optimized | ||
(function() { | ||
console.log(A ? "A" : "!A"); | ||
var A = 1; | ||
console.log("A" ); | ||
console.log("B"); | ||
console.log("B" ); | ||
console.log("C" ); | ||
console.log("D" ); | ||
})(); |
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,53 @@ | ||
console.log(function() { | ||
if (x) return "HELLO"; // TDZ | ||
const x = 1; // keep | ||
return "WORLD"; // not reached | ||
}()); | ||
|
||
const unused1 = 1; // drop | ||
let unused2 = 2; // drop | ||
var unused3 = 3; // drop | ||
|
||
const C = 1 + C + 2; // TDZ | ||
let L = L; // TDZ | ||
var V = V; // TODO: uncommon scenario, but should be dropped | ||
|
||
const X = 1; // drop | ||
console.log(X ? "X+" : "X-"); // optimize | ||
|
||
console.log(Y ? "Y+" : "Y-"); // TDZ | ||
const Y = 2; // keep | ||
|
||
console.log(Z ? "Z+" : "Z-"); // TDZ | ||
const Z = 3; // keep | ||
console.log(Z ? "Z+" : "Z-"); // keep | ||
|
||
console.log(obj.x.y ? 1 : 2); // TDZ | ||
const obj = { // keep | ||
x: { | ||
y: true | ||
} | ||
}; | ||
console.log(obj.x.y ? 3 : 4); // keep | ||
|
||
V2, L2; // TDZ for L2 | ||
var V2; // drop | ||
V3 = 10, L3 = 20; // TDZ for L3 | ||
let L1, L2, L3, L4; // keep L2, L3 | ||
var V3; // drop | ||
L3 = 30; // keep | ||
L4 = 40; // drop | ||
|
||
// Note that typical var/const/let use is still optimized | ||
(function() { | ||
console.log(A ? "A" : "!A"); | ||
var A = 1, B = 2; | ||
const C = 3; | ||
let D = 4; | ||
console.log(A ? "A" : "!A"); | ||
if (B) console.log("B"); | ||
else console.log("!B"); | ||
console.log(B ? "B" : "!B"); | ||
console.log(C ? "C" : "!C"); | ||
console.log(D ? "D" : "!D"); | ||
})(); |
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,3 @@ | ||
module.exports = { | ||
description: 'exercise `var` variables before their declarations' | ||
}; |
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,47 @@ | ||
var results = [], log = x => results.push(x); | ||
|
||
(function () { | ||
var a = "PASS1"; | ||
for (var b = 2; --b >= 0; ) { | ||
(function() { | ||
var c = function() { | ||
return 1; | ||
}(c && (a = "FAIL1")); | ||
})(); | ||
} | ||
log(a); | ||
})(); | ||
|
||
log(a ? "FAIL2" : "PASS2"); | ||
var a = 1; | ||
|
||
var b = 2; | ||
log(b ? "PASS3" : "FAIL3"); | ||
|
||
log(c ? "FAIL4" : "PASS4"); | ||
var c = 3; | ||
log(c ? "PASS5" : "FAIL5"); | ||
|
||
/* TODO: multi-scope var-use-before-defined bug to be addressed in a future PR | ||
(function () { | ||
var first = state(); | ||
var on = true; | ||
var obj = { | ||
state: state | ||
}; | ||
log(first, obj.state()); | ||
function state() { | ||
return on ? "ON" : "OFF"; | ||
} | ||
})(); | ||
*/ | ||
|
||
/* TODO: previously existing bug still fails, but in a different way | ||
log(function() { | ||
if (x) return "HELLO"; | ||
var x = 1; | ||
return "WORLD"; | ||
}()); | ||
*/ | ||
|
||
assert.strictEqual(results.join(" "), "PASS1 PASS2 PASS3 PASS4 PASS5"); |
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.
Although dropping the
return "WORLD";
line for this test doesn't affect the outcome of the preserved TDZ violation above, I am not sure why it was dropped due to a side effect being reported on the Identifierx
inif (x) return "HELLO";
. If this example were changed to usevar
instead ofconst
, the rollup result in this PR would be incorrect due to the droppedreturn "WORLD";
:Expected:
Actual using this PR:
Note that rollup v2.50.6 is also wrong, but in a different way: