@@ -10,156 +10,154 @@ const fs = require('fs');
10
10
let ignoreDirectivesRelativeTo ;
11
11
12
12
function addDefaultIgnores ( ) {
13
- deepIgnore . add ( [
14
- 'node_modules/unexpected*' ,
15
- 'node_modules/array-changes' ,
16
- 'node_modules/arraydiff-papandreou' ,
17
- 'node_modules/array-changes-async' ,
18
- 'node_modules/arraydiff-async' ,
19
- 'node_modules/greedy-interval-packer' ,
20
- 'node_modules/magicpen' ,
21
- 'node_modules/mocha' ,
22
- 'node_modules/jest' ,
23
- 'node_modules/jasmine' ,
24
- 'node_modules/chance' ,
25
- 'node_modules/chance-generators' ,
26
- 'node_modules/sinon'
27
- ] ) ;
28
- shallowIgnore . add ( [
29
- 'test' ,
30
- '*.spec.js' ,
31
- '../../unexpected-check/lib/unexpected-check.js'
32
- ] ) ;
13
+ deepIgnore . add ( [
14
+ 'node_modules/unexpected*' ,
15
+ 'node_modules/array-changes' ,
16
+ 'node_modules/arraydiff-papandreou' ,
17
+ 'node_modules/array-changes-async' ,
18
+ 'node_modules/arraydiff-async' ,
19
+ 'node_modules/greedy-interval-packer' ,
20
+ 'node_modules/magicpen' ,
21
+ 'node_modules/mocha' ,
22
+ 'node_modules/jest' ,
23
+ 'node_modules/jasmine' ,
24
+ 'node_modules/chance' ,
25
+ 'node_modules/chance-generators' ,
26
+ 'node_modules/sinon'
27
+ ] ) ;
28
+ shallowIgnore . add ( [
29
+ 'test' ,
30
+ '*.spec.js' ,
31
+ '../../unexpected-check/lib/unexpected-check.js'
32
+ ] ) ;
33
33
}
34
34
35
35
if ( process . env . DFL_IGNORE ) {
36
- // Root-relative paths will be interpreted as absolute
37
- shallowIgnore . add ( process . env . DFL_IGNORE ) ;
36
+ // Root-relative paths will be interpreted as absolute
37
+ shallowIgnore . add ( process . env . DFL_IGNORE ) ;
38
38
} else {
39
- const closestPackageJson = pkgUp . sync ( ) ;
40
- if ( closestPackageJson ) {
41
- try {
42
- const packageDirName = pathModule . dirname ( closestPackageJson ) ;
43
- ignoreDirectivesRelativeTo = packageDirName ;
44
-
45
- const dflIgnorePath = pathModule . resolve (
46
- packageDirName ,
47
- '.dflignore'
48
- ) ;
49
- // Root-relative paths will be interpreted as relative
50
- // to the .dflignore file, as per the .gitignore convention:
51
- shallowIgnore . add ( fs . readFileSync ( dflIgnorePath , 'utf-8' ) ) ;
52
- } catch ( err ) {
53
- addDefaultIgnores ( ) ;
54
- }
55
- } else {
56
- addDefaultIgnores ( ) ;
39
+ const closestPackageJson = pkgUp . sync ( ) ;
40
+ if ( closestPackageJson ) {
41
+ try {
42
+ const packageDirName = pathModule . dirname ( closestPackageJson ) ;
43
+ ignoreDirectivesRelativeTo = packageDirName ;
44
+
45
+ const dflIgnorePath = pathModule . resolve ( packageDirName , '.dflignore' ) ;
46
+ // Root-relative paths will be interpreted as relative
47
+ // to the .dflignore file, as per the .gitignore convention:
48
+ shallowIgnore . add ( fs . readFileSync ( dflIgnorePath , 'utf-8' ) ) ;
49
+ } catch ( err ) {
50
+ addDefaultIgnores ( ) ;
57
51
}
52
+ } else {
53
+ addDefaultIgnores ( ) ;
54
+ }
58
55
}
59
56
60
57
let locations ;
61
58
let prevLocation ;
62
59
63
60
function initialize ( ) {
64
- global . recordLocation . locations = locations = Object . create ( null ) ;
65
- global . recordProximity . proximity = Object . create ( null ) ;
66
- prevLocation = 0 ;
61
+ global . recordLocation . locations = locations = Object . create ( null ) ;
62
+ global . recordProximity . proximity = Object . create ( null ) ;
63
+ prevLocation = 0 ;
67
64
}
68
65
69
- global . recordLocation = ( location ) => {
70
- const key = location ^ prevLocation ;
71
- locations [ key ] = ( locations [ key ] || 0 ) + 1 ;
72
- prevLocation = location >> 1 ;
66
+ global . recordLocation = location => {
67
+ const key = location ^ prevLocation ;
68
+ locations [ key ] = ( locations [ key ] || 0 ) + 1 ;
69
+ prevLocation = location >> 1 ;
73
70
} ;
74
71
75
72
function evaluate ( left , operator , right ) {
76
- switch ( operator ) {
73
+ switch ( operator ) {
77
74
case '===' :
78
- return left === right ;
75
+ return left === right ;
79
76
case '!==' :
80
- return left !== right ;
77
+ return left !== right ;
81
78
case '<' :
82
- return left < right ;
79
+ return left < right ;
83
80
case '>' :
84
- return left > right ;
81
+ return left > right ;
85
82
case '<=' :
86
- return left <= right ;
83
+ return left <= right ;
87
84
case '>=' :
88
- return left >= right ;
85
+ return left >= right ;
89
86
case '==' :
90
- return left == right ; // eslint-disable-line eqeqeq
91
- }
87
+ return left == right ; // eslint-disable-line eqeqeq
88
+ }
92
89
}
93
90
94
91
function calculateNumberProximity ( left , operator , right ) {
95
- var difference = Math . abs ( left - right ) ;
96
- if ( difference > 1000 ) {
97
- return null ;
98
- }
92
+ var difference = Math . abs ( left - right ) ;
93
+ if ( difference > 1000 ) {
94
+ return null ;
95
+ }
99
96
100
- switch ( operator ) {
97
+ switch ( operator ) {
101
98
case '!==' :
102
- return Number . isInteger ( left ) && Number . isInteger ( right ) ? 1 : null ;
99
+ return Number . isInteger ( left ) && Number . isInteger ( right ) ? 1 : null ;
103
100
case '===' :
104
101
case '==' :
105
- return Number . isInteger ( left ) && Number . isInteger ( right )
106
- ? difference
107
- : null ;
102
+ return Number . isInteger ( left ) && Number . isInteger ( right )
103
+ ? difference
104
+ : null ;
108
105
case '<' :
109
106
case '>' :
110
- return difference ;
107
+ return difference ;
111
108
case '<=' :
112
109
case '>=' :
113
- return difference + 1 ;
110
+ return difference + 1 ;
114
111
default :
115
- return null ;
116
- }
112
+ return null ;
113
+ }
117
114
}
118
115
119
116
function calculateStringProximity ( left , operator , right ) {
120
- switch ( operator ) {
117
+ switch ( operator ) {
121
118
case '!==' :
122
- return 1 ;
119
+ return 1 ;
123
120
case '===' :
124
121
case '==' :
125
- const lengthDifference = Math . abs ( left . length - right . length ) ;
126
- const limit = 30 ;
127
-
128
- let difference = lengthDifference ;
129
- if ( difference >= limit ) {
130
- for ( var i = 0 ; i < Math . min ( left . length , right . length ) ; i += 1 ) {
131
- if ( left [ i ] !== right [ i ] ) {
132
- difference ++ ;
133
- }
134
- }
122
+ const lengthDifference = Math . abs ( left . length - right . length ) ;
123
+ const limit = 30 ;
124
+
125
+ let difference = lengthDifference ;
126
+ if ( difference >= limit ) {
127
+ for ( var i = 0 ; i < Math . min ( left . length , right . length ) ; i += 1 ) {
128
+ if ( left [ i ] !== right [ i ] ) {
129
+ difference ++ ;
130
+ }
135
131
}
132
+ }
136
133
137
- return difference > limit ? null : difference ;
134
+ return difference > limit ? null : difference ;
138
135
default :
139
- return null ;
140
- }
136
+ return null ;
137
+ }
141
138
}
142
139
143
140
global . recordProximity = ( left , operator , right ) => {
144
- let result = evaluate ( left , operator , right ) ;
145
- const leftType = typeof left ;
146
- const rightType = typeof right ;
141
+ let result = evaluate ( left , operator , right ) ;
142
+ const leftType = typeof left ;
143
+ const rightType = typeof right ;
147
144
148
- let proximity = null ;
145
+ let proximity = null ;
149
146
150
- if ( ! result ) {
151
- if ( leftType === 'number' && rightType === 'number' ) {
152
- proximity = calculateNumberProximity ( left , operator , right ) ;
153
- } else if ( leftType === 'string' && rightType === 'string' ) {
154
- proximity = calculateStringProximity ( left , operator , right ) ;
155
- }
147
+ if ( ! result ) {
148
+ if ( leftType === 'number' && rightType === 'number' ) {
149
+ proximity = calculateNumberProximity ( left , operator , right ) ;
150
+ } else if ( leftType === 'string' && rightType === 'string' ) {
151
+ proximity = calculateStringProximity ( left , operator , right ) ;
156
152
}
153
+ }
157
154
158
- if ( proximity !== null && proximity > 0 ) {
159
- global . recordProximity . proximity [ proximity ] = ( global . recordProximity . proximity [ proximity ] || 0 ) + 1 ;
160
- }
155
+ if ( proximity !== null && proximity > 0 ) {
156
+ global . recordProximity . proximity [ proximity ] =
157
+ ( global . recordProximity . proximity [ proximity ] || 0 ) + 1 ;
158
+ }
161
159
162
- return result ;
160
+ return result ;
163
161
} ;
164
162
165
163
initialize ( ) ;
@@ -169,39 +167,48 @@ global.recordLocation.magicValues = new Set();
169
167
170
168
var nextLocationNumber = 1 ;
171
169
function getNextLocationNumber ( ) {
172
- return nextLocationNumber ++ ;
170
+ return nextLocationNumber ++ ;
173
171
}
174
172
175
173
const oldRequireHook = require . extensions [ '.js' ] ;
176
- require . extensions [ '.js' ] = function ( module , absoluteFileName ) {
177
- let code ;
178
- oldRequireHook (
179
- Object . create ( module , {
180
- _compile : {
181
- value : _code => code = _code
182
- }
183
- } ) ,
184
- absoluteFileName
174
+ require . extensions [ '.js' ] = function ( module , absoluteFileName ) {
175
+ let code ;
176
+ oldRequireHook (
177
+ Object . create ( module , {
178
+ _compile : {
179
+ value : _code => ( code = _code )
180
+ }
181
+ } ) ,
182
+ absoluteFileName
183
+ ) ;
184
+ let fileNameToCheck ;
185
+ if ( ignoreDirectivesRelativeTo ) {
186
+ fileNameToCheck = pathModule . relative (
187
+ ignoreDirectivesRelativeTo ,
188
+ absoluteFileName
185
189
) ;
186
- let fileNameToCheck ;
187
- if ( ignoreDirectivesRelativeTo ) {
188
- fileNameToCheck = pathModule . relative ( ignoreDirectivesRelativeTo , absoluteFileName ) ;
189
- } else {
190
- fileNameToCheck = absoluteFileName ;
191
- }
192
-
193
- if ( deepIgnore . ignores ( fileNameToCheck ) || ( module . parent && module . parent . _unexpectedCheckDeepIgnored ) ) {
194
- module . _unexpectedCheckDeepIgnored = true ;
195
- } else if ( ! shallowIgnore . ignores ( fileNameToCheck ) ) {
196
- console . log ( 'instrument' , fileNameToCheck ) ;
197
- const ast = esprima . parseScript ( code , {
198
- source : pathModule . relative ( process . cwd ( ) , absoluteFileName )
199
- } ) ;
200
- const { instrumentedAst, magicValues } = instrumentAst ( ast , getNextLocationNumber ) ;
201
- code = escodegen . generate ( instrumentedAst ) ;
202
- for ( const magicValue of magicValues ) {
203
- global . recordLocation . magicValues . add ( magicValue ) ;
204
- }
190
+ } else {
191
+ fileNameToCheck = absoluteFileName ;
192
+ }
193
+
194
+ if (
195
+ deepIgnore . ignores ( fileNameToCheck ) ||
196
+ ( module . parent && module . parent . _unexpectedCheckDeepIgnored )
197
+ ) {
198
+ module . _unexpectedCheckDeepIgnored = true ;
199
+ } else if ( ! shallowIgnore . ignores ( fileNameToCheck ) ) {
200
+ console . log ( 'instrument' , fileNameToCheck ) ;
201
+ const ast = esprima . parseScript ( code , {
202
+ source : pathModule . relative ( process . cwd ( ) , absoluteFileName )
203
+ } ) ;
204
+ const { instrumentedAst, magicValues } = instrumentAst (
205
+ ast ,
206
+ getNextLocationNumber
207
+ ) ;
208
+ code = escodegen . generate ( instrumentedAst ) ;
209
+ for ( const magicValue of magicValues ) {
210
+ global . recordLocation . magicValues . add ( magicValue ) ;
205
211
}
206
- module . _compile ( code , absoluteFileName ) ;
212
+ }
213
+ module . _compile ( code , absoluteFileName ) ;
207
214
} ;
0 commit comments