Skip to content

Add warning message empty THEN clause #5375

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

Merged
merged 1 commit into from
Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12227,6 +12227,11 @@ namespace ts {

checkExpression(node.expression);
checkSourceElement(node.thenStatement);

if (node.thenStatement.kind === SyntaxKind.EmptyStatement) {
error(node.thenStatement, Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement);
}

checkSourceElement(node.elseStatement);
}

Expand Down
6 changes: 5 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,11 @@
"'=' can only be used in an object literal property inside a destructuring assignment.": {
"category": "Error",
"code": 1312
},
},
"The body of an 'if' statement cannot be the empty statement.": {
"category": "Error",
"code": 1313
},
"Duplicate identifier '{0}'.": {
"category": "Error",
"code": 2300
Expand Down
15 changes: 15 additions & 0 deletions tests/baselines/reference/emptyThenWarning.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/compiler/emptyThenWarning.ts(1,6): error TS1313: The body of an 'if' statement cannot be the empty statement.
tests/cases/compiler/emptyThenWarning.ts(4,19): error TS1313: The body of an 'if' statement cannot be the empty statement.


==== tests/cases/compiler/emptyThenWarning.ts (2 errors) ====
if(1);
~
!!! error TS1313: The body of an 'if' statement cannot be the empty statement.

let x = 0;
if (true === true); {
~
!!! error TS1313: The body of an 'if' statement cannot be the empty statement.
x = 1;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that shows that

if(someComplexConditionICantBeBotheredToInvert) {
}
else {
    // some code ...
}

does not warn? It's not good style, but it's intended, so I want to document that it will not warn.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, if you guys decide that you want to merge this feature - #5375 (comment)

17 changes: 17 additions & 0 deletions tests/baselines/reference/emptyThenWarning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [emptyThenWarning.ts]
if(1);

let x = 0;
if (true === true); {
x = 1;
}

//// [emptyThenWarning.js]
if (1)
;
var x = 0;
if (true === true)
;
{
x = 1;
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/emptyThenWithoutWarning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [emptyThenWithoutWarning.ts]
let a = 4;

if(a === 1 || a === 2 || a === 3) {
}
else {
let message = "Ooops";
}

//// [emptyThenWithoutWarning.js]
var a = 4;
if (a === 1 || a === 2 || a === 3) {
}
else {
var message = "Ooops";
}
13 changes: 13 additions & 0 deletions tests/baselines/reference/emptyThenWithoutWarning.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/emptyThenWithoutWarning.ts ===
let a = 4;
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))

if(a === 1 || a === 2 || a === 3) {
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))
>a : Symbol(a, Decl(emptyThenWithoutWarning.ts, 0, 3))
}
else {
let message = "Ooops";
>message : Symbol(message, Decl(emptyThenWithoutWarning.ts, 5, 7))
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/emptyThenWithoutWarning.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/emptyThenWithoutWarning.ts ===
let a = 4;
>a : number
>4 : number

if(a === 1 || a === 2 || a === 3) {
>a === 1 || a === 2 || a === 3 : boolean
>a === 1 || a === 2 : boolean
>a === 1 : boolean
>a : number
>1 : number
>a === 2 : boolean
>a : number
>2 : number
>a === 3 : boolean
>a : number
>3 : number
}
else {
let message = "Ooops";
>message : string
>"Ooops" : string
}
6 changes: 6 additions & 0 deletions tests/cases/compiler/emptyThenWarning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if(1);

let x = 0;
if (true === true); {
x = 1;
}
7 changes: 7 additions & 0 deletions tests/cases/compiler/emptyThenWithoutWarning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let a = 4;

if(a === 1 || a === 2 || a === 3) {
}
else {
let message = "Ooops";
}