Skip to content

Commit fcb774b

Browse files
bnjjjokonet
authored andcommitted
feat: Add relative option to allow passing relative paths to linters (#534)
1 parent 5e607ef commit fcb774b

7 files changed

Lines changed: 79 additions & 7 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ Notice that the linting commands now are nested into the `linters` object. The f
148148
* `ignore` - `['**/docs/**/*.js']` - array of glob patterns to entirely ignore from any task.
149149
* `linters``Object` — keys (`String`) are glob patterns, values (`Array<String> | String`) are commands to execute.
150150
* `subTaskConcurrency``1` — Controls concurrency for processing chunks generated for each linter. This option is only applicable on Windows. Execution is **not** concurrent by default(see [#225](https://github.com/okonet/lint-staged/issues/225))
151+
* `relative``false` — if `true` it will give the relative path from your `package.json` directory to your linter arguments.
151152

152153
## Filtering files
153154

@@ -287,6 +288,17 @@ The following is equivalent:
287288
}
288289
```
289290

291+
### Use ng lint with angular cli >= 7.0.0
292+
293+
```json
294+
{
295+
"linters": {
296+
"*.ts": "ng lint myProjectName --files"
297+
},
298+
"relative": true
299+
}
300+
```
301+
290302
### Stylelint for CSS with defaults and for SCSS with SCSS syntax
291303

292304
```json

src/generateTasks.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ module.exports = function generateTasks(config, stagedRelFiles) {
3333
.map(file => path.relative(cwd, file)),
3434
patterns,
3535
globOptions
36-
)
36+
).map(file => {
37+
// if you set relative option, then the file path will be relative to your package.json
38+
if (config.relative) {
39+
return path.normalize(file)
40+
}
3741
// Return absolute path after the filter is run
38-
.map(file => path.resolve(cwd, file))
42+
return path.resolve(cwd, file)
43+
})
3944

4045
const task = { pattern, commands, fileList }
4146
debug('Generated task: \n%O', task)

src/getConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const defaultConfig = {
2828
linters: {},
2929
ignore: [],
3030
subTaskConcurrency: 1,
31-
renderer: 'update'
31+
renderer: 'update',
32+
relative: false
3233
}
3334

3435
/**

test/__snapshots__/getConfig.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Object {
1010
},
1111
"ignore": Array [],
1212
"linters": Object {},
13+
"relative": false,
1314
"renderer": "update",
1415
"subTaskConcurrency": 1,
1516
}
@@ -25,6 +26,7 @@ Object {
2526
},
2627
"ignore": Array [],
2728
"linters": Object {},
29+
"relative": false,
2830
"renderer": "update",
2931
"subTaskConcurrency": 1,
3032
}
@@ -46,6 +48,7 @@ Object {
4648
],
4749
".*rc": "jsonlint",
4850
},
51+
"relative": false,
4952
"renderer": "update",
5053
"subTaskConcurrency": 1,
5154
}

test/__snapshots__/index.spec.js.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ LOG {
1515
},
1616
ignore: [],
1717
subTaskConcurrency: 1,
18-
renderer: 'verbose'
18+
renderer: 'verbose',
19+
relative: false
1920
}"
2021
`;
2122

@@ -34,7 +35,8 @@ LOG {
3435
},
3536
ignore: [],
3637
subTaskConcurrency: 1,
37-
renderer: 'verbose'
38+
renderer: 'verbose',
39+
relative: false
3840
}"
3941
`;
4042

@@ -55,7 +57,8 @@ LOG {
5557
},
5658
ignore: [],
5759
subTaskConcurrency: 1,
58-
renderer: 'verbose'
60+
renderer: 'verbose',
61+
relative: false
5962
}"
6063
`;
6164

test/generateTasks.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ describe('generateTasks', () => {
8686
})
8787
})
8888

89+
it('should return relative paths', () => {
90+
const [task] = generateTasks(
91+
{
92+
linters: {
93+
'*': 'lint'
94+
},
95+
relative: true
96+
},
97+
files
98+
)
99+
task.fileList.forEach(file => {
100+
expect(path.isAbsolute(file)).toBe(false)
101+
})
102+
})
103+
89104
it('should not match non-children files', () => {
90105
const relPath = path.join(process.cwd(), '..')
91106
resolveGitDir.mockReturnValueOnce(relPath)
@@ -126,6 +141,22 @@ describe('generateTasks', () => {
126141
})
127142
})
128143

144+
it('should match pattern "*.js" and return relative path', () => {
145+
const result = generateTasks(Object.assign({}, config, { relative: true }), files)
146+
const linter = result.find(item => item.pattern === '*.js')
147+
expect(linter).toEqual({
148+
pattern: '*.js',
149+
commands: 'root-js',
150+
fileList: [
151+
`test.js`,
152+
`deeper/test.js`,
153+
`deeper/test2.js`,
154+
`even/deeper/test.js`,
155+
`.hidden/test.js`
156+
].map(path.normalize)
157+
})
158+
})
159+
129160
it('should match pattern "**/*.js"', () => {
130161
const result = generateTasks(config, files)
131162
const linter = result.find(item => item.pattern === '**/*.js')
@@ -142,6 +173,22 @@ describe('generateTasks', () => {
142173
})
143174
})
144175

176+
it('should match pattern "**/*.js" with relative path', () => {
177+
const result = generateTasks(Object.assign({}, config, { relative: true }), files)
178+
const linter = result.find(item => item.pattern === '**/*.js')
179+
expect(linter).toEqual({
180+
pattern: '**/*.js',
181+
commands: 'any-js',
182+
fileList: [
183+
`test.js`,
184+
`deeper/test.js`,
185+
`deeper/test2.js`,
186+
`even/deeper/test.js`,
187+
`.hidden/test.js`
188+
].map(path.normalize)
189+
})
190+
})
191+
145192
it('should match pattern "deeper/*.js"', () => {
146193
const result = generateTasks(config, files)
147194
const linter = result.find(item => item.pattern === 'deeper/*.js')

test/getConfig.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ describe('getConfig', () => {
141141
},
142142
ignore: ['docs/**/*.js'],
143143
subTaskConcurrency: 10,
144-
renderer: 'custom'
144+
renderer: 'custom',
145+
relative: true
145146
}
146147
expect(getConfig(cloneDeep(src))).toEqual(src)
147148
})

0 commit comments

Comments
 (0)