Skip to content

Commit

Permalink
refactor: remove dependency on path-is-inside
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj authored and okonet committed Jul 1, 2019
1 parent 767edbd commit e829646
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -37,7 +37,6 @@
"listr": "^0.14.3",
"log-symbols": "^3.0.0",
"micromatch": "^4.0.2",
"path-is-inside": "^1.0.2",
"please-upgrade-node": "^3.1.1",
"string-argv": "^0.3.0",
"stringify-object": "^3.3.0"
Expand Down
16 changes: 14 additions & 2 deletions src/generateTasks.js
@@ -1,11 +1,23 @@
'use strict'

const micromatch = require('micromatch')
const pathIsInside = require('path-is-inside')
const path = require('path')

const debug = require('debug')('lint-staged:gen-tasks')

/**
* Test if `child` path is inside `parent` path
* https://stackoverflow.com/a/45242825
*
* @param {String} parent
* @param {String} child
* @returns {Boolean}
*/
const isPathInside = (parent, child) => {
const relative = path.relative(parent, child)
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}

module.exports = async function generateTasks(linters, gitDir, stagedRelFiles) {
debug('Generating linter tasks')

Expand All @@ -20,7 +32,7 @@ module.exports = async function generateTasks(linters, gitDir, stagedRelFiles) {
stagedFiles
// Only worry about children of the CWD unless the pattern explicitly
// specifies that it concerns a parent directory.
.filter(file => isParentDirPattern || pathIsInside(file, cwd))
.filter(file => isParentDirPattern || isPathInside(cwd, file))
// Make the paths relative to CWD for filtering
.map(file => path.relative(cwd, file)),
pattern,
Expand Down

0 comments on commit e829646

Please sign in to comment.