-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not break
calc
with single var (#69)
- Loading branch information
1 parent
34fe682
commit 185f0fb
Showing
3 changed files
with
96 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,73 @@ | ||
const order = { | ||
"*": 0, | ||
"/": 0, | ||
"+": 1, | ||
"-": 1, | ||
}; | ||
|
||
function round(value, prec) { | ||
if (prec !== false) { | ||
const precision = Math.pow(10, prec); | ||
return Math.round(value * precision) / precision; | ||
} | ||
return value; | ||
} | ||
|
||
function stringify(node, prec) { | ||
switch (node.type) { | ||
case "MathExpression": { | ||
const {left, right, operator: op} = node; | ||
let str = ""; | ||
|
||
if (left.type === 'MathExpression' && order[op] < order[left.operator]) | ||
str += `(${stringify(left, prec)})`; | ||
else | ||
str += stringify(left, prec); | ||
|
||
str += order[op] ? ` ${node.operator} ` : node.operator; | ||
|
||
if (right.type === 'MathExpression' && order[op] < order[right.operator]) | ||
str += `(${stringify(right, prec)})`; | ||
else | ||
str += stringify(right, prec); | ||
|
||
return str; | ||
} | ||
case "Value": | ||
return round(node.value, prec); | ||
case 'Function': | ||
return node.value; | ||
default: | ||
return round(node.value, prec) + node.unit; | ||
} | ||
} | ||
|
||
export default function ( | ||
calc, | ||
node, | ||
originalValue, | ||
options, | ||
result, | ||
item | ||
) { | ||
let str = stringify(node, options.precision); | ||
|
||
if (node.type === "MathExpression") { | ||
// if calc expression couldn't be resolved to a single value, re-wrap it as | ||
// a calc() | ||
str = `${calc}(${str})`; | ||
|
||
// if the warnWhenCannotResolve option is on, inform the user that the calc | ||
// expression could not be resolved to a single value | ||
if (options.warnWhenCannotResolve) { | ||
result.warn( | ||
"Could not reduce expression: " + originalValue, | ||
{ plugin: 'postcss-calc', node: item }); | ||
} | ||
} | ||
return str; | ||
} | ||
const order = { | ||
"*": 0, | ||
"/": 0, | ||
"+": 1, | ||
"-": 1, | ||
}; | ||
|
||
function round(value, prec) { | ||
if (prec !== false) { | ||
const precision = Math.pow(10, prec); | ||
return Math.round(value * precision) / precision; | ||
} | ||
return value; | ||
} | ||
|
||
function stringify(node, prec) { | ||
switch (node.type) { | ||
case "MathExpression": { | ||
const {left, right, operator: op} = node; | ||
let str = ""; | ||
|
||
if (left.type === 'MathExpression' && order[op] < order[left.operator]) | ||
str += `(${stringify(left, prec)})`; | ||
else | ||
str += stringify(left, prec); | ||
|
||
str += order[op] ? ` ${node.operator} ` : node.operator; | ||
|
||
if (right.type === 'MathExpression' && order[op] < order[right.operator]) | ||
str += `(${stringify(right, prec)})`; | ||
else | ||
str += stringify(right, prec); | ||
|
||
return str; | ||
} | ||
case "Value": | ||
return round(node.value, prec); | ||
case 'Function': | ||
return node.value; | ||
default: | ||
return round(node.value, prec) + node.unit; | ||
} | ||
} | ||
|
||
export default function ( | ||
calc, | ||
node, | ||
originalValue, | ||
options, | ||
result, | ||
item | ||
) { | ||
let str = stringify(node, options.precision); | ||
|
||
const shouldPrintCalc = | ||
node.type === "MathExpression" || | ||
(node.type === "Function" && node.value.toLowerCase().slice(0, 4) === "var("); | ||
|
||
if (shouldPrintCalc) { | ||
// if calc expression couldn't be resolved to a single value, re-wrap it as | ||
// a calc() | ||
str = `${calc}(${str})`; | ||
|
||
// if the warnWhenCannotResolve option is on, inform the user that the calc | ||
// expression could not be resolved to a single value | ||
if (options.warnWhenCannotResolve) { | ||
result.warn( | ||
"Could not reduce expression: " + originalValue, | ||
{ plugin: 'postcss-calc', node: item }); | ||
} | ||
} | ||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters