mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
* 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.