Skip to content

Commit ef5b0d8

Browse files
committed
Add warning message empty THEN clause
1 parent 6e78b9c commit ef5b0d8

9 files changed

+106
-0
lines changed

src/compiler/checker.ts

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

1222812228
checkExpression(node.expression);
1222912229
checkSourceElement(node.thenStatement);
12230+
12231+
if (node.thenStatement.kind === SyntaxKind.EmptyStatement) {
12232+
error(node.thenStatement, Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement);
12233+
}
12234+
1223012235
checkSourceElement(node.elseStatement);
1223112236
}
1223212237

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,5 +2469,9 @@
24692469
"A type assertion expression is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.": {
24702470
"category": "Error",
24712471
"code": 17007
2472+
},
2473+
"The body of an 'if' statement cannot be the empty statement.": {
2474+
"category": "Warning",
2475+
"code": 20000
24722476
}
24732477
}
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): warning TS20000: The body of an 'if' statement cannot be the empty statement.
2+
tests/cases/compiler/emptyThenWarning.ts(4,19): warning TS20000: 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+
!!! warning TS20000: The body of an 'if' statement cannot be the empty statement.
9+
10+
let x = 0;
11+
if (true === true); {
12+
~
13+
!!! warning TS20000: 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)