Skip to content

Commit 82eded4

Browse files
committed
refactor: use commander implies() syntax for flag
1 parent f3378be commit 82eded4

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

.changeset/flat-ducks-double.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

bin/lint-staged.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ cli
6868
new Option(
6969
'--no-stash',
7070
'disable the backup stash, and do not revert in case of errors. Implies "--no-hide-partially-staged".'
71-
).default(false)
71+
)
72+
.default(false)
73+
.implies({ hidePartiallyStaged: false })
7274
)
7375

7476
/**
@@ -79,7 +81,7 @@ cli
7981
cli
8082
.addOption(
8183
new Option('--hide-partially-staged', 'hide unstaged changes from partially staged files')
82-
.default(null)
84+
.default(true)
8385
.hideHelp()
8486
)
8587
.addOption(
@@ -122,8 +124,7 @@ const options = {
122124
relative: !!cliOptions.relative,
123125
shell: cliOptions.shell /* Either a boolean or a string pointing to the shell */,
124126
stash: !!cliOptions.stash, // commander inverts `no-<x>` flags to `!x`
125-
hidePartiallyStaged:
126-
cliOptions.hidePartiallyStaged == null ? !!cliOptions.stash : !!cliOptions.hidePartiallyStaged, // commander inverts `no-<x>` flags to `!x`
127+
hidePartiallyStaged: !!cliOptions.hidePartiallyStaged, // commander inverts `no-<x>` flags to `!x`
127128
verbose: !!cliOptions.verbose,
128129
}
129130

test/e2e/no-stash.test.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,40 @@ describe('lint-staged', () => {
3535
res = await lintStaged('--diff=master...my-branch')
3636
expect(res.stderr).toMatch('Skipping backup because `--diff` was used.')
3737

38-
try {
39-
await lintStaged('--diff=master...my-branch --stash')
40-
} catch (err) {
41-
expect(err.stderr).toMatch('lint-staged failed due to a git error.')
42-
}
38+
await expect(lintStaged('--diff=master...my-branch --stash')).rejects.toThrowError(
39+
expect.objectContaining({
40+
stderr: expect.stringContaining('lint-staged failed due to a git error.'),
41+
})
42+
)
4343

4444
res = await lintStaged('--diff=master...my-branch --no-stash')
4545
expect(res.stderr).toMatch('Skipping backup because `--diff` was used.')
4646
})
4747
)
48+
49+
test(
50+
'--no-stash implies --no-hide-partially-staged',
51+
withGitIntegration(async ({ execGit, readFile, writeFile, cwd }) => {
52+
const lintStaged = getLintStagedExecutor(cwd)
53+
54+
await writeFile('.lintstagedrc.json', JSON.stringify(configFixtures.prettierListDifferent))
55+
56+
// Stage ugly file
57+
await writeFile('test.js', fileFixtures.uglyJS)
58+
await execGit(['add', 'test.js'])
59+
60+
// modify file with unstaged changes
61+
await writeFile('test.js', fileFixtures.uglyJSWithChanges)
62+
63+
// lint-staged fails because file is ugly
64+
await expect(lintStaged('--no-stash')).rejects.toMatchObject({
65+
stderr: expect.stringContaining(
66+
'Skipping hiding unstaged changes from partially staged files because `--no-stash` was used'
67+
),
68+
})
69+
70+
// unstaged changes were not touched
71+
expect(await readFile('test.js')).toEqual(fileFixtures.uglyJSWithChanges)
72+
})
73+
)
4874
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy