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 calc() in length-zero-no-unit #4175

Merged
merged 12 commits into from Aug 11, 2019
2 changes: 1 addition & 1 deletion lib/rules/length-zero-no-unit/README.md
@@ -1,6 +1,6 @@
# length-zero-no-unit

Disallow units for zero lengths.
Disallow units for zero lengths. Ignores `calc` value in favor of `function-calc-no-invalid` rule.
Copy link
Member

Choose a reason for hiding this comment

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

Minor doc nitpick. Let's revert the change to this sentence and add the following below the example:

This rule ignores lengths within `calc` functions in favor of the [`function-calc-no-invalid`](../function-calc-no-invalid/README.md) rule.

This is to be consistent with other rule READMEs e.g.
https://github.com/stylelint/stylelint/tree/master/lib/rules/font-family-name-quotes#font-family-name-quotes

Otherwise, this PR LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated


```css
a { top: 0px; }
Expand Down
20 changes: 12 additions & 8 deletions lib/rules/length-zero-no-unit/__tests__/index.js
Expand Up @@ -12,6 +12,18 @@ testRule(rule, {
code: "a { top: 0; }",
description: "unitless zero"
},
{
description: "ignore calc",
code: "a { padding: calc(0px +\n 0px); }"
},
{
description: "ignore calc. insensitive",
code: "a { padding: cAlc(0px + 0px); }"
},
{
description: "ignore calc. empty calc",
code: "a { padding: calc(); }"
},
Copy link
Member

Choose a reason for hiding this comment

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

Can we add test like padding: calc(1in + 0in * 2)) 0in, should have one error

Copy link
Member Author

Choose a reason for hiding this comment

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

awesome suggestion. I have improve it to padding: calc(1in + 0in * 2)) 0in calc(0px) 0px since it was needed to rework regexp, too

{
code: "a { padding: 0 /* 0px */; }",
description: "united zero in comment"
Expand Down Expand Up @@ -246,14 +258,6 @@ testRule(rule, {
line: 1,
column: 27
},
{
code: "a { padding: calc(1in + 0in * 2)); }",
fixed: "a { padding: calc(1in + 0 * 2)); }",

message: messages.rejected,
line: 1,
column: 26
},
{
code: "@media (min-width: 0px) {}",
fixed: "@media (min-width: 0) {}",
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/length-zero-no-unit/index.js
Expand Up @@ -5,6 +5,7 @@ const beforeBlockString = require("../../utils/beforeBlockString");
const blurComments = require("../../utils/blurComments");
const hasBlock = require("../../utils/hasBlock");
const isCustomProperty = require("../../utils/isCustomProperty");
const isMathFunction = require("../../utils/isMathFunction");
const keywordSets = require("../../reference/keywordSets");
const optionsMatches = require("../../utils/optionsMatches");
const report = require("../../utils/report");
Expand All @@ -28,7 +29,11 @@ const rule = function(actual, secondary, context) {
}

root.walkDecls(decl => {
check(blurComments(decl.toString()), decl);
const stringValue = blurComments(decl.toString());

if (!isMathFunction(stringValue)) {
check(stringValue, decl);
}
});

root.walkAtRules(atRule => {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/isMathFunction.js
@@ -0,0 +1,5 @@
"use strict";

module.exports = function isMathFunction(valueString) {
return /calc\((.|\s)*\)/i.test(valueString);
};