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" ;
33
44// eslint-disable-next-line import/no-unresolved
5- import { parser } from ' ../parser' ;
5+ import { parser } from " ../parser" ;
66
7- import reducer from ' ./reducer' ;
8- import stringifier from ' ./stringifier' ;
7+ import reducer from " ./reducer" ;
8+ import stringifier from " ./stringifier" ;
99
1010const MATCH_CALC = / ( (?: - ( m o z | w e b k i t ) - ) ? c a l c ) / i;
1111
1212function 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+ }
1819
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 ) ;
2223
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 ) ;
2627
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+ ) ;
3638
37- return false ;
38- } ) . toString ( ) ;
39+ return false ;
40+ } )
41+ . toString ( ) ;
3942}
4043
4144function transformSelector ( value , options , result , item ) {
4245 return selectorParser ( selectors => {
4346 selectors . walk ( node => {
4447 // attribute value
4548 // 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 ) {
4750 node . setValue ( transformValue ( node . value , options , result , item ) ) ;
4851 }
4952
5053 // tag value
5154 // 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" ) {
5356 node . value = transformValue ( node . value , options , result , item ) ;
5457 }
5558
@@ -59,9 +62,18 @@ function transformSelector(value, options, result, item) {
5962}
6063
6164export 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+ }
6577
6678 // if the preserve option is enabled and the value has changed, write the
6779 // transformed value into a cloned node which is inserted before the current
0 commit comments