Skip to content

Commit

Permalink
Warn once through util.deprecate, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Apr 11, 2015
1 parent 712e75c commit ef2f039
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const assert = require('assert').ok;
const fs = require('fs');
const path = require('path');


// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
// See: https://github.com/joyent/node/issues/1707
Expand Down Expand Up @@ -125,6 +124,11 @@ function tryExtensions(p, exts) {
}


const noopDeprecateRequireDot = util.deprecate(function() {},
`warning: require('.') did resolve outside the package directory. ` +
`This functionality will be deprecated soon.`);


Module._findPath = function(request, paths) {
var exts = Object.keys(Module._extensions);

Expand Down Expand Up @@ -169,9 +173,8 @@ Module._findPath = function(request, paths) {
}

if (filename) {
if (request === '.' && i > 0) {
console.error(`(node) warning: require('.') resolved to ${filename}`);
}
// Warn once if '.' resolved outside the module dir
if (request === '.' && i > 0) noopDeprecateRequireDot();
Module._pathCache[cacheKey] = filename;
return filename;
}
Expand Down Expand Up @@ -215,11 +218,14 @@ Module._resolveLookupPaths = function(request, parent) {
paths = parent.paths.concat(paths);
}

// For '.', put the module's dir at the front of the resolve paths
// TODO(silverwind): Treat '.' exactly the same as './'
// Maintain backwards compat with certain broken uses of require('.')
// by putting the module's directory in front of the lookup paths.
if (request === '.') {
paths.splice(0, 0, parent && parent.filename ?
path.dirname(parent.filename) : path.resolve(request));
if (parent && parent.filename) {
paths.splice(0, 0, path.dirname(parent.filename));
} else {
paths.splice(0, 0, path.resolve(request));
}
}

return [request, paths];
Expand Down

0 comments on commit ef2f039

Please sign in to comment.