Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create shorter title for function tasks with many staged files #706

Merged
merged 3 commits into from Sep 26, 2019

Conversation

iiroj
Copy link
Member

@iiroj iiroj commented Sep 25, 2019

This PR makes function task titles shorter by omitting the full list of staged filenames, instead passing them a single [file] as the argument. This means the task still shows the expected function, but is shorter; for example eslint --fix [file] && git add [file].

The mechanism of creating the title is a bit complex, since we need to first create as long an array of [file] strings and evaluate the function task against it. This makes sure the output is the same as for the actual file list. After this, from a single command string multiple [file] entries are truncated into one, so for example ng lint --file [file] --file [file] --file [file] turns into ng lint --file [file].

I also refactored away usages of linter to more generic task and command.

Fixes #674

@iiroj iiroj requested a review from okonet September 25, 2019 04:17
@codecov
Copy link

codecov bot commented Sep 25, 2019

Codecov Report

Merging #706 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #706      +/-   ##
==========================================
+ Coverage   99.67%   99.67%   +<.01%     
==========================================
  Files          11       11              
  Lines         304      307       +3     
  Branches       60       63       +3     
==========================================
+ Hits          303      306       +3     
  Misses          1        1
Impacted Files Coverage Δ
src/runAll.js 100% <ø> (ø) ⬆️
src/makeCmdTasks.js 100% <100%> (ø) ⬆️
src/resolveTaskFn.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 88d9d4f...6cb19df. Read the comment docs.

@okonet okonet merged commit 1dcdb89 into master Sep 26, 2019
@okonet okonet deleted the fn-task-title branch September 26, 2019 10:30
@okonet
Copy link
Collaborator

okonet commented Sep 26, 2019

🎉 This PR is included in version 9.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@VitorLuizC
Copy link

Why this call command with mockedFiles? Now my command is running two times, one of them with an array of "[file]" as argument and throws a Cannot read property 'replace' of undefined.

@iiroj
Copy link
Member Author

iiroj commented Oct 4, 2019

@VitorLuizC the first time is to create a short title for the task. It’s not actually run, just the function evaluated to a string / array of strings.

What is the command that’s failing for you? I can see if I’ve let a bug slip by.

@VitorLuizC
Copy link

It seems to run my command.

https://github.com/okonet/lint-staged/blob/6cb19df7c87b663b2f8e3567ca9da4a6dcdfc1b7/src/makeCmdTasks.js#L30-L34

And to prevent erros I have to return received mocked files (that array of mockedFiles).

const runLinters = files => {
  if (files && files[0] === '[file]')
    return files;
  }
  
  // ...
};

module.exports = {
  '{applications,packages}/**/*.ts?(x)': runLinters
};

If I return an empty array or my own array of commands it throws Cannot read property 'replace' of undefined.

@VitorLuizC
Copy link

I'll create an issue and probably another PR.

@VitorLuizC
Copy link

The error thrown can be related to this mockCommands[i], it's length may differs commands length.

https://github.com/okonet/lint-staged/blob/6cb19df7c87b663b2f8e3567ca9da4a6dcdfc1b7/src/makeCmdTasks.js#L36-L42

@iiroj
Copy link
Member Author

iiroj commented Oct 4, 2019

@VitorLuizC if you can create a PR it would be appreciated! It seems I’ve made some assumptions that are not always true. Might be easier to just skip generating names for function tasks, though.

// Create a matching command array with [file] in place of file names
let mockCommands
if (isFn) {
const mockFileList = Array(commands.length).fill('[file]')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think I made an error here as simple as creating the mock file array from the length of the commands array, instead of the staged files array?

To have the mock file array match the actual staged files in length, this should be:

Suggested change
const mockFileList = Array(commands.length).fill('[file]')
const mockFileList = Array(files.length).fill('[file]')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Using functions as linters prints file list to console, static linters do not
3 participants