postcss-calc 7.0.5 → 8.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5,38 +5,60 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _postcss = require("postcss");
9
-
10
8
  var _transform = _interopRequireDefault(require("./lib/transform"));
11
9
 
12
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
11
 
14
- var _default = (0, _postcss.plugin)('postcss-calc', function (opts) {
15
- var options = Object.assign({
12
+ /**
13
+ * @typedef {{precision?: number | false,
14
+ * preserve?: boolean,
15
+ * warnWhenCannotResolve?: boolean,
16
+ * mediaQueries?: boolean,
17
+ * selectors?: boolean}} PostCssCalcOptions
18
+ *
19
+ * @param {PostCssCalcOptions} opts
20
+ */
21
+ function pluginCreator(opts) {
22
+ const options = Object.assign({
16
23
  precision: 5,
17
24
  preserve: false,
18
25
  warnWhenCannotResolve: false,
19
26
  mediaQueries: false,
20
27
  selectors: false
21
28
  }, opts);
22
- return function (css, result) {
23
- css.walk(function (node) {
24
- var type = node.type;
25
-
26
- if (type === 'decl') {
27
- (0, _transform.default)(node, "value", options, result);
28
- }
29
-
30
- if (type === 'atrule' && options.mediaQueries) {
31
- (0, _transform.default)(node, "params", options, result);
32
- }
33
-
34
- if (type === 'rule' && options.selectors) {
35
- (0, _transform.default)(node, "selector", options, result);
36
- }
37
- });
29
+ return {
30
+ postcssPlugin: 'postcss-calc',
31
+
32
+ /**
33
+ * @param {import('postcss').Root} css
34
+ * @param {{result: import('postcss').Result}} helpers
35
+ */
36
+ OnceExit(css, {
37
+ result
38
+ }) {
39
+ css.walk(node => {
40
+ const {
41
+ type
42
+ } = node;
43
+
44
+ if (type === 'decl') {
45
+ (0, _transform.default)(node, "value", options, result);
46
+ }
47
+
48
+ if (type === 'atrule' && options.mediaQueries) {
49
+ (0, _transform.default)(node, "params", options, result);
50
+ }
51
+
52
+ if (type === 'rule' && options.selectors) {
53
+ (0, _transform.default)(node, "selector", options, result);
54
+ }
55
+ });
56
+ }
57
+
38
58
  };
39
- });
59
+ }
40
60
 
61
+ pluginCreator.postcss = true;
62
+ var _default = pluginCreator;
41
63
  exports.default = _default;
42
64
  module.exports = exports.default;
@@ -4,7 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var conversions = {
7
+
8
+ /**
9
+ * @type {{[key:string]: {[key:string]: number}}}
10
+ */
11
+ const conversions = {
8
12
  // Absolute length units
9
13
  'px': {
10
14
  'px': 1,
@@ -129,10 +133,16 @@ var conversions = {
129
133
  'dppx': 1
130
134
  }
131
135
  };
136
+ /**
137
+ * @param {number} value
138
+ * @param {string} sourceUnit
139
+ * @param {string} targetUnit
140
+ * @param {number|false} precision
141
+ */
132
142
 
133
143
  function convertUnit(value, sourceUnit, targetUnit, precision) {
134
- var sourceUnitNormalized = sourceUnit.toLowerCase();
135
- var targetUnitNormalized = targetUnit.toLowerCase();
144
+ const sourceUnitNormalized = sourceUnit.toLowerCase();
145
+ const targetUnitNormalized = targetUnit.toLowerCase();
136
146
 
137
147
  if (!conversions[targetUnitNormalized]) {
138
148
  throw new Error("Cannot convert to " + targetUnit);
@@ -142,10 +152,10 @@ function convertUnit(value, sourceUnit, targetUnit, precision) {
142
152
  throw new Error("Cannot convert from " + sourceUnit + " to " + targetUnit);
143
153
  }
144
154
 
145
- var converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
155
+ const converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
146
156
 
147
157
  if (precision !== false) {
148
- precision = Math.pow(10, parseInt(precision) || 5);
158
+ precision = Math.pow(10, Math.ceil(precision) || 5);
149
159
  return Math.round(converted * precision) / precision;
150
160
  }
151
161
 
@@ -9,8 +9,12 @@ var _convertUnit = _interopRequireDefault(require("./convertUnit"));
9
9
 
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
 
12
- function isValueType(type) {
13
- switch (type) {
12
+ /**
13
+ * @param {import('../parser').CalcNode} node
14
+ * @return {node is import('../parser').ValueExpression}
15
+ */
16
+ function isValueType(node) {
17
+ switch (node.type) {
14
18
  case 'LengthValue':
15
19
  case 'AngleValue':
16
20
  case 'TimeValue':
@@ -31,35 +35,54 @@ function isValueType(type) {
31
35
 
32
36
  return false;
33
37
  }
38
+ /** @param {'-'|'+'} operator */
39
+
34
40
 
35
41
  function flip(operator) {
36
42
  return operator === '+' ? '-' : '+';
37
43
  }
44
+ /**
45
+ * @param {string} operator
46
+ * @returns {operator is '+'|'-'}
47
+ */
48
+
38
49
 
39
50
  function isAddSubOperator(operator) {
40
51
  return operator === '+' || operator === '-';
41
52
  }
53
+ /**
54
+ * @typedef {{preOperator: '+'|'-', node: import('../parser').CalcNode}} Collectible
55
+ */
56
+
57
+ /**
58
+ * @param {'+'|'-'} preOperator
59
+ * @param {import('../parser').CalcNode} node
60
+ * @param {Collectible[]} collected
61
+ * @param {number} precision
62
+ */
63
+
42
64
 
43
65
  function collectAddSubItems(preOperator, node, collected, precision) {
44
66
  if (!isAddSubOperator(preOperator)) {
45
67
  throw new Error(`invalid operator ${preOperator}`);
46
68
  }
47
69
 
48
- var type = node.type;
49
-
50
- if (isValueType(type)) {
51
- var itemIndex = collected.findIndex(function (x) {
52
- return x.node.type === type;
53
- });
70
+ if (isValueType(node)) {
71
+ const itemIndex = collected.findIndex(x => x.node.type === node.type);
54
72
 
55
73
  if (itemIndex >= 0) {
56
74
  if (node.value === 0) {
57
75
  return;
58
- }
76
+ } // can cast because of the criterion used to find itemIndex
77
+
59
78
 
60
- var _covertNodesUnits = covertNodesUnits(collected[itemIndex].node, node, precision),
61
- reducedNode = _covertNodesUnits.left,
62
- current = _covertNodesUnits.right;
79
+ const otherValueNode =
80
+ /** @type import('../parser').ValueExpression*/
81
+ collected[itemIndex].node;
82
+ const {
83
+ left: reducedNode,
84
+ right: current
85
+ } = convertNodesUnits(otherValueNode, node, precision);
63
86
 
64
87
  if (collected[itemIndex].preOperator === '-') {
65
88
  collected[itemIndex].preOperator = '+';
@@ -100,25 +123,26 @@ function collectAddSubItems(preOperator, node, collected, precision) {
100
123
  });
101
124
  }
102
125
  }
103
- } else if (type === "MathExpression") {
126
+ } else if (node.type === "MathExpression") {
104
127
  if (isAddSubOperator(node.operator)) {
105
128
  collectAddSubItems(preOperator, node.left, collected, precision);
106
- var collectRightOperator = preOperator === '-' ? flip(node.operator) : node.operator;
129
+ const collectRightOperator = preOperator === '-' ? flip(node.operator) : node.operator;
107
130
  collectAddSubItems(collectRightOperator, node.right, collected, precision);
108
131
  } else {
109
132
  // * or /
110
- var _reducedNode = reduce(node, precision); // prevent infinite recursive call
111
-
133
+ const reducedNode = reduce(node, precision); // prevent infinite recursive call
112
134
 
113
- if (_reducedNode.type !== "MathExpression" || isAddSubOperator(_reducedNode.operator)) {
114
- collectAddSubItems(preOperator, _reducedNode, collected, precision);
135
+ if (reducedNode.type !== "MathExpression" || isAddSubOperator(reducedNode.operator)) {
136
+ collectAddSubItems(preOperator, reducedNode, collected, precision);
115
137
  } else {
116
138
  collected.push({
117
- node: _reducedNode,
139
+ node: reducedNode,
118
140
  preOperator
119
141
  });
120
142
  }
121
143
  }
144
+ } else if (node.type === 'ParenthesizedExpression') {
145
+ collectAddSubItems(preOperator, node.content, collected, precision);
122
146
  } else {
123
147
  collected.push({
124
148
  node,
@@ -126,33 +150,38 @@ function collectAddSubItems(preOperator, node, collected, precision) {
126
150
  });
127
151
  }
128
152
  }
153
+ /**
154
+ * @param {import('../parser').CalcNode} node
155
+ * @param {number} precision
156
+ */
157
+
129
158
 
130
159
  function reduceAddSubExpression(node, precision) {
131
- var collected = [];
160
+ /** @type Collectible[] */
161
+ const collected = [];
132
162
  collectAddSubItems('+', node, collected, precision);
133
- var withoutZeroItem = collected.filter(function (item) {
134
- return !(isValueType(item.node.type) && item.node.value === 0);
135
- });
136
- var firstNonZeroItem = withoutZeroItem[0]; // could be undefined
163
+ const withoutZeroItem = collected.filter(item => !(isValueType(item.node) && item.node.value === 0));
164
+ const firstNonZeroItem = withoutZeroItem[0]; // could be undefined
137
165
  // prevent producing "calc(-var(--a))" or "calc()"
138
166
  // which is invalid css
139
167
 
140
- if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node.type)) {
141
- var firstZeroItem = collected.find(function (item) {
142
- return isValueType(item.node.type) && item.node.value === 0;
143
- });
144
- withoutZeroItem.unshift(firstZeroItem);
168
+ if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node)) {
169
+ const firstZeroItem = collected.find(item => isValueType(item.node) && item.node.value === 0);
170
+
171
+ if (firstZeroItem) {
172
+ withoutZeroItem.unshift(firstZeroItem);
173
+ }
145
174
  } // make sure the preOperator of the first item is +
146
175
 
147
176
 
148
- if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node.type)) {
177
+ if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node)) {
149
178
  withoutZeroItem[0].node.value *= -1;
150
179
  withoutZeroItem[0].preOperator = '+';
151
180
  }
152
181
 
153
- var root = withoutZeroItem[0].node;
182
+ let root = withoutZeroItem[0].node;
154
183
 
155
- for (var i = 1; i < withoutZeroItem.length; i++) {
184
+ for (let i = 1; i < withoutZeroItem.length; i++) {
156
185
  root = {
157
186
  type: 'MathExpression',
158
187
  operator: withoutZeroItem[i].preOperator,
@@ -163,9 +192,13 @@ function reduceAddSubExpression(node, precision) {
163
192
 
164
193
  return root;
165
194
  }
195
+ /**
196
+ * @param {import('../parser').MathExpression} node
197
+ */
198
+
166
199
 
167
200
  function reduceDivisionExpression(node) {
168
- if (!isValueType(node.right.type)) {
201
+ if (!isValueType(node.right)) {
169
202
  return node;
170
203
  }
171
204
 
@@ -174,7 +207,14 @@ function reduceDivisionExpression(node) {
174
207
  }
175
208
 
176
209
  return applyNumberDivision(node.left, node.right.value);
177
- } // apply (expr) / number
210
+ }
211
+ /**
212
+ * apply (expr) / number
213
+ *
214
+ * @param {import('../parser').CalcNode} node
215
+ * @param {number} divisor
216
+ * @return {import('../parser').CalcNode}
217
+ */
178
218
 
179
219
 
180
220
  function applyNumberDivision(node, divisor) {
@@ -182,7 +222,7 @@ function applyNumberDivision(node, divisor) {
182
222
  throw new Error('Cannot divide by zero');
183
223
  }
184
224
 
185
- if (isValueType(node.type)) {
225
+ if (isValueType(node)) {
186
226
  node.value /= divisor;
187
227
  return node;
188
228
  }
@@ -213,6 +253,10 @@ function applyNumberDivision(node, divisor) {
213
253
  }
214
254
  };
215
255
  }
256
+ /**
257
+ * @param {import('../parser').MathExpression} node
258
+ */
259
+
216
260
 
217
261
  function reduceMultiplicationExpression(node) {
218
262
  // (expr) * number
@@ -226,11 +270,17 @@ function reduceMultiplicationExpression(node) {
226
270
  }
227
271
 
228
272
  return node;
229
- } // apply (expr) / number
273
+ }
274
+ /**
275
+ * apply (expr) * number
276
+ * @param {number} multiplier
277
+ * @param {import('../parser').CalcNode} node
278
+ * @return {import('../parser').CalcNode}
279
+ */
230
280
 
231
281
 
232
282
  function applyNumberMultiplication(node, multiplier) {
233
- if (isValueType(node.type)) {
283
+ if (isValueType(node)) {
234
284
  node.value *= multiplier;
235
285
  return node;
236
286
  }
@@ -261,8 +311,14 @@ function applyNumberMultiplication(node, multiplier) {
261
311
  }
262
312
  };
263
313
  }
314
+ /**
315
+ * @param {import('../parser').ValueExpression} left
316
+ * @param {import('../parser').ValueExpression} right
317
+ * @param {number} precision
318
+ */
264
319
 
265
- function covertNodesUnits(left, right, precision) {
320
+
321
+ function convertNodesUnits(left, right, precision) {
266
322
  switch (left.type) {
267
323
  case 'LengthValue':
268
324
  case 'AngleValue':
@@ -270,7 +326,7 @@ function covertNodesUnits(left, right, precision) {
270
326
  case 'FrequencyValue':
271
327
  case 'ResolutionValue':
272
328
  if (right.type === left.type && right.unit && left.unit) {
273
- var converted = (0, _convertUnit.default)(right.value, right.unit, left.unit, precision);
329
+ const converted = (0, _convertUnit.default)(right.value, right.unit, left.unit, precision);
274
330
  right = {
275
331
  type: left.type,
276
332
  value: converted,
@@ -290,6 +346,12 @@ function covertNodesUnits(left, right, precision) {
290
346
  };
291
347
  }
292
348
  }
349
+ /**
350
+ * @param {import('../parser').CalcNode} node
351
+ * @param {number} precision
352
+ * @return {import('../parser').CalcNode}
353
+ */
354
+
293
355
 
294
356
  function reduce(node, precision) {
295
357
  if (node.type === "MathExpression") {
@@ -303,15 +365,21 @@ function reduce(node, precision) {
303
365
 
304
366
  switch (node.operator) {
305
367
  case "/":
306
- return reduceDivisionExpression(node, precision);
368
+ return reduceDivisionExpression(node);
307
369
 
308
370
  case "*":
309
- return reduceMultiplicationExpression(node, precision);
371
+ return reduceMultiplicationExpression(node);
310
372
  }
311
373
 
312
374
  return node;
313
375
  }
314
376
 
377
+ if (node.type === 'ParenthesizedExpression') {
378
+ if (node.content.type !== 'Function') {
379
+ return reduce(node.content, precision);
380
+ }
381
+ }
382
+
315
383
  return node;
316
384
  }
317
385
 
@@ -4,30 +4,43 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = _default;
7
- var order = {
7
+ const order = {
8
8
  "*": 0,
9
9
  "/": 0,
10
10
  "+": 1,
11
11
  "-": 1
12
12
  };
13
+ /**
14
+ * @param {number} value
15
+ * @param {number | false} prec
16
+ */
13
17
 
14
18
  function round(value, prec) {
15
19
  if (prec !== false) {
16
- var precision = Math.pow(10, prec);
20
+ const precision = Math.pow(10, prec);
17
21
  return Math.round(value * precision) / precision;
18
22
  }
19
23
 
20
24
  return value;
21
25
  }
26
+ /**
27
+ * @param {number | false} prec
28
+ * @param {import('../parser').CalcNode} node
29
+ *
30
+ * @return {string}
31
+ */
32
+
22
33
 
23
34
  function stringify(node, prec) {
24
35
  switch (node.type) {
25
36
  case "MathExpression":
26
37
  {
27
- var left = node.left,
28
- right = node.right,
29
- op = node.operator;
30
- var str = "";
38
+ const {
39
+ left,
40
+ right,
41
+ operator: op
42
+ } = node;
43
+ let str = "";
31
44
 
32
45
  if (left.type === 'MathExpression' && order[op] < order[left.operator]) {
33
46
  str += `(${stringify(left, prec)})`;
@@ -47,19 +60,33 @@ function stringify(node, prec) {
47
60
  }
48
61
 
49
62
  case 'Number':
50
- return round(node.value, prec);
63
+ return round(node.value, prec).toString();
51
64
 
52
65
  case 'Function':
53
- return node.value;
66
+ return node.value.toString();
67
+
68
+ case 'ParenthesizedExpression':
69
+ return `(${stringify(node.content, prec)})`;
54
70
 
55
71
  default:
56
72
  return round(node.value, prec) + node.unit;
57
73
  }
58
74
  }
75
+ /**
76
+ * @param {string} calc
77
+ * @param {import('../parser').CalcNode} node
78
+ * @param {string} originalValue
79
+ * @param {{precision: number | false, warnWhenCannotResolve: boolean}} options
80
+ * @param {import("postcss").Result} result
81
+ * @param {import("postcss").ChildNode} item
82
+ *
83
+ * @returns {string}
84
+ */
85
+
59
86
 
60
87
  function _default(calc, node, originalValue, options, result, item) {
61
- var str = stringify(node, options.precision);
62
- var shouldPrintCalc = node.type === "MathExpression" || node.type === "Function";
88
+ let str = stringify(node, options.precision);
89
+ const shouldPrintCalc = node.type === "MathExpression" || node.type === "Function";
63
90
 
64
91
  if (shouldPrintCalc) {
65
92
  // if calc expression couldn't be resolved to a single value, re-wrap it as
@@ -18,42 +18,56 @@ var _stringifier = _interopRequireDefault(require("./stringifier"));
18
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
19
 
20
20
  // eslint-disable-next-line import/no-unresolved
21
- var MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
21
+ const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
22
+ /**
23
+ * @param {string} value
24
+ * @param {{precision: number, warnWhenCannotResolve: boolean}} options
25
+ * @param {import("postcss").Result} result
26
+ * @param {import("postcss").ChildNode} item
27
+ */
22
28
 
23
29
  function transformValue(value, options, result, item) {
24
- return (0, _postcssValueParser.default)(value).walk(function (node) {
30
+ return (0, _postcssValueParser.default)(value).walk(node => {
25
31
  // skip anything which isn't a calc() function
26
- if (node.type !== 'function' || !MATCH_CALC.test(node.value)) {
27
- return node;
32
+ if (node.type !== "function" || !MATCH_CALC.test(node.value)) {
33
+ return;
28
34
  } // stringify calc expression and produce an AST
29
35
 
30
36
 
31
- var contents = _postcssValueParser.default.stringify(node.nodes);
37
+ const contents = _postcssValueParser.default.stringify(node.nodes);
32
38
 
33
- var ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
39
+ const ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
34
40
  // or a simplified calc expression
35
41
 
36
42
 
37
- var reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
43
+ const reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
38
44
 
39
- node.type = 'word';
45
+ /** @type {valueParser.Node} */
46
+ node.type = "word";
40
47
  node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
41
48
  return false;
42
49
  }).toString();
43
50
  }
51
+ /**
52
+ * @param {import("postcss-selector-parser").Selectors} value
53
+ * @param {{precision: number, warnWhenCannotResolve: boolean}} options
54
+ * @param {import("postcss").Result} result
55
+ * @param {import("postcss").ChildNode} item
56
+ */
57
+
44
58
 
45
59
  function transformSelector(value, options, result, item) {
46
- return (0, _postcssSelectorParser.default)(function (selectors) {
47
- selectors.walk(function (node) {
60
+ return (0, _postcssSelectorParser.default)(selectors => {
61
+ selectors.walk(node => {
48
62
  // attribute value
49
63
  // e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
50
- if (node.type === 'attribute' && node.value) {
64
+ if (node.type === "attribute" && node.value) {
51
65
  node.setValue(transformValue(node.value, options, result, item));
52
66
  } // tag value
53
67
  // e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
54
68
 
55
69
 
56
- if (node.type === 'tag') {
70
+ if (node.type === "tag") {
57
71
  node.value = transformValue(node.value, options, result, item);
58
72
  }
59
73
 
@@ -61,15 +75,39 @@ function transformSelector(value, options, result, item) {
61
75
  });
62
76
  }).processSync(value);
63
77
  }
64
-
65
- var _default = function _default(node, property, options, result) {
66
- var value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node); // if the preserve option is enabled and the value has changed, write the
78
+ /**
79
+ * @param {any} node
80
+ * @param {{precision: number, preserve: boolean, warnWhenCannotResolve: boolean}} options
81
+ * @param {'value'|'params'|'selector'} property
82
+ * @param {import("postcss").Result} result
83
+ */
84
+
85
+
86
+ var _default = (node, property, options, result) => {
87
+ let value = node[property];
88
+
89
+ try {
90
+ value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node);
91
+ } catch (error) {
92
+ if (error instanceof Error) {
93
+ result.warn(error.message, {
94
+ node
95
+ });
96
+ } else {
97
+ result.warn('Error', {
98
+ node
99
+ });
100
+ }
101
+
102
+ return;
103
+ } // if the preserve option is enabled and the value has changed, write the
67
104
  // transformed value into a cloned node which is inserted before the current
68
105
  // node, preserving the original value. Otherwise, overwrite the original
69
106
  // value.
70
107
 
108
+
71
109
  if (options.preserve && node[property] !== value) {
72
- var clone = node.clone();
110
+ const clone = node.clone();
73
111
  clone[property] = value;
74
112
  node.parent.insertBefore(node, clone);
75
113
  } else {
package/dist/parser.js CHANGED
@@ -828,8 +828,6 @@ case 1:
828
828
 
829
829
  case 2:
830
830
  /*! Production:: math_expression : CALC LPAREN math_expression RPAREN */
831
- case 7:
832
- /*! Production:: math_expression : LPAREN math_expression RPAREN */
833
831
 
834
832
  this.$ = yyvstack[yysp - 1];
835
833
  break;
@@ -846,6 +844,12 @@ case 6:
846
844
  this.$ = { type: 'MathExpression', operator: yyvstack[yysp - 1], left: yyvstack[yysp - 2], right: yyvstack[yysp] };
847
845
  break;
848
846
 
847
+ case 7:
848
+ /*! Production:: math_expression : LPAREN math_expression RPAREN */
849
+
850
+ this.$ = { type: 'ParenthesizedExpression', content: yyvstack[yysp - 1] };
851
+ break;
852
+
849
853
  case 8:
850
854
  /*! Production:: math_expression : function */
851
855
  case 9:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-calc",
3
- "version": "7.0.5",
3
+ "version": "8.2.1",
4
4
  "description": "PostCSS plugin to reduce calc()",
5
5
  "keywords": [
6
6
  "css",
@@ -10,52 +10,50 @@
10
10
  "calc"
11
11
  ],
12
12
  "main": "dist/index.js",
13
+ "types": "types/index.d.ts",
13
14
  "files": [
14
15
  "dist",
16
+ "types",
15
17
  "LICENSE"
16
18
  ],
17
- "scripts": {
18
- "prepublish": "npm run build",
19
- "build": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist --ignore src/__tests__/**/*.js && jison src/parser.jison -o dist/parser.js",
20
- "pretest": "npm run build && eslint src",
21
- "test": "ava"
22
- },
23
19
  "author": "Andy Jansson",
24
20
  "license": "MIT",
25
21
  "repository": "https://github.com/postcss/postcss-calc.git",
26
22
  "eslintConfig": {
27
- "parser": "babel-eslint",
28
- "extends": "eslint-config-i-am-meticulous",
23
+ "extends": [
24
+ "eslint:recommended",
25
+ "plugin:import/recommended"
26
+ ],
29
27
  "rules": {
30
28
  "curly": "error"
31
29
  }
32
30
  },
33
31
  "devDependencies": {
34
- "@babel/cli": "^7.1.2",
35
- "@babel/core": "^7.1.2",
36
- "@babel/polyfill": "^7.0.0",
37
- "@babel/preset-env": "^7.1.0",
38
- "@babel/register": "^7.0.0",
39
- "ava": "^1.4.1",
40
- "babel-eslint": "^10.0.1",
32
+ "@babel/cli": "^7.16.0",
33
+ "@babel/core": "^7.16.5",
34
+ "@babel/plugin-transform-modules-commonjs": "^7.16.5",
35
+ "@babel/register": "^7.16.7",
41
36
  "babel-plugin-add-module-exports": "^1.0.0",
42
- "cross-env": "^5.2.0",
43
- "del-cli": "^1.1.0",
44
- "eslint": "^5.7.0",
45
- "eslint-config-i-am-meticulous": "^11.0.0",
46
- "eslint-plugin-babel": "^5.2.1",
47
- "eslint-plugin-import": "^2.14.0",
48
- "jison-gho": "^0.6.1-215"
37
+ "eslint": "^8.5.0",
38
+ "eslint-plugin-import": "^2.25.3",
39
+ "jison-gho": "^0.6.1-216",
40
+ "postcss": "^8.2.2",
41
+ "rimraf": "^3.0.2",
42
+ "typescript": "^4.5.4",
43
+ "uvu": "^0.5.3"
49
44
  },
50
45
  "dependencies": {
51
- "postcss": "^7.0.27",
52
46
  "postcss-selector-parser": "^6.0.2",
53
47
  "postcss-value-parser": "^4.0.2"
54
48
  },
55
- "ava": {
56
- "require": [
57
- "@babel/register",
58
- "@babel/polyfill"
59
- ]
60
- }
61
- }
49
+ "peerDependencies": {
50
+ "postcss": "^8.2.2"
51
+ },
52
+ "scripts": {
53
+ "build": "rimraf dist && babel src --out-dir dist --ignore src/__tests__/**/*.js && jison src/parser.jison -o dist/parser.js",
54
+ "lint": "eslint src && tsc",
55
+ "pretest": "pnpm run build",
56
+ "test": "uvu -r @babel/register src/__tests__"
57
+ },
58
+ "readme": "# PostCSS Calc [<img src=\"https://postcss.github.io/postcss/logo.svg\" alt=\"PostCSS\" width=\"90\" height=\"90\" align=\"right\">][PostCSS]\n\n[![NPM Version][npm-img]][npm-url]\n[![Build Status][cli-img]][cli-url]\n[![Support Chat][git-img]][git-url]\n\n[PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This\ncan be particularly useful with the [PostCSS Custom Properties] plugin.\n\nWhen multiple units are mixed together in the same expression, the `calc()`\nstatement is left as is, to fallback to the [W3C calc() implementation].\n\n## Installation\n\n```bash\nnpm install postcss-calc\n```\n\n## Usage\n\n```js\n// dependencies\nvar fs = require(\"fs\")\nvar postcss = require(\"postcss\")\nvar calc = require(\"postcss-calc\")\n\n// css to be processed\nvar css = fs.readFileSync(\"input.css\", \"utf8\")\n\n// process css\nvar output = postcss()\n .use(calc())\n .process(css)\n .css\n```\n\n**Example** (with [PostCSS Custom Properties] enabled as well):\n\n```js\n// dependencies\nvar fs = require(\"fs\")\nvar postcss = require(\"postcss\")\nvar customProperties = require(\"postcss-custom-properties\")\nvar calc = require(\"postcss-calc\")\n\n// css to be processed\nvar css = fs.readFileSync(\"input.css\", \"utf8\")\n\n// process css\nvar output = postcss()\n .use(customProperties())\n .use(calc())\n .process(css)\n .css\n```\n\nUsing this `input.css`:\n\n```css\n:root {\n --main-font-size: 16px;\n}\n\nbody {\n font-size: var(--main-font-size);\n}\n\nh1 {\n font-size: calc(var(--main-font-size) * 2);\n height: calc(100px - 2em);\n margin-bottom: calc(\n var(--main-font-size)\n * 1.5\n )\n}\n```\n\nyou will get:\n\n```css\nbody {\n font-size: 16px\n}\n\nh1 {\n font-size: 32px;\n height: calc(100px - 2em);\n margin-bottom: 24px\n}\n```\n\nCheckout [tests] for more examples.\n\n### Options\n\n#### `precision` (default: `5`)\n\nAllow you to define the precision for decimal numbers.\n\n```js\nvar out = postcss()\n .use(calc({precision: 10}))\n .process(css)\n .css\n```\n\n#### `preserve` (default: `false`)\n\nAllow you to preserve calc() usage in output so browsers will handle decimal\nprecision themselves.\n\n```js\nvar out = postcss()\n .use(calc({preserve: true}))\n .process(css)\n .css\n```\n\n#### `warnWhenCannotResolve` (default: `false`)\n\nAdds warnings when calc() are not reduced to a single value.\n\n```js\nvar out = postcss()\n .use(calc({warnWhenCannotResolve: true}))\n .process(css)\n .css\n```\n\n#### `mediaQueries` (default: `false`)\n\nAllows calc() usage as part of media query declarations.\n\n```js\nvar out = postcss()\n .use(calc({mediaQueries: true}))\n .process(css)\n .css\n```\n\n#### `selectors` (default: `false`)\n\nAllows calc() usage as part of selectors.\n\n```js\nvar out = postcss()\n .use(calc({selectors: true}))\n .process(css)\n .css\n```\n\nExample:\n\n```css\ndiv[data-size=\"calc(3*3)\"] {\n width: 100px;\n}\n```\n\n---\n\n## Contributing\n\nWork on a branch, install dev-dependencies, respect coding style & run tests\nbefore submitting a bug fix or a feature.\n\n```bash\ngit clone git@github.com:postcss/postcss-calc.git\ngit checkout -b patch-1\nnpm install\nnpm test\n```\n\n## [Changelog](CHANGELOG.md)\n\n## [License](LICENSE)\n\n[cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg\n[cli-url]: https://travis-ci.org/postcss/postcss-calc\n[git-img]: https://img.shields.io/badge/support-chat-blue.svg\n[git-url]: https://gitter.im/postcss/postcss\n[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg\n[npm-url]: https://www.npmjs.com/package/postcss-calc\n\n[PostCSS]: https://github.com/postcss\n[PostCSS Calc]: https://github.com/postcss/postcss-calc\n[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties\n[tests]: src/__tests__/index.js\n[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation\n"
59
+ }
@@ -0,0 +1,30 @@
1
+ export default pluginCreator;
2
+ export type PostCssCalcOptions = {
3
+ precision?: number | false;
4
+ preserve?: boolean;
5
+ warnWhenCannotResolve?: boolean;
6
+ mediaQueries?: boolean;
7
+ selectors?: boolean;
8
+ };
9
+ /**
10
+ * @typedef {{precision?: number | false,
11
+ * preserve?: boolean,
12
+ * warnWhenCannotResolve?: boolean,
13
+ * mediaQueries?: boolean,
14
+ * selectors?: boolean}} PostCssCalcOptions
15
+ *
16
+ * @param {PostCssCalcOptions} opts
17
+ */
18
+ declare function pluginCreator(opts: PostCssCalcOptions): {
19
+ postcssPlugin: string;
20
+ /**
21
+ * @param {import('postcss').Root} css
22
+ * @param {{result: import('postcss').Result}} helpers
23
+ */
24
+ OnceExit(css: import('postcss').Root, { result }: {
25
+ result: import('postcss').Result;
26
+ }): void;
27
+ };
28
+ declare namespace pluginCreator {
29
+ const postcss: boolean;
30
+ }
@@ -0,0 +1,8 @@
1
+ export default convertUnit;
2
+ /**
3
+ * @param {number} value
4
+ * @param {string} sourceUnit
5
+ * @param {string} targetUnit
6
+ * @param {number|false} precision
7
+ */
8
+ declare function convertUnit(value: number, sourceUnit: string, targetUnit: string, precision: number | false): number;
@@ -0,0 +1,11 @@
1
+ export default reduce;
2
+ export type Collectible = {
3
+ preOperator: '+' | '-';
4
+ node: import('../parser').CalcNode;
5
+ };
6
+ /**
7
+ * @param {import('../parser').CalcNode} node
8
+ * @param {number} precision
9
+ * @return {import('../parser').CalcNode}
10
+ */
11
+ declare function reduce(node: import('../parser').CalcNode, precision: number): import('../parser').CalcNode;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @param {string} calc
3
+ * @param {import('../parser').CalcNode} node
4
+ * @param {string} originalValue
5
+ * @param {{precision: number | false, warnWhenCannotResolve: boolean}} options
6
+ * @param {import("postcss").Result} result
7
+ * @param {import("postcss").ChildNode} item
8
+ *
9
+ * @returns {string}
10
+ */
11
+ export default function _default(calc: string, node: import('../parser').CalcNode, originalValue: string, options: {
12
+ precision: number | false;
13
+ warnWhenCannotResolve: boolean;
14
+ }, result: import("postcss").Result, item: import("postcss").ChildNode): string;
@@ -0,0 +1,6 @@
1
+ declare function _default(node: any, property: 'value' | 'params' | 'selector', options: {
2
+ precision: number;
3
+ preserve: boolean;
4
+ warnWhenCannotResolve: boolean;
5
+ }, result: import("postcss").Result): void;
6
+ export default _default;
package/CHANGELOG.md DELETED
@@ -1,122 +0,0 @@
1
- # 7.0.5
2
-
3
- - Fixed: reduction
4
-
5
- # 7.0.4
6
-
7
- - Fixed: strips away important factors from multiplications in calc() ([#107](https://github.com/postcss/postcss-calc/issues/107))
8
-
9
- # 7.0.3
10
-
11
- - Fixed: substracted css-variable from zero ([#111](https://github.com/postcss/postcss-calc/issues/111))
12
-
13
- # 7.0.2
14
-
15
- - Fixed: incorrect reduction of subtraction from zero ([#88](https://github.com/postcss/postcss-calc/issues/88))
16
- - Fixed: doesn't remove calc for single function
17
- - Fixed: relax parser on unknown units ([#76](https://github.com/postcss/postcss-calc/issues/76))
18
- - Fixed: handle numbers with exponen composed ([#83](https://github.com/postcss/postcss-calc/pull/83))
19
- - Fixed: handle plus sign before value ([#79](https://github.com/postcss/postcss-calc/pull/79))
20
- - Fixed: better handle precision for nested calc ([#75](https://github.com/postcss/postcss-calc/pull/75))
21
- - Fixed: properly handle nested add and sub expression inside sub expression ([#64](https://github.com/postcss/postcss-calc/issues/64))
22
- - Fixed: handle uppercase units and functions ([#71](https://github.com/postcss/postcss-calc/pull/71))
23
- - Fixed: do not break `calc` with single var ([cssnano/cssnano#725](https://github.com/cssnano/cssnano/issues/725))
24
- - Updated: `postcss` to 7.0.27 (patch)
25
- - Updated: `postcss-selector-parser` to 6.0.2
26
- - Updated: `postcss-value-parser` to 4.0.2
27
-
28
- # 7.0.1
29
-
30
- - Updated: `postcss` to 7.0.2 (patch)
31
- - Updated: `postcss-selector-parser` to 5.0.0-rc.4 (patch)
32
- - Updated: `postcss-value-parser` to 3.3.1 (patch)
33
-
34
- # 7.0.0
35
-
36
- - Changed: Updated postcss-selector-parser to version 5.0.0-rc.3
37
- - Changed: Dropped reduce-css-calc as a dependency
38
- - Fixed: Support constant() and env() ([#42](https://github.com/postcss/postcss-calc/issues/42), [#48](https://github.com/postcss/postcss-calc/issues/48))
39
- - Fixed: Support custom properties with "calc" in its name ([#50](https://github.com/postcss/postcss-calc/issues/50))
40
- - Fixed: Remove unnecessary whitespace around `*` and `/` ([cssnano#625](https://github.com/cssnano/cssnano/issues/625))
41
- - Fixed: Arithmetic bugs around subtraction ([#49](https://github.com/postcss/postcss-calc/issues/49))
42
- - Fixed: Handling of nested calc statements ([reduce-css-calc#49](https://github.com/MoOx/reduce-css-calc/issues/49))
43
- - Fixed: Bugs regarding complex calculations ([reduce-cs-calc#45](https://github.com/MoOx/reduce-css-calc/issues/45))
44
- - Fixed: `100%` incorrectly being transformed to `1` ([reduce-css-calc#44](https://github.com/MoOx/reduce-css-calc/issues/44))
45
- - Added: support for case-insensitive calc statements
46
-
47
- # 6.0.2 - 2018-09-25
48
-
49
- - Fixed: use PostCSS 7 (thanks to @douglasduteil)
50
-
51
- # 6.0.1 - 2017-10-10
52
-
53
- - Fixed: throwing error for attribute selectors without a value
54
-
55
- # 6.0.0 - 2017-05-08
56
-
57
- - Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x
58
- to v2.x (thanks to @andyjansson).
59
-
60
- # 5.3.1 - 2016-08-22
61
-
62
- - Fixed: avoid security issue related to ``reduce-css-calc@< 1.2.4``.
63
-
64
- # 5.3.0 - 2016-07-11
65
-
66
- - Added: support for selector transformation via `selectors` option.
67
- ([#29](https://github.com/postcss/postcss-calc/pull/29) - @uniquegestaltung)
68
-
69
- # 5.2.1 - 2016-04-10
70
-
71
- - Fixed: support for multiline value
72
- ([#27](https://github.com/postcss/postcss-calc/pull/27))
73
-
74
- # 5.2.0 - 2016-01-08
75
-
76
- - Added: "mediaQueries" option for `@media` support
77
- ([#22](https://github.com/postcss/postcss-calc/pull/22))
78
-
79
- # 5.1.0 - 2016-01-07
80
-
81
- - Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value
82
- ([#20](https://github.com/postcss/postcss-calc/pull/20))
83
-
84
- # 5.0.0 - 2015-08-25
85
-
86
- - Removed: compatibility with postcss v4.x
87
- - Added: compatibility with postcss v5.x
88
-
89
- # 4.1.0 - 2015-04-09
90
-
91
- - Added: compatibility with postcss v4.1.x ([#12](https://github.com/postcss/postcss-calc/pull/12))
92
-
93
- # 4.0.1 - 2015-04-09
94
-
95
- - Fixed: `preserve` option does not create duplicated values ([#7](https://github.com/postcss/postcss-calc/issues/7))
96
-
97
- # 4.0.0 - 2015-01-26
98
-
99
- - Added: compatibility with postcss v4.x
100
- - Changed: partial compatiblity with postcss v3.x (stack traces have lost filename)
101
-
102
- # 3.0.0 - 2014-11-24
103
-
104
- - Added: GNU like exceptions ([#4](https://github.com/postcss/postcss-calc/issues/4))
105
- - Added: `precision` option ([#5](https://github.com/postcss/postcss-calc/issues/5))
106
- - Added: `preserve` option ([#6](https://github.com/postcss/postcss-calc/issues/6))
107
-
108
- # 2.1.0 - 2014-10-15
109
-
110
- - Added: source of the error (gnu like message) (fix [#3](https://github.com/postcss/postcss-calc/issues/3))
111
-
112
- # 2.0.1 - 2014-08-10
113
-
114
- - Fixed: correctly ignore unrecognized values (fix [#2](https://github.com/postcss/postcss-calc/issues/2))
115
-
116
- # 2.0.0 - 2014-08-06
117
-
118
- - Changed: Plugin now return a function to have a consistent api. ([ref 1](https://github.com/ianstormtaylor/rework-color-function/issues/6), [ref 2](https://twitter.com/jongleberry/status/496552790416576513))
119
-
120
- # 1.0.0 - 2014-08-04
121
-
122
- ✨ First release based on [rework-calc](https://github.com/reworkcss/rework-calc) v1.1.0 (code mainly exported to [`reduce-css-calc`](https://github.com/MoOx/reduce-css-calc))