@@ -8,6 +8,10 @@ var isCore = require('is-core-module');
88
99var realpathFS = process . platform !== 'win32' && fs . realpath && typeof fs . realpath . native === 'function' ? fs . realpath . native : fs . realpath ;
1010
11+ var relativePathRegex = / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ / \\ ] ) / ;
12+ var windowsDriveRegex = / ^ \w : [ / \\ ] * $ / ;
13+ var nodeModulesRegex = / [ / \\ ] n o d e _ m o d u l e s [ / \\ ] * $ / ;
14+
1115var homedir = getHomedir ( ) ;
1216var defaultPaths = function ( ) {
1317 return [
@@ -138,10 +142,10 @@ module.exports = function resolve(x, options, callback) {
138142
139143 var res ;
140144 function validBasedir ( basedir ) {
141- if ( ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ / \\ ] ) / ) . test ( x ) ) {
145+ if ( relativePathRegex . test ( x ) ) {
142146 res = path . resolve ( basedir , x ) ;
143147 if ( x === '.' || x === '..' || x . slice ( - 1 ) === '/' ) res += '/' ;
144- if ( ( / \/ $ / ) . test ( x ) && res === basedir ) {
148+ if ( x . slice ( - 1 ) === '/' && res === basedir ) {
145149 loadAsDirectory ( res , opts . package , onfile ) ;
146150 } else loadAsFile ( res , opts . package , onfile ) ;
147151 } else if ( includeCoreModules && isCore ( x ) ) {
@@ -229,10 +233,10 @@ module.exports = function resolve(x, options, callback) {
229233
230234 function loadpkg ( dir , cb ) {
231235 if ( dir === '' || dir === '/' ) return cb ( null ) ;
232- if ( process . platform === 'win32' && ( / ^ \w : [ / \\ ] * $ / ) . test ( dir ) ) {
236+ if ( process . platform === 'win32' && windowsDriveRegex . test ( dir ) ) {
233237 return cb ( null ) ;
234238 }
235- if ( ( / [ / \\ ] n o d e _ m o d u l e s [ / \\ ] * $ / ) . test ( dir ) ) return cb ( null ) ;
239+ if ( nodeModulesRegex . test ( dir ) ) return cb ( null ) ;
236240
237241 maybeRealpath ( realpath , dir , opts , function ( unwrapErr , pkgdir ) {
238242 if ( unwrapErr ) return loadpkg ( path . dirname ( dir ) , cb ) ;
0 commit comments