1
- import selectorParser from ' postcss-selector-parser' ;
2
- import valueParser from ' postcss-value-parser' ;
1
+ import selectorParser from " postcss-selector-parser" ;
2
+ import valueParser from " postcss-value-parser" ;
3
3
4
4
// eslint-disable-next-line import/no-unresolved
5
- import { parser } from ' ../parser' ;
5
+ import { parser } from " ../parser" ;
6
6
7
- import reducer from ' ./reducer' ;
8
- import stringifier from ' ./stringifier' ;
7
+ import reducer from " ./reducer" ;
8
+ import stringifier from " ./stringifier" ;
9
9
10
10
const MATCH_CALC = / ( (?: - ( m o z | w e b k i t ) - ) ? c a l c ) / i;
11
11
12
12
function transformValue ( value , options , result , item ) {
13
- return valueParser ( value ) . walk ( node => {
14
- // skip anything which isn't a calc() function
15
- if ( node . type !== 'function' || ! MATCH_CALC . test ( node . value ) ) {
16
- return node ;
17
- }
13
+ return valueParser ( value )
14
+ . walk ( node => {
15
+ // skip anything which isn't a calc() function
16
+ if ( node . type !== "function" || ! MATCH_CALC . test ( node . value ) ) {
17
+ return node ;
18
+ }
18
19
19
- // stringify calc expression and produce an AST
20
- const contents = valueParser . stringify ( node . nodes ) ;
21
- const ast = parser . parse ( contents ) ;
20
+ // stringify calc expression and produce an AST
21
+ const contents = valueParser . stringify ( node . nodes ) ;
22
+ const ast = parser . parse ( contents ) ;
22
23
23
- // reduce AST to its simplest form, that is, either to a single value
24
- // or a simplified calc expression
25
- const reducedAst = reducer ( ast , options . precision ) ;
24
+ // reduce AST to its simplest form, that is, either to a single value
25
+ // or a simplified calc expression
26
+ const reducedAst = reducer ( ast , options . precision ) ;
26
27
27
- // stringify AST and write it back
28
- node . type = 'word' ;
29
- node . value = stringifier (
30
- node . value ,
31
- reducedAst ,
32
- value ,
33
- options ,
34
- result ,
35
- item ) ;
28
+ // stringify AST and write it back
29
+ node . type = "word" ;
30
+ node . value = stringifier (
31
+ node . value ,
32
+ reducedAst ,
33
+ value ,
34
+ options ,
35
+ result ,
36
+ item
37
+ ) ;
36
38
37
- return false ;
38
- } ) . toString ( ) ;
39
+ return false ;
40
+ } )
41
+ . toString ( ) ;
39
42
}
40
43
41
44
function transformSelector ( value , options , result , item ) {
42
45
return selectorParser ( selectors => {
43
46
selectors . walk ( node => {
44
47
// attribute value
45
48
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
46
- if ( node . type === ' attribute' && node . value ) {
49
+ if ( node . type === " attribute" && node . value ) {
47
50
node . setValue ( transformValue ( node . value , options , result , item ) ) ;
48
51
}
49
52
50
53
// tag value
51
54
// e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
52
- if ( node . type === ' tag' ) {
55
+ if ( node . type === " tag" ) {
53
56
node . value = transformValue ( node . value , options , result , item ) ;
54
57
}
55
58
@@ -59,9 +62,18 @@ function transformSelector(value, options, result, item) {
59
62
}
60
63
61
64
export default ( node , property , options , result ) => {
62
- const value = property === "selector"
63
- ? transformSelector ( node [ property ] , options , result , node )
64
- : transformValue ( node [ property ] , options , result , node ) ;
65
+ let value = node [ property ] ;
66
+
67
+ try {
68
+ value =
69
+ property === "selector"
70
+ ? transformSelector ( node [ property ] , options , result , node )
71
+ : transformValue ( node [ property ] , options , result , node ) ;
72
+ } catch ( error ) {
73
+ result . warn ( error . message , { node } ) ;
74
+
75
+ return ;
76
+ }
65
77
66
78
// if the preserve option is enabled and the value has changed, write the
67
79
// transformed value into a cloned node which is inserted before the current
0 commit comments