-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
If the expression after the return statement is on a new line, the generated code is incorrect #2704
Comments
This is by design due to how Automatic-Semicolon-Insertion (ASI) works for JavaScript. When you write return
((charCode >= 'a'.charCodeAt(0) && charCode <= 'z'.charCodeAt(0)) ||
charCode == '_'.charCodeAt(0)); It is syntactically equivalent to return;
((charCode >= 'a'.charCodeAt(0) && charCode <= 'z'.charCodeAt(0)) ||
charCode == '_'.charCodeAt(0)); This is covered in the ECMAScript spec here:
...
|
Ugh.. that's not fun. My 2c on how this could be improved, 3 alternatives:
|
That said, these would be an appropriate area for a Linter to help out with. |
@CyrusNajmabadi - I'd be keen for a linter to allow this to flag a warning, or even an error (as I believe it is in C#). I was just bitten by this today in code I wrote for a team member. In my code, I now see via the intellisense popup on my method that the inferred return type was void. If I had put the expected return type in the method signature I would've had this at least flagged... Something to keep in mind :) (a VS 2013/2015 extension that put the inferred return type in grey next to the method name would be fantastic!) Your points 1 & 3 are quite valid, and I take advantage of 3 frequently. I just didn't mean to take advantage this time unknowingly... |
There is already a linter rule for this : https://github.com/palantir/tslint/blob/master/src/rules/noUnreachableRule.ts |
Typescript:
Generated js:
Notice the
;
afterreturn
in the generated code.The text was updated successfully, but these errors were encountered: