-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Detect usage of block-scoped variables from earlier case blocks #47160
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9611488
Detect usage of block-scoped variables from earlier case blocks
RyanCavanaugh 309dfa3
Fix up an illegal let
RyanCavanaugh 40ac03c
Add nested switch block testcase
RyanCavanaugh 40faaca
Correctly handle nested switches
RyanCavanaugh e223f1b
Lint
RyanCavanaugh 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 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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3361,6 +3361,11 @@ | |||||||||||
"category": "Error", | ||||||||||||
"code": 2836 | ||||||||||||
}, | ||||||||||||
"Variable '{0}' is declared in a prior case block.": { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case needs to be quoted
Suggested change
It's tough to give some more context, but I think it could be helpful. Maybe something like
Suggested change
|
||||||||||||
"category": "Error", | ||||||||||||
"code": 2837 | ||||||||||||
}, | ||||||||||||
|
||||||||||||
|
||||||||||||
"Import declaration '{0}' is using private name '{1}'.": { | ||||||||||||
"category": "Error", | ||||||||||||
|
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,135 @@ | ||
tests/cases/compiler/switchCaseTdz.ts(3,9): error TS2448: Block-scoped variable 'x' used before its declaration. | ||
tests/cases/compiler/switchCaseTdz.ts(4,9): error TS2448: Block-scoped variable 'y' used before its declaration. | ||
tests/cases/compiler/switchCaseTdz.ts(5,9): error TS2448: Block-scoped variable 'z' used before its declaration. | ||
tests/cases/compiler/switchCaseTdz.ts(19,9): error TS2837: Variable 'x' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(20,9): error TS2837: Variable 'y' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(21,9): error TS2837: Variable 'z' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(24,13): error TS2837: Variable 'x' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(25,13): error TS2837: Variable 'y' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(26,13): error TS2837: Variable 'z' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(30,13): error TS2837: Variable 'x' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(31,13): error TS2837: Variable 'y' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(32,13): error TS2837: Variable 'z' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(39,9): error TS2837: Variable 'x' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(40,9): error TS2837: Variable 'y' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(41,9): error TS2837: Variable 'z' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(44,13): error TS2837: Variable 'x' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(45,13): error TS2837: Variable 'y' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(46,13): error TS2837: Variable 'z' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(50,13): error TS2837: Variable 'x' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(51,13): error TS2837: Variable 'y' is declared in a prior case block. | ||
tests/cases/compiler/switchCaseTdz.ts(52,13): error TS2837: Variable 'z' is declared in a prior case block. | ||
|
||
|
||
==== tests/cases/compiler/switchCaseTdz.ts (21 errors) ==== | ||
switch (1 + 1) { | ||
case -1: | ||
x; | ||
~ | ||
!!! error TS2448: Block-scoped variable 'x' used before its declaration. | ||
!!! related TS2728 tests/cases/compiler/switchCaseTdz.ts:9:13: 'x' is declared here. | ||
y; | ||
~ | ||
!!! error TS2448: Block-scoped variable 'y' used before its declaration. | ||
!!! related TS2728 tests/cases/compiler/switchCaseTdz.ts:10:15: 'y' is declared here. | ||
z; | ||
~ | ||
!!! error TS2448: Block-scoped variable 'z' used before its declaration. | ||
!!! related TS2728 tests/cases/compiler/switchCaseTdz.ts:11:16: 'z' is declared here. | ||
|
||
case 0: | ||
var ok = 0; | ||
let x = 0; | ||
const y = 0; | ||
const [z] = [0]; | ||
|
||
ok; | ||
x; | ||
y; | ||
z; | ||
|
||
case 1: | ||
x = 0; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'x' is declared in a prior case block. | ||
y; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'y' is declared in a prior case block. | ||
z; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'z' is declared in a prior case block. | ||
ok; | ||
if (true) { | ||
x = 0; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'x' is declared in a prior case block. | ||
y; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'y' is declared in a prior case block. | ||
z; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'z' is declared in a prior case block. | ||
ok; | ||
} | ||
let f1 = function () { | ||
x = 0; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'x' is declared in a prior case block. | ||
y; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'y' is declared in a prior case block. | ||
z; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'z' is declared in a prior case block. | ||
ok; | ||
} | ||
break; | ||
|
||
case 2: | ||
case 3: | ||
x; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'x' is declared in a prior case block. | ||
y; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'y' is declared in a prior case block. | ||
z; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'z' is declared in a prior case block. | ||
ok; | ||
if (true) { | ||
x; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'x' is declared in a prior case block. | ||
y; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'y' is declared in a prior case block. | ||
z; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'z' is declared in a prior case block. | ||
ok; | ||
} | ||
let f2 = function () { | ||
x; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'x' is declared in a prior case block. | ||
y; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'y' is declared in a prior case block. | ||
z; // <- bad | ||
~ | ||
!!! error TS2837: Variable 'z' is declared in a prior case block. | ||
ok; | ||
} | ||
|
||
} | ||
|
||
switch(2 + 2) { | ||
case 0: | ||
let x = 1; | ||
switch(x + x) { | ||
case 2: | ||
// Legal | ||
x; | ||
} | ||
} |
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,129 @@ | ||
//// [switchCaseTdz.ts] | ||
switch (1 + 1) { | ||
case -1: | ||
x; | ||
y; | ||
z; | ||
|
||
case 0: | ||
var ok = 0; | ||
let x = 0; | ||
const y = 0; | ||
const [z] = [0]; | ||
|
||
ok; | ||
x; | ||
y; | ||
z; | ||
|
||
case 1: | ||
x = 0; // <- bad | ||
y; // <- bad | ||
z; // <- bad | ||
ok; | ||
if (true) { | ||
x = 0; // <- bad | ||
y; // <- bad | ||
z; // <- bad | ||
ok; | ||
} | ||
let f1 = function () { | ||
x = 0; // <- bad | ||
y; // <- bad | ||
z; // <- bad | ||
ok; | ||
} | ||
break; | ||
|
||
case 2: | ||
case 3: | ||
x; // <- bad | ||
y; // <- bad | ||
z; // <- bad | ||
ok; | ||
if (true) { | ||
x; // <- bad | ||
y; // <- bad | ||
z; // <- bad | ||
ok; | ||
} | ||
let f2 = function () { | ||
x; // <- bad | ||
y; // <- bad | ||
z; // <- bad | ||
ok; | ||
} | ||
|
||
} | ||
|
||
switch(2 + 2) { | ||
case 0: | ||
let x = 1; | ||
switch(x + x) { | ||
case 2: | ||
// Legal | ||
x; | ||
} | ||
} | ||
|
||
//// [switchCaseTdz.js] | ||
switch (1 + 1) { | ||
case -1: | ||
x_1; | ||
y_1; | ||
z_1; | ||
case 0: | ||
var ok = 0; | ||
var x_1 = 0; | ||
var y_1 = 0; | ||
var z_1 = [0][0]; | ||
ok; | ||
x_1; | ||
y_1; | ||
z_1; | ||
case 1: | ||
x_1 = 0; // <- bad | ||
y_1; // <- bad | ||
z_1; // <- bad | ||
ok; | ||
if (true) { | ||
x_1 = 0; // <- bad | ||
y_1; // <- bad | ||
z_1; // <- bad | ||
ok; | ||
} | ||
var f1 = function () { | ||
x_1 = 0; // <- bad | ||
y_1; // <- bad | ||
z_1; // <- bad | ||
ok; | ||
}; | ||
break; | ||
case 2: | ||
case 3: | ||
x_1; // <- bad | ||
y_1; // <- bad | ||
z_1; // <- bad | ||
ok; | ||
if (true) { | ||
x_1; // <- bad | ||
y_1; // <- bad | ||
z_1; // <- bad | ||
ok; | ||
} | ||
var f2 = function () { | ||
x_1; // <- bad | ||
y_1; // <- bad | ||
z_1; // <- bad | ||
ok; | ||
}; | ||
} | ||
switch (2 + 2) { | ||
case 0: | ||
var x = 1; | ||
switch (x + x) { | ||
case 2: | ||
// Legal | ||
x; | ||
} | ||
} |
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.
Add a "'0' is declared here" related span to the
valueDeclaration
.