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 ignoreUnits option to number-max-precision rule #2941

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lib/rules/number-max-precision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,57 @@ a { top: 3.24px; }
```css
@media (min-width: 3.23em) {}
```

## Optional secondary options

### `ignoreUnits: ["/regex/", "string"]`

Ignore the precision of numbers for values with the specified units.

For example, with `2`.

Given:

```js
["/^my-/", "%"]
```

The following patterns are considered violations:

```css
a { top: 3.245px; }
```

```css
a { top: 3.245634px; }
```

```css
@media (min-width: 3.234em) {}
```

The following patterns are *not* considered violations:

```css
a { top: 3.245%; }
```

```css
@media (min-width: 3.23em) {}
```

```css
a {
width: 10.5432%;
}
```

```css
a { top: 3.245my-unit; }
```

```css
a {
width: 10.989my-other-unit;
}
```
56 changes: 56 additions & 0 deletions lib/rules/number-max-precision/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,59 @@ testRule(rule, {
}
]
});

testRule(rule, {
ruleName,
config: [0, { ignoreUnits: ["%", "/^my-/"] }],

accept: [
{
code: "a { top: 3%; }"
},
{
code: "a { top: 3.1%; }"
},
{
code: "a { top: 3.123456%; }"
},
{
code: "a { top: 3px; }"
},
{
code: "a { color: #ff00; }"
},
{
code: "a { padding: 6.123% 3.1234%; }"
},
{
code: "a { top: 3.245my-unit; }"
}
],

reject: [
{
code: "a { top: 3.1px; }",
message: messages.expected(3.1, 0),
line: 1,
column: 10
},
{
code: "a { top: 3.123em; }",
message: messages.expected(3.123, 0),
line: 1,
column: 10
},
{
code: "a { padding: 6.123px 3.1234px; }",
message: messages.expected(6.123, 0),
line: 1,
column: 14
},
{
code: "a { top: 6.123other-unit; }",
message: messages.expected(6.123, 0),
line: 1,
column: 10
}
]
});
29 changes: 24 additions & 5 deletions lib/rules/number-max-precision/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const atRuleParamIndex = require("../../utils/atRuleParamIndex");
const declarationValueIndex = require("../../utils/declarationValueIndex");
const getUnitFromValueNode = require("../../utils/getUnitFromValueNode");
const optionsMatches = require("../../utils/optionsMatches");
const report = require("../../utils/report");
const ruleMessages = require("../../utils/ruleMessages");
const validateOptions = require("../../utils/validateOptions");
Expand All @@ -16,12 +18,23 @@ const messages = ruleMessages(ruleName, {
`Expected "${number}" to be "${number.toFixed(precision)}"`
});

const rule = function(precision) {
const rule = function(precision, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: precision,
possible: [_.isNumber]
});
const validOptions = validateOptions(
result,
ruleName,
{
actual: precision,
possible: [_.isNumber]
},
{
optional: true,
actual: options,
possible: {
ignoreUnits: [_.isString]
}
}
);
if (!validOptions) {
return;
}
Expand All @@ -43,6 +56,12 @@ const rule = function(precision) {
}

valueParser(value).walk(valueNode => {
const unit = getUnitFromValueNode(valueNode);

if (optionsMatches(options, "ignoreUnits", unit)) {
return;
}

// Ignore `url` function
if (
valueNode.type === "function" &&
Expand Down