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

Ignore x unit in some cases #4427

Merged
merged 3 commits into from Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 37 additions & 0 deletions lib/rules/unit-no-unknown/__tests__/index.js
Expand Up @@ -220,6 +220,22 @@ testRule(rule, {
code: "@import 'foo.css'",
description: 'ignore non-media and non-variable at-rule',
},
{
code: "a { background-image: image-set('img-1x.jpg' 1x, 'img-2x.jpg' 2x, 'img-3x.jpg' 3x) }",
description: 'ignore `x` unit in image-set',
},
{
code: '@media (resolution: 2x) {}',
description: 'ignore `x` unit in @media with `resolution`',
},
{
code: '@media ( resOLution: 2x) {}',
description: 'ignore `x` unit in @media with `resolution`',
},
{
code: 'a { image-resolution: 1x; }',
description: 'ignore `x` unit in image-resolution',
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add test with @media (resolution: 2x) and (min-width: 200x) {}, second should be rejected

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to update logic so that such a case could be handled properly.
Not sure about implementation though.
Any suggestions are welcome.

],

reject: [
Expand Down Expand Up @@ -356,6 +372,27 @@ testRule(rule, {
line: 1,
column: 13,
},
{
code: 'a { width: 400x; }',
message: messages.rejected('x'),
description: '`x` is not allowed for non-resolution props',
line: 1,
column: 12,
},
{
code: '@media (resolution: 2x) and (min-width: 200x) {}',
message: messages.rejected('x'),
description: '`x` rejected with inappropriate property',
line: 1,
column: 41,
},
{
code: '@media ( resolution: /* comment */ 2x ) and (min-width: 200x) {}',
message: messages.rejected('x'),
description: '`x` rejected with inappropriate property',
line: 1,
column: 44,
},
],
});

Expand Down
35 changes: 34 additions & 1 deletion lib/rules/unit-no-unknown/index.js
Expand Up @@ -5,6 +5,7 @@ const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getUnitFromValueNode = require('../../utils/getUnitFromValueNode');
const keywordSets = require('../../reference/keywordSets');
const mediaParser = require('postcss-media-query-parser').default;
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
Expand Down Expand Up @@ -62,10 +63,42 @@ const rule = function(actual, options) {
return;
}

if (keywordSets.units.has(unit.toLowerCase())) {
if (keywordSets.units.has(unit.toLowerCase()) && unit.toLowerCase() !== 'x') {
return;
}

if (unit.toLowerCase() === 'x') {
if (
node.type === 'atrule' &&
node.name === 'media' &&
node.params.toLowerCase().includes('resolution')
) {
let ignoreUnit = false;

mediaParser(node.params).walk((mediaNode, i, mediaNodes) => {
if (
mediaNode.value.toLowerCase().includes('resolution') &&
_.last(mediaNodes).sourceIndex === valueNode.sourceIndex
) {
ignoreUnit = true;

return false;
}
});

if (ignoreUnit) {
return;
}
}

if (
node.type === 'decl' &&
(node.prop.toLowerCase() === 'image-resolution' || /^image-set/i.test(node.value))
) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can parse decl here too, because you can use background: image-set("../../media/examples/star.png" 1x) left 20x / 15% 60% repeat-x

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice case. Updated the logic.

return;
}
}

report({
index: getIndex(node) + valueNode.sourceIndex,
message: messages.rejected(unit),
Expand Down