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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message percentage/number precision for alpha-value-notation #4802

Merged
merged 2 commits into from Jun 2, 2020
Merged

Fix error message percentage/number precision for alpha-value-notation #4802

merged 2 commits into from Jun 2, 2020

Conversation

mattxwang
Copy link
Member

@mattxwang mattxwang commented May 23, 2020

Hi there,

This is a PR that aims to tackle a problem addressed in #4792, namely that with floating-point conversions, the error messages (and "fixed" outputs) have numbers that are unpleasant to look at/unreasonable.

My preliminary shot at this (which might need some feedback) is to use Javascript's in-built .toPrecision() function. I used this instead of .toFixed() as the latter rounds down very small numbers to 0 (e.g. 0.003, which I included as a test case), which would create an incorrect error message. I added these precision-specifies to asNumber() and asPercentage().

I am curious if this is the correct approach to solve this problem:

  • I (somewhat arbitrarily) chose a precision of 3 digits - is this an alright magic number? should this be user-configurable?
  • Is using .toPrecision() the right way to go around this? I wasn't able to personally find any problems, but I imagine it might create some problems with too many trailing zeroes, or possibly truncating important data
  • Should I be covering more edge cases (e.g. very large or very very small numbers), or is that out of scope of stylelint? Should I add special checks in the functions?

Would love to get @jeddy3's input since they implemented the rule, or @m-allanson who originally responded to the issue!

Which issue, if any, is this issue related to?

Closes #4792.

Is there anything in the PR that needs further explanation?

Nothing else not in this giant wall of text of a PR 馃槃

possible issues with very small numbers & specifying precision
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

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

@malsf21 Thanks for this. Looking good. @m-allanson and I looked through it together.

I (somewhat arbitrarily) chose a precision of 3 digits - is this an alright magic number? should this be user-configurable?

Let's go with 3 for now, and only add an option if it's requested

Is using .toPrecision() the right way to go around this?

Yes, I believe so.

Should I be covering more edge cases (e.g. very large or very very small numbers)

I think we're good.


I've requested a simple change to the tests. Other than that, LGTM.

config: ['percentage'],
fix: true,

reject: [
Copy link
Member

Choose a reason for hiding this comment

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

Let's move these tests into the testRules above and remove the comment in favour of adding a description: .. to the test case.

We only tend to create a new testRule when the config changes or the syntax property is used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, sounds good! I updated the PR, moving the test cases and adding a description key.

@mattxwang mattxwang requested a review from jeddy3 May 27, 2020 01:06
Copy link
Member

@m-allanson m-allanson left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thank you @malsf21!

Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

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

@malsf21 Thanks for making the changes, LGTM.

@m-allanson m-allanson merged commit 925395d into stylelint:master Jun 2, 2020
@m-allanson
Copy link
Member

Updated changelog:

  • Fixed: alpha-value-notation number precision errors (#4802)

Thanks again @malsf21 馃憤

m-allanson added a commit that referenced this pull request Jun 3, 2020
# By Mike Allanson (6) and others
# Via GitHub
* master:
  Bump @types/lodash from 4.14.152 to 4.14.154 (#4817)
  Update CHANGELOG.md
  Fix with workaround a TypeError thrown for "html" (#4797)
  Update CHANGELOG.md
  Fix false positives for variables in font-family-no-missing-generic-family-keyword (#4806)
  Update CHANGELOG.md
  Add ignoreSelectors option to block-opening-brace-space-before (#4640)
  Update CHANGELOG.md
  Fix error message percentage/number precision for alpha-value-notation (#4802)
  Create new 'createPartialStylelintResult' module (#4815)
  Move function normalizeAllRuleSettings() out to a separate module (#4810)
@mattxwang mattxwang deleted the fix/error-message-percentage-alpha-value-notation branch June 3, 2020 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Fix error message for alpha-value-notation
3 participants