Issues (117)

lib/patternlibrary/load-helpers.js (1 issue)

Severity
1 1
var fs   = require('fs');
2 1
var path = require('path');
3 1
var readdir = require('../util/readdir.js');
4
5
/**
6
 * Looks for files with the .js extension within the given directory, and attempts to add them as Handlebars helpers.
7
 * @param {string} dir - Folder to check for helpers.
8
 */
9
function loadHandlebarsHelpers (dir) {
10 92
    var helpers = readdir(dir, '**/*.js');
11 92
    for (var i in helpers) {
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...
12
	    var helper;
13 1
	    var name = path.basename(helpers[i], '.js');
14
15 1
	    try {
16 2
	        if (this.handlebars.helpers[name]){
17
		        //delete require.cache[require.resolve(path.join(helpers[i]))];
18
		        delete require.cache[path.join(process.cwd(), helpers[i])];
19
		        this.handlebars.unregisterHelper(name);
20
	        }
21
	
22 1
	        helper = require(path.join(process.cwd(), helpers[i]));
23 1
	        this.handlebars.registerHelper(name, helper);
24
	    }
25
	    catch (e) {
26
	        console.warn('Error when loading ' + name + '.js as a Handlebars helper.', e);
27
	    }
28
    }
29
}
30
31
module.exports = loadHandlebarsHelpers;