Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Warn once through util.deprecate, refactor
  • Loading branch information
silverwind committed Apr 11, 2015
commit 9a2e88513cedcfefe30f8222a32a40cf3b9986d0
21 changes: 14 additions & 7 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ function tryExtensions(p, exts) {
}


const noopDeprecateRequireDot = util.deprecate(function() {},
`warning: require('.') did resolve outside the package directory. ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the use of a template string seems unnecessary here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, "did resolve" could be just "resolved"

`This functionality will be deprecated soon.`);


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

Expand Down Expand Up @@ -169,9 +174,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 +219,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