Issues (117)

lib/patternlibrary/load-data.js (4 issues)

1 1
var glob     = require('glob');
2 1
var fs       = require('fs');
3 1
var path     = require('path');
4 1
var yaml     = require('js-yaml');
5
6
/**
7
 * Looks for files with .js, .json, or .yml extensions within the given directory, and adds them as Handlebars variables matching the name of the file.
8
 * @param {string} dir - Folder to check for data files.
9
 */
10 1
module.exports = function(dir) {
11 20
	var dataFiles = loadFiles(dir, '**/*.{js,json,yml}');
12
	
13 20
	for (var i in dataFiles) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
14 5
	    var file = fs.readFileSync(dataFiles[i]);
0 ignored issues
show
The variable file seems to be never used. Consider removing it.
Loading history...
15 5
	    var ext = path.extname(dataFiles[i]);
16 5
	    var name = path.basename(dataFiles[i], ext);
17
	    var data;
18
	
19 5
	    if (ext === '.json' || ext === '.js') {
20 4
	        delete require.cache[require.resolve(dataFiles[i])];
21 4
	        data = require(dataFiles[i])
22
	    }
23 2
	    else if (ext === '.yml') {
24 1
	        data = yaml.safeLoad(fs.readFileSync(dataFiles[i]));
25
	    }
26
	
27 5
	    this.data[name] = data;
0 ignored issues
show
The variable data seems to not be initialized for all possible execution paths.
Loading history...
28
	}
29
}
30
31
/**
32
 * Load a set of files
33
 * @param  {string|array} dir
34
 * @param  {string}       pattern
35
 * @return {array}
36
 */
37
function loadFiles (dir, pattern) {
38 20
    var files = [];
39
40 20
    dir = !Array.isArray(dir) ? [dir] : dir;
41
42 20
    for (var i in dir) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
43 21
        files = files.concat(glob.sync(path.join(process.cwd(), dir[i], pattern)));
44
    }
45
46 20
    return files;
47
}
48