module: replace magic numbers by constants
- add new constants - replace "magic" numbers in "module" by constants PR-URL: https://github.com/nodejs/node/pull/18785 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matheus Marchini <matheus@sthima.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
parent
2960096a91
commit
c86fe511f4
@ -2,15 +2,20 @@
|
||||
|
||||
module.exports = {
|
||||
// Alphabet chars.
|
||||
CHAR_UPPERCASE_A: 65, /*A*/
|
||||
CHAR_LOWERCASE_A: 97, /*a*/
|
||||
CHAR_UPPERCASE_Z: 90, /*Z*/
|
||||
CHAR_LOWERCASE_Z: 122, /*z*/
|
||||
CHAR_UPPERCASE_A: 65, /* A */
|
||||
CHAR_LOWERCASE_A: 97, /* a */
|
||||
CHAR_UPPERCASE_Z: 90, /* Z */
|
||||
CHAR_LOWERCASE_Z: 122, /* z */
|
||||
|
||||
// Non-alphabetic chars.
|
||||
CHAR_DOT: 46, /*.*/
|
||||
CHAR_FORWARD_SLASH: 47, /*/*/
|
||||
CHAR_BACKWARD_SLASH: 92, /*\*/
|
||||
CHAR_COLON: 58, /*:*/
|
||||
CHAR_QUESTION_MARK: 63, /*?*/
|
||||
CHAR_DOT: 46, /* . */
|
||||
CHAR_FORWARD_SLASH: 47, /* / */
|
||||
CHAR_BACKWARD_SLASH: 92, /* \ */
|
||||
CHAR_COLON: 58, /* : */
|
||||
CHAR_QUESTION_MARK: 63, /* ? */
|
||||
CHAR_UNDERSCORE: 95, /* _ */
|
||||
|
||||
// Digits
|
||||
CHAR_0: 48, /* 0 */
|
||||
CHAR_9: 57, /* 9 */
|
||||
};
|
||||
|
@ -47,6 +47,19 @@ module.exports = Module;
|
||||
const internalESModule = require('internal/process/modules');
|
||||
const ModuleJob = require('internal/loader/ModuleJob');
|
||||
const createDynamicModule = require('internal/loader/CreateDynamicModule');
|
||||
const {
|
||||
CHAR_UPPERCASE_A,
|
||||
CHAR_LOWERCASE_A,
|
||||
CHAR_UPPERCASE_Z,
|
||||
CHAR_LOWERCASE_Z,
|
||||
CHAR_FORWARD_SLASH,
|
||||
CHAR_BACKWARD_SLASH,
|
||||
CHAR_COLON,
|
||||
CHAR_DOT,
|
||||
CHAR_UNDERSCORE,
|
||||
CHAR_0,
|
||||
CHAR_9,
|
||||
} = require('internal/constants');
|
||||
|
||||
function stat(filename) {
|
||||
filename = path.toNamespacedPath(filename);
|
||||
@ -201,7 +214,7 @@ Module._findPath = function(request, paths, isMain) {
|
||||
|
||||
var exts;
|
||||
var trailingSlash = request.length > 0 &&
|
||||
request.charCodeAt(request.length - 1) === 47/*/*/;
|
||||
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
|
||||
|
||||
// For each path
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
@ -276,8 +289,8 @@ if (process.platform === 'win32') {
|
||||
|
||||
// return root node_modules when path is 'D:\\'.
|
||||
// path.resolve will make sure from.length >=3 in Windows.
|
||||
if (from.charCodeAt(from.length - 1) === 92/*\*/ &&
|
||||
from.charCodeAt(from.length - 2) === 58/*:*/)
|
||||
if (from.charCodeAt(from.length - 1) === CHAR_BACKWARD_SLASH &&
|
||||
from.charCodeAt(from.length - 2) === CHAR_COLON)
|
||||
return [from + 'node_modules'];
|
||||
|
||||
const paths = [];
|
||||
@ -290,7 +303,9 @@ if (process.platform === 'win32') {
|
||||
// Use colon as an extra condition since we can get node_modules
|
||||
// path for drive root like 'C:\node_modules' and don't need to
|
||||
// parse drive name.
|
||||
if (code === 92/*\*/ || code === 47/*/*/ || code === 58/*:*/) {
|
||||
if (code === CHAR_BACKWARD_SLASH ||
|
||||
code === CHAR_FORWARD_SLASH ||
|
||||
code === CHAR_COLON) {
|
||||
if (p !== nmLen)
|
||||
paths.push(from.slice(0, last) + '\\node_modules');
|
||||
last = i;
|
||||
@ -324,7 +339,7 @@ if (process.platform === 'win32') {
|
||||
var last = from.length;
|
||||
for (var i = from.length - 1; i >= 0; --i) {
|
||||
const code = from.charCodeAt(i);
|
||||
if (code === 47/*/*/) {
|
||||
if (code === CHAR_FORWARD_SLASH) {
|
||||
if (p !== nmLen)
|
||||
paths.push(from.slice(0, last) + '/node_modules');
|
||||
last = i;
|
||||
@ -357,9 +372,9 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
|
||||
|
||||
// Check for relative path
|
||||
if (request.length < 2 ||
|
||||
request.charCodeAt(0) !== 46/*.*/ ||
|
||||
(request.charCodeAt(1) !== 46/*.*/ &&
|
||||
request.charCodeAt(1) !== 47/*/*/)) {
|
||||
request.charCodeAt(0) !== CHAR_DOT ||
|
||||
(request.charCodeAt(1) !== CHAR_DOT &&
|
||||
request.charCodeAt(1) !== CHAR_FORWARD_SLASH)) {
|
||||
var paths = modulePaths;
|
||||
if (parent) {
|
||||
if (!parent.paths)
|
||||
@ -407,10 +422,10 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
|
||||
// We matched 'index.', let's validate the rest
|
||||
for (; i < base.length; ++i) {
|
||||
const code = base.charCodeAt(i);
|
||||
if (code !== 95/*_*/ &&
|
||||
(code < 48/*0*/ || code > 57/*9*/) &&
|
||||
(code < 65/*A*/ || code > 90/*Z*/) &&
|
||||
(code < 97/*a*/ || code > 122/*z*/))
|
||||
if (code !== CHAR_UNDERSCORE &&
|
||||
(code < CHAR_0 || code > CHAR_9) &&
|
||||
(code < CHAR_UPPERCASE_A || code > CHAR_UPPERCASE_Z) &&
|
||||
(code < CHAR_LOWERCASE_A || code > CHAR_LOWERCASE_Z))
|
||||
break;
|
||||
}
|
||||
if (i === base.length) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user