This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
[unnecessary-else] Allowed skipping else if
statements via options
#4599
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,16 @@ export class Rule extends Lint.Rules.AbstractRule { | |
description: Lint.Utils.dedent` | ||
Disallows \`else\` blocks following \`if\` blocks ending with a \`break\`, \`continue\`, \`return\`, or \`throw\` statement.`, | ||
descriptionDetails: "", | ||
optionExamples: [true], | ||
options: null, | ||
optionsDescription: "Not configurable.", | ||
optionExamples: [true, [true, { allowElseIf: true }]], | ||
options: { | ||
type: "object", | ||
properties: { | ||
allowElseIf: { type: "boolean" }, | ||
}, | ||
}, | ||
optionsDescription: Lint.Utils.dedent` | ||
You can optionally specify the option \`"allowElseIf"\` to allow "else if" statements. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Rules typically create an all-caps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 03b2414 |
||
`, | ||
rationale: Lint.Utils.dedent` | ||
When an \`if\` block is guaranteed to exit control flow when entered, | ||
it is unnecessary to add an \`else\` statement. | ||
|
@@ -46,7 +53,11 @@ export class Rule extends Lint.Rules.AbstractRule { | |
} | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
return this.applyWithFunction( | ||
sourceFile, | ||
walk, | ||
parseOptions(this.ruleArguments[0] as Partial<Options> | undefined), | ||
); | ||
} | ||
} | ||
|
||
|
@@ -55,7 +66,18 @@ interface IJumpAndIfStatement { | |
node: ts.IfStatement; | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext): void { | ||
interface Options { | ||
allowElseIf: boolean; | ||
} | ||
|
||
function parseOptions(option: Partial<Options> | undefined): Options { | ||
return { | ||
allowElseIf: false, | ||
...option, | ||
}; | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<Options>): void { | ||
const ifStatementStack: IJumpAndIfStatement[] = []; | ||
|
||
function visitIfStatement(node: ts.IfStatement) { | ||
|
@@ -68,7 +90,8 @@ function walk(ctx: Lint.WalkContext): void { | |
if ( | ||
jumpStatement !== undefined && | ||
node.elseStatement !== undefined && | ||
!recentStackParentMissingJumpStatement() | ||
!recentStackParentMissingJumpStatement() && | ||
(!utils.isIfStatement(node.elseStatement) || !ctx.options.allowElseIf) | ||
) { | ||
const elseKeyword = getPositionOfElseKeyword(node, ts.SyntaxKind.ElseKeyword); | ||
ctx.addFailureAtNode(elseKeyword, Rule.FAILURE_STRING(jumpStatement)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
const testReturn = (a) => { | ||
if (a===0) { | ||
return 0; | ||
} else { | ||
~~~~ [return] | ||
return a; | ||
} | ||
} | ||
|
||
const testReturn = (a) => { | ||
if (a===0) return 0; | ||
else return a; | ||
~~~~ [return] | ||
} | ||
|
||
const testReturn = (a) => { | ||
if (a===0) | ||
return 0; | ||
else | ||
~~~~ [return] | ||
return a; | ||
} | ||
|
||
const testReturn = (a) => { | ||
if (a>0) { | ||
if (a%2 ===0) { | ||
return "even" ; | ||
} else { | ||
~~~~ [return] | ||
return "odd"; | ||
} | ||
} | ||
return "negative"; | ||
} | ||
|
||
const testReturn = (a) => { | ||
if (a===0) { | ||
return 0; | ||
} | ||
return a; | ||
} | ||
|
||
const testReturn = (a) => { | ||
if (a<0) { | ||
return; | ||
} else if (a>0) { | ||
if (a%2 === 0) { | ||
return ; | ||
} else if (a === 0) { | ||
return ; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
const testReturn = (a) => { | ||
if (a<0) { | ||
return; | ||
} | ||
if (a===1) { | ||
return ; | ||
} else { | ||
~~~~ [return] | ||
return ; | ||
} | ||
} | ||
|
||
const testReturn = (a) => { | ||
if (a>0) { | ||
if (a%3===0) { | ||
return; | ||
} else { | ||
~~~~ [return] | ||
console.log(a) | ||
} | ||
} | ||
else { | ||
console.log("negative"); | ||
} | ||
} | ||
|
||
const testThrow = (a) => { | ||
if ( a===0 ) { | ||
throw "error"; | ||
} else { | ||
~~~~ [throw] | ||
return 100/a; | ||
} | ||
} | ||
|
||
const testThrow = (a) => { | ||
if (a===0) | ||
throw "error; | ||
else if (a < 0) | ||
console.log(100/a); | ||
} | ||
|
||
const testThrow = (a) => { | ||
if (a===0) throw "error; | ||
else console.log(100/a); | ||
~~~~ [throw] | ||
} | ||
|
||
const testThrow = (a) => { | ||
switch (a) { | ||
case 1: | ||
break; | ||
case 2: | ||
if (true) { | ||
throw "error"; | ||
} else { | ||
~~~~ [throw] | ||
break; | ||
} | ||
default : | ||
break; | ||
} | ||
} | ||
|
||
const testThrow = (a) => { | ||
let i = 1; | ||
do { | ||
if (a-i === 0) { | ||
throw "error; | ||
} else { | ||
~~~~ [throw] | ||
console.log(i/a-i); | ||
} | ||
++i; | ||
} | ||
} | ||
|
||
const testThrow = (a) => { | ||
if (a===0) { | ||
throw "error"; | ||
} | ||
return 100/a; | ||
} | ||
|
||
const testThrow = (a) => { | ||
if (a===0) throw "error"; | ||
return 100/a; | ||
} | ||
|
||
const testContinue = () => { | ||
for (let i = 1; i < 10; i++) { | ||
if (i===8) { | ||
continue ; | ||
} else { | ||
~~~~ [continue] | ||
console.log(i); | ||
} | ||
} | ||
} | ||
|
||
const testContinue = () => { | ||
for (let i = 1; i < 10; i++) { | ||
if (i===8) continue ; | ||
else console.log(i); | ||
~~~~ [continue] | ||
} | ||
} | ||
|
||
const testContinue = () => { | ||
for (let i = 1; i < 10; i++) { | ||
if (i===8) | ||
continue ; | ||
else | ||
~~~~ [continue] | ||
console.log(i); | ||
} | ||
} | ||
|
||
const testContinue = () => { | ||
for (let i = 1; i < 10; i++) { | ||
if (i===4) { | ||
continue ; | ||
} | ||
console.log(i); | ||
} | ||
} | ||
|
||
const testContinue = () => { | ||
for (let i = 1; i < 10; i++) { | ||
if (i===4) | ||
continue ; | ||
console.log(i); | ||
} | ||
} | ||
|
||
const testBreak = (a) => { | ||
let i = 0; | ||
while(i < 20) { | ||
if (i === a) { | ||
break ; | ||
} else { | ||
~~~~ [break] | ||
i++; | ||
} | ||
} | ||
return i-1; | ||
} | ||
|
||
const testBreak = (a) => { | ||
let i = 0; | ||
while(i < 20) { | ||
if (i === a) { | ||
break ; | ||
} | ||
i++; | ||
} | ||
return i-1; | ||
} | ||
|
||
const testBreak = (a) => { | ||
let i = 0; | ||
while(i < 20) { | ||
if (i === a) | ||
break ; | ||
i++; | ||
} | ||
return i-1; | ||
} | ||
|
||
const testBreak = (a) => { | ||
let i = 0; | ||
while(i < 20) { | ||
if (i === a) break ; | ||
i++; | ||
} | ||
return i-1; | ||
} | ||
|
||
const testBreak = (a) => { | ||
let i = 0; | ||
while(i < 20) { | ||
if (i === a) break ; | ||
else i++; | ||
~~~~ [break] | ||
} | ||
return i-1; | ||
} | ||
|
||
const testNoJump = (a) => { | ||
if (a % 2 === 0) { | ||
console.log(a); | ||
} else { | ||
console.log(a * 2); | ||
} | ||
} | ||
|
||
[return]: The preceding `if` block ends with a `return` statement. This `else` is unnecessary. | ||
[throw]: The preceding `if` block ends with a `throw` statement. This `else` is unnecessary. | ||
[break]: The preceding `if` block ends with a `break` statement. This `else` is unnecessary. | ||
[continue]: The preceding `if` block ends with a `continue` statement. This `else` is unnecessary. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"unnecessary-else": [true, { "allowElseIf": true }] | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TSLint rules use dash-case, so this should be
allow-else-if
. For example,noAnyRule.ts
hasconst OPTION_IGNORE_REST_ARGS = "ignore-rest-args";
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 03b2414