@@ -2,8 +2,8 @@ const fs = require('fs');
2
2
const path = require ( 'path' ) ;
3
3
4
4
function loadContent ( content ) {
5
- var Module = module . constructor ;
6
- var m = new Module ( ) ;
5
+ const Module = module . constructor ;
6
+ const m = new Module ( ) ;
7
7
m . _compile ( content , "tmp.js" ) ;
8
8
m . exports . ignore_order = content . indexOf ( "\n// ignore-order\n" ) !== - 1 ||
9
9
content . startsWith ( "// ignore-order\n" ) ;
@@ -26,16 +26,16 @@ function contentToDiffLine(key, value) {
26
26
// the diff between the two items.
27
27
function betterLookingDiff ( entry , data ) {
28
28
let output = ' {\n' ;
29
- let spaces = ' ' ;
30
- for ( let key in entry ) {
29
+ const spaces = ' ' ;
30
+ for ( const key in entry ) {
31
31
if ( ! entry . hasOwnProperty ( key ) ) {
32
32
continue ;
33
33
}
34
34
if ( ! data || ! data . hasOwnProperty ( key ) ) {
35
35
output += '-' + spaces + contentToDiffLine ( key , entry [ key ] ) + '\n' ;
36
36
continue ;
37
37
}
38
- let value = data [ key ] ;
38
+ const value = data [ key ] ;
39
39
if ( value !== entry [ key ] ) {
40
40
output += '-' + spaces + contentToDiffLine ( key , entry [ key ] ) + '\n' ;
41
41
output += '+' + spaces + contentToDiffLine ( key , value ) + '\n' ;
@@ -47,31 +47,28 @@ function betterLookingDiff(entry, data) {
47
47
}
48
48
49
49
function lookForEntry ( entry , data ) {
50
- for ( var i = 0 ; i < data . length ; ++ i ) {
51
- var allGood = true ;
52
- for ( var key in entry ) {
50
+ return data . findIndex ( data_entry => {
51
+ let allGood = true ;
52
+ for ( const key in entry ) {
53
53
if ( ! entry . hasOwnProperty ( key ) ) {
54
54
continue ;
55
55
}
56
- var value = data [ i ] [ key ] ;
56
+ let value = data_entry [ key ] ;
57
57
// To make our life easier, if there is a "parent" type, we add it to the path.
58
- if ( key === 'path' && data [ i ] [ 'parent' ] !== undefined ) {
58
+ if ( key === 'path' && data_entry [ 'parent' ] !== undefined ) {
59
59
if ( value . length > 0 ) {
60
- value += '::' + data [ i ] [ 'parent' ] [ 'name' ] ;
60
+ value += '::' + data_entry [ 'parent' ] [ 'name' ] ;
61
61
} else {
62
- value = data [ i ] [ 'parent' ] [ 'name' ] ;
62
+ value = data_entry [ 'parent' ] [ 'name' ] ;
63
63
}
64
64
}
65
65
if ( value !== entry [ key ] ) {
66
66
allGood = false ;
67
67
break ;
68
68
}
69
69
}
70
- if ( allGood === true ) {
71
- return i ;
72
- }
73
- }
74
- return null ;
70
+ return allGood === true ;
71
+ } ) ;
75
72
}
76
73
77
74
// This function checks if `expected` has all the required fields needed for the checks.
@@ -97,13 +94,12 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
97
94
} else {
98
95
fieldsToCheck = [ ] ;
99
96
}
100
- for ( var i = 0 ; i < fieldsToCheck . length ; ++ i ) {
101
- const field = fieldsToCheck [ i ] ;
97
+ for ( const field of fieldsToCheck ) {
102
98
if ( ! expected . hasOwnProperty ( field ) ) {
103
99
let text = `${ queryName } ==> Mandatory key \`${ field } \` is not present` ;
104
100
if ( fullPath . length > 0 ) {
105
101
text += ` in field \`${ fullPath } \`` ;
106
- if ( position != null ) {
102
+ if ( position !== null ) {
107
103
text += ` (position ${ position } )` ;
108
104
}
109
105
}
@@ -114,7 +110,8 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
114
110
115
111
function valueCheck ( fullPath , expected , result , error_text , queryName ) {
116
112
if ( Array . isArray ( expected ) ) {
117
- for ( var i = 0 ; i < expected . length ; ++ i ) {
113
+ let i ;
114
+ for ( i = 0 ; i < expected . length ; ++ i ) {
118
115
checkNeededFields ( fullPath , expected [ i ] , error_text , queryName , i ) ;
119
116
if ( i >= result . length ) {
120
117
error_text . push ( `${ queryName } ==> EXPECTED has extra value in array from field ` +
@@ -154,8 +151,8 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
154
151
valueCheck ( obj_path , expected [ key ] , result_v , error_text , queryName ) ;
155
152
}
156
153
} else {
157
- expectedValue = JSON . stringify ( expected ) ;
158
- resultValue = JSON . stringify ( result ) ;
154
+ const expectedValue = JSON . stringify ( expected ) ;
155
+ const resultValue = JSON . stringify ( result ) ;
159
156
if ( expectedValue != resultValue ) {
160
157
error_text . push ( `${ queryName } ==> Different values for field \`${ fullPath } \`:\n` +
161
158
`EXPECTED: \`${ expectedValue } \`\nRESULT: \`${ resultValue } \`` ) ;
@@ -164,7 +161,7 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
164
161
}
165
162
166
163
function runParser ( query , expected , parseQuery , queryName ) {
167
- var error_text = [ ] ;
164
+ const error_text = [ ] ;
168
165
checkNeededFields ( "" , expected , error_text , queryName , null ) ;
169
166
if ( error_text . length === 0 ) {
170
167
valueCheck ( '' , expected , parseQuery ( query ) , error_text , queryName ) ;
@@ -176,48 +173,48 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
176
173
const ignore_order = loadedFile . ignore_order ;
177
174
const exact_check = loadedFile . exact_check ;
178
175
179
- var results = doSearch ( query , loadedFile . FILTER_CRATE ) ;
180
- var error_text = [ ] ;
176
+ const results = doSearch ( query , loadedFile . FILTER_CRATE ) ;
177
+ const error_text = [ ] ;
181
178
182
- for ( var key in expected ) {
179
+ for ( const key in expected ) {
183
180
if ( ! expected . hasOwnProperty ( key ) ) {
184
181
continue ;
185
182
}
186
183
if ( ! results . hasOwnProperty ( key ) ) {
187
184
error_text . push ( '==> Unknown key "' + key + '"' ) ;
188
185
break ;
189
186
}
190
- var entry = expected [ key ] ;
187
+ const entry = expected [ key ] ;
191
188
192
189
if ( exact_check == true && entry . length !== results [ key ] . length ) {
193
190
error_text . push ( queryName + "==> Expected exactly " + entry . length +
194
191
" results but found " + results [ key ] . length + " in '" + key + "'" ) ;
195
192
}
196
193
197
- var prev_pos = - 1 ;
198
- for ( var i = 0 ; i < entry . length ; ++ i ) {
199
- var entry_pos = lookForEntry ( entry [ i ] , results [ key ] ) ;
200
- if ( entry_pos === null ) {
194
+ let prev_pos = - 1 ;
195
+ entry . forEach ( ( elem , index ) => {
196
+ const entry_pos = lookForEntry ( elem , results [ key ] ) ;
197
+ if ( entry_pos === - 1 ) {
201
198
error_text . push ( queryName + "==> Result not found in '" + key + "': '" +
202
- JSON . stringify ( entry [ i ] ) + "'" ) ;
199
+ JSON . stringify ( elem ) + "'" ) ;
203
200
// By default, we just compare the two first items.
204
201
let item_to_diff = 0 ;
205
- if ( ( ignore_order === false || exact_check === true ) && i < results [ key ] . length ) {
206
- item_to_diff = i ;
202
+ if ( ( ! ignore_order || exact_check ) && index < results [ key ] . length ) {
203
+ item_to_diff = index ;
207
204
}
208
205
error_text . push ( "Diff of first error:\n" +
209
- betterLookingDiff ( entry [ i ] , results [ key ] [ item_to_diff ] ) ) ;
206
+ betterLookingDiff ( elem , results [ key ] [ item_to_diff ] ) ) ;
210
207
} else if ( exact_check === true && prev_pos + 1 !== entry_pos ) {
211
208
error_text . push ( queryName + "==> Exact check failed at position " + ( prev_pos + 1 ) +
212
- ": expected '" + JSON . stringify ( entry [ i ] ) + "' but found '" +
213
- JSON . stringify ( results [ key ] [ i ] ) + "'" ) ;
209
+ ": expected '" + JSON . stringify ( elem ) + "' but found '" +
210
+ JSON . stringify ( results [ key ] [ index ] ) + "'" ) ;
214
211
} else if ( ignore_order === false && entry_pos < prev_pos ) {
215
- error_text . push ( queryName + "==> '" + JSON . stringify ( entry [ i ] ) + "' was supposed " +
212
+ error_text . push ( queryName + "==> '" + JSON . stringify ( elem ) + "' was supposed " +
216
213
"to be before '" + JSON . stringify ( results [ key ] [ entry_pos ] ) + "'" ) ;
217
214
} else {
218
215
prev_pos = entry_pos ;
219
216
}
220
- }
217
+ } ) ;
221
218
}
222
219
return error_text ;
223
220
}
@@ -252,15 +249,15 @@ function runCheck(loadedFile, key, callback) {
252
249
console . log ( `==> QUERY variable should have the same length as ${ key } ` ) ;
253
250
return 1 ;
254
251
}
255
- for ( var i = 0 ; i < query . length ; ++ i ) {
256
- var error_text = callback ( query [ i ] , expected [ i ] , "[ query `" + query [ i ] + "`]" ) ;
252
+ for ( let i = 0 ; i < query . length ; ++ i ) {
253
+ const error_text = callback ( query [ i ] , expected [ i ] , "[ query `" + query [ i ] + "`]" ) ;
257
254
if ( checkResult ( error_text , loadedFile , false ) !== 0 ) {
258
255
return 1 ;
259
256
}
260
257
}
261
258
console . log ( "OK" ) ;
262
259
} else {
263
- var error_text = callback ( query , expected , "" ) ;
260
+ const error_text = callback ( query , expected , "" ) ;
264
261
if ( checkResult ( error_text , loadedFile , true ) !== 0 ) {
265
262
return 1 ;
266
263
}
@@ -269,9 +266,9 @@ function runCheck(loadedFile, key, callback) {
269
266
}
270
267
271
268
function runChecks ( testFile , doSearch , parseQuery ) {
272
- var checkExpected = false ;
273
- var checkParsed = false ;
274
- var testFileContent = readFile ( testFile ) + 'exports.QUERY = QUERY;' ;
269
+ let checkExpected = false ;
270
+ let checkParsed = false ;
271
+ let testFileContent = readFile ( testFile ) + 'exports.QUERY = QUERY;' ;
275
272
276
273
if ( testFileContent . indexOf ( "FILTER_CRATE" ) !== - 1 ) {
277
274
testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;" ;
@@ -294,7 +291,7 @@ function runChecks(testFile, doSearch, parseQuery) {
294
291
}
295
292
296
293
const loadedFile = loadContent ( testFileContent ) ;
297
- var res = 0 ;
294
+ let res = 0 ;
298
295
299
296
if ( checkExpected ) {
300
297
res += runCheck ( loadedFile , "EXPECTED" , ( query , expected , text ) => {
@@ -323,8 +320,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
323
320
const searchIndex = require ( searchIndexJs ) ;
324
321
325
322
const staticFiles = path . join ( doc_folder , "static.files" ) ;
326
- const searchJs = fs . readdirSync ( staticFiles ) . find (
327
- f => f . match ( / s e a r c h .* \. j s $ / ) ) ;
323
+ const searchJs = fs . readdirSync ( staticFiles ) . find ( f => f . match ( / s e a r c h .* \. j s $ / ) ) ;
328
324
const searchModule = require ( path . join ( staticFiles , searchJs ) ) ;
329
325
const searchWords = searchModule . initSearch ( searchIndex . searchIndex ) ;
330
326
@@ -334,7 +330,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
334
330
filterCrate , currentCrate ) ;
335
331
} ,
336
332
parseQuery : searchModule . parseQuery ,
337
- }
333
+ } ;
338
334
}
339
335
340
336
function showHelp ( ) {
@@ -349,22 +345,22 @@ function showHelp() {
349
345
}
350
346
351
347
function parseOptions ( args ) {
352
- var opts = {
348
+ const opts = {
353
349
"crate_name" : "" ,
354
350
"resource_suffix" : "" ,
355
351
"doc_folder" : "" ,
356
352
"test_folder" : "" ,
357
353
"test_file" : [ ] ,
358
354
} ;
359
- var correspondences = {
355
+ const correspondences = {
360
356
"--resource-suffix" : "resource_suffix" ,
361
357
"--doc-folder" : "doc_folder" ,
362
358
"--test-folder" : "test_folder" ,
363
359
"--test-file" : "test_file" ,
364
360
"--crate-name" : "crate_name" ,
365
361
} ;
366
362
367
- for ( var i = 0 ; i < args . length ; ++ i ) {
363
+ for ( let i = 0 ; i < args . length ; ++ i ) {
368
364
if ( correspondences . hasOwnProperty ( args [ i ] ) ) {
369
365
i += 1 ;
370
366
if ( i >= args . length ) {
@@ -398,17 +394,18 @@ function parseOptions(args) {
398
394
}
399
395
400
396
function main ( argv ) {
401
- var opts = parseOptions ( argv . slice ( 2 ) ) ;
397
+ const opts = parseOptions ( argv . slice ( 2 ) ) ;
402
398
if ( opts === null ) {
403
399
return 1 ;
404
400
}
405
401
406
- let parseAndSearch = loadSearchJS (
402
+ const parseAndSearch = loadSearchJS (
407
403
opts [ "doc_folder" ] ,
408
- opts [ "resource_suffix" ] ) ;
409
- var errors = 0 ;
404
+ opts [ "resource_suffix" ]
405
+ ) ;
406
+ let errors = 0 ;
410
407
411
- let doSearch = function ( queryStr , filterCrate ) {
408
+ const doSearch = function ( queryStr , filterCrate ) {
412
409
return parseAndSearch . doSearch ( queryStr , filterCrate , opts [ "crate_name" ] ) ;
413
410
} ;
414
411
0 commit comments