Skip to content

Commit f63faaf

Browse files
committed
[Refactor] consistent spacing and quotes; run some basic linting manually.
1 parent 7f0ce87 commit f63faaf

35 files changed

+409
-416
lines changed

Diff for: example/async.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var resolve = require('../');
22
resolve('tap', { basedir: __dirname }, function (err, res) {
3-
if (err) console.error(err)
4-
else console.log(res)
3+
if (err) console.error(err);
4+
else console.log(res);
55
});

Diff for: index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var core = require('./lib/core');
22
exports = module.exports = require('./lib/async');
33
exports.core = core;
4-
exports.isCore = function (x) { return core[x] };
4+
exports.isCore = function isCore(x) { return core[x]; };
55
exports.sync = require('./lib/sync');

Diff for: lib/async.js

+80-83
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,78 @@ var fs = require('fs');
33
var path = require('path');
44
var caller = require('./caller.js');
55
var nodeModulesPaths = require('./node-modules-paths.js');
6-
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
76

8-
module.exports = function resolve (x, opts, cb) {
7+
module.exports = function resolve(x, options, callback) {
8+
var cb = callback;
9+
var opts = options || {};
910
if (typeof opts === 'function') {
1011
cb = opts;
1112
opts = {};
1213
}
13-
if (!opts) opts = {};
1414
if (typeof x !== 'string') {
1515
var err = new TypeError('path must be a string');
1616
return process.nextTick(function () {
1717
cb(err);
1818
});
1919
}
20-
20+
2121
var isFile = opts.isFile || function (file, cb) {
2222
fs.stat(file, function (err, stat) {
23-
if (err && err.code === 'ENOENT') cb(null, false)
24-
else if (err) cb(err)
25-
else cb(null, stat.isFile() || stat.isFIFO())
23+
if (err && err.code === 'ENOENT') cb(null, false);
24+
else if (err) cb(err);
25+
else cb(null, stat.isFile() || stat.isFIFO());
2626
});
2727
};
2828
var readFile = opts.readFile || fs.readFile;
29-
30-
var extensions = opts.extensions || [ '.js' ];
29+
30+
var extensions = opts.extensions || ['.js'];
3131
var y = opts.basedir || path.dirname(caller());
32-
32+
3333
opts.paths = opts.paths || [];
34-
34+
3535
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
3636
var res = path.resolve(y, x);
3737
if (x === '..') res += '/';
3838
if (/\/$/.test(x) && res === y) {
3939
loadAsDirectory(res, opts.package, onfile);
40-
}
41-
else loadAsFile(res, opts.package, onfile);
42-
}
43-
else loadNodeModules(x, y, function (err, n, pkg) {
44-
if (err) cb(err)
45-
else if (n) cb(null, n, pkg)
40+
} else loadAsFile(res, opts.package, onfile);
41+
} else loadNodeModules(x, y, function (err, n, pkg) {
42+
if (err) cb(err);
43+
else if (n) cb(null, n, pkg);
4644
else if (core[x]) return cb(null, x);
47-
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
45+
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"));
4846
});
49-
50-
function onfile (err, m, pkg) {
51-
if (err) cb(err)
52-
else if (m) cb(null, m, pkg)
47+
48+
function onfile(err, m, pkg) {
49+
if (err) cb(err);
50+
else if (m) cb(null, m, pkg);
5351
else loadAsDirectory(res, function (err, d, pkg) {
54-
if (err) cb(err)
55-
else if (d) cb(null, d, pkg)
56-
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
57-
})
52+
if (err) cb(err);
53+
else if (d) cb(null, d, pkg);
54+
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"));
55+
});
5856
}
59-
60-
function loadAsFile (x, pkg, cb) {
57+
58+
function loadAsFile(x, pkg, callback) {
59+
var cb = callback;
6160
if (typeof pkg === 'function') {
6261
cb = pkg;
6362
pkg = undefined;
6463
}
65-
64+
6665
var exts = [''].concat(extensions);
67-
load(exts, x, pkg)
66+
load(exts, x, pkg);
6867

69-
function load (exts, x, pkg) {
68+
function load(exts, x, pkg) {
7069
if (exts.length === 0) return cb(null, undefined, pkg);
7170
var file = x + exts[0];
72-
73-
if (pkg) onpkg(null, pkg)
71+
72+
if (pkg) onpkg(null, pkg);
7473
else loadpkg(path.dirname(file), onpkg);
75-
76-
function onpkg (err, pkg_, dir) {
74+
75+
function onpkg(err, pkg_, dir) {
7776
pkg = pkg_;
78-
if (err) return cb(err)
77+
if (err) return cb(err);
7978
if (dir && pkg && opts.pathFilter) {
8079
var rfile = path.relative(dir, file);
8180
var rel = rfile.slice(0, rfile.length - exts[0].length);
@@ -88,66 +87,63 @@ module.exports = function resolve (x, opts, cb) {
8887
}
8988
isFile(file, onex);
9089
}
91-
function onex (err, ex) {
92-
if (err) cb(err)
93-
else if (!ex) load(exts.slice(1), x, pkg)
94-
else cb(null, file, pkg)
90+
function onex(err, ex) {
91+
if (err) cb(err);
92+
else if (!ex) load(exts.slice(1), x, pkg);
93+
else cb(null, file, pkg);
9594
}
9695
}
9796
}
98-
99-
function loadpkg (dir, cb) {
97+
98+
function loadpkg(dir, cb) {
10099
if (dir === '' || dir === '/') return cb(null);
101-
if (process.platform === 'win32' && /^\w:[\\\/]*$/.test(dir)) {
100+
if (process.platform === 'win32' && (/^\w:[\\\/]*$/).test(dir)) {
102101
return cb(null);
103102
}
104103
if (/[\\\/]node_modules[\\\/]*$/.test(dir)) return cb(null);
105-
104+
106105
var pkgfile = path.join(dir, 'package.json');
107106
isFile(pkgfile, function (err, ex) {
108107
// on err, ex is false
109-
if (!ex) return loadpkg(
110-
path.dirname(dir), cb
111-
);
112-
108+
if (!ex) return loadpkg(path.dirname(dir), cb);
109+
113110
readFile(pkgfile, function (err, body) {
114111
if (err) cb(err);
115-
try { var pkg = JSON.parse(body) }
116-
catch (err) {}
117-
112+
try { var pkg = JSON.parse(body); } catch (jsonErr) {}
113+
118114
if (pkg && opts.packageFilter) {
119115
pkg = opts.packageFilter(pkg, pkgfile);
120116
}
121117
cb(null, pkg, dir);
122118
});
123119
});
124120
}
125-
126-
function loadAsDirectory (x, fpkg, cb) {
121+
122+
function loadAsDirectory(x, fpkg, callback) {
123+
var cb = callback;
127124
if (typeof fpkg === 'function') {
128125
cb = fpkg;
129126
fpkg = opts.package;
130127
}
131-
128+
132129
var pkgfile = path.join(x, '/package.json');
133130
isFile(pkgfile, function (err, ex) {
134131
if (err) return cb(err);
135132
if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb);
136-
133+
137134
readFile(pkgfile, function (err, body) {
138135
if (err) return cb(err);
139136
try {
140137
var pkg = JSON.parse(body);
141-
}
142-
catch (err) {}
143-
138+
} catch (jsonErr) {}
139+
144140
if (opts.packageFilter) {
145141
pkg = opts.packageFilter(pkg, pkgfile);
146142
}
147-
143+
148144
if (pkg.main) {
149-
if (pkg.main === '.' || pkg.main === './'){
150-
pkg.main = 'index'
145+
if (pkg.main === '.' || pkg.main === './') {
146+
pkg.main = 'index';
151147
}
152148
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
153149
if (err) return cb(err);
@@ -163,31 +159,32 @@ module.exports = function resolve (x, opts, cb) {
163159
});
164160
return;
165161
}
166-
162+
167163
loadAsFile(path.join(x, '/index'), pkg, cb);
168164
});
169165
});
170166
}
171-
172-
function loadNodeModules (x, start, cb) {
173-
(function process (dirs) {
174-
if (dirs.length === 0) return cb(null, undefined);
175-
var dir = dirs[0];
176-
177-
var file = path.join(dir, '/', x);
178-
loadAsFile(file, undefined, onfile);
179-
180-
function onfile (err, m, pkg) {
181-
if (err) return cb(err);
182-
if (m) return cb(null, m, pkg);
183-
loadAsDirectory(path.join(dir, '/', x), undefined, ondir);
184-
}
185-
186-
function ondir (err, n, pkg) {
187-
if (err) return cb(err);
188-
if (n) return cb(null, n, pkg);
189-
process(dirs.slice(1));
190-
}
191-
})(nodeModulesPaths(start, opts));
167+
168+
function processDirs(cb, dirs) {
169+
if (dirs.length === 0) return cb(null, undefined);
170+
var dir = dirs[0];
171+
172+
var file = path.join(dir, '/', x);
173+
loadAsFile(file, undefined, onfile);
174+
175+
function onfile(err, m, pkg) {
176+
if (err) return cb(err);
177+
if (m) return cb(null, m, pkg);
178+
loadAsDirectory(path.join(dir, '/', x), undefined, ondir);
179+
}
180+
181+
function ondir(err, n, pkg) {
182+
if (err) return cb(err);
183+
if (n) return cb(null, n, pkg);
184+
processDirs(cb, dirs.slice(1));
185+
}
186+
}
187+
function loadNodeModules(x, start, cb) {
188+
processDirs(cb, nodeModulesPaths(start, opts));
192189
}
193190
};

Diff for: lib/caller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = function () {
22
// see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
33
var origPrepareStackTrace = Error.prepareStackTrace;
4-
Error.prepareStackTrace = function (_, stack) { return stack };
4+
Error.prepareStackTrace = function (_, stack) { return stack; };
55
var stack = (new Error()).stack;
66
Error.prepareStackTrace = origPrepareStackTrace;
77
return stack[2].getFileName();

Diff for: lib/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function versionIncluded(version) {
44
if (version === '*') return true;
55
var versionParts = version.split('.');
66
for (var i = 0; i < 3; ++i) {
7-
if ((current[i] || 0) >= (versionParts[i] || 0)) return true;
7+
if ((current[i] || 0) >= (versionParts[i] || 0)) return true;
88
}
99
return false;
1010
}

Diff for: lib/node-modules-paths.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ module.exports = function (start, opts) {
2424
var dirs = [];
2525
for (var i = parts.length - 1; i >= 0; i--) {
2626
if (modules.indexOf(parts[i]) !== -1) continue;
27-
dirs = dirs.concat(modules.map(function(module_dir) {
28-
return prefix + path.join(
27+
dirs = dirs.concat(modules.map(function (module_dir) {
28+
return prefix + path.join(prefix,
2929
path.join.apply(path, parts.slice(0, i + 1)),
3030
module_dir
3131
);
3232
}));
3333
}
34-
if (process.platform === 'win32'){
35-
dirs[dirs.length-1] = dirs[dirs.length-1].replace(":", ":\\");
34+
if (process.platform === 'win32') {
35+
dirs[dirs.length - 1] = dirs[dirs.length - 1].replace(':', ':\\');
3636
}
3737
return dirs.concat(opts.paths);
38-
}
38+
};

0 commit comments

Comments
 (0)