Monday, May 20, 2024
56
rated 0 times [  63] [ 7]  / answers: 1 / hits: 16054  / 7 Years ago, fri, march 17, 2017, 12:00:00

I'm using TSLint extension for VS Code. I got bunch of [tslint] misplaced 'else' (one-line) warnings. For example, at the below code sample, linter underlines else word and gives same warning:



Code:



if (user.isAdmin) {
this.uiRouter.stateService.go('app.admin');
}
else {
this.sharedVariables.user = user;
this.navigateByUserType(user);
}


What is the problem with this code?


More From » visual-studio-code

 Answers
2

From the tslint one-line rule documentation




Rule: one-line



Requires the specified tokens to be on the same line as
the expression preceding them.




You have a tslint rule enabled called one-line with a token specified called check-else. This tells tslint to warn for every else statement that isn't on the same line as the previous ending curly brace. If you move up the else statement to the same line as the ending curly brace of the if statement the tslint rule should be satisfied.



if (user.isAdmin) {
this.uiRouter.stateService.go('app.admin');
} else {
this.sharedVariables.user = user;
this.navigateByUserType(user);
}

[#58510] Wednesday, March 15, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
laytonlamontm

Total Points: 745
Total Questions: 130
Total Answers: 130

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
;