-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
no-depends-on-boolean-flag
rule (#447)
* feat: no-depends-on-boolean-flag rule * test: fix UTs * test: skip ts-dep-check * test: fix workflow ref * chore: retry * test: back to main * test: tmp fail * Update no-depends-on-boolean-flag.md * chore: back to warn * chore: add link to doc * test: skipTsCheck bool=true * test: skipTsCheck bool=false * test: skipTsCheck bool=true * chore: back to main * pr review feedback (#448) * refactor: pr review * refactor: min/max/default * chore: update msg * chore: wrong doc --------- Co-authored-by: Shane McLaughlin <[email protected]>
- Loading branch information
1 parent
e5352fa
commit bb0f816
Showing
16 changed files
with
291 additions
and
80 deletions.
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
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.