diff --git a/package.json b/package.json index e6af24c02..b534fd083 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/generateTasks.js b/src/generateTasks.js index efa62d902..d74201260 100644 --- a/src/generateTasks.js +++ b/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') @@ -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,