Is there a way to run lint-staged only on new files (the ones that were untracked by git before)
Asked Answered
J

3

2

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?

Jermayne answered 28/2, 2019 at 9:0 Comment(0)
G
2

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

Garnes answered 10/5, 2019 at 15:1 Comment(0)
T
1

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

Tableland answered 16/8, 2022 at 17:57 Comment(0)
S
0

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.

Schiffman answered 28/2, 2019 at 9:17 Comment(1)
I'd prefer to not change my current project structure, but thanks anywayJermayne

© 2022 - 2024 — McMap. All rights reserved.