Skip to content

Commit

Permalink
feat: Add relative option to allow passing relative paths to linters (
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjjj authored and okonet committed Nov 21, 2018
1 parent 5e607ef commit fcb774b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -148,6 +148,7 @@ Notice that the linting commands now are nested into the `linters` object. The f
* `ignore` - `['**/docs/**/*.js']` - array of glob patterns to entirely ignore from any task.
* `linters``Object` — keys (`String`) are glob patterns, values (`Array<String> | String`) are commands to execute.
* `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))
* `relative``false` — if `true` it will give the relative path from your `package.json` directory to your linter arguments.

## Filtering files

Expand Down Expand Up @@ -287,6 +288,17 @@ The following is equivalent:
}
```

### Use ng lint with angular cli >= 7.0.0

```json
{
"linters": {
"*.ts": "ng lint myProjectName --files"
},
"relative": true
}
```

### Stylelint for CSS with defaults and for SCSS with SCSS syntax

```json
Expand Down
9 changes: 7 additions & 2 deletions src/generateTasks.js
Expand Up @@ -33,9 +33,14 @@ module.exports = function generateTasks(config, stagedRelFiles) {
.map(file => path.relative(cwd, file)),
patterns,
globOptions
)
).map(file => {
// if you set relative option, then the file path will be relative to your package.json
if (config.relative) {
return path.normalize(file)
}
// Return absolute path after the filter is run
.map(file => path.resolve(cwd, file))
return path.resolve(cwd, file)
})

const task = { pattern, commands, fileList }
debug('Generated task: \n%O', task)
Expand Down
3 changes: 2 additions & 1 deletion src/getConfig.js
Expand Up @@ -28,7 +28,8 @@ const defaultConfig = {
linters: {},
ignore: [],
subTaskConcurrency: 1,
renderer: 'update'
renderer: 'update',
relative: false
}

/**
Expand Down
3 changes: 3 additions & 0 deletions test/__snapshots__/getConfig.spec.js.snap
Expand Up @@ -10,6 +10,7 @@ Object {
},
"ignore": Array [],
"linters": Object {},
"relative": false,
"renderer": "update",
"subTaskConcurrency": 1,
}
Expand All @@ -25,6 +26,7 @@ Object {
},
"ignore": Array [],
"linters": Object {},
"relative": false,
"renderer": "update",
"subTaskConcurrency": 1,
}
Expand All @@ -46,6 +48,7 @@ Object {
],
".*rc": "jsonlint",
},
"relative": false,
"renderer": "update",
"subTaskConcurrency": 1,
}
Expand Down
9 changes: 6 additions & 3 deletions test/__snapshots__/index.spec.js.snap
Expand Up @@ -15,7 +15,8 @@ LOG {
},
ignore: [],
subTaskConcurrency: 1,
renderer: 'verbose'
renderer: 'verbose',
relative: false
}"
`;

Expand All @@ -34,7 +35,8 @@ LOG {
},
ignore: [],
subTaskConcurrency: 1,
renderer: 'verbose'
renderer: 'verbose',
relative: false
}"
`;

Expand All @@ -55,7 +57,8 @@ LOG {
},
ignore: [],
subTaskConcurrency: 1,
renderer: 'verbose'
renderer: 'verbose',
relative: false
}"
`;

Expand Down
47 changes: 47 additions & 0 deletions test/generateTasks.spec.js
Expand Up @@ -86,6 +86,21 @@ describe('generateTasks', () => {
})
})

it('should return relative paths', () => {
const [task] = generateTasks(
{
linters: {
'*': 'lint'
},
relative: true
},
files
)
task.fileList.forEach(file => {
expect(path.isAbsolute(file)).toBe(false)
})
})

it('should not match non-children files', () => {
const relPath = path.join(process.cwd(), '..')
resolveGitDir.mockReturnValueOnce(relPath)
Expand Down Expand Up @@ -126,6 +141,22 @@ describe('generateTasks', () => {
})
})

it('should match pattern "*.js" and return relative path', () => {
const result = generateTasks(Object.assign({}, config, { relative: true }), files)
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
commands: 'root-js',
fileList: [
`test.js`,
`deeper/test.js`,
`deeper/test2.js`,
`even/deeper/test.js`,
`.hidden/test.js`
].map(path.normalize)
})
})

it('should match pattern "**/*.js"', () => {
const result = generateTasks(config, files)
const linter = result.find(item => item.pattern === '**/*.js')
Expand All @@ -142,6 +173,22 @@ describe('generateTasks', () => {
})
})

it('should match pattern "**/*.js" with relative path', () => {
const result = generateTasks(Object.assign({}, config, { relative: true }), files)
const linter = result.find(item => item.pattern === '**/*.js')
expect(linter).toEqual({
pattern: '**/*.js',
commands: 'any-js',
fileList: [
`test.js`,
`deeper/test.js`,
`deeper/test2.js`,
`even/deeper/test.js`,
`.hidden/test.js`
].map(path.normalize)
})
})

it('should match pattern "deeper/*.js"', () => {
const result = generateTasks(config, files)
const linter = result.find(item => item.pattern === 'deeper/*.js')
Expand Down
3 changes: 2 additions & 1 deletion test/getConfig.spec.js
Expand Up @@ -141,7 +141,8 @@ describe('getConfig', () => {
},
ignore: ['docs/**/*.js'],
subTaskConcurrency: 10,
renderer: 'custom'
renderer: 'custom',
relative: true
}
expect(getConfig(cloneDeep(src))).toEqual(src)
})
Expand Down

0 comments on commit fcb774b

Please sign in to comment.