Passed
Push — master ( 238446...a1cd93 )
by Dmytro
26:52 queued 13s
created

tests/init.js   A

Complexity

Total Complexity 6
Complexity/F 1.2

Size

Lines of Code 37
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 21
mnd 1
bc 1
fnc 5
dl 0
loc 37
bpm 0.2
cpm 1.2
noi 0
c 0
b 0
f 0
rs 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
A init.js ➔ preventParentScopeModules 0 12 3
A init.js ➔ clearRequireCache 0 5 2
A init.js ➔ isPathInside 0 10 1
1
/* eslint-disable import/no-commonjs */
2
const { Module } = require('module');
3
const path = require('path');
4
5
function clearRequireCache() {
6
    for (const key of Object.keys(require.cache)) {
7
        delete require.cache[key];
8
    }
9
}
10
11
function isPathInside(childPath, parentPath) {
12
    const relation = path.relative(parentPath, childPath);
13
14
    return Boolean(
15
        relation &&
16
		relation !== '..' &&
17
		!relation.startsWith(`..${path.sep}`) &&
18
		relation !== path.resolve(childPath)
19
    );
20
}
21
22
const ROOT_FOLDER = process.cwd();
23
24
function preventParentScopeModules() {
25
    const nodeModulePaths = Module._nodeModulePaths;
26
27
    Module._nodeModulePaths = function (from) {
28
        const originalPath = nodeModulePaths.call(this, from);
29
30
31
        return originalPath.filter(function (p) {
32
            return isPathInside(p, ROOT_FOLDER);
33
        });
34
    };
35
}
36
37
clearRequireCache();
38
preventParentScopeModules();
39