postcss-calc 8.0.0 → 8.2.2

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/README.md CHANGED
@@ -1,12 +1,9 @@
1
1
  # PostCSS Calc [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
2
2
 
3
3
  [![NPM Version][npm-img]][npm-url]
4
- [![Build Status][cli-img]][cli-url]
5
4
  [![Support Chat][git-img]][git-url]
6
5
 
7
- [PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This
8
- can be particularly useful with the [PostCSS Custom Properties] plugin.
9
-
6
+ [PostCSS Calc] lets you reduce `calc()` references whenever it's possible.
10
7
  When multiple units are mixed together in the same expression, the `calc()`
11
8
  statement is left as is, to fallback to the [W3C calc() implementation].
12
9
 
@@ -34,61 +31,27 @@ var output = postcss()
34
31
  .css
35
32
  ```
36
33
 
37
- **Example** (with [PostCSS Custom Properties] enabled as well):
38
-
39
- ```js
40
- // dependencies
41
- var fs = require("fs")
42
- var postcss = require("postcss")
43
- var customProperties = require("postcss-custom-properties")
44
- var calc = require("postcss-calc")
45
-
46
- // css to be processed
47
- var css = fs.readFileSync("input.css", "utf8")
48
-
49
- // process css
50
- var output = postcss()
51
- .use(customProperties())
52
- .use(calc())
53
- .process(css)
54
- .css
55
- ```
56
-
57
34
  Using this `input.css`:
58
35
 
59
36
  ```css
60
- :root {
61
- --main-font-size: 16px;
62
- }
63
-
64
- body {
65
- font-size: var(--main-font-size);
66
- }
67
-
68
37
  h1 {
69
- font-size: calc(var(--main-font-size) * 2);
38
+ font-size: calc(16px * 2);
70
39
  height: calc(100px - 2em);
71
- margin-bottom: calc(
72
- var(--main-font-size)
73
- * 1.5
74
- )
40
+ width: calc(2*var(--base-width));
41
+ margin-bottom: calc(16px * 1.5);
75
42
  }
76
43
  ```
77
44
 
78
45
  you will get:
79
46
 
80
47
  ```css
81
- body {
82
- font-size: 16px
83
- }
84
-
85
48
  h1 {
86
49
  font-size: 32px;
87
50
  height: calc(100px - 2em);
51
+ width: calc(2*var(--base-width));
88
52
  margin-bottom: 24px
89
53
  }
90
54
  ```
91
-
92
55
  Checkout [tests] for more examples.
93
56
 
94
57
  ### Options
@@ -159,6 +122,9 @@ div[data-size="calc(3*3)"] {
159
122
 
160
123
  ---
161
124
 
125
+ ## Related PostCSS plugins
126
+ To replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].
127
+
162
128
  ## Contributing
163
129
 
164
130
  Work on a branch, install dev-dependencies, respect coding style & run tests
@@ -175,8 +141,6 @@ npm test
175
141
 
176
142
  ## [License](LICENSE)
177
143
 
178
- [cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg
179
- [cli-url]: https://travis-ci.org/postcss/postcss-calc
180
144
  [git-img]: https://img.shields.io/badge/support-chat-blue.svg
181
145
  [git-url]: https://gitter.im/postcss/postcss
182
146
  [npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
package/dist/index.js CHANGED
@@ -9,6 +9,15 @@ var _transform = _interopRequireDefault(require("./lib/transform"));
9
9
 
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
 
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
+ */
12
21
  function pluginCreator(opts) {
13
22
  const options = Object.assign({
14
23
  precision: 5,
@@ -20,6 +29,10 @@ function pluginCreator(opts) {
20
29
  return {
21
30
  postcssPlugin: 'postcss-calc',
22
31
 
32
+ /**
33
+ * @param {import('postcss').Root} css
34
+ * @param {{result: import('postcss').Result}} helpers
35
+ */
23
36
  OnceExit(css, {
24
37
  result
25
38
  }) {
@@ -4,6 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+
8
+ /**
9
+ * @type {{[key:string]: {[key:string]: number}}}
10
+ */
7
11
  const conversions = {
8
12
  // Absolute length units
9
13
  'px': {
@@ -129,6 +133,12 @@ const 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
144
  const sourceUnitNormalized = sourceUnit.toLowerCase();
@@ -145,7 +155,7 @@ function convertUnit(value, sourceUnit, targetUnit, precision) {
145
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,34 +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
- const type = node.type;
49
-
50
- if (isValueType(type)) {
51
- const itemIndex = collected.findIndex(x => x.node.type === type);
70
+ if (isValueType(node)) {
71
+ const itemIndex = collected.findIndex(x => x.node.type === node.type);
52
72
 
53
73
  if (itemIndex >= 0) {
54
74
  if (node.value === 0) {
55
75
  return;
56
- }
76
+ } // can cast because of the criterion used to find itemIndex
57
77
 
78
+
79
+ const otherValueNode =
80
+ /** @type import('../parser').ValueExpression*/
81
+ collected[itemIndex].node;
58
82
  const {
59
83
  left: reducedNode,
60
84
  right: current
61
- } = covertNodesUnits(collected[itemIndex].node, node, precision);
85
+ } = convertNodesUnits(otherValueNode, node, precision);
62
86
 
63
87
  if (collected[itemIndex].preOperator === '-') {
64
88
  collected[itemIndex].preOperator = '+';
@@ -99,7 +123,7 @@ function collectAddSubItems(preOperator, node, collected, precision) {
99
123
  });
100
124
  }
101
125
  }
102
- } else if (type === "MathExpression") {
126
+ } else if (node.type === "MathExpression") {
103
127
  if (isAddSubOperator(node.operator)) {
104
128
  collectAddSubItems(preOperator, node.left, collected, precision);
105
129
  const collectRightOperator = preOperator === '-' ? flip(node.operator) : node.operator;
@@ -117,6 +141,8 @@ function collectAddSubItems(preOperator, node, collected, precision) {
117
141
  });
118
142
  }
119
143
  }
144
+ } else if (node.type === 'ParenthesizedExpression') {
145
+ collectAddSubItems(preOperator, node.content, collected, precision);
120
146
  } else {
121
147
  collected.push({
122
148
  node,
@@ -124,22 +150,31 @@ function collectAddSubItems(preOperator, node, collected, precision) {
124
150
  });
125
151
  }
126
152
  }
153
+ /**
154
+ * @param {import('../parser').CalcNode} node
155
+ * @param {number} precision
156
+ */
157
+
127
158
 
128
159
  function reduceAddSubExpression(node, precision) {
160
+ /** @type Collectible[] */
129
161
  const collected = [];
130
162
  collectAddSubItems('+', node, collected, precision);
131
- const withoutZeroItem = collected.filter(item => !(isValueType(item.node.type) && item.node.value === 0));
163
+ const withoutZeroItem = collected.filter(item => !(isValueType(item.node) && item.node.value === 0));
132
164
  const firstNonZeroItem = withoutZeroItem[0]; // could be undefined
133
165
  // prevent producing "calc(-var(--a))" or "calc()"
134
166
  // which is invalid css
135
167
 
136
- if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node.type)) {
137
- const firstZeroItem = collected.find(item => isValueType(item.node.type) && item.node.value === 0);
138
- 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
+ }
139
174
  } // make sure the preOperator of the first item is +
140
175
 
141
176
 
142
- if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node.type)) {
177
+ if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node)) {
143
178
  withoutZeroItem[0].node.value *= -1;
144
179
  withoutZeroItem[0].preOperator = '+';
145
180
  }
@@ -157,9 +192,13 @@ function reduceAddSubExpression(node, precision) {
157
192
 
158
193
  return root;
159
194
  }
195
+ /**
196
+ * @param {import('../parser').MathExpression} node
197
+ */
198
+
160
199
 
161
200
  function reduceDivisionExpression(node) {
162
- if (!isValueType(node.right.type)) {
201
+ if (!isValueType(node.right)) {
163
202
  return node;
164
203
  }
165
204
 
@@ -168,7 +207,14 @@ function reduceDivisionExpression(node) {
168
207
  }
169
208
 
170
209
  return applyNumberDivision(node.left, node.right.value);
171
- } // 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
+ */
172
218
 
173
219
 
174
220
  function applyNumberDivision(node, divisor) {
@@ -176,7 +222,7 @@ function applyNumberDivision(node, divisor) {
176
222
  throw new Error('Cannot divide by zero');
177
223
  }
178
224
 
179
- if (isValueType(node.type)) {
225
+ if (isValueType(node)) {
180
226
  node.value /= divisor;
181
227
  return node;
182
228
  }
@@ -207,6 +253,10 @@ function applyNumberDivision(node, divisor) {
207
253
  }
208
254
  };
209
255
  }
256
+ /**
257
+ * @param {import('../parser').MathExpression} node
258
+ */
259
+
210
260
 
211
261
  function reduceMultiplicationExpression(node) {
212
262
  // (expr) * number
@@ -220,11 +270,17 @@ function reduceMultiplicationExpression(node) {
220
270
  }
221
271
 
222
272
  return node;
223
- } // 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
+ */
224
280
 
225
281
 
226
282
  function applyNumberMultiplication(node, multiplier) {
227
- if (isValueType(node.type)) {
283
+ if (isValueType(node)) {
228
284
  node.value *= multiplier;
229
285
  return node;
230
286
  }
@@ -255,8 +311,14 @@ function applyNumberMultiplication(node, multiplier) {
255
311
  }
256
312
  };
257
313
  }
314
+ /**
315
+ * @param {import('../parser').ValueExpression} left
316
+ * @param {import('../parser').ValueExpression} right
317
+ * @param {number} precision
318
+ */
258
319
 
259
- function covertNodesUnits(left, right, precision) {
320
+
321
+ function convertNodesUnits(left, right, precision) {
260
322
  switch (left.type) {
261
323
  case 'LengthValue':
262
324
  case 'AngleValue':
@@ -284,6 +346,20 @@ function covertNodesUnits(left, right, precision) {
284
346
  };
285
347
  }
286
348
  }
349
+ /**
350
+ * @param {import('../parser').ParenthesizedExpression} node
351
+ */
352
+
353
+
354
+ function includesNoCssProperties(node) {
355
+ return node.content.type !== 'Function' && (node.content.type !== 'MathExpression' || node.content.right.type !== 'Function' && node.content.left.type !== 'Function');
356
+ }
357
+ /**
358
+ * @param {import('../parser').CalcNode} node
359
+ * @param {number} precision
360
+ * @return {import('../parser').CalcNode}
361
+ */
362
+
287
363
 
288
364
  function reduce(node, precision) {
289
365
  if (node.type === "MathExpression") {
@@ -297,15 +373,21 @@ function reduce(node, precision) {
297
373
 
298
374
  switch (node.operator) {
299
375
  case "/":
300
- return reduceDivisionExpression(node, precision);
376
+ return reduceDivisionExpression(node);
301
377
 
302
378
  case "*":
303
- return reduceMultiplicationExpression(node, precision);
379
+ return reduceMultiplicationExpression(node);
304
380
  }
305
381
 
306
382
  return node;
307
383
  }
308
384
 
385
+ if (node.type === 'ParenthesizedExpression') {
386
+ if (includesNoCssProperties(node)) {
387
+ return reduce(node.content, precision);
388
+ }
389
+ }
390
+
309
391
  return node;
310
392
  }
311
393
 
@@ -10,6 +10,10 @@ const order = {
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) {
@@ -19,6 +23,13 @@ function round(value, prec) {
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) {
@@ -49,15 +60,29 @@ function stringify(node, prec) {
49
60
  }
50
61
 
51
62
  case 'Number':
52
- return round(node.value, prec);
63
+ return round(node.value, prec).toString();
53
64
 
54
65
  case 'Function':
55
- return node.value;
66
+ return node.value.toString();
67
+
68
+ case 'ParenthesizedExpression':
69
+ return `(${stringify(node.content, prec)})`;
56
70
 
57
71
  default:
58
72
  return round(node.value, prec) + node.unit;
59
73
  }
60
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
+
61
86
 
62
87
  function _default(calc, node, originalValue, options, result, item) {
63
88
  let str = stringify(node, options.precision);
@@ -19,12 +19,18 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
19
19
 
20
20
  // eslint-disable-next-line import/no-unresolved
21
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
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
 
@@ -36,24 +42,32 @@ function transformValue(value, options, result, item) {
36
42
 
37
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
60
  return (0, _postcssSelectorParser.default)(selectors => {
47
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,13 +75,37 @@ function transformSelector(value, options, result, item) {
61
75
  });
62
76
  }).processSync(value);
63
77
  }
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
+
64
85
 
65
86
  var _default = (node, property, options, result) => {
66
- const 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
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
110
  const clone = node.clone();
73
111
  clone[property] = value;
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": "8.0.0",
3
+ "version": "8.2.2",
4
4
  "description": "PostCSS plugin to reduce calc()",
5
5
  "keywords": [
6
6
  "css",
@@ -10,43 +10,37 @@
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": "^3.15.0",
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": "^7.0.0",
43
- "del-cli": "^3.0.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",
37
+ "eslint": "^8.5.0",
38
+ "eslint-plugin-import": "^2.25.3",
48
39
  "jison-gho": "^0.6.1-216",
49
- "postcss": "^8.2.2"
40
+ "postcss": "^8.2.2",
41
+ "rimraf": "^3.0.2",
42
+ "typescript": "^4.5.4",
43
+ "uvu": "^0.5.3"
50
44
  },
51
45
  "dependencies": {
52
46
  "postcss-selector-parser": "^6.0.2",
@@ -55,10 +49,11 @@
55
49
  "peerDependencies": {
56
50
  "postcss": "^8.2.2"
57
51
  },
58
- "ava": {
59
- "require": [
60
- "@babel/register",
61
- "@babel/polyfill"
62
- ]
63
- }
64
- }
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[![Support Chat][git-img]][git-url]\n\n[PostCSS Calc] lets you reduce `calc()` references whenever it's possible.\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\nUsing this `input.css`:\n\n```css\nh1 {\n font-size: calc(16px * 2);\n height: calc(100px - 2em);\n width: calc(2*var(--base-width));\n margin-bottom: calc(16px * 1.5);\n}\n```\n\nyou will get:\n\n```css\nh1 {\n font-size: 32px;\n height: calc(100px - 2em);\n width: calc(2*var(--base-width));\n margin-bottom: 24px\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## Related PostCSS plugins\nTo replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].\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[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,126 +0,0 @@
1
- # 8.0.0
2
-
3
- - Breaking: Updated PostCSS from v7.x to v8.x ([#125](https://github.com/postcss/postcss-calc/pull/125))
4
-
5
- # 7.0.5
6
-
7
- - Fixed: reduction
8
-
9
- # 7.0.4
10
-
11
- - Fixed: strips away important factors from multiplications in calc() ([#107](https://github.com/postcss/postcss-calc/issues/107))
12
-
13
- # 7.0.3
14
-
15
- - Fixed: substracted css-variable from zero ([#111](https://github.com/postcss/postcss-calc/issues/111))
16
-
17
- # 7.0.2
18
-
19
- - Fixed: incorrect reduction of subtraction from zero ([#88](https://github.com/postcss/postcss-calc/issues/88))
20
- - Fixed: doesn't remove calc for single function
21
- - Fixed: relax parser on unknown units ([#76](https://github.com/postcss/postcss-calc/issues/76))
22
- - Fixed: handle numbers with exponen composed ([#83](https://github.com/postcss/postcss-calc/pull/83))
23
- - Fixed: handle plus sign before value ([#79](https://github.com/postcss/postcss-calc/pull/79))
24
- - Fixed: better handle precision for nested calc ([#75](https://github.com/postcss/postcss-calc/pull/75))
25
- - Fixed: properly handle nested add and sub expression inside sub expression ([#64](https://github.com/postcss/postcss-calc/issues/64))
26
- - Fixed: handle uppercase units and functions ([#71](https://github.com/postcss/postcss-calc/pull/71))
27
- - Fixed: do not break `calc` with single var ([cssnano/cssnano#725](https://github.com/cssnano/cssnano/issues/725))
28
- - Updated: `postcss` to 7.0.27 (patch)
29
- - Updated: `postcss-selector-parser` to 6.0.2
30
- - Updated: `postcss-value-parser` to 4.0.2
31
-
32
- # 7.0.1
33
-
34
- - Updated: `postcss` to 7.0.2 (patch)
35
- - Updated: `postcss-selector-parser` to 5.0.0-rc.4 (patch)
36
- - Updated: `postcss-value-parser` to 3.3.1 (patch)
37
-
38
- # 7.0.0
39
-
40
- - Changed: Updated postcss-selector-parser to version 5.0.0-rc.3
41
- - Changed: Dropped reduce-css-calc as a dependency
42
- - Fixed: Support constant() and env() ([#42](https://github.com/postcss/postcss-calc/issues/42), [#48](https://github.com/postcss/postcss-calc/issues/48))
43
- - Fixed: Support custom properties with "calc" in its name ([#50](https://github.com/postcss/postcss-calc/issues/50))
44
- - Fixed: Remove unnecessary whitespace around `*` and `/` ([cssnano#625](https://github.com/cssnano/cssnano/issues/625))
45
- - Fixed: Arithmetic bugs around subtraction ([#49](https://github.com/postcss/postcss-calc/issues/49))
46
- - Fixed: Handling of nested calc statements ([reduce-css-calc#49](https://github.com/MoOx/reduce-css-calc/issues/49))
47
- - Fixed: Bugs regarding complex calculations ([reduce-cs-calc#45](https://github.com/MoOx/reduce-css-calc/issues/45))
48
- - Fixed: `100%` incorrectly being transformed to `1` ([reduce-css-calc#44](https://github.com/MoOx/reduce-css-calc/issues/44))
49
- - Added: support for case-insensitive calc statements
50
-
51
- # 6.0.2 - 2018-09-25
52
-
53
- - Fixed: use PostCSS 7 (thanks to @douglasduteil)
54
-
55
- # 6.0.1 - 2017-10-10
56
-
57
- - Fixed: throwing error for attribute selectors without a value
58
-
59
- # 6.0.0 - 2017-05-08
60
-
61
- - Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x
62
- to v2.x (thanks to @andyjansson).
63
-
64
- # 5.3.1 - 2016-08-22
65
-
66
- - Fixed: avoid security issue related to ``reduce-css-calc@< 1.2.4``.
67
-
68
- # 5.3.0 - 2016-07-11
69
-
70
- - Added: support for selector transformation via `selectors` option.
71
- ([#29](https://github.com/postcss/postcss-calc/pull/29) - @uniquegestaltung)
72
-
73
- # 5.2.1 - 2016-04-10
74
-
75
- - Fixed: support for multiline value
76
- ([#27](https://github.com/postcss/postcss-calc/pull/27))
77
-
78
- # 5.2.0 - 2016-01-08
79
-
80
- - Added: "mediaQueries" option for `@media` support
81
- ([#22](https://github.com/postcss/postcss-calc/pull/22))
82
-
83
- # 5.1.0 - 2016-01-07
84
-
85
- - Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value
86
- ([#20](https://github.com/postcss/postcss-calc/pull/20))
87
-
88
- # 5.0.0 - 2015-08-25
89
-
90
- - Removed: compatibility with postcss v4.x
91
- - Added: compatibility with postcss v5.x
92
-
93
- # 4.1.0 - 2015-04-09
94
-
95
- - Added: compatibility with postcss v4.1.x ([#12](https://github.com/postcss/postcss-calc/pull/12))
96
-
97
- # 4.0.1 - 2015-04-09
98
-
99
- - Fixed: `preserve` option does not create duplicated values ([#7](https://github.com/postcss/postcss-calc/issues/7))
100
-
101
- # 4.0.0 - 2015-01-26
102
-
103
- - Added: compatibility with postcss v4.x
104
- - Changed: partial compatiblity with postcss v3.x (stack traces have lost filename)
105
-
106
- # 3.0.0 - 2014-11-24
107
-
108
- - Added: GNU like exceptions ([#4](https://github.com/postcss/postcss-calc/issues/4))
109
- - Added: `precision` option ([#5](https://github.com/postcss/postcss-calc/issues/5))
110
- - Added: `preserve` option ([#6](https://github.com/postcss/postcss-calc/issues/6))
111
-
112
- # 2.1.0 - 2014-10-15
113
-
114
- - Added: source of the error (gnu like message) (fix [#3](https://github.com/postcss/postcss-calc/issues/3))
115
-
116
- # 2.0.1 - 2014-08-10
117
-
118
- - Fixed: correctly ignore unrecognized values (fix [#2](https://github.com/postcss/postcss-calc/issues/2))
119
-
120
- # 2.0.0 - 2014-08-06
121
-
122
- - 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))
123
-
124
- # 1.0.0 - 2014-08-04
125
-
126
- ✨ 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))