@@ -2,8 +2,8 @@ const fs = require('fs');
22const path = require ( 'path' ) ;
33
44function loadContent ( content ) {
5- var Module = module . constructor ;
6- var m = new Module ( ) ;
5+ const Module = module . constructor ;
6+ const m = new Module ( ) ;
77 m . _compile ( content , "tmp.js" ) ;
88 m . exports . ignore_order = content . indexOf ( "\n// ignore-order\n" ) !== - 1 ||
99 content . startsWith ( "// ignore-order\n" ) ;
@@ -26,16 +26,16 @@ function contentToDiffLine(key, value) {
2626// the diff between the two items.
2727function betterLookingDiff ( entry , data ) {
2828 let output = ' {\n' ;
29- let spaces = ' ' ;
30- for ( let key in entry ) {
29+ const spaces = ' ' ;
30+ for ( const key in entry ) {
3131 if ( ! entry . hasOwnProperty ( key ) ) {
3232 continue ;
3333 }
3434 if ( ! data || ! data . hasOwnProperty ( key ) ) {
3535 output += '-' + spaces + contentToDiffLine ( key , entry [ key ] ) + '\n' ;
3636 continue ;
3737 }
38- let value = data [ key ] ;
38+ const value = data [ key ] ;
3939 if ( value !== entry [ key ] ) {
4040 output += '-' + spaces + contentToDiffLine ( key , entry [ key ] ) + '\n' ;
4141 output += '+' + spaces + contentToDiffLine ( key , value ) + '\n' ;
@@ -47,31 +47,28 @@ function betterLookingDiff(entry, data) {
4747}
4848
4949function 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 ) {
5353 if ( ! entry . hasOwnProperty ( key ) ) {
5454 continue ;
5555 }
56- var value = data [ i ] [ key ] ;
56+ let value = data_entry [ key ] ;
5757 // 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 ) {
5959 if ( value . length > 0 ) {
60- value += '::' + data [ i ] [ 'parent' ] [ 'name' ] ;
60+ value += '::' + data_entry [ 'parent' ] [ 'name' ] ;
6161 } else {
62- value = data [ i ] [ 'parent' ] [ 'name' ] ;
62+ value = data_entry [ 'parent' ] [ 'name' ] ;
6363 }
6464 }
6565 if ( value !== entry [ key ] ) {
6666 allGood = false ;
6767 break ;
6868 }
6969 }
70- if ( allGood === true ) {
71- return i ;
72- }
73- }
74- return null ;
70+ return allGood === true ;
71+ } ) ;
7572}
7673
7774// 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)
9794 } else {
9895 fieldsToCheck = [ ] ;
9996 }
100- for ( var i = 0 ; i < fieldsToCheck . length ; ++ i ) {
101- const field = fieldsToCheck [ i ] ;
97+ for ( const field of fieldsToCheck ) {
10298 if ( ! expected . hasOwnProperty ( field ) ) {
10399 let text = `${ queryName } ==> Mandatory key \`${ field } \` is not present` ;
104100 if ( fullPath . length > 0 ) {
105101 text += ` in field \`${ fullPath } \`` ;
106- if ( position != null ) {
102+ if ( position !== null ) {
107103 text += ` (position ${ position } )` ;
108104 }
109105 }
@@ -114,7 +110,8 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
114110
115111function valueCheck ( fullPath , expected , result , error_text , queryName ) {
116112 if ( Array . isArray ( expected ) ) {
117- for ( var i = 0 ; i < expected . length ; ++ i ) {
113+ let i ;
114+ for ( i = 0 ; i < expected . length ; ++ i ) {
118115 checkNeededFields ( fullPath , expected [ i ] , error_text , queryName , i ) ;
119116 if ( i >= result . length ) {
120117 error_text . push ( `${ queryName } ==> EXPECTED has extra value in array from field ` +
@@ -154,8 +151,8 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
154151 valueCheck ( obj_path , expected [ key ] , result_v , error_text , queryName ) ;
155152 }
156153 } else {
157- expectedValue = JSON . stringify ( expected ) ;
158- resultValue = JSON . stringify ( result ) ;
154+ const expectedValue = JSON . stringify ( expected ) ;
155+ const resultValue = JSON . stringify ( result ) ;
159156 if ( expectedValue != resultValue ) {
160157 error_text . push ( `${ queryName } ==> Different values for field \`${ fullPath } \`:\n` +
161158 `EXPECTED: \`${ expectedValue } \`\nRESULT: \`${ resultValue } \`` ) ;
@@ -164,7 +161,7 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
164161}
165162
166163function runParser ( query , expected , parseQuery , queryName ) {
167- var error_text = [ ] ;
164+ const error_text = [ ] ;
168165 checkNeededFields ( "" , expected , error_text , queryName , null ) ;
169166 if ( error_text . length === 0 ) {
170167 valueCheck ( '' , expected , parseQuery ( query ) , error_text , queryName ) ;
@@ -176,48 +173,48 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
176173 const ignore_order = loadedFile . ignore_order ;
177174 const exact_check = loadedFile . exact_check ;
178175
179- var results = doSearch ( query , loadedFile . FILTER_CRATE ) ;
180- var error_text = [ ] ;
176+ const results = doSearch ( query , loadedFile . FILTER_CRATE ) ;
177+ const error_text = [ ] ;
181178
182- for ( var key in expected ) {
179+ for ( const key in expected ) {
183180 if ( ! expected . hasOwnProperty ( key ) ) {
184181 continue ;
185182 }
186183 if ( ! results . hasOwnProperty ( key ) ) {
187184 error_text . push ( '==> Unknown key "' + key + '"' ) ;
188185 break ;
189186 }
190- var entry = expected [ key ] ;
187+ const entry = expected [ key ] ;
191188
192189 if ( exact_check == true && entry . length !== results [ key ] . length ) {
193190 error_text . push ( queryName + "==> Expected exactly " + entry . length +
194191 " results but found " + results [ key ] . length + " in '" + key + "'" ) ;
195192 }
196193
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 ) {
201198 error_text . push ( queryName + "==> Result not found in '" + key + "': '" +
202- JSON . stringify ( entry [ i ] ) + "'" ) ;
199+ JSON . stringify ( elem ) + "'" ) ;
203200 // By default, we just compare the two first items.
204201 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 ;
207204 }
208205 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 ] ) ) ;
210207 } else if ( exact_check === true && prev_pos + 1 !== entry_pos ) {
211208 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 ] ) + "'" ) ;
214211 } 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 " +
216213 "to be before '" + JSON . stringify ( results [ key ] [ entry_pos ] ) + "'" ) ;
217214 } else {
218215 prev_pos = entry_pos ;
219216 }
220- }
217+ } ) ;
221218 }
222219 return error_text ;
223220}
@@ -252,15 +249,15 @@ function runCheck(loadedFile, key, callback) {
252249 console . log ( `==> QUERY variable should have the same length as ${ key } ` ) ;
253250 return 1 ;
254251 }
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 ] + "`]" ) ;
257254 if ( checkResult ( error_text , loadedFile , false ) !== 0 ) {
258255 return 1 ;
259256 }
260257 }
261258 console . log ( "OK" ) ;
262259 } else {
263- var error_text = callback ( query , expected , "" ) ;
260+ const error_text = callback ( query , expected , "" ) ;
264261 if ( checkResult ( error_text , loadedFile , true ) !== 0 ) {
265262 return 1 ;
266263 }
@@ -269,9 +266,9 @@ function runCheck(loadedFile, key, callback) {
269266}
270267
271268function 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;' ;
275272
276273 if ( testFileContent . indexOf ( "FILTER_CRATE" ) !== - 1 ) {
277274 testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;" ;
@@ -294,7 +291,7 @@ function runChecks(testFile, doSearch, parseQuery) {
294291 }
295292
296293 const loadedFile = loadContent ( testFileContent ) ;
297- var res = 0 ;
294+ let res = 0 ;
298295
299296 if ( checkExpected ) {
300297 res += runCheck ( loadedFile , "EXPECTED" , ( query , expected , text ) => {
@@ -323,8 +320,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
323320 const searchIndex = require ( searchIndexJs ) ;
324321
325322 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 $ / ) ) ;
328324 const searchModule = require ( path . join ( staticFiles , searchJs ) ) ;
329325 const searchWords = searchModule . initSearch ( searchIndex . searchIndex ) ;
330326
@@ -334,7 +330,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
334330 filterCrate , currentCrate ) ;
335331 } ,
336332 parseQuery : searchModule . parseQuery ,
337- }
333+ } ;
338334}
339335
340336function showHelp ( ) {
@@ -349,22 +345,22 @@ function showHelp() {
349345}
350346
351347function parseOptions ( args ) {
352- var opts = {
348+ const opts = {
353349 "crate_name" : "" ,
354350 "resource_suffix" : "" ,
355351 "doc_folder" : "" ,
356352 "test_folder" : "" ,
357353 "test_file" : [ ] ,
358354 } ;
359- var correspondences = {
355+ const correspondences = {
360356 "--resource-suffix" : "resource_suffix" ,
361357 "--doc-folder" : "doc_folder" ,
362358 "--test-folder" : "test_folder" ,
363359 "--test-file" : "test_file" ,
364360 "--crate-name" : "crate_name" ,
365361 } ;
366362
367- for ( var i = 0 ; i < args . length ; ++ i ) {
363+ for ( let i = 0 ; i < args . length ; ++ i ) {
368364 if ( correspondences . hasOwnProperty ( args [ i ] ) ) {
369365 i += 1 ;
370366 if ( i >= args . length ) {
@@ -398,17 +394,18 @@ function parseOptions(args) {
398394}
399395
400396function main ( argv ) {
401- var opts = parseOptions ( argv . slice ( 2 ) ) ;
397+ const opts = parseOptions ( argv . slice ( 2 ) ) ;
402398 if ( opts === null ) {
403399 return 1 ;
404400 }
405401
406- let parseAndSearch = loadSearchJS (
402+ const parseAndSearch = loadSearchJS (
407403 opts [ "doc_folder" ] ,
408- opts [ "resource_suffix" ] ) ;
409- var errors = 0 ;
404+ opts [ "resource_suffix" ]
405+ ) ;
406+ let errors = 0 ;
410407
411- let doSearch = function ( queryStr , filterCrate ) {
408+ const doSearch = function ( queryStr , filterCrate ) {
412409 return parseAndSearch . doSearch ( queryStr , filterCrate , opts [ "crate_name" ] ) ;
413410 } ;
414411
0 commit comments