Skip to content

Commit

Permalink
Add ignoreProperties: [] to number-max-precision (#5421)
Browse files Browse the repository at this point in the history
  • Loading branch information
eljusto committed Aug 10, 2021
1 parent 6fe8a03 commit 421b073
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/rules/number-max-precision/README.md
Expand Up @@ -46,6 +46,32 @@ a { top: 3.24px; }

## Optional secondary options

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

Ignore the precision of numbers for the specified properties.

For example, with `0`.

Given:

```json
["transition"]
```

The following patterns are considered violations:

<!-- prettier-ignore -->
```css
a { top: 10.5px; }
```

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
a { transition: all 4.5s ease; }
```

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

Ignore the precision of numbers for values with the specified units.
Expand Down
100 changes: 100 additions & 0 deletions lib/rules/number-max-precision/__tests__/index.js
Expand Up @@ -231,3 +231,103 @@ testRule({
},
],
});

testRule({
ruleName,
config: [0, { ignoreProperties: ['letter-spacing', '/^my-/'] }],

accept: [
{
code: 'a { letter-spacing: 1.2px; }',
},
{
code: 'a { my-prop: 3.1%; }',
},
],

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 { myprop: 6.123px 3.1234px; }',
warnings: [
{
message: messages.expected(6.123, 0),
line: 1,
column: 13,
},
{
message: messages.expected(3.1234, 0),
line: 1,
column: 21,
},
],
},
],
});

testRule({
ruleName,
config: [
0,
{
ignoreProperties: ['letter-spacing', '/^my-/'],
ignoreUnits: ['%', '/^my-/'],
},
],

accept: [
{
code: 'a { letter-spacing: 1.2px; }',
},
{
code: 'a { my-prop: 3.1%; }',
},
{
code: 'a { top: 1.2%; }',
},
{
code: 'a { top: 1.2my-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 { myprop: 6.123px 3.1234px; }',
warnings: [
{
message: messages.expected(6.123, 0),
line: 1,
column: 13,
},
{
message: messages.expected(3.1234, 0),
line: 1,
column: 21,
},
],
},
],
});
7 changes: 7 additions & 0 deletions lib/rules/number-max-precision/index.js
Expand Up @@ -32,6 +32,7 @@ function rule(precision, options) {
optional: true,
actual: options,
possible: {
ignoreProperties: [isString, isRegExp],
ignoreUnits: [isString, isRegExp],
},
},
Expand All @@ -57,6 +58,12 @@ function rule(precision, options) {
return;
}

const prop = node.prop;

if (optionsMatches(options, 'ignoreProperties', prop)) {
return;
}

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

Expand Down

0 comments on commit 421b073

Please sign in to comment.