Skip to content

Commit c86fe51

Browse files
dayninBridgeAR
authored andcommitted
module: replace magic numbers by constants
- add new constants - replace "magic" numbers in "module" by constants PR-URL: #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>
1 parent 2960096 commit c86fe51

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

lib/internal/constants.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
module.exports = {
44
// Alphabet chars.
5-
CHAR_UPPERCASE_A: 65, /*A*/
6-
CHAR_LOWERCASE_A: 97, /*a*/
7-
CHAR_UPPERCASE_Z: 90, /*Z*/
8-
CHAR_LOWERCASE_Z: 122, /*z*/
5+
CHAR_UPPERCASE_A: 65, /* A */
6+
CHAR_LOWERCASE_A: 97, /* a */
7+
CHAR_UPPERCASE_Z: 90, /* Z */
8+
CHAR_LOWERCASE_Z: 122, /* z */
99

1010
// Non-alphabetic chars.
11-
CHAR_DOT: 46, /*.*/
12-
CHAR_FORWARD_SLASH: 47, /*/*/
13-
CHAR_BACKWARD_SLASH: 92, /*\*/
14-
CHAR_COLON: 58, /*:*/
15-
CHAR_QUESTION_MARK: 63, /*?*/
11+
CHAR_DOT: 46, /* . */
12+
CHAR_FORWARD_SLASH: 47, /* / */
13+
CHAR_BACKWARD_SLASH: 92, /* \ */
14+
CHAR_COLON: 58, /* : */
15+
CHAR_QUESTION_MARK: 63, /* ? */
16+
CHAR_UNDERSCORE: 95, /* _ */
17+
18+
// Digits
19+
CHAR_0: 48, /* 0 */
20+
CHAR_9: 57, /* 9 */
1621
};

lib/module.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ module.exports = Module;
4747
const internalESModule = require('internal/process/modules');
4848
const ModuleJob = require('internal/loader/ModuleJob');
4949
const createDynamicModule = require('internal/loader/CreateDynamicModule');
50+
const {
51+
CHAR_UPPERCASE_A,
52+
CHAR_LOWERCASE_A,
53+
CHAR_UPPERCASE_Z,
54+
CHAR_LOWERCASE_Z,
55+
CHAR_FORWARD_SLASH,
56+
CHAR_BACKWARD_SLASH,
57+
CHAR_COLON,
58+
CHAR_DOT,
59+
CHAR_UNDERSCORE,
60+
CHAR_0,
61+
CHAR_9,
62+
} = require('internal/constants');
5063

5164
function stat(filename) {
5265
filename = path.toNamespacedPath(filename);
@@ -201,7 +214,7 @@ Module._findPath = function(request, paths, isMain) {
201214

202215
var exts;
203216
var trailingSlash = request.length > 0 &&
204-
request.charCodeAt(request.length - 1) === 47/*/*/;
217+
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
205218

206219
// For each path
207220
for (var i = 0; i < paths.length; i++) {
@@ -276,8 +289,8 @@ if (process.platform === 'win32') {
276289

277290
// return root node_modules when path is 'D:\\'.
278291
// path.resolve will make sure from.length >=3 in Windows.
279-
if (from.charCodeAt(from.length - 1) === 92/*\*/ &&
280-
from.charCodeAt(from.length - 2) === 58/*:*/)
292+
if (from.charCodeAt(from.length - 1) === CHAR_BACKWARD_SLASH &&
293+
from.charCodeAt(from.length - 2) === CHAR_COLON)
281294
return [from + 'node_modules'];
282295

283296
const paths = [];
@@ -290,7 +303,9 @@ if (process.platform === 'win32') {
290303
// Use colon as an extra condition since we can get node_modules
291304
// path for drive root like 'C:\node_modules' and don't need to
292305
// parse drive name.
293-
if (code === 92/*\*/ || code === 47/*/*/ || code === 58/*:*/) {
306+
if (code === CHAR_BACKWARD_SLASH ||
307+
code === CHAR_FORWARD_SLASH ||
308+
code === CHAR_COLON) {
294309
if (p !== nmLen)
295310
paths.push(from.slice(0, last) + '\\node_modules');
296311
last = i;
@@ -324,7 +339,7 @@ if (process.platform === 'win32') {
324339
var last = from.length;
325340
for (var i = from.length - 1; i >= 0; --i) {
326341
const code = from.charCodeAt(i);
327-
if (code === 47/*/*/) {
342+
if (code === CHAR_FORWARD_SLASH) {
328343
if (p !== nmLen)
329344
paths.push(from.slice(0, last) + '/node_modules');
330345
last = i;
@@ -357,9 +372,9 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
357372

358373
// Check for relative path
359374
if (request.length < 2 ||
360-
request.charCodeAt(0) !== 46/*.*/ ||
361-
(request.charCodeAt(1) !== 46/*.*/ &&
362-
request.charCodeAt(1) !== 47/*/*/)) {
375+
request.charCodeAt(0) !== CHAR_DOT ||
376+
(request.charCodeAt(1) !== CHAR_DOT &&
377+
request.charCodeAt(1) !== CHAR_FORWARD_SLASH)) {
363378
var paths = modulePaths;
364379
if (parent) {
365380
if (!parent.paths)
@@ -407,10 +422,10 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
407422
// We matched 'index.', let's validate the rest
408423
for (; i < base.length; ++i) {
409424
const code = base.charCodeAt(i);
410-
if (code !== 95/*_*/ &&
411-
(code < 48/*0*/ || code > 57/*9*/) &&
412-
(code < 65/*A*/ || code > 90/*Z*/) &&
413-
(code < 97/*a*/ || code > 122/*z*/))
425+
if (code !== CHAR_UNDERSCORE &&
426+
(code < CHAR_0 || code > CHAR_9) &&
427+
(code < CHAR_UPPERCASE_A || code > CHAR_UPPERCASE_Z) &&
428+
(code < CHAR_LOWERCASE_A || code > CHAR_LOWERCASE_Z))
414429
break;
415430
}
416431
if (i === base.length) {

0 commit comments

Comments
 (0)