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

Conversation

MartyIX
Copy link
Contributor

@MartyIX MartyIX commented Oct 23, 2015

Proposed patch for issue #5109.

@MartyIX
Copy link
Contributor Author

MartyIX commented Oct 23, 2015

I'm at loss how my pull request managed to break commentsBlocks.ts test.

~
!!! warning TS20000: Potentially wrong empty statement in THEN clause.
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)

@mhegazy
Copy link
Contributor

mhegazy commented Oct 23, 2015

the fourslash test likely failed because of violating incremental parsing assumptions. i would move the check to cheker.ts.

@DanielRosenwasser
Copy link
Member

After thinking about this more, I'm starting to wonder whether or not this is a good idea. We say many other errors can be taken care of by a linter. It sets a slightly odd precedent if we pick this case specially.

@MartyIX
Copy link
Contributor Author

MartyIX commented Oct 24, 2015

@DanielRosenwasser This pull request would add a very first warning to diagnosticMessages.json. It's up to you guys. :-)

@mhegazy
Copy link
Contributor

mhegazy commented Oct 27, 2015

After thinking about this more, I'm starting to wonder whether or not this is a good idea. We say many other errors can be taken care of by a linter. It sets a slightly odd precedent if we pick this case specially.

I think the line is fairly subjective, e.g. the rechability checks. but my reasoning is that this is an issue that is wrong 99.9% of the time, and not just a matter of best practices.

@MartyIX please let us know if you are still interested in perusing this PR, and if so, please move the check to the checker as i noted earlier, and update the tests.

@MartyIX
Copy link
Contributor Author

MartyIX commented Oct 27, 2015

@mhegazy Yes, I'm still interested in this PR, so I'll modify it. :)

@MartyIX MartyIX force-pushed the issue-5109 branch 4 times, most recently from 1c7f74f to ef5b0d8 Compare October 27, 2015 21:49
@MartyIX
Copy link
Contributor Author

MartyIX commented Oct 27, 2015

@mhegazy I have updated the PR.

@@ -2469,5 +2469,9 @@
"A type assertion expression is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.": {
"category": "Error",
"code": 17007
},
"The body of an 'if' statement cannot be the empty statement.": {
"category": "Warning",
Copy link
Member

Choose a reason for hiding this comment

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

Just make this an error the same as any other (probably in the 1xxx category); we don't need to introduce a new concept of a "warning" right now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean a new concept for users? I can see Warning here: https://github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts#L2043 ...

@billti
Copy link
Member

billti commented Oct 27, 2015

Interesting... I'll sometimes write code with just a // TODO: Handle blah... in the else (or even if) clause while work is in progress. So this is going to be an error in my compilations now until I finish the TODO?

I agree with @DanielRosenwasser . Seems firmly in the realm of linter-land. There is absolutely nothing wrong syntactically, semantically, or in the use of types by having an empty block.

@MartyIX
Copy link
Contributor Author

MartyIX commented Oct 27, 2015

@billti I'm not sure how your code looks like exactly, but the idea was that for the following script

// Show warning
if (1);

// Dont show warning
if (1) {
    // TODO: Handle blah...
}     

the compiler should return:

C:\Work\TypeScript>node built\local\tsc.js is5109.ts
is5109.ts(2,7): warning TS20000: The body of an 'if' statement cannot be the empty statement.

To clarify further, EmptyStatement is not the same as empty block.

@billti
Copy link
Member

billti commented Oct 27, 2015

Ah, right. Fair enough then. Yes, almost certainly a bug to warn on :-)

@MartyIX
Copy link
Contributor Author

MartyIX commented Oct 28, 2015

@RyanCavanaugh I have made the modification as you requested.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 28, 2015

thanks!

@RyanCavanaugh
Copy link
Member

👍

mhegazy added a commit that referenced this pull request Oct 28, 2015
Add warning message empty THEN clause
@mhegazy mhegazy merged commit 8e732c9 into microsoft:master Oct 28, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 28, 2015

thanks @MartyIX!

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants