On my project written in Typescript I want to start enforcing type annotations for returned types and parameter types of all functions. For that I created a separate tslint config that extends my main config and adds this rule on top of that. I'm running tslint via husky and lint-staged with this extended config to check all staged files before committing changes. The problem is: if I do a change in one of the old files (some of them are pretty big) lint staged will check this file too and won't let me to commit unless I type-annotate all the functions in all files. But it's a lot of work and I want to enforce this only on new files. Is there a way to filter staged files to only ones that are added in this commit?
lint-staged makes use of another library named staged-git-files(sgf) for retrieving the list of files staged before a git commit.
Here are some status codes which sgf uses.
- Added (A)
- Copied (C)
- Deleted (D)
- Modified (M)
- Renamed (R)
lint-staged passed the status code ACM to sgf package.
sgf.cwd = gitDir
*return pify(sgf)('ACM').then(files => {*
/* files is an Object{ filename: String, status: String } */
const filenames = files.map(file => file.filename)
If you want lint-staged
to retrieve added files only, then you can fork the repo and pass AC instead to sgf. It would be ideal if we have a way of configuring this. I raised an issue in the repo. I hope they address it
Starting with version 12.5.0 of lint-staged, you can use --diff-filter
flag with params like @Deadfish wrote before.
From lint-staged
docs:
--diff-filter: By default only files that are added, copied, modified, or renamed are included. Use this flag to override the default ACMR value with something else: added (A), copied (C), deleted (D), modified (M), renamed (R), type changed (T), unmerged (U), unknown (X), or pairing broken (B).
It would be something like this in your package.json file:
"scripts": {
"lint:staged": "lint-staged --diff-filter=A"
}
Release notes here: v0.12.5
You could do this if you'd separate your "new" code from "old" using directory structure. After that's it's just 2 different glob patterns in your lint-staged config.
That's the simplest way of doing so that I can think of and this works great with other code quality tools like ESLint since you can put .eslintrc
into sub-directories.
© 2022 - 2024 — McMap. All rights reserved.