tests/init.js   A
last analyzed

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
const { Module } = require('module');
2
const path = require('path');
3
4
function clearRequireCache() {
5
    for (const key of Object.keys(require.cache)) {
6
        delete require.cache[key];
7
    }
8
}
9
10
function isPathInside(childPath, parentPath) {
11
    const relation = path.relative(parentPath, childPath);
12
13
    return Boolean(
14
        relation &&
15
		relation !== '..' &&
16
		!relation.startsWith(`..${path.sep}`) &&
17
		relation !== path.resolve(childPath)
18
    );
19
}
20
21
const ROOT_FOLDER = process.cwd();
22
23
function preventParentScopeModules() {
24
    const nodeModulePaths = Module._nodeModulePaths;
25
26
    Module._nodeModulePaths = function (from) {
27
        const originalPath = nodeModulePaths.call(this, from);
28
29
30
        return originalPath.filter(function (p) {
31
            return isPathInside(p, ROOT_FOLDER);
32
        });
33
    };
34
}
35
36
clearRequireCache();
37
preventParentScopeModules();
38