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 ignoreProperties: [] to number-max-precision #5421

Merged
merged 5 commits into from Aug 10, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

eljusto marked this conversation as resolved.
Show resolved Hide resolved
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