@@ -73,55 +73,57 @@ const IDVisitor = {
73
73
} ,
74
74
} ;
75
75
76
- const FUNCTIONS = {
77
- mock : args => {
78
- if ( args . length === 1 ) {
79
- return args [ 0 ] . isStringLiteral ( ) ;
80
- } else if ( args . length === 2 ) {
81
- const moduleFactory = args [ 1 ] ;
82
- invariant (
83
- moduleFactory . isFunction ( ) ,
84
- 'The second argument of `jest.mock` must be a function.'
85
- ) ;
86
-
87
- const ids = new Set ( ) ;
88
- const parentScope = moduleFactory . parentPath . scope ;
89
- moduleFactory . traverse ( IDVisitor , { ids} ) ;
90
- for ( const id of ids ) {
91
- const name = id . node . name ;
92
- let found = false ;
93
- let scope = id . scope ;
76
+ const FUNCTIONS = Object . create ( null ) ;
77
+ FUNCTIONS . mock = args => {
78
+ if ( args . length === 1 ) {
79
+ return args [ 0 ] . isStringLiteral ( ) ;
80
+ } else if ( args . length === 2 ) {
81
+ const moduleFactory = args [ 1 ] ;
82
+ invariant (
83
+ moduleFactory . isFunction ( ) ,
84
+ 'The second argument of `jest.mock` must be a function.'
85
+ ) ;
94
86
95
- while ( scope !== parentScope ) {
96
- if ( scope . bindings [ name ] ) {
97
- found = true ;
98
- break ;
99
- }
87
+ const ids = new Set ( ) ;
88
+ const parentScope = moduleFactory . parentPath . scope ;
89
+ moduleFactory . traverse ( IDVisitor , { ids} ) ;
90
+ for ( const id of ids ) {
91
+ const name = id . node . name ;
92
+ let found = false ;
93
+ let scope = id . scope ;
100
94
101
- scope = scope . parent ;
95
+ while ( scope !== parentScope ) {
96
+ if ( scope . bindings [ name ] ) {
97
+ found = true ;
98
+ break ;
102
99
}
103
100
104
- if ( ! found ) {
105
- invariant (
106
- scope . hasGlobal ( name ) && WHITELISTED_IDENTIFIERS [ name ] ,
107
- 'The second argument of `jest.mock()` is not allowed to ' +
108
- 'reference any outside variables.\n' +
109
- 'Invalid variable access: ' + name + '\n' +
110
- 'Whitelisted objects: ' +
111
- Object . keys ( WHITELISTED_IDENTIFIERS ) . join ( ', ' ) + '.'
112
- ) ;
113
- }
101
+ scope = scope . parent ;
114
102
}
115
103
116
- return true ;
104
+ if ( ! found ) {
105
+ invariant (
106
+ scope . hasGlobal ( name ) && WHITELISTED_IDENTIFIERS [ name ] ,
107
+ 'The second argument of `jest.mock()` is not allowed to ' +
108
+ 'reference any outside variables.\n' +
109
+ 'Invalid variable access: ' + name + '\n' +
110
+ 'Whitelisted objects: ' +
111
+ Object . keys ( WHITELISTED_IDENTIFIERS ) . join ( ', ' ) + '.'
112
+ ) ;
113
+ }
117
114
}
118
- return false ;
119
- } ,
120
- unmock : args => args . length === 1 && args [ 0 ] . isStringLiteral ( ) ,
121
- disableAutomock : args => args . length === 0 ,
122
- enableAutomock : args => args . length === 0 ,
115
+
116
+ return true ;
117
+ }
118
+ return false ;
123
119
} ;
124
120
121
+ FUNCTIONS . unmock = args => args . length === 1 && args [ 0 ] . isStringLiteral ( ) ;
122
+
123
+ FUNCTIONS . disableAutomock =
124
+ FUNCTIONS . enableAutomock =
125
+ args => args . length === 0 ;
126
+
125
127
module . exports = babel => {
126
128
const shouldHoistExpression = expr => {
127
129
if ( ! expr . isCallExpression ( ) ) {
0 commit comments