Skip to content

Commit 8e732c9

Browse files
committed
Merge pull request #5375 from MartyIX/issue-5109
Add warning message empty THEN clause
2 parents 9201ba2 + 1e21088 commit 8e732c9

9 files changed

+107
-1
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12230,6 +12230,11 @@ namespace ts {
1223012230

1223112231
checkExpression(node.expression);
1223212232
checkSourceElement(node.thenStatement);
12233+
12234+
if (node.thenStatement.kind === SyntaxKind.EmptyStatement) {
12235+
error(node.thenStatement, Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement);
12236+
}
12237+
1223312238
checkSourceElement(node.elseStatement);
1223412239
}
1223512240

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,11 @@
799799
"'=' can only be used in an object literal property inside a destructuring assignment.": {
800800
"category": "Error",
801801
"code": 1312
802-
},
802+
},
803+
"The body of an 'if' statement cannot be the empty statement.": {
804+
"category": "Error",
805+
"code": 1313
806+
},
803807
"Duplicate identifier '{0}'.": {
804808
"category": "Error",
805809
"code": 2300
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/emptyThenWarning.ts(1,6): error TS1313: The body of an 'if' statement cannot be the empty statement.
2+
tests/cases/compiler/emptyThenWarning.ts(4,19): error TS1313: The body of an 'if' statement cannot be the empty statement.
3+
4+
5+
==== tests/cases/compiler/emptyThenWarning.ts (2 errors) ====
6+
if(1);
7+
~
8+
!!! error TS1313: The body of an 'if' statement cannot be the empty statement.
9+
10+
let x = 0;
11+
if (true === true); {
12+
~
13+
!!! error TS1313: The body of an 'if' statement cannot be the empty statement.
14+
x = 1;
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [emptyThenWarning.ts]
2+
if(1);
3+
4+
let x = 0;
5+
if (true === true); {
6+
x = 1;
7+
}
8+
9+
//// [emptyThenWarning.js]
10+
if (1)
11+
;
12+
var x = 0;
13+
if (true === true)
14+
;
15+
{
16+
x = 1;
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [emptyThenWithoutWarning.ts]
2+
let a = 4;
3+
4+
if(a === 1 || a === 2 || a === 3) {
5+
}
6+
else {
7+
let message = "Ooops";
8+
}
9+
10+
//// [emptyThenWithoutWarning.js]
11+
var a = 4;
12+
if (a === 1 || a === 2 || a === 3) {
13+
}
14+
else {
15+
var message = "Ooops";
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/emptyThenWithoutWarning.ts ===
2+
let a = 4;
3+
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))
4+
5+
if(a === 1 || a === 2 || a === 3) {
6+
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))
7+
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))
8+
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))
9+
}
10+
else {
11+
let message = "Ooops";
12+
>message : Symbol(message, Decl(emptyThenWithoutWarning.ts, 5, 7))
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/emptyThenWithoutWarning.ts ===
2+
let a = 4;
3+
>a : number
4+
>4 : number
5+
6+
if(a === 1 || a === 2 || a === 3) {
7+
>a === 1 || a === 2 || a === 3 : boolean
8+
>a === 1 || a === 2 : boolean
9+
>a === 1 : boolean
10+
>a : number
11+
>1 : number
12+
>a === 2 : boolean
13+
>a : number
14+
>2 : number
15+
>a === 3 : boolean
16+
>a : number
17+
>3 : number
18+
}
19+
else {
20+
let message = "Ooops";
21+
>message : string
22+
>"Ooops" : string
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if(1);
2+
3+
let x = 0;
4+
if (true === true); {
5+
x = 1;
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let a = 4;
2+
3+
if(a === 1 || a === 2 || a === 3) {
4+
}
5+
else {
6+
let message = "Ooops";
7+
}

0 commit comments

Comments
 (0)