From fcb774b1ff46bf072e77b757a6b1133bb72b596c Mon Sep 17 00:00:00 2001 From: Coenen Benjamin Date: Wed, 21 Nov 2018 15:15:36 +0100 Subject: [PATCH] feat: Add `relative` option to allow passing relative paths to linters (#534) --- README.md | 12 ++++++ src/generateTasks.js | 9 ++++- src/getConfig.js | 3 +- test/__snapshots__/getConfig.spec.js.snap | 3 ++ test/__snapshots__/index.spec.js.snap | 9 +++-- test/generateTasks.spec.js | 47 +++++++++++++++++++++++ test/getConfig.spec.js | 3 +- 7 files changed, 79 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a74917e50..450c88046 100644 --- a/README.md +++ b/README.md @@ -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`) 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 @@ -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 diff --git a/src/generateTasks.js b/src/generateTasks.js index 14bd319f2..bd120aa98 100644 --- a/src/generateTasks.js +++ b/src/generateTasks.js @@ -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) diff --git a/src/getConfig.js b/src/getConfig.js index e8576a0ad..0c8d05172 100644 --- a/src/getConfig.js +++ b/src/getConfig.js @@ -28,7 +28,8 @@ const defaultConfig = { linters: {}, ignore: [], subTaskConcurrency: 1, - renderer: 'update' + renderer: 'update', + relative: false } /** diff --git a/test/__snapshots__/getConfig.spec.js.snap b/test/__snapshots__/getConfig.spec.js.snap index 9e6d96016..6f3146751 100644 --- a/test/__snapshots__/getConfig.spec.js.snap +++ b/test/__snapshots__/getConfig.spec.js.snap @@ -10,6 +10,7 @@ Object { }, "ignore": Array [], "linters": Object {}, + "relative": false, "renderer": "update", "subTaskConcurrency": 1, } @@ -25,6 +26,7 @@ Object { }, "ignore": Array [], "linters": Object {}, + "relative": false, "renderer": "update", "subTaskConcurrency": 1, } @@ -46,6 +48,7 @@ Object { ], ".*rc": "jsonlint", }, + "relative": false, "renderer": "update", "subTaskConcurrency": 1, } diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index dd168196d..1c8dc3891 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -15,7 +15,8 @@ LOG { }, ignore: [], subTaskConcurrency: 1, - renderer: 'verbose' + renderer: 'verbose', + relative: false }" `; @@ -34,7 +35,8 @@ LOG { }, ignore: [], subTaskConcurrency: 1, - renderer: 'verbose' + renderer: 'verbose', + relative: false }" `; @@ -55,7 +57,8 @@ LOG { }, ignore: [], subTaskConcurrency: 1, - renderer: 'verbose' + renderer: 'verbose', + relative: false }" `; diff --git a/test/generateTasks.spec.js b/test/generateTasks.spec.js index eb426aa0a..2564e4233 100644 --- a/test/generateTasks.spec.js +++ b/test/generateTasks.spec.js @@ -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) @@ -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') @@ -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') diff --git a/test/getConfig.spec.js b/test/getConfig.spec.js index 3ffd0eb18..2bf4d1085 100644 --- a/test/getConfig.spec.js +++ b/test/getConfig.spec.js @@ -141,7 +141,8 @@ describe('getConfig', () => { }, ignore: ['docs/**/*.js'], subTaskConcurrency: 10, - renderer: 'custom' + renderer: 'custom', + relative: true } expect(getConfig(cloneDeep(src))).toEqual(src) })