refactor: extract `matchFiles` and add unit tests · lint-staged/lint-staged@b392a9f

11

import path from 'node:path'

223-

import picomatch from 'picomatch'

4-53

import { createDebug } from './debug.js'

4+

import { matchFiles } from './matchFiles.js'

65

import { normalizePath } from './normalizePath.js'

7687

const debugLog = createDebug('lint-staged:generateTasks')

@@ -30,28 +29,15 @@ export const generateTasks = ({ config, cwd = process.cwd(), files, relative = f

30293130

// Only worry about children of the CWD unless the pattern explicitly

3231

// specifies that it concerns a parent directory.

33-

const filteredFiles = relativeFiles.filter((file) => {

32+

const includedFiles = relativeFiles.filter((file) => {

3433

if (isParentDirPattern) return true

3534

return !file.filepath.startsWith('..') && !path.isAbsolute(file.filepath)

3635

})

373638-

const isMatch = picomatch(pattern, {

39-

cwd,

40-

dot: true,

41-

// If the pattern doesn't look like a path, enable `matchBase` to

42-

// match against filenames in every directory. This makes `*.js`

43-

// match both `test.js` and `subdirectory/test.js`.

44-

matchBase: !pattern.includes('/'),

45-

posixSlashes: true,

46-

strictBrackets: true,

47-

})

48-49-

const fileList = filteredFiles

50-

.filter((file) => isMatch(file.filepath))

51-

.map((file) => ({

52-

filepath: normalizePath(relative ? file.filepath : path.resolve(cwd, file.filepath)),

53-

status: file.status,

54-

}))

37+

const fileList = matchFiles(includedFiles, pattern, cwd).map((file) => ({

38+

filepath: normalizePath(relative ? file.filepath : path.resolve(cwd, file.filepath)),

39+

status: file.status,

40+

}))

55415642

const task = { pattern, commands, fileList }

5743

debugLog('Generated task: \n%O', task)