-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(parseOptions): better handle errors (#674)
* refactor(parseOptions): better handle errors For the case where `parseOptions()` is passed a string that does not start with a hyphen, we should reject this string instead of returning the same value. This has no change on any other test cases, and should not affect any commands since we are careful about what input we pass to `parseOptions()` This also adjust how we were handling error cases in the function. We were previously using two different calls to `common.error()`, one for if `errorOptions` is passed, and one without. This hurts readability, because it reads like "if we have errorOptions, then this is an error, and if we don't then it's also an error," instead of the more appropriate, "we reached an error case and should use errorOptions if it's available, otherwise we should signal an error without using it." We do not need to use `errorOptions` for the added call to `error()`, since this is an error which indicates misuse, and should not be recoverable. This adds a test case as well, for a line which was not previously covered in our tests. Partial fix for #671 * Refactor parseOptions() This validates input at the top of `parseOptions()` for type correctness. This changes `typeof x === 'object'` to `x instanceOf Object` to improve robustness. Now we can safely use `errorOptions` knowing that if it has a truthy value, it will be an object, otherwise we should use defaults instead. The new tests ensure that we have 100% unit test coverage for this function. * Use exceptions, not common.error()s, for internal misuse * Add common.isObject() helper function The `isObject()` helper is used throughout common.js to reliably test if variables are JavaScript objects.
- Loading branch information
Showing
2 changed files
with
75 additions
and
24 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