Issues (117)

lib/patternlibrary/adapter.js (2 issues)

1 1
var path = require('path');
2
3 1
module.exports = function(name, func) {
4
    // Load a built-in module if available
5 741
    if (typeof func === 'undefined') {
6 738
	    try {
7 738
	        func = require(path.join('../adapters/', name));
8
	    }
9
	    catch (e) {
10 1
	        throw new Error('"'+name+'" is not a built-in Patternlibrary adapter. '+e.message);
11
	    }
12
    }
13
14
    // Don't use one of the built-in meta terms
15 740
    var reserved = ['docs', 'fileName', '_adapterData'];
16 740
    for (var i in reserved) {
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...
17 2218
	    if (name === reserved[i]) {
18 1
	        throw new Error('"'+name+'" is a reserved keyword, and can\'t be used as the name of a Patternlibrary adapter.');
19
	    }
20
    }
21
22
    // Make sure the adapter is an object with both of the required methods.
23 739
    if (typeof func !== 'function') {
24 1
        throw new Error('Patternlibrary adapters must be functions.')
25
    }
26
27
    // If no config object exists, add it
28 738
    if (!func.config) func.config = {};
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
30 738
    this.adapters[name] = func;
31
32 738
    return this;
33
}
34