Skip to content

Commit

Permalink
Add ignoreUnits option to number-max-precision rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sendilkumarn committed Oct 4, 2017
1 parent f02a160 commit 520b777
Show file tree
Hide file tree
Showing 5 changed files with 4,942 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Head

- Added: `"ignoreUnits"` option to `no-max-precision` ([#2785](https://github.com/stylelint/stylelint/issues/2785)).
- Added: autofix of syntax errors in standard CSS e.g. unclosed braces and brackets ([#2886](https://github.com/stylelint/stylelint/issues/2886)).
- Added: `length-zero-no-unit` autofix ([#2861](https://github.com/stylelint/stylelint/issues/2861)).
- Added: `selector-max-specificity` support for level 4 evaluation context pseudo-classes ([#2857](https://github.com/stylelint/stylelint/issues/2857)).
Expand Down
43 changes: 43 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,46 @@ a { top: 3.24px; }
```css
@media (min-width: 3.23em) {}
```
## Optional secondary options

### `ignoreUnits: ["string"]`

Ignore number-max-precision rule for the value with the unit.

For example, with `2`.

Given:

```js
["%"]
```

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%;
}
```
47 changes: 47 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,50 @@ testRule(rule, {
}
]
});

testRule(rule, {
ruleName,
config: [0, { ignoreUnits: ["%"] }],

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%; }"
}
],

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
}
]
});
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
Loading

0 comments on commit 520b777

Please sign in to comment.