Skip to content

Commit 935fb5e

Browse files
authored
Merge pull request #598 from nellh/eslint-for-in-guard
Enable eslint guard-for-in to catch for..in vs for..of errors
2 parents 5bf12b4 + 19673a2 commit 935fb5e

File tree

6 files changed

+109
-81
lines changed

6 files changed

+109
-81
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"rules": {
1919
"prettier/prettier": "error",
2020
"no-console": ["error", { "allow": ["warn"] }],
21-
"linebreak-style": ["error", "unix"]
21+
"linebreak-style": ["error", "unix"],
22+
"guard-for-in": "error"
2223
}
2324
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"scripts": {
1414
"lint": "./node_modules/eslint/bin/eslint.js ./**/*.js",
15-
"test": "./node_modules/.bin/mocha tests/ tests/**/**.spec.js",
15+
"test": "./node_modules/.bin/mocha tests/ tests/**/**.spec.js '**/__tests__/*.js' --exclude 'node_modules/**/*'",
1616
"coverage": "./node_modules/.bin/nyc yarn test",
1717
"codecov": "./node_modules/.bin/codecov",
1818
"npmPublish": "publish"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const assert = require('chai').assert
2+
const { getFolderSubjects } = require('../checkAnyDataPresent.js')
3+
4+
describe('checkAnyDataPresent', () => {
5+
describe('getFolderSubjects()', () => {
6+
it('returns only unique subjects', () => {
7+
// Native FileList but an array simulates it
8+
const fileList = [
9+
{ relativePath: 'sub-01/files' },
10+
{ relativePath: 'sub-01/another' },
11+
{ relativePath: 'sub-02/data' },
12+
]
13+
assert.equal(2, getFolderSubjects(fileList).length)
14+
})
15+
it('filters out emptyroom subject', () => {
16+
const fileList = [
17+
{ relativePath: 'sub-01/files' },
18+
{ relativePath: 'sub-emptyroom/data' },
19+
]
20+
assert.equal(1, getFolderSubjects(fileList).length)
21+
})
22+
})
23+
})

validators/checkAnyDataPresent.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
var utils = require('../utils')
22
var Issue = utils.issues.Issue
33

4-
function addIfNotPresent(folderSubjects, subject) {
5-
if (folderSubjects.indexOf(subject) == -1 && subject !== 'emptyroom') {
6-
folderSubjects.push(subject)
7-
}
8-
}
4+
// Match sub-.../... files, except sub-emptyroom
5+
const matchSubjectPath = file =>
6+
file.relativePath.match(/sub-((?!emptyroom).)*(?=\/)/)
97

10-
function getFolderSubjects(fileList) {
11-
var folderSubjects = []
12-
for (var key in fileList) {
13-
var file = fileList[key]
14-
var match = file.relativePath.match(/sub-(.*?)(?=\/)/)
15-
if (match) {
16-
// console.log('match:', match)
17-
addIfNotPresent(folderSubjects, match[1])
18-
}
19-
}
20-
return folderSubjects
21-
}
8+
// Helper for filtering unique values in an array
9+
const uniqueArray = (value, index, self) => self.indexOf(value) === index
10+
11+
/**
12+
* Find unique subjects from FileList
13+
* @param {FileList} fileList Browser FileList or Node equivalent
14+
*/
15+
const getFolderSubjects = fileList =>
16+
Array.from(fileList)
17+
.filter(matchSubjectPath)
18+
.map(f => matchSubjectPath(f)[1])
19+
.filter(uniqueArray)
2220

2321
/**
2422
* checkAnyDataPresent
2523
*
2624
* Takes a list of files and participants with valid data. Checks if they match.
2725
*/
28-
var checkAnyDataPresent = function checkAnyDataPresent(
29-
fileList,
30-
summarySubjects,
31-
) {
26+
function checkAnyDataPresent(fileList, summarySubjects) {
3227
var issues = []
3328
var folderSubjects = getFolderSubjects(fileList)
3429
var subjectsWithoutAnyValidData = folderSubjects.filter(function(i) {
@@ -55,3 +50,4 @@ var checkAnyDataPresent = function checkAnyDataPresent(
5550
}
5651

5752
module.exports = checkAnyDataPresent
53+
module.exports.getFolderSubjects = getFolderSubjects

validators/headerFields.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ var headerFields = function headerFields(headers) {
3434
}
3535
}
3636

37-
for (file in allIssues39Dict) {
38-
var firstIssue = allIssues39Dict[file][0]
39-
var evidence = ''
40-
for (var issue of allIssues39Dict[file]) {
41-
evidence = evidence + ' ' + allIssues39Dict[file][issue].reason
37+
for (let file in allIssues39Dict) {
38+
if (allIssues39Dict.hasOwnProperty(file)) {
39+
const firstIssue = allIssues39Dict[file][0]
40+
let evidence = ''
41+
for (var issue of allIssues39Dict[file]) {
42+
evidence = evidence + ' ' + allIssues39Dict[file][issue].reason
43+
}
44+
firstIssue.reason = evidence
45+
finalIssues.push(firstIssue)
4246
}
43-
firstIssue.reason = evidence
44-
finalIssues.push(firstIssue)
4547
}
4648

4749
return finalIssues
@@ -190,16 +192,18 @@ var headerField = function headerField(headers, field) {
190192
}
191193
}
192194
}
193-
for (var nifti_key in nifti_types) {
194-
var nifti_type = nifti_types[nifti_key]
195-
var max_field_value = Object.keys(nifti_type)[0]
196-
for (var field_value_key in nifti_type) {
197-
field_value = nifti_type[field_value_key]
198-
if (field_value.count > nifti_type[max_field_value].count) {
199-
max_field_value = field_value_key
195+
for (let nifti_key of Object.keys(nifti_types)) {
196+
const nifti_type = nifti_types[nifti_key]
197+
let max_field_value = Object.keys(nifti_type)[0]
198+
for (let field_value_key in nifti_type) {
199+
if (nifti_type.hasOwnProperty(field_value_key)) {
200+
field_value = nifti_type[field_value_key]
201+
if (field_value.count > nifti_type[max_field_value].count) {
202+
max_field_value = field_value_key
203+
}
200204
}
201205
}
202-
for (field_value_key in nifti_type) {
206+
for (let field_value_key of Object.keys(nifti_type)) {
203207
field_value = nifti_type[field_value_key]
204208
if (
205209
max_field_value !== field_value_key &&

validators/session.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,67 @@ var Issue = utils.issues.Issue
88
* directories. Then generates a warning if a given subject is missing any
99
* files from the set.
1010
*/
11-
var session = function missingSessionFiles(fileList) {
12-
var subjects = {}
13-
var issues = []
14-
for (var key in fileList) {
15-
var file = fileList[key]
16-
var filename
11+
const session = function missingSessionFiles(fileList) {
12+
const subjects = {}
13+
const issues = []
14+
for (let key in fileList) {
15+
if (fileList.hasOwnProperty(key)) {
16+
const file = fileList[key]
17+
let filename
1718

18-
if (!file || (typeof window != 'undefined' && !file.webkitRelativePath)) {
19-
continue
20-
}
19+
if (!file || (typeof window != 'undefined' && !file.webkitRelativePath)) {
20+
continue
21+
}
2122

22-
var path = file.relativePath
23-
if (!utils.type.isBIDS(path) || utils.type.file.isStimuliData(path)) {
24-
continue
25-
}
26-
var subject
27-
//match the subject identifier up to the '/' in the full path to a file.
28-
var match = path.match(/sub-(.*?)(?=\/)/)
29-
if (match === null) {
30-
continue
31-
} else {
32-
subject = match[0]
33-
}
23+
const path = file.relativePath
24+
if (!utils.type.isBIDS(path) || utils.type.file.isStimuliData(path)) {
25+
continue
26+
}
27+
let subject
28+
//match the subject identifier up to the '/' in the full path to a file.
29+
const match = path.match(/sub-(.*?)(?=\/)/)
30+
if (match === null) {
31+
continue
32+
} else {
33+
subject = match[0]
34+
}
3435

35-
// suppress inconsistent subject warnings for sub-emptyroom scans
36-
// in MEG data
37-
if (subject == 'sub-emptyroom') {
38-
continue
39-
}
36+
// suppress inconsistent subject warnings for sub-emptyroom scans
37+
// in MEG data
38+
if (subject == 'sub-emptyroom') {
39+
continue
40+
}
4041

41-
// initialize an empty array if we haven't seen this subject before
42-
if (typeof subjects[subject] === 'undefined') {
43-
subjects[subject] = []
42+
// initialize an empty array if we haven't seen this subject before
43+
if (typeof subjects[subject] === 'undefined') {
44+
subjects[subject] = []
45+
}
46+
// files are prepended with subject name, the following two commands
47+
// remove the subject from the file name to allow filenames to be more
48+
// easily compared
49+
filename = path.substring(path.match(subject).index + subject.length)
50+
filename = filename.replace(subject, '<sub>')
51+
subjects[subject].push(filename)
4452
}
45-
// files are prepended with subject name, the following two commands
46-
// remove the subject from the file name to allow filenames to be more
47-
// easily compared
48-
filename = path.substring(path.match(subject).index + subject.length)
49-
filename = filename.replace(subject, '<sub>')
50-
subjects[subject].push(filename)
5153
}
5254

53-
var subject_files = []
55+
const subject_files = []
5456

55-
for (var subjKey in subjects) {
56-
subject = subjects[subjKey]
57-
for (var i = 0; i < subject.length; i++) {
58-
file = subject[i]
59-
if (subject_files.indexOf(file) < 0) {
60-
subject_files.push(file)
57+
for (let subjKey in subjects) {
58+
if (subjects.hasOwnProperty(subjKey)) {
59+
const subject = subjects[subjKey]
60+
for (var i = 0; i < subject.length; i++) {
61+
const file = subject[i]
62+
if (subject_files.indexOf(file) < 0) {
63+
subject_files.push(file)
64+
}
6165
}
6266
}
6367
}
6468

6569
var subjectKeys = Object.keys(subjects).sort()
6670
for (var j = 0; j < subjectKeys.length; j++) {
67-
subject = subjectKeys[j]
71+
const subject = subjectKeys[j]
6872
for (var set_file = 0; set_file < subject_files.length; set_file++) {
6973
if (subjects[subject].indexOf(subject_files[set_file]) === -1) {
7074
var fileThatsMissing =

0 commit comments

Comments
 (0)