1
- const fs = require ( 'fs' ) ;
2
- const path = require ( ' path' ) ;
1
+ const fs = require ( "fs" ) ;
2
+ const path = require ( " path" ) ;
3
3
4
4
function loadContent ( content ) {
5
5
const Module = module . constructor ;
@@ -15,7 +15,7 @@ function loadContent(content) {
15
15
}
16
16
17
17
function readFile ( filePath ) {
18
- return fs . readFileSync ( filePath , ' utf8' ) ;
18
+ return fs . readFileSync ( filePath , " utf8" ) ;
19
19
}
20
20
21
21
function contentToDiffLine ( key , value ) {
@@ -25,41 +25,41 @@ function contentToDiffLine(key, value) {
25
25
// This function is only called when no matching result was found and therefore will only display
26
26
// the diff between the two items.
27
27
function betterLookingDiff ( entry , data ) {
28
- let output = ' {\n' ;
29
- const spaces = ' ' ;
28
+ let output = " {\n" ;
29
+ const spaces = " " ;
30
30
for ( const key in entry ) {
31
- if ( ! entry . hasOwnProperty ( key ) ) {
31
+ if ( ! Object . prototype . hasOwnProperty . call ( entry , key ) ) {
32
32
continue ;
33
33
}
34
- if ( ! data || ! data . hasOwnProperty ( key ) ) {
35
- output += '-' + spaces + contentToDiffLine ( key , entry [ key ] ) + '\n' ;
34
+ if ( ! data || ! Object . prototype . hasOwnProperty . call ( data , key ) ) {
35
+ output += "-" + spaces + contentToDiffLine ( key , entry [ key ] ) + "\n" ;
36
36
continue ;
37
37
}
38
38
const value = data [ key ] ;
39
39
if ( value !== entry [ key ] ) {
40
- output += '-' + spaces + contentToDiffLine ( key , entry [ key ] ) + '\n' ;
41
- output += '+' + spaces + contentToDiffLine ( key , value ) + '\n' ;
40
+ output += "-" + spaces + contentToDiffLine ( key , entry [ key ] ) + "\n" ;
41
+ output += "+" + spaces + contentToDiffLine ( key , value ) + "\n" ;
42
42
} else {
43
- output += spaces + contentToDiffLine ( key , value ) + '\n' ;
43
+ output += spaces + contentToDiffLine ( key , value ) + "\n" ;
44
44
}
45
45
}
46
- return output + ' }' ;
46
+ return output + " }" ;
47
47
}
48
48
49
49
function lookForEntry ( entry , data ) {
50
50
return data . findIndex ( data_entry => {
51
51
let allGood = true ;
52
52
for ( const key in entry ) {
53
- if ( ! entry . hasOwnProperty ( key ) ) {
53
+ if ( ! Object . prototype . hasOwnProperty . call ( entry , key ) ) {
54
54
continue ;
55
55
}
56
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_entry [ ' parent' ] !== undefined ) {
58
+ if ( key === " path" && data_entry [ " parent" ] !== undefined ) {
59
59
if ( value . length > 0 ) {
60
- value += '::' + data_entry [ ' parent' ] [ ' name' ] ;
60
+ value += "::" + data_entry [ " parent" ] [ " name" ] ;
61
61
} else {
62
- value = data_entry [ ' parent' ] [ ' name' ] ;
62
+ value = data_entry [ " parent" ] [ " name" ] ;
63
63
}
64
64
}
65
65
if ( value !== entry [ key ] ) {
@@ -95,7 +95,7 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
95
95
fieldsToCheck = [ ] ;
96
96
}
97
97
for ( const field of fieldsToCheck ) {
98
- if ( ! expected . hasOwnProperty ( field ) ) {
98
+ if ( ! Object . prototype . hasOwnProperty . call ( expected , field ) ) {
99
99
let text = `${ queryName } ==> Mandatory key \`${ field } \` is not present` ;
100
100
if ( fullPath . length > 0 ) {
101
101
text += ` in field \`${ fullPath } \`` ;
@@ -117,22 +117,22 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
117
117
error_text . push ( `${ queryName } ==> EXPECTED has extra value in array from field ` +
118
118
`\`${ fullPath } \` (position ${ i } ): \`${ JSON . stringify ( expected [ i ] ) } \`` ) ;
119
119
} else {
120
- valueCheck ( fullPath + '[' + i + ']' , expected [ i ] , result [ i ] , error_text , queryName ) ;
120
+ valueCheck ( fullPath + "[" + i + "]" , expected [ i ] , result [ i ] , error_text , queryName ) ;
121
121
}
122
122
}
123
123
for ( ; i < result . length ; ++ i ) {
124
124
error_text . push ( `${ queryName } ==> RESULT has extra value in array from field ` +
125
125
`\`${ fullPath } \` (position ${ i } ): \`${ JSON . stringify ( result [ i ] ) } \` ` +
126
- ' compared to EXPECTED' ) ;
126
+ " compared to EXPECTED" ) ;
127
127
}
128
128
} else if ( expected !== null && typeof expected !== "undefined" &&
129
- expected . constructor == Object ) {
129
+ expected . constructor == Object ) { // eslint-disable-line eqeqeq
130
130
for ( const key in expected ) {
131
- if ( ! expected . hasOwnProperty ( key ) ) {
131
+ if ( ! Object . prototype . hasOwnProperty . call ( expected , key ) ) {
132
132
continue ;
133
133
}
134
- if ( ! result . hasOwnProperty ( key ) ) {
135
- error_text . push ( ' ==> Unknown key "' + key + '"' ) ;
134
+ if ( ! Object . prototype . hasOwnProperty . call ( result , key ) ) {
135
+ error_text . push ( " ==> Unknown key \"" + key + "\"" ) ;
136
136
break ;
137
137
}
138
138
let result_v = result [ key ] ;
@@ -147,13 +147,13 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
147
147
} ) ;
148
148
result_v = result_v . join ( "" ) ;
149
149
}
150
- const obj_path = fullPath + ( fullPath . length > 0 ? '.' : '' ) + key ;
150
+ const obj_path = fullPath + ( fullPath . length > 0 ? "." : "" ) + key ;
151
151
valueCheck ( obj_path , expected [ key ] , result_v , error_text , queryName ) ;
152
152
}
153
153
} else {
154
154
const expectedValue = JSON . stringify ( expected ) ;
155
155
const resultValue = JSON . stringify ( result ) ;
156
- if ( expectedValue != resultValue ) {
156
+ if ( expectedValue !== resultValue ) {
157
157
error_text . push ( `${ queryName } ==> Different values for field \`${ fullPath } \`:\n` +
158
158
`EXPECTED: \`${ expectedValue } \`\nRESULT: \`${ resultValue } \`` ) ;
159
159
}
@@ -164,7 +164,7 @@ function runParser(query, expected, parseQuery, queryName) {
164
164
const error_text = [ ] ;
165
165
checkNeededFields ( "" , expected , error_text , queryName , null ) ;
166
166
if ( error_text . length === 0 ) {
167
- valueCheck ( '' , expected , parseQuery ( query ) , error_text , queryName ) ;
167
+ valueCheck ( "" , expected , parseQuery ( query ) , error_text , queryName ) ;
168
168
}
169
169
return error_text ;
170
170
}
@@ -177,16 +177,16 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
177
177
const error_text = [ ] ;
178
178
179
179
for ( const key in expected ) {
180
- if ( ! expected . hasOwnProperty ( key ) ) {
180
+ if ( ! Object . prototype . hasOwnProperty . call ( expected , key ) ) {
181
181
continue ;
182
182
}
183
- if ( ! results . hasOwnProperty ( key ) ) {
184
- error_text . push ( ' ==> Unknown key "' + key + '"' ) ;
183
+ if ( ! Object . prototype . hasOwnProperty . call ( results , key ) ) {
184
+ error_text . push ( " ==> Unknown key \"" + key + "\"" ) ;
185
185
break ;
186
186
}
187
187
const entry = expected [ key ] ;
188
188
189
- if ( exact_check == true && entry . length !== results [ key ] . length ) {
189
+ if ( exact_check && entry . length !== results [ key ] . length ) {
190
190
error_text . push ( queryName + "==> Expected exactly " + entry . length +
191
191
" results but found " + results [ key ] . length + " in '" + key + "'" ) ;
192
192
}
@@ -268,7 +268,7 @@ function runCheck(loadedFile, key, callback) {
268
268
function runChecks ( testFile , doSearch , parseQuery ) {
269
269
let checkExpected = false ;
270
270
let checkParsed = false ;
271
- let testFileContent = readFile ( testFile ) + ' exports.QUERY = QUERY;' ;
271
+ let testFileContent = readFile ( testFile ) + " exports.QUERY = QUERY;" ;
272
272
273
273
if ( testFileContent . indexOf ( "FILTER_CRATE" ) !== - 1 ) {
274
274
testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;" ;
@@ -277,11 +277,11 @@ function runChecks(testFile, doSearch, parseQuery) {
277
277
}
278
278
279
279
if ( testFileContent . indexOf ( "\nconst EXPECTED" ) !== - 1 ) {
280
- testFileContent += ' exports.EXPECTED = EXPECTED;' ;
280
+ testFileContent += " exports.EXPECTED = EXPECTED;" ;
281
281
checkExpected = true ;
282
282
}
283
283
if ( testFileContent . indexOf ( "\nconst PARSED" ) !== - 1 ) {
284
- testFileContent += ' exports.PARSED = PARSED;' ;
284
+ testFileContent += " exports.PARSED = PARSED;" ;
285
285
checkParsed = true ;
286
286
}
287
287
if ( ! checkParsed && ! checkExpected ) {
@@ -325,7 +325,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
325
325
const searchWords = searchModule . initSearch ( searchIndex . searchIndex ) ;
326
326
327
327
return {
328
- doSearch : function ( queryStr , filterCrate , currentCrate ) {
328
+ doSearch : function ( queryStr , filterCrate , currentCrate ) {
329
329
return searchModule . execQuery ( searchModule . parseQuery ( queryStr ) , searchWords ,
330
330
filterCrate , currentCrate ) ;
331
331
} ,
@@ -361,22 +361,24 @@ function parseOptions(args) {
361
361
} ;
362
362
363
363
for ( let i = 0 ; i < args . length ; ++ i ) {
364
- if ( correspondences . hasOwnProperty ( args [ i ] ) ) {
364
+ const arg = args [ i ] ;
365
+ if ( Object . prototype . hasOwnProperty . call ( correspondences , arg ) ) {
365
366
i += 1 ;
366
367
if ( i >= args . length ) {
367
- console . log ( "Missing argument after `" + args [ i - 1 ] + "` option." ) ;
368
+ console . log ( "Missing argument after `" + arg + "` option." ) ;
368
369
return null ;
369
370
}
370
- if ( args [ i - 1 ] !== "--test-file" ) {
371
- opts [ correspondences [ args [ i - 1 ] ] ] = args [ i ] ;
371
+ const arg_value = args [ i ] ;
372
+ if ( arg !== "--test-file" ) {
373
+ opts [ correspondences [ arg ] ] = arg_value ;
372
374
} else {
373
- opts [ correspondences [ args [ i - 1 ] ] ] . push ( args [ i ] ) ;
375
+ opts [ correspondences [ arg ] ] . push ( arg_value ) ;
374
376
}
375
- } else if ( args [ i ] === "--help" ) {
377
+ } else if ( arg === "--help" ) {
376
378
showHelp ( ) ;
377
379
process . exit ( 0 ) ;
378
380
} else {
379
- console . log ( "Unknown option `" + args [ i ] + "`." ) ;
381
+ console . log ( "Unknown option `" + arg + "`." ) ;
380
382
console . log ( "Use `--help` to see the list of options" ) ;
381
383
return null ;
382
384
}
@@ -405,17 +407,17 @@ function main(argv) {
405
407
) ;
406
408
let errors = 0 ;
407
409
408
- const doSearch = function ( queryStr , filterCrate ) {
410
+ const doSearch = function ( queryStr , filterCrate ) {
409
411
return parseAndSearch . doSearch ( queryStr , filterCrate , opts [ "crate_name" ] ) ;
410
412
} ;
411
413
412
414
if ( opts [ "test_file" ] . length !== 0 ) {
413
- opts [ "test_file" ] . forEach ( function ( file ) {
415
+ opts [ "test_file" ] . forEach ( file => {
414
416
process . stdout . write ( `Testing ${ file } ... ` ) ;
415
417
errors += runChecks ( file , doSearch , parseAndSearch . parseQuery ) ;
416
418
} ) ;
417
419
} else if ( opts [ "test_folder" ] . length !== 0 ) {
418
- fs . readdirSync ( opts [ "test_folder" ] ) . forEach ( function ( file ) {
420
+ fs . readdirSync ( opts [ "test_folder" ] ) . forEach ( file => {
419
421
if ( ! file . endsWith ( ".js" ) ) {
420
422
return ;
421
423
}
0 commit comments