Skip to content

Commit defcdfc

Browse files
Iiro Jäppinenokonet
authored andcommitted
refactor: generateTasks doesn't calculate gitDir itself
1 parent 8921989 commit defcdfc

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

src/generateTasks.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ const path = require('path')
44
const micromatch = require('micromatch')
55
const pathIsInside = require('path-is-inside')
66
const { getConfig } = require('./getConfig')
7-
const resolveGitDir = require('./resolveGitDir')
87

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

11-
module.exports = async function generateTasks(config, stagedRelFiles) {
10+
module.exports = async function generateTasks(config, gitDir, stagedRelFiles) {
1211
debug('Generating linter tasks')
1312

1413
const normalizedConfig = getConfig(config) // Ensure we have a normalized config
1514
const { linters, globOptions, ignore } = normalizedConfig
1615

17-
const gitDir = await resolveGitDir()
1816
const cwd = process.cwd()
1917
const stagedFiles = stagedRelFiles.map(file => path.resolve(gitDir, file))
2018

src/runAll.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = async function runAll(config) {
3434
const filenames = files.map(file => file.filename)
3535
debug('Loaded list of staged files in git:\n%O', filenames)
3636

37-
const tasks = (await generateTasks(config, filenames)).map(task => ({
37+
const tasks = (await generateTasks(config, gitDir, filenames)).map(task => ({
3838
title: `Running tasks for ${task.pattern}`,
3939
task: async () =>
4040
new Listr(

test/generateTasks.spec.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('generateTasks', () => {
5353
{
5454
'*.js': 'lint'
5555
},
56+
workDir,
5657
['test.js']
5758
)
5859
const commands = result.map(match => match.commands)
@@ -66,6 +67,7 @@ describe('generateTasks', () => {
6667
'*.js': 'lint'
6768
}
6869
},
70+
workDir,
6971
['test.js']
7072
)
7173
const commands = result.map(match => match.commands)
@@ -79,6 +81,7 @@ describe('generateTasks', () => {
7981
'*': 'lint'
8082
}
8183
},
84+
workDir,
8285
files
8386
)
8487
task.fileList.forEach(file => {
@@ -94,6 +97,7 @@ describe('generateTasks', () => {
9497
},
9598
relative: true
9699
},
100+
workDir,
97101
files
98102
)
99103
task.fileList.forEach(file => {
@@ -103,8 +107,7 @@ describe('generateTasks', () => {
103107

104108
it('should not match non-children files', async () => {
105109
const relPath = path.join(process.cwd(), '..')
106-
resolveGitDir.mockResolvedValueOnce(relPath)
107-
const result = await generateTasks({ ...config }, files)
110+
const result = await generateTasks({ ...config }, relPath, files)
108111
const linter = result.find(item => item.pattern === '*.js')
109112
expect(linter).toEqual({
110113
pattern: '*.js',
@@ -114,7 +117,7 @@ describe('generateTasks', () => {
114117
})
115118

116119
it('should return an empty file list for linters with no matches.', async () => {
117-
const result = await generateTasks(config, files)
120+
const result = await generateTasks(config, workDir, files)
118121

119122
result.forEach(task => {
120123
if (task.commands === 'unknown-js') {
@@ -126,7 +129,7 @@ describe('generateTasks', () => {
126129
})
127130

128131
it('should match pattern "*.js"', async () => {
129-
const result = await generateTasks(config, files)
132+
const result = await generateTasks(config, workDir, files)
130133
const linter = result.find(item => item.pattern === '*.js')
131134
expect(linter).toEqual({
132135
pattern: '*.js',
@@ -142,7 +145,11 @@ describe('generateTasks', () => {
142145
})
143146

144147
it('should match pattern "*.js" and return relative path', async () => {
145-
const result = await generateTasks(Object.assign({}, config, { relative: true }), files)
148+
const result = await generateTasks(
149+
Object.assign({}, config, { relative: true }),
150+
workDir,
151+
files
152+
)
146153
const linter = result.find(item => item.pattern === '*.js')
147154
expect(linter).toEqual({
148155
pattern: '*.js',
@@ -158,7 +165,7 @@ describe('generateTasks', () => {
158165
})
159166

160167
it('should match pattern "**/*.js"', async () => {
161-
const result = await generateTasks(config, files)
168+
const result = await generateTasks(config, workDir, files)
162169
const linter = result.find(item => item.pattern === '**/*.js')
163170
expect(linter).toEqual({
164171
pattern: '**/*.js',
@@ -174,7 +181,11 @@ describe('generateTasks', () => {
174181
})
175182

176183
it('should match pattern "**/*.js" with relative path', async () => {
177-
const result = await generateTasks(Object.assign({}, config, { relative: true }), files)
184+
const result = await generateTasks(
185+
Object.assign({}, config, { relative: true }),
186+
workDir,
187+
files
188+
)
178189
const linter = result.find(item => item.pattern === '**/*.js')
179190
expect(linter).toEqual({
180191
pattern: '**/*.js',
@@ -190,7 +201,7 @@ describe('generateTasks', () => {
190201
})
191202

192203
it('should match pattern "deeper/*.js"', async () => {
193-
const result = await generateTasks(config, files)
204+
const result = await generateTasks(config, workDir, files)
194205
const linter = result.find(item => item.pattern === 'deeper/*.js')
195206
expect(linter).toEqual({
196207
pattern: 'deeper/*.js',
@@ -200,7 +211,7 @@ describe('generateTasks', () => {
200211
})
201212

202213
it('should match pattern ".hidden/*.js"', async () => {
203-
const result = await generateTasks(config, files)
214+
const result = await generateTasks(config, workDir, files)
204215
const linter = result.find(item => item.pattern === '.hidden/*.js')
205216
expect(linter).toEqual({
206217
pattern: '.hidden/*.js',
@@ -210,7 +221,7 @@ describe('generateTasks', () => {
210221
})
211222

212223
it('should match pattern "*.{css,js}"', async () => {
213-
const result = await generateTasks(config, files)
224+
const result = await generateTasks(config, workDir, files)
214225
const linter = result.find(item => item.pattern === '*.{css,js}')
215226
expect(linter).toEqual({
216227
pattern: '*.{css,js}',
@@ -241,6 +252,7 @@ describe('generateTasks', () => {
241252
'TeSt.*': 'lint'
242253
}
243254
},
255+
workDir,
244256
files
245257
)
246258
const linter = result.find(item => item.pattern === 'TeSt.*')
@@ -261,6 +273,7 @@ describe('generateTasks', () => {
261273
ignore: ['**/ignore/**', '**/ignore.*'],
262274
linters: { [pattern]: commands }
263275
},
276+
workDir,
264277
['ignore/me.js', 'ignore.me.js', 'cool/js.js']
265278
)
266279
expect(result[0]).toEqual({
@@ -279,6 +292,7 @@ describe('generateTasks', () => {
279292
'../outside/*.js': 'my-cmd'
280293
}
281294
},
295+
workDir,
282296
['root.js', 'prj/test.js', 'outside/test.js', 'outside/test2.js']
283297
)
284298

0 commit comments

Comments
 (0)