Skip to content

Commit a2dd4fd

Browse files
author
spalger
committed
normalize path segments all path segments to '/'
1 parent f072e8a commit a2dd4fd

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/rules/no-reaching-inside.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,56 @@ module.exports = function noReachingInside(context) {
1111
const allowRegexps = (options.allow || []).map(p => minimatch.makeRe(p))
1212

1313
// 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
1515
// "lodash/**" will match both "lodash" (which requires the trailing /) and "lodash/get"
1616
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 + '/'))
1818
}
1919

2020
function isRelativeStep (step) {
2121
return step === '' || step === '.' || step === '..'
2222
}
2323

24+
function normalizeSep(somePath) {
25+
return somePath.split('\\').join('/')
26+
}
27+
2428
function report(reachedTo, node) {
2529
context.report({
2630
node,
27-
message: `Reaching into "${reachedTo}" is not allowed.`,
31+
message: `Reaching into "${normalizeSep(reachedTo)}" is not allowed.`,
2832
})
2933
}
3034

3135
function findNotAllowedReach(importPath, startingBase, join, ignoreStep) {
32-
const steps = importPath.split('/').filter(Boolean)
36+
const steps = normalizeSep(importPath).split('/').filter(Boolean)
37+
3338
let parentDir = startingBase
3439
while (steps.length) {
3540
const step = steps.shift()
36-
parentDir = join(parentDir, step)
37-
38-
if (ignoreStep && ignoreStep(step)) continue
41+
parentDir = normalizeSep(join(parentDir, step))
3942

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
4448
}
4549
}
4650
}
4751

4852
function checkRelativeImportForReaching(importPath, node) {
4953
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+
}
5157
}
5258

5359
function checkAbsoluteImportForReaching(importPath, node) {
5460
const reachedInto = findNotAllowedReach(importPath, '', path.join)
55-
if (reachedInto) report(reachedInto, node)
61+
if (reachedInto) {
62+
report(reachedInto, node)
63+
}
5664
}
5765

5866
function checkImportForReaching(importPath, node) {

0 commit comments

Comments
 (0)