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
Changes from all commits
297d8db
5dd12b0
76501f2
8ed5186
d37e933
fedba5a
f294149
dbcefbf
0e69ed6
37e4cab
338bb1f
eb32f00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
module.exports = ["calc"]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,26 @@ 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(); }" | ||
}, | ||
{ | ||
description: "ignore calc. several `calc`s", | ||
code: "a { padding: calc(1in + 0in * 2)) 0 calc(0px) 0 }" | ||
}, | ||
{ | ||
description: "ignore calc, but not inner functions", | ||
code: "padding: calc(var(--foo, 0) + 10px) 0" | ||
}, | ||
{ | ||
code: "a { padding: 0 /* 0px */; }", | ||
description: "united zero in comment" | ||
|
@@ -247,12 +267,22 @@ testRule(rule, { | |
column: 27 | ||
}, | ||
{ | ||
code: "a { padding: calc(1in + 0in * 2)); }", | ||
fixed: "a { padding: calc(1in + 0 * 2)); }", | ||
description: "ignore calc, but not inner functions", | ||
code: "padding: calc(var(--foo, 0in) + 10px) 0px", | ||
fixed: "padding: calc(var(--foo, 0) + 10px) 0", | ||
|
||
message: messages.rejected, | ||
line: 1, | ||
column: 27 | ||
}, | ||
{ | ||
description: "ignore calc. has another zero units", | ||
code: "a { padding: calc(1in + 0in * 2)) 0in calc(0px) 0px }", | ||
fixed: "a { padding: calc(1in + 0in * 2)) 0 calc(0px) 0 }", | ||
|
||
message: messages.rejected, | ||
line: 1, | ||
column: 26 | ||
column: 36 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And other hardcore example: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think yes, we catch a bug |
||
}, | ||
{ | ||
code: "@media (min-width: 0px) {}", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"use strict"; | ||
|
||
const isMathFunction = require("../isMathFunction"); | ||
const valueParser = require("postcss-value-parser"); | ||
|
||
describe("isMathFunction", () => { | ||
it("matches calc", () => { | ||
expect(isMathFunction(valueParser("calc(10% + 10px)").nodes[0])).toBe(true); | ||
}); | ||
|
||
it("matches calc. insensitive", () => { | ||
expect(isMathFunction(valueParser("cALc(10% + 10px)").nodes[0])).toBe(true); | ||
}); | ||
|
||
it("does not match var", () => { | ||
expect(isMathFunction(valueParser("var(--foo, 1px)").nodes[0])).toBe(false); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* @flow */ | ||
"use strict"; | ||
|
||
const MATH_FUNCTIONS = require("../reference/mathFunctions"); | ||
|
||
/** | ||
* Check whether a node is math function | ||
* | ||
* @param {Node} postcss-value-parser node | ||
* @return {boolean} If `true`, the node is math function | ||
*/ | ||
module.exports = function isMathFunction(node /*: Object */) /*: boolean*/ { | ||
return ( | ||
node.type === "function" && | ||
MATH_FUNCTIONS.includes(node.value.toLowerCase()) | ||
); | ||
}; |
There was a problem hiding this comment.
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 errorThere was a problem hiding this comment.
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