UNPKG

2.55 kBJavaScriptView Raw
1(function() {
2 var path, resolve;
3
4 path = require('path');
5
6 resolve = require('resolve').sync;
7
8 // moduleName is a NodeJS module, or a path to a module NodeJS can load.
9 module.exports = {
10 require: function(moduleName) {
11 var rulePath;
12 try {
13 // Try to find the project-level rule first.
14 rulePath = resolve(moduleName, {
15 basedir: process.cwd(),
16 extensions: ['.js', '.coffee', '.litcoffee', '.coffee.md']
17 });
18 return require(rulePath);
19 } catch (error) {}
20 try {
21 // Globally installed rule
22 return require(moduleName);
23 } catch (error) {}
24 try {
25 // Maybe the user used a relative path from the command line. This
26 // doesn't make much sense from a config file, but seems natural
27 // with the --rules option.
28
29 // No try around this one, an exception here should abort the rest of
30 // this function.
31 return require(path.resolve(process.cwd(), moduleName));
32 } catch (error) {}
33 // This was already tried once. It will definitely fail, but it will
34 // fail with a more sensible error message than the last require()
35 // above.
36 return require(moduleName);
37 },
38 loadFromConfig: function(coffeelint, config) {
39 var data, results, ruleName;
40 results = [];
41 for (ruleName in config) {
42 data = config[ruleName];
43 if ((data != null ? data.module : void 0) != null) {
44 results.push(this.loadRule(coffeelint, data.module, ruleName));
45 }
46 }
47 return results;
48 },
49 // moduleName is a NodeJS module, or a path to a module NodeJS can load.
50 loadRule: function(coffeelint, moduleName, ruleName = void 0) {
51 var e, i, len, results, rule, ruleModule;
52 try {
53 ruleModule = this.require(moduleName);
54 // Most rules can export as a single constructor function
55 if (typeof ruleModule === 'function') {
56 return coffeelint.registerRule(ruleModule, ruleName);
57 } else {
58 results = [];
59 // Or it can export an array of rules to load.
60 for (i = 0, len = ruleModule.length; i < len; i++) {
61 rule = ruleModule[i];
62 results.push(coffeelint.registerRule(rule));
63 }
64 return results;
65 }
66 } catch (error) {
67 e = error;
68 // coffeelint: disable=no_debugger
69 console.error(`Error loading ${moduleName}`);
70 throw e;
71 }
72 }
73 };
74
75 // coffeelint: enable=no_debugger
76
77}).call(this);