@@ -11,48 +11,56 @@ module.exports = function noReachingInside(context) {
11
11
const allowRegexps = ( options . allow || [ ] ) . map ( p => minimatch . makeRe ( p ) )
12
12
13
13
// test if reaching into this directory is allowed by the
14
- // config, path.sep is automatically added so that globs like
14
+ // config, '/' is automatically added so that globs like
15
15
// "lodash/**" will match both "lodash" (which requires the trailing /) and "lodash/get"
16
16
function reachingAllowed ( someDir ) {
17
- return ! ! find ( allowRegexps , re => re . test ( someDir ) || re . test ( someDir + path . sep ) )
17
+ return ! ! find ( allowRegexps , re => re . test ( someDir ) || re . test ( someDir + '/' ) )
18
18
}
19
19
20
20
function isRelativeStep ( step ) {
21
21
return step === '' || step === '.' || step === '..'
22
22
}
23
23
24
+ function normalizeSep ( somePath ) {
25
+ return somePath . split ( '\\' ) . join ( '/' )
26
+ }
27
+
24
28
function report ( reachedTo , node ) {
25
29
context . report ( {
26
30
node,
27
- message : `Reaching into "${ reachedTo } " is not allowed.` ,
31
+ message : `Reaching into "${ normalizeSep ( reachedTo ) } " is not allowed.` ,
28
32
} )
29
33
}
30
34
31
35
function findNotAllowedReach ( importPath , startingBase , join , ignoreStep ) {
32
- const steps = importPath . split ( '/' ) . filter ( Boolean )
36
+ const steps = normalizeSep ( importPath ) . split ( '/' ) . filter ( Boolean )
37
+
33
38
let parentDir = startingBase
34
39
while ( steps . length ) {
35
40
const step = steps . shift ( )
36
- parentDir = join ( parentDir , step )
37
-
38
- if ( ignoreStep && ignoreStep ( step ) ) continue
41
+ parentDir = normalizeSep ( join ( parentDir , step ) )
39
42
40
- if ( steps . length ) {
41
- if ( ! reachingAllowed ( parentDir ) ) {
42
- return parentDir
43
- }
43
+ if ( ignoreStep && ignoreStep ( step ) ) {
44
+ continue
45
+ }
46
+ if ( steps . length && ! reachingAllowed ( parentDir ) ) {
47
+ return parentDir
44
48
}
45
49
}
46
50
}
47
51
48
52
function checkRelativeImportForReaching ( importPath , node ) {
49
53
const reachedInto = findNotAllowedReach ( importPath , dirname , path . resolve , isRelativeStep )
50
- if ( reachedInto ) report ( path . relative ( dirname , reachedInto ) , node )
54
+ if ( reachedInto ) {
55
+ report ( path . relative ( dirname , reachedInto ) , node )
56
+ }
51
57
}
52
58
53
59
function checkAbsoluteImportForReaching ( importPath , node ) {
54
60
const reachedInto = findNotAllowedReach ( importPath , '' , path . join )
55
- if ( reachedInto ) report ( reachedInto , node )
61
+ if ( reachedInto ) {
62
+ report ( reachedInto , node )
63
+ }
56
64
}
57
65
58
66
function checkImportForReaching ( importPath , node ) {
0 commit comments