Skip to content
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

Add contribution formatter parameter validation #54

Conversation

florblue
Copy link
Contributor

@florblue florblue commented Aug 27, 2018

Description

It PR add an input parameter validation in format option.
The motivation appeared when I noticed that in the case of a invalid formatter input, it not handle any exception.
So, to validate the input, I decided to check it into an array with all the valid options. Also, it handle exception if appears an error when try to look for the required module.

To reproduce the changes:

--format option with a valid parameter (eg. -- format table [file])
--format option with an invalid parameter (eg. -- format tabe [file])

Fixes #(issue)

There are no previous issues related.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • Existing unit tests pass locally with my changes

…posible output format options that the chosen format exists, and if not, it outputs a console error.
@coveralls
Copy link

coveralls commented Aug 27, 2018

Coverage Status

Coverage remained the same at 99.657% when pulling d64a724 on Cambalab:add-contribution-formatter-parameter-validation into 2263ccb on protofire:master.

solhint.js Outdated
'table',
'tap',
'unix',
'visualstudio'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we delegate the formatting to eslint, instead of hardcoding the list of possible formatters I think a better option is to handle this in the catch block when the formatter is required.

The downside of this approach is that the error will be throw after the linter is executed, and that wastes user time unnecessarily. But I think it's OK for now. (A possible improvement to avoid this is to get the formatter before the linter is run.)

solhint.js Outdated
}

function getFormatter(formatter){
const formatterName = formatter || 'stylish';
if (typeof formatterName === 'string' && formatterOptions.includes(formatterName)) {
if (typeof formatterName === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this check necessary? In which situations can formatterName not be a string? And even if it is, I think the try-catch below will handle things correctly anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think so. The try-catch will be enough.

try { // to check if is a valid formatter before execute linter
formatterFn = getFormatter(program.formatter);
} catch (ex) {
console.error(ex.message);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this error message more clear? Maybe prefixing it with There was a problem loading the specified formatter "${program.formatter}": ${ex.message} (or something like that).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, sure, so I will add ${program.formatter} to show the input too.

@fvictorio fvictorio merged commit 9e7f851 into protofire:master Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants