Skip to content

Commit

Permalink
Add ignoreUnits option to number-max-precision rule (#2941)
Browse files Browse the repository at this point in the history
  • Loading branch information
sendilkumarn authored and jeddy3 committed Oct 5, 2017
1 parent 4156b8b commit 9f22b43
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
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

0 comments on commit 9f22b43

Please sign in to comment.