@@ -21,6 +21,7 @@ module.exports = function inlineInvariant(context) {
21
21
(%%cond%%) || devAssert(0, %%args%%)
22
22
` ) ;
23
23
24
+ const t = context . types ;
24
25
return {
25
26
visitor : {
26
27
CallExpression ( path ) {
@@ -38,13 +39,35 @@ module.exports = function inlineInvariant(context) {
38
39
const calleeName = node . callee . name ;
39
40
if ( calleeName === 'invariant' ) {
40
41
const [ cond , args ] = node . arguments ;
41
- path . addComment ( 'leading' , ' istanbul ignore next ' ) ;
42
- path . replaceWith ( invariantTemplate ( { cond, args } ) ) ;
42
+
43
+ // Check if it is unreachable invariant: "invariant(false, ...)"
44
+ if ( cond . type === 'BooleanLiteral' && cond . value === false ) {
45
+ addIstanbulIgnoreElse ( path ) ;
46
+ } else {
47
+ path . replaceWith ( invariantTemplate ( { cond, args } ) ) ;
48
+ }
43
49
} else if ( calleeName === 'devAssert' ) {
44
50
const [ cond , args ] = node . arguments ;
45
51
path . replaceWith ( assertTemplate ( { cond, args } ) ) ;
46
52
}
53
+
54
+ path . addComment ( 'leading' , ' istanbul ignore next ' ) ;
47
55
} ,
48
56
} ,
49
57
} ;
58
+
59
+ function addIstanbulIgnoreElse ( path ) {
60
+ const parentStatement = path . getStatementParent ( ) ;
61
+ const previousStatement =
62
+ parentStatement . container [ parentStatement . key - 1 ] ;
63
+ if ( previousStatement . type === 'IfStatement' ) {
64
+ let lastIf = previousStatement ;
65
+ while ( lastIf . alternate && lastIf . alternate . type === 'IfStatement' ) {
66
+ lastIf = lastIf . alternate ;
67
+ }
68
+ if ( lastIf . alternate == null ) {
69
+ t . addComment ( lastIf , 'leading' , ' istanbul ignore else ' ) ;
70
+ }
71
+ }
72
+ }
50
73
} ;
0 commit comments