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 ignore: ["inside-function"] to declaration-property-unit-allowed-list #5194

Merged
merged 7 commits into from Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion lib/rules/color-named/index.js
Expand Up @@ -24,7 +24,7 @@ const messages = ruleMessages(ruleName, {
rejected: (named) => `Unexpected named color "${named}"`,
});

// Todo tested on case insensivity
// Todo tested on case insensitivity
const NODE_TYPES = new Set(['word', 'function']);

function rule(expectation, options) {
Expand Down
31 changes: 31 additions & 0 deletions lib/rules/declaration-property-unit-allowed-list/README.md
Expand Up @@ -83,3 +83,34 @@ a { animation-duration: 5s; }
```css
a { line-height: 1; }
```

## Optional secondary options

### `ignore: ["inside-function"]`

Ignore units that are inside a function.

For example, given:

```
{
'/^border/': ['px'],
'/^background/': ['%'],
}
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
```

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
button {
button { border: 1px solid hsla(162deg, 51%, 35%, 0.8); }
}
```

<!-- prettier-ignore -->
```css
button {
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
background-image: linear-gradient(hsla(162deg, 51%, 35%, 0.8), hsla(62deg, 51%, 35%, 0.8));
}
```
Expand Up @@ -154,6 +154,10 @@ testRule({
{
code: 'a { -webkit-animation-duration: 300ms; }',
},
{
code:
'a { animation: 3ms cubic-bezier(.17,.67,.83,.67) 1ms infinite alternate none running slidein; }',
},
],

reject: [
Expand All @@ -175,3 +179,58 @@ testRule({
},
],
});

testRule({
ruleName,

config: [
{
'/^border/': ['px'],
'/^background/': ['%'],
},
{
ignore: ['inside-function'],
},
],

skipBasicChecks: true,

accept: [
{
code: 'button { border-color: hsla(162deg, 51%, 35%, 0.8); }',
},
{
code: 'button { border: 1px solid hsla(162deg, 51%, 35%, 0.8); }',
},
{
code:
'button { background-image: linear-gradient(hsla(162deg, 51%, 35%, 0.8), hsla(62deg, 51%, 35%, 0.8)); }',
},
{
code: 'button { background-image: url("https://example.com/img.png"); }',
},
{
code:
'button { background: center center / 50% 50% linear-gradient(hsla(162deg, 51%, 35%, 0.8), hsla(62deg, 51%, 35%, 0.8)); }',
},
{
code: 'button { background-color: var(--bg-color); }',
},
],

reject: [
{
code: 'button { border: 1rem solid hsla(162deg, 51%, 35%, 0.8); }',
message: messages.rejected('border', 'rem'),
},
{
code: 'button { background-position: 5rem 45%; }',
message: messages.rejected('background-position', 'rem'),
},
{
code:
'button { background: center center / 50px 10% linear-gradient(hsla(162deg, 51%, 35%, 0.8), hsla(62deg, 51%, 35%, 0.8)); }',
abea marked this conversation as resolved.
Show resolved Hide resolved
message: messages.rejected('background', 'px'),
},
],
});
29 changes: 24 additions & 5 deletions lib/rules/declaration-property-unit-allowed-list/index.js
Expand Up @@ -6,6 +6,7 @@ const _ = require('lodash');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getUnitFromValueNode = require('../../utils/getUnitFromValueNode');
const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
Expand All @@ -18,12 +19,24 @@ const messages = ruleMessages(ruleName, {
rejected: (property, unit) => `Unexpected unit "${unit}" for property "${property}"`,
});

function rule(list) {
function rule(list, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: list,
possible: [_.isObject],
});
const validOptions = validateOptions(
result,
ruleName,
{
actual: list,
possible: [_.isObject],
},
{
actual: options,
possible: {
ignoreProperties: [_.isString, _.isRegExp],
abea marked this conversation as resolved.
Show resolved Hide resolved
ignore: ['inside-function'],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand All @@ -49,6 +62,12 @@ function rule(list) {
return false;
}

const type = node.type;

if (optionsMatches(options, 'ignore', 'inside-function') && type === 'function') {
return false;
}

ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
if (node.type === 'string') {
return;
}
Expand Down