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 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
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
34 changes: 34 additions & 0 deletions lib/rules/declaration-property-unit-allowed-list/README.md
Expand Up @@ -83,3 +83,37 @@ 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/": ["%"],
},
{
"ignore": ["inside-function"],
},
```

The following patterns are _not_ considered violations:

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

<!-- prettier-ignore -->
```css
a {
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: 'a { border-color: hsla(162deg, 51%, 35%, 0.8); }',
},
{
code: 'a { border: 1px solid hsla(162deg, 51%, 35%, 0.8); }',
},
{
code:
'a { background-image: linear-gradient(hsla(162deg, 51%, 35%, 0.8), hsla(62deg, 51%, 35%, 0.8)); }',
},
{
code: 'a { background-image: url("https://example.com/img.png"); }',
},
{
code:
'a { background: center center / 50% 50% linear-gradient(hsla(162deg, 51%, 35%, 0.8), hsla(62deg, 51%, 35%, 0.8)); }',
},
{
code: 'a { background-color: var(--bg-color); }',
},
],

reject: [
{
code: 'a { border: 1rem solid hsla(162deg, 51%, 35%, 0.8); }',
message: messages.rejected('border', 'rem'),
},
{
code: 'a { background-position: 5rem 45%; }',
message: messages.rejected('background-position', 'rem'),
},
{
code:
'a { background: center center / 50px 10% linear-gradient(hsla(162deg, 51%, 35%, 0.8), hsla(62deg, 51%, 35%, 0.8)); }',
message: messages.rejected('background', 'px'),
},
],
});
32 changes: 25 additions & 7 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,23 @@ 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: {
ignore: ['inside-function'],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand All @@ -45,8 +57,14 @@ function rule(list) {

valueParser(value).walk((node) => {
// Ignore wrong units within `url` function
if (node.type === 'function' && node.value.toLowerCase() === 'url') {
return false;
if (node.type === 'function') {
if (node.value.toLowerCase() === 'url') {
return false;
}

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

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