Skip to content

Commit f0f247d

Browse files
isaacsry
authored andcommitted
Fix dirname so that dirname('/a/b/') -> '/a', like sh's does.
Before there was this comment: Can't strip trailing slashes since module.js incorrectly thinks dirname('/a/b/') should yield '/a/b' instead of '/a'. But now, such thinking is corrected.
1 parent e0d6f14 commit f0f247d

3 files changed

Lines changed: 5 additions & 6 deletions

File tree

lib/module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function resolveModulePath(request, parent) {
197197
}
198198

199199
var parentIdPath = path.dirname(parent.id +
200-
(path.basename(parent.filename).match(new RegExp('^index\\.(' + exts.join('|') + ')$')) ? "/" : ""));
200+
(path.basename(parent.filename).match(new RegExp('^index\\.(' + exts.join('|') + ')$')) ? "/." : ""));
201201
id = path.join(parentIdPath, request);
202202
// make sure require('./path') and require('path') get distinct ids, even
203203
// when called from the toplevel js file

lib/path.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ exports.normalize = function (path, keepBlanks) {
3838
};
3939

4040
exports.dirname = function (path) {
41-
// Can't strip trailing slashes since module.js incorrectly thinks
42-
// dirname('/a/b/') should yield '/a/b' instead of '/a'.
43-
// if (path.length > 1 && '/' === path[path.length-1]) {
44-
// path = path.replace(/\/+$/, '');
45-
// }
41+
if (path.length > 1 && '/' === path[path.length-1]) {
42+
path = path.replace(/\/+$/, '');
43+
}
4644
var lastSlash = path.lastIndexOf('/');
4745
switch (lastSlash) {
4846
case -1:

test/simple/test-path.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ assert.equal(path.basename(f), "test-path.js");
88
assert.equal(path.basename(f, ".js"), "test-path");
99
assert.equal(path.extname(f), ".js");
1010
assert.equal(path.dirname(f).substr(-11), "test/simple");
11+
assert.equal(path.dirname("/a/b/"), "/a");
1112
assert.equal(path.dirname("/a/b"), "/a");
1213
assert.equal(path.dirname("/a"), "/");
1314
assert.equal(path.dirname("/"), "/");

0 commit comments

Comments
 (0)