forked from linkedin/eyeglass
/
ModuleFunctions.js
73 lines (62 loc) · 2.06 KB
/
ModuleFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
var syncFn = require("../util/sync");
var debug = require("../util/debug");
var merge = require("lodash.merge");
var ARGUMENTS_REGEX = /\s*\(.*\)$/;
var DELIM = "\n\t\u2022 ";
function getFunctionName(fnSignature) {
return fnSignature.replace(ARGUMENTS_REGEX, "");
}
function checkConflicts(obj1, obj2) {
// return early if either collection is empty
if (!obj1 || !obj2) {
return;
}
var functions = {};
// collect all the function names and signatures from the first collection
Object.keys(obj1).forEach(function(fn) {
var fnName = getFunctionName(fn);
functions[fnName] = fn;
});
// check all the function names and signatures from the second collection
Object.keys(obj2).forEach(function(fn) {
var fnName = getFunctionName(fn);
var currentFunction = functions[fnName];
// if the current signature does not match the new signature...
if (currentFunction && currentFunction !== fn) {
// throw a warning
console.warn("WARNING: Function " + fnName +
" was redeclared with conflicting function signatures: " +
currentFunction + " vs. " + fn);
}
});
}
function ModuleFunctions(eyeglass, sass, options, existingFunctions) {
var functions = eyeglass.modules.list.reduce(function(fns, mod) {
if (!mod.functions) {
return fns;
}
// log any functions found in this module
/* istanbul ignore next - don't test debug */
debug.functions && debug.functions(
"functions discovered in module %s:%s%s",
mod.name,
DELIM,
Object.keys(mod.functions).join(DELIM)
);
checkConflicts(fns, mod.functions);
return merge(fns, mod.functions);
}, {});
checkConflicts(functions, existingFunctions);
functions = merge(functions, existingFunctions);
functions = syncFn.all(functions);
// log all the functions we discovered
/* istanbul ignore next - don't test debug */
debug.functions && debug.functions(
"all discovered functions:%s%s",
DELIM,
Object.keys(functions).join(DELIM)
);
return functions;
}
module.exports = ModuleFunctions;