Skip to content

Commit

Permalink
normalize path segments all path segments to '/'
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Aug 12, 2016
1 parent f072e8a commit a2dd4fd
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/rules/no-reaching-inside.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,56 @@ module.exports = function noReachingInside(context) {
const allowRegexps = (options.allow || []).map(p => minimatch.makeRe(p))

// test if reaching into this directory is allowed by the
// config, path.sep is automatically added so that globs like
// config, '/' is automatically added so that globs like
// "lodash/**" will match both "lodash" (which requires the trailing /) and "lodash/get"
function reachingAllowed(someDir) {
return !!find(allowRegexps, re => re.test(someDir) || re.test(someDir + path.sep))
return !!find(allowRegexps, re => re.test(someDir) || re.test(someDir + '/'))
}

function isRelativeStep (step) {
return step === '' || step === '.' || step === '..'
}

function normalizeSep(somePath) {
return somePath.split('\\').join('/')
}

function report(reachedTo, node) {
context.report({
node,
message: `Reaching into "${reachedTo}" is not allowed.`,
message: `Reaching into "${normalizeSep(reachedTo)}" is not allowed.`,
})
}

function findNotAllowedReach(importPath, startingBase, join, ignoreStep) {
const steps = importPath.split('/').filter(Boolean)
const steps = normalizeSep(importPath).split('/').filter(Boolean)

let parentDir = startingBase
while (steps.length) {
const step = steps.shift()
parentDir = join(parentDir, step)

if (ignoreStep && ignoreStep(step)) continue
parentDir = normalizeSep(join(parentDir, step))

if (steps.length) {
if (!reachingAllowed(parentDir)) {
return parentDir
}
if (ignoreStep && ignoreStep(step)) {
continue
}
if (steps.length && !reachingAllowed(parentDir)) {
return parentDir
}
}
}

function checkRelativeImportForReaching(importPath, node) {
const reachedInto = findNotAllowedReach(importPath, dirname, path.resolve, isRelativeStep)
if (reachedInto) report(path.relative(dirname, reachedInto), node)
if (reachedInto) {
report(path.relative(dirname, reachedInto), node)
}
}

function checkAbsoluteImportForReaching(importPath, node) {
const reachedInto = findNotAllowedReach(importPath, '', path.join)
if (reachedInto) report(reachedInto, node)
if (reachedInto) {
report(reachedInto, node)
}
}

function checkImportForReaching(importPath, node) {
Expand Down

0 comments on commit a2dd4fd

Please sign in to comment.