-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: no-depends-on-boolean-flag
rule
#447
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
afde4d1
feat: no-depends-on-boolean-flag rule
cristiand391 033815f
test: fix UTs
cristiand391 5c3f55e
test: skip ts-dep-check
cristiand391 3823959
test: fix workflow ref
cristiand391 c0866fd
chore: retry
cristiand391 c92bf43
test: back to main
cristiand391 59dda9a
test: tmp fail
cristiand391 aeebd0b
Update no-depends-on-boolean-flag.md
cristiand391 496d99a
chore: back to warn
cristiand391 b71159d
chore: add link to doc
cristiand391 56b1bfd
test: skipTsCheck bool=true
cristiand391 a917de4
test: skipTsCheck bool=false
cristiand391 b1a47a3
test: skipTsCheck bool=true
cristiand391 528dd81
chore: back to main
cristiand391 890b822
pr review feedback (#448)
mshanemc bbaff14
chore: update msg
cristiand391 502e025
chore: wrong doc
cristiand391 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Do not allow flags to depend on boolean flags (`sf-plugin/no-depends-on-boolean-flag`) | ||
|
||
⚠️ This rule _warns_ in the following configs: ✈️ `migration`, ✅ `recommended`. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Flags depending on other boolean flags via `dependsOn` can cause unexpected behaviors: | ||
|
||
```ts | ||
// src/commands/data/query.ts | ||
|
||
export class DataSoqlQueryCommand extends SfCommand<unknown> { | ||
public static readonly flags = { | ||
query: Flags.string({ | ||
char: 'q', | ||
summary: messages.getMessage('flags.query.summary'), | ||
}), | ||
bulk: Flags.boolean({ | ||
char: 'b', | ||
default: false, | ||
summary: messages.getMessage('flags.bulk.summary'), | ||
}), | ||
wait: Flags.duration({ | ||
unit: 'minutes', | ||
char: 'w', | ||
summary: messages.getMessage('flags.wait.summary'), | ||
dependsOn: ['bulk'], | ||
}) | ||
} | ||
} | ||
``` | ||
|
||
This code is supposed to only allow `--wait` to be used when `--bulk` was provided. | ||
|
||
However, because `--bulk` has a default value of `false`, oclif's flag parser will allow `--wait` even without passing in `--bulk` because the `wait.dependsOn` check only ensures that `bulk` has a value, so the following execution would be allowed: | ||
|
||
``` | ||
sf data query -q 'select name,id from account limit 1' --wait 10 | ||
``` | ||
|
||
But even if `--bulk` didn't have a default value, it could still allow a wrong combination if it had `allowNo: true`: | ||
|
||
```ts | ||
bulk: Flags.boolean({ | ||
char: 'b', | ||
default: false, | ||
summary: messages.getMessage('flags.bulk.summary'), | ||
allowNo: true // Support reversible boolean flag with `--no-` prefix (e.g. `--no-bulk`). | ||
}), | ||
``` | ||
|
||
The following example would still run because `--no-bulk` sets `bulk` value to `false`: | ||
|
||
``` | ||
sf data query -q 'select name,id from account limit 1' --wait 10 --no-bulk | ||
``` | ||
|
||
If the desired behavior is to only allow a flag when another boolean flag was provided you should use oclif's relationships feature to verify the boolean flag value is `true`: | ||
|
||
```ts | ||
bulk: Flags.boolean({ | ||
char: 'b', | ||
default: false, | ||
summary: messages.getMessage('flags.bulk.summary'), | ||
allowNo: true // Support reversible boolean flag with `--no-` prefix (e.g. `--no-bulk`). | ||
}), | ||
wait: Flags.duration({ | ||
unit: 'minutes', | ||
char: 'w', | ||
summary: messages.getMessage('flags.wait.summary'), | ||
relationships: [ | ||
{ | ||
type: 'some', | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
flags: [{ name: 'bulk', when: async (flags): Promise<boolean> => Promise.resolve(flags['bulk'] === true) }], | ||
}, | ||
] | ||
}) | ||
``` | ||
|
||
See: https://oclif.io/docs/flags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ import { esmMessageImport } from './rules/esm-message-import'; | |
import { noDefaultDependsOnFlags } from './rules/no-default-depends-on-flags'; | ||
import { onlyExtendSfCommand } from './rules/only-extend-sfCommand'; | ||
import { spreadBaseFlags } from './rules/spread-base-flags'; | ||
import { noDependsOnBooleanFlags } from './rules/no-depends-on-boolean-flags'; | ||
|
||
const library = { | ||
plugins: ['sf-plugin'], | ||
|
@@ -90,6 +91,7 @@ const recommended = { | |
'sf-plugin/no-default-and-depends-on-flags': 'error', | ||
'sf-plugin/only-extend-SfCommand': 'warn', | ||
'sf-plugin/spread-base-flags': 'warn', | ||
'sf-plugin/no-depends-on-boolean-flag': 'warn', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warn instead of error because there are cases where depending on a boolean flag works as expected:
there's no way |
||
}, | ||
}; | ||
|
||
|
@@ -124,6 +126,7 @@ export = { | |
'no-h-short-char': dashH, | ||
'no-default-and-depends-on-flags': noDefaultDependsOnFlags, | ||
'only-extend-SfCommand': onlyExtendSfCommand, | ||
'no-depends-on-boolean-flag': noDependsOnBooleanFlags, | ||
'spread-base-flags': spreadBaseFlags, | ||
'no-duplicate-short-characters': noDuplicateShortCharacters, | ||
'run-matches-class-type': runMatchesClassType, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opt-in way to disable the ts-dep-check, see: salesforcecli/github-workflows#120
this is a devDep so it's ok to ship TS