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 some properties with 0 value (#4250) #4394

Merged
merged 5 commits into from Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
71 changes: 71 additions & 0 deletions lib/rules/length-zero-no-unit/__tests__/index.js
Expand Up @@ -183,6 +183,37 @@ testRule(rule, {
code: 'a { margin : 0; }',
description: 'space before and after colon',
},
{
code: 'a { line-height : 0px; }',
description: 'ignore line-height property',
},
{
code: 'a { lIne-hEight: 0px; }',
},
{
code: 'a { grid-auto-columns: 0fr; }',
description: 'ignore some grid properties with `fr`',
},
{
code: 'a { grid-Auto-cOlUmns: 0fr; }',
description: 'ignore some grid properties with `fr`',
},
{
code: 'a { grid-template-columns: repeat(10, 0fr); }',
description: 'ignore some grid properties with `fr`',
},
{
code: 'a { Grid-TEMplate-cOlumns : repeat( 10, 0fr ); }',
description: 'ignore some grid properties with `fr`',
},
{
code: 'a { grid-template-rows: 0fr 1fr; }',
description: 'ignore some grid properties with `fr`',
},
{
code: 'a { grId-tEMplate-Rows:0fr 1fr; }',
description: 'ignore some grid properties with `fr`',
},
],

reject: [
Expand Down Expand Up @@ -400,6 +431,46 @@ testRule(rule, {
line: 1,
column: 11,
},
{
code: 'a { grid-template-columns: 0px 0fr 1fr };',
fixed: 'a { grid-template-columns: 0 0fr 1fr };',

message: messages.rejected,
line: 1,
column: 29,
},
{
code: 'a { grid-template-colUMns: 0px 0fr 1fr };',
fixed: 'a { grid-template-colUMns: 0 0fr 1fr };',

message: messages.rejected,
line: 1,
column: 29,
},
{
code: 'a { grid-template-rows: 40px 4fr 0px; };',
fixed: 'a { grid-template-rows: 40px 4fr 0; };',

message: messages.rejected,
line: 1,
column: 35,
},
{
code: 'a { grid-auto-columns: 0px };',
fixed: 'a { grid-auto-columns: 0 };',

message: messages.rejected,
line: 1,
column: 25,
},
{
code: 'a { grid-template-columns: repeat(2, 50px 0px) 100px; };',
fixed: 'a { grid-template-columns: repeat(2, 50px 0) 100px; };',

message: messages.rejected,
line: 1,
column: 44,
},
],
});

Expand Down
19 changes: 19 additions & 0 deletions lib/rules/length-zero-no-unit/index.js
Expand Up @@ -20,6 +20,13 @@ const messages = ruleMessages(ruleName, {
rejected: 'Unexpected unit',
});

const gridPropertiesToIgnore = [
'grid-template-columns',
'grid-template-rows',
'grid-auto-columns',
'grid-auto-rows',
];
hudochenkov marked this conversation as resolved.
Show resolved Hide resolved

const rule = function(actual, secondary, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
Expand All @@ -29,6 +36,10 @@ const rule = function(actual, secondary, context) {
}

root.walkDecls((decl) => {
if (decl.prop.toLowerCase() === 'line-height') {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

To be honest I completely lost the idea why I'd ignored line-height 😕 .
This is the same:

line-height: 0;
line-height: 0px;
line-height: 0em;

So in case of zero value the dimension unit is useless and the warning should be flagged (or it should be auto-fixed).
The only unit to ignore here is fr since 0fr and 0 mean completely different things.

Copy link
Member

Choose a reason for hiding this comment

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

line-height: 0; !== line-height: 0px;

Сlarifications and examples - cssnano/cssnano#768

Copy link
Member Author

Choose a reason for hiding this comment

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

Make sense.
But what the point to ignore stroke-width for example. It doesn't matter whether it has unit or not. Example: https://jsfiddle.net/vc2otk06/

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yes, my mistake, you are right, don't found other properties with same ability as line-height has, let's add tests for this cases:

a { font: normal normal 400 16px/0px cursive; }
a { font: normal normal 400 16px/0 cursive; }

Copy link
Member Author

@fanich37 fanich37 Nov 8, 2019

Choose a reason for hiding this comment

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

The funny thing is these cases don't break tests in master branch but they have to.
I added some logic to process font declaration and updated test cases.

return;
}

const stringValue = blurComments(decl.toString());
const ignorableIndexes = new Array(stringValue.length).fill(false);
const parsedValue = valueParser(stringValue);
Expand Down Expand Up @@ -111,6 +122,14 @@ const rule = function(actual, secondary, context) {
return;
}

if (
node.type === 'decl' &&
gridPropertiesToIgnore.includes(node.prop.toLowerCase()) &&
parsedValue.unit.toLowerCase() === 'fr'
) {
return;
}

// Add the indexes to ignorableIndexes so the same value will not
// be checked multiple times.
_.range(valueWithZeroStart, valueWithZeroEnd).forEach((i) => (ignorableIndexes[i] = true));
Expand Down