File tree Expand file tree Collapse file tree 4 files changed +142
-0
lines changed Expand file tree Collapse file tree 4 files changed +142
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { noShorthand } from './no-shorthand'
7
7
import { noType } from './no-type'
8
8
import { noXAbove } from './no-x-above'
9
9
import { regex101 } from './regex101'
10
+ import { reverseIfElse } from './reverse-if-else'
10
11
import { toArrow } from './to-arrow'
11
12
import { toDestructuring } from './to-destructuring'
12
13
import { toDynamicImport } from './to-dynamic-import'
@@ -30,6 +31,7 @@ export {
30
31
noType ,
31
32
noXAbove ,
32
33
regex101 ,
34
+ reverseIfElse ,
33
35
toArrow ,
34
36
toDestructuring ,
35
37
toDynamicImport ,
@@ -54,6 +56,7 @@ export const builtinCommands = [
54
56
noType ,
55
57
noXAbove ,
56
58
regex101 ,
59
+ reverseIfElse ,
57
60
toArrow ,
58
61
toDestructuring ,
59
62
toDynamicImport ,
Original file line number Diff line number Diff line change
1
+ # ` reverse-if-else `
2
+
3
+ Reverse the order of if-else statements and negate the condition.
4
+
5
+ ## Triggers
6
+
7
+ - ` /// reverse-if-else `
8
+ - ` /// rife `
9
+ - ` /// rif `
10
+
11
+ ## Examples
12
+
13
+ ``` ts
14
+ // / reverse-if-else
15
+ if (a === 1 ) {
16
+ a = 2
17
+ }
18
+ else {
19
+ a = 3
20
+ }
21
+ ```
22
+
23
+ Will be converted to:
24
+
25
+ ``` ts
26
+ if (! (a === 1 )) {
27
+ a = 3
28
+ }
29
+ else {
30
+ a = 2
31
+ }
32
+ ```
Original file line number Diff line number Diff line change
1
+ import { $ , run } from './_test-utils'
2
+ import { reverseIfElse as command } from './reverse-if-else'
3
+
4
+ run (
5
+ command ,
6
+ {
7
+ code : $ `
8
+ /// reverse-if-else
9
+ const foo = 'bar'
10
+ ` ,
11
+ errors : [ 'command-error' ] ,
12
+ } ,
13
+ // with else if
14
+ {
15
+ code : $ `
16
+ /// reverse-if-else
17
+ if (a === 1) {
18
+ a = 2
19
+ }
20
+ else if (a === 2) {
21
+ a = 3
22
+ }
23
+ ` ,
24
+ errors : [ 'command-error' ] ,
25
+ } ,
26
+ {
27
+ code : $ `
28
+ /// reverse-if-else
29
+ if (a === 1) {
30
+ a = 2
31
+ }
32
+ else {
33
+ a = 3
34
+ }
35
+ ` ,
36
+ output : $ `
37
+ if (!(a === 1)) {
38
+ a = 3
39
+ }
40
+ else {
41
+ a = 2
42
+ }
43
+ ` ,
44
+ errors : [ 'command-fix' ] ,
45
+ } ,
46
+ // without if
47
+ {
48
+ code : $ `
49
+ /// reverse-if-else
50
+ if (a === 1 || b === 2) {
51
+ a = 2
52
+ }
53
+ ` ,
54
+ output : ( i ) => {
55
+ expect ( i ) . toMatchInlineSnapshot ( `
56
+ "if (!(a === 1 || b === 2)) {
57
+ }
58
+ else {
59
+ a = 2
60
+ }"
61
+ ` )
62
+ } ,
63
+ errors : [ 'command-fix' ] ,
64
+ } ,
65
+ )
Original file line number Diff line number Diff line change
1
+ import type { Command } from '../types'
2
+ import { AST_NODE_TYPES } from '@typescript-eslint/utils'
3
+
4
+ export const reverseIfElse : Command = {
5
+ name : 'reverse-if-else' ,
6
+ match : / ^ \s * [ / : @ ] \s * ( r e v e r s e - i f - e l s e | r i f e | r i f ) $ / ,
7
+ action ( ctx ) {
8
+ const node = ctx . findNodeBelow ( 'IfStatement' )
9
+
10
+ if ( ! node )
11
+ return ctx . reportError ( 'Cannot find if statement' )
12
+
13
+ const elseNode = node . alternate
14
+
15
+ const isElseif = elseNode ?. type === AST_NODE_TYPES . IfStatement
16
+ if ( isElseif )
17
+ return ctx . reportError ( 'Unable reverse when `else if` statement exist' )
18
+
19
+ const ifNode = node . consequent
20
+
21
+ ctx . report ( {
22
+ loc : node . loc ,
23
+ message : 'Reverse if-else' ,
24
+ fix ( fixer ) {
25
+ const lineIndent = ctx . getIndentOfLine ( node . loc . start . line )
26
+
27
+ const conditionText = ctx . getTextOf ( node . test )
28
+ const ifText = ctx . getTextOf ( ifNode )
29
+ const elseText = elseNode ? ctx . getTextOf ( elseNode ) : '{\n}'
30
+
31
+ const str = [
32
+ `if (!(${ conditionText } )) ${ elseText } ` ,
33
+ `else ${ ifText } ` ,
34
+ ]
35
+ . map ( ( line , idx ) => idx ? lineIndent + line : line )
36
+ . join ( '\n' )
37
+
38
+ return fixer . replaceText ( node , str )
39
+ } ,
40
+ } )
41
+ } ,
42
+ }
You can’t perform that action at this time.
0 commit comments