Skip to content

refactor(parseOptions): better handle errors#674

Merged
freitagbr merged 4 commits intomasterfrom
refactor-fix-parse-options
Mar 9, 2017
Merged

refactor(parseOptions): better handle errors#674
freitagbr merged 4 commits intomasterfrom
refactor-fix-parse-options

Conversation

@nfischer
Copy link
Copy Markdown
Member

@nfischer nfischer commented Feb 26, 2017

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 now reject invalid input at the top of the function. These are regular
Errors instead of calls to common.error() because they represent misuse
of an internal function.

Added test cases bring this function to 100% test coverage.

Partial fix for #671

src/common.js Outdated
if (typeof opt === 'string') {
if (opt[0] !== '-') {
return options;
error("Must start the options string with a '-'");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error("Options string must start with a '-'")?

src/common.js Outdated
});

if (!opt) return options; // defaults
var hasErrorOpts = typeof errorOptions === 'object';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be careful here, because typeof null === 'object'. This check exists elsewhere in this function (and in wrap).

@nfischer
Copy link
Copy Markdown
Member Author

nfischer commented Mar 5, 2017

@freitagbr PTAL

src/common.js Outdated
function parseOptions(opt, map, errorOptions) {
if (!map) error('parseOptions() internal error: no map given');
// Validate input
if (typeof opt !== 'string' && !(opt instanceof Object)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.create(null) instanceof Object === false, but Object.create(null) indeed creates an object. This is the correct way to check that something is an object:

typeof a === 'object' && a !== null

Maybe make a function for this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/common.js Outdated
}
});
} else if (typeof opt === 'object') {
} else { // opt instanceof Option
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opt is object?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@nfischer
Copy link
Copy Markdown
Member Author

nfischer commented Mar 5, 2017

PTAL

@codecov-io
Copy link
Copy Markdown

codecov-io commented Mar 5, 2017

Codecov Report

Merging #674 into master will increase coverage by 0.33%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #674      +/-   ##
==========================================
+ Coverage   94.33%   94.67%   +0.33%     
==========================================
  Files          33       33              
  Lines        1183     1183              
==========================================
+ Hits         1116     1120       +4     
+ Misses         67       63       -4
Impacted Files Coverage Δ
src/common.js 98.9% <100%> (+2.19%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b5a28d...649611a. Read the comment docs.

@freitagbr
Copy link
Copy Markdown
Contributor

LGTM, just need to resolve the merge conflicts.

nfischer added 4 commits March 7, 2017 23:00
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
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.
The `isObject()` helper is used throughout common.js to reliably test if
variables are JavaScript objects.
@nfischer nfischer force-pushed the refactor-fix-parse-options branch from 7607a94 to 649611a Compare March 8, 2017 07:01
@nfischer
Copy link
Copy Markdown
Member Author

nfischer commented Mar 8, 2017

Should be resolved now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants