postcss-calc 11.2.1 → 11.2.3

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.
Files changed (49) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +47 -35
  3. package/package.json +15 -15
  4. package/src/index.js +2 -1
  5. package/src/lib/analyze.js +0 -6
  6. package/src/lib/compile.js +1 -1
  7. package/src/lib/function-analyzers.js +119 -0
  8. package/src/lib/functions.js +25 -175
  9. package/src/lib/node.js +61 -4
  10. package/src/lib/parser/opaque.js +152 -0
  11. package/src/lib/parser/tokens.js +242 -0
  12. package/src/lib/parser.js +32 -377
  13. package/src/lib/serialize/expression.js +329 -0
  14. package/src/lib/serialize/precedence.js +47 -0
  15. package/src/lib/serialize/precision.js +244 -0
  16. package/src/lib/serialize.js +24 -561
  17. package/src/lib/simplify/bucket.js +22 -5
  18. package/src/lib/simplify/cancel.js +35 -11
  19. package/src/lib/simplify/clamp.js +2 -2
  20. package/src/lib/simplify/exact.js +18 -0
  21. package/src/lib/simplify/fold.js +15 -1
  22. package/src/lib/simplify/min-max.js +2 -2
  23. package/src/lib/simplify/product-barrier.js +55 -0
  24. package/src/lib/simplify/product-quotient.js +141 -0
  25. package/src/lib/simplify/product.js +92 -48
  26. package/src/lib/simplify/sum.js +6 -2
  27. package/src/lib/simplify.js +22 -5
  28. package/src/lib/types.js +94 -0
  29. package/src/reduce.js +2 -1
  30. package/types/lib/function-analyzers.d.ts +27 -0
  31. package/types/lib/functions.d.ts +3 -28
  32. package/types/lib/node.d.ts +17 -2
  33. package/types/lib/parser/opaque.d.ts +12 -0
  34. package/types/lib/parser/tokens.d.ts +83 -0
  35. package/types/lib/parser.d.ts +4 -44
  36. package/types/lib/serialize/expression.d.ts +55 -0
  37. package/types/lib/serialize/precedence.d.ts +18 -0
  38. package/types/lib/serialize/precision.d.ts +102 -0
  39. package/types/lib/simplify/bucket.d.ts +4 -1
  40. package/types/lib/simplify/cancel.d.ts +11 -7
  41. package/types/lib/simplify/exact.d.ts +9 -0
  42. package/types/lib/simplify/fold.d.ts +14 -1
  43. package/types/lib/simplify/product-barrier.d.ts +26 -0
  44. package/types/lib/simplify/product-quotient.d.ts +48 -0
  45. package/types/lib/simplify/product.d.ts +3 -7
  46. package/types/lib/simplify/sum.d.ts +2 -1
  47. package/types/lib/simplify.d.ts +3 -8
  48. package/types/lib/types.d.ts +48 -0
  49. package/types/lib/calculation-type.d.ts +0 -18
@@ -0,0 +1,48 @@
1
+ export type Node = import('../node.js').Node;
2
+ export type ProductFactor = import('../node.js').ProductFactor;
3
+ /**
4
+ * @param {{exponent: 1 | -1, value: number}[]} chain
5
+ * @return {number}
6
+ */
7
+ declare function chainValue(chain: {
8
+ exponent: 1 | -1;
9
+ value: number;
10
+ }[]): number;
11
+ /**
12
+ * Whether the opaque factors are a single Sum of only Num/Dim terms.
13
+ * @param {ProductFactor[]} opaque
14
+ * @return {boolean}
15
+ */
16
+ declare function isScalarSum(opaque: ProductFactor[]): boolean;
17
+ /**
18
+ * Whether a folded quotient prints exactly. A quotient distributed over a
19
+ * scalar Sum is judged term by term, so `(3px + 6em) / 3` still folds.
20
+ * @param {number} value
21
+ * @param {ProductFactor[]} opaque
22
+ * @param {boolean} distributable
23
+ * @param {number | false} precision
24
+ * @return {boolean}
25
+ */
26
+ declare function foldsExactly(value: number, opaque: ProductFactor[], distributable: boolean, precision: number | false): boolean;
27
+ /**
28
+ * Emit an inexact quotient as `numerator * dims * opaque / denominator`, with
29
+ * integer parts reduced by their gcd. A lone Dim absorbs the numerator.
30
+ * @param {number} numerator
31
+ * @param {number} denominator
32
+ * @param {{value: number, unit: string, rawUnit?: string} | null} loneDim
33
+ * @param {{exponent: 1 | -1, value: number, unit: string, rawUnit?: string}[]} dims
34
+ * @param {ProductFactor[]} opaque
35
+ * @param {number | false} precision
36
+ * @return {Node | null} null when a part would be rounded on output
37
+ */
38
+ declare function rationalProduct(numerator: number, denominator: number, loneDim: {
39
+ value: number;
40
+ unit: string;
41
+ rawUnit?: string;
42
+ } | null, dims: {
43
+ exponent: 1 | -1;
44
+ value: number;
45
+ unit: string;
46
+ rawUnit?: string;
47
+ }[], opaque: ProductFactor[], precision: number | false): Node | null;
48
+ export { chainValue, foldsExactly, isScalarSum, rationalProduct };
@@ -2,16 +2,12 @@ export type Node = import('../node.js').Node;
2
2
  export type Product = import('../node.js').Product;
3
3
  export type ProductFactor = import('../node.js').ProductFactor;
4
4
  export type SimplifyFn = import('../simplify.js').SimplifyFn;
5
- /**
6
- * @typedef {import('../node.js').Node} Node
7
- * @typedef {import('../node.js').Product} Product
8
- * @typedef {import('../node.js').ProductFactor} ProductFactor
9
- * @typedef {import('../simplify.js').SimplifyFn} SimplifyFn
10
- */
11
5
  /**
12
6
  * @param {Product} product
13
7
  * @param {SimplifyFn} simplify
8
+ * @param {number | false} [precision] A quotient that is not exact at this
9
+ * precision stays symbolic (`100% / 3`) instead of being rounded.
14
10
  * @return {Node}
15
11
  */
16
- declare function simplifyProduct(product: Product, simplify: SimplifyFn): Node;
12
+ declare function simplifyProduct(product: Product, simplify: SimplifyFn, precision?: number | false): Node;
17
13
  export { simplifyProduct };
@@ -6,7 +6,8 @@ export type UnitBucket = import('./bucket.js').UnitBucket;
6
6
  /**
7
7
  * @param {Sum} sum
8
8
  * @param {SimplifyFn} simplify
9
+ * @param {number | false} [precision]
9
10
  * @return {Node}
10
11
  */
11
- declare function simplifySum(sum: Sum, simplify: SimplifyFn): Node;
12
+ declare function simplifySum(sum: Sum, simplify: SimplifyFn, precision?: number | false): Node;
12
13
  export { simplifySum };
@@ -1,18 +1,13 @@
1
1
  export type Node = import('./node.js').Node;
2
2
  export type SimplifyFn = (node: Node) => Node;
3
- /**
4
- * @typedef {import('./node.js').Node} Node
5
- *
6
- * Recursive simplifier reference, threaded into Sum/Product/Call/OpaqueCall. Lets
7
- * leaf fold modules avoid circular imports of the entry function.
8
- * @typedef {(node: Node) => Node} SimplifyFn
9
- */
10
3
  /**
11
4
  * Simplify is an independent, composable AST transformation. It may
12
5
  * synthesize canonical nodes while preserving the Node -> Node contract.
13
6
  * @param {Node} node
7
+ * @param {number | false} [precision] When set, divisions and unit
8
+ * conversions that are not exact at this precision stay symbolic.
14
9
  * @param {number} [depth]
15
10
  * @return {Node}
16
11
  */
17
- declare function simplify(node: Node, depth?: number): Node;
12
+ declare function simplify(node: Node, precision?: number | false, depth?: number): Node;
18
13
  export { simplify };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * `percent` marks a value that resolves in the same percentage context as its
3
+ * peers (a pure percentage). It is only produced at leaves, by abs(), and by
4
+ * homogeneous sums and calls — never by a product, where an unpaired
5
+ * percentage could no longer cancel against anything.
6
+ * @typedef {{kind: 'number'} | {kind: 'dimension', base: string | null} | {kind: 'unknown', percent?: true} | {kind: 'failure'}} CalculationType
7
+ */
8
+ export type CalculationType = {
9
+ kind: 'number';
10
+ } | {
11
+ kind: 'dimension';
12
+ base: string | null;
13
+ } | {
14
+ kind: 'unknown';
15
+ percent?: true;
16
+ } | {
17
+ kind: 'failure';
18
+ };
19
+ /** @type {CalculationType} */ declare const numberType: CalculationType;
20
+ /** @type {CalculationType} */ declare const unknownType: CalculationType;
21
+ /** @type {CalculationType} */ declare const percentageType: CalculationType;
22
+ /** @type {CalculationType} */ declare const failureType: CalculationType;
23
+ /** @param {CalculationType} type @return {boolean} */
24
+ declare function isFailure(type: CalculationType): boolean;
25
+ /** @param {CalculationType} type @return {boolean} */
26
+ declare function isPercentage(type: CalculationType): boolean;
27
+ /** @param {CalculationType} a @param {CalculationType} b @return {CalculationType} */
28
+ declare function addTypes(a: CalculationType, b: CalculationType): CalculationType;
29
+ /**
30
+ * Check an all-number function without assuming anything about an unresolved
31
+ * operand. A concrete dimension can never become a number, so it is still a
32
+ * definite error when another argument is opaque.
33
+ * @param {CalculationType[]} args
34
+ * @param {number} min
35
+ * @param {number} max
36
+ * @return {CalculationType}
37
+ */
38
+ declare function numberArguments(args: CalculationType[], min: number, max: number): CalculationType;
39
+ /**
40
+ * Check values that must share a calculation type. Unknown operands remain
41
+ * unknown: they might resolve to the concrete type required by their peers.
42
+ * @param {CalculationType[]} args
43
+ * @param {number} min
44
+ * @param {number} max
45
+ * @return {CalculationType}
46
+ */
47
+ declare function matchingArguments(args: CalculationType[], min: number, max: number): CalculationType;
48
+ export { addTypes, failureType, isFailure, isPercentage, matchingArguments, numberArguments, numberType, percentageType, unknownType, };
@@ -1,18 +0,0 @@
1
- import { analyze } from './analyze.js';
2
- import { MAX_CALCULATION_DEPTH, checkCalculationDepth } from './limits.js';
3
- export type Node = import('./node.js').Node;
4
- export type CalculationType = {
5
- kind: 'number';
6
- } | {
7
- kind: 'dimension';
8
- base: string | null;
9
- } | {
10
- kind: 'unknown';
11
- } | {
12
- kind: 'failure';
13
- };
14
- /** @typedef {import('./node.js').Node} Node */
15
- /** @typedef {{kind: 'number'} | {kind: 'dimension', base: string | null} | {kind: 'unknown'} | {kind: 'failure'}} CalculationType */
16
- /** @param {Node} node @return {CalculationType} */
17
- declare function checkCalculationType(node: Node): CalculationType;
18
- export { MAX_CALCULATION_DEPTH, checkCalculationDepth, checkCalculationType, analyze, };