11import type { FileInfo , API , Options } from 'jscodeshift' ;
22import postcss , { Plugin , Declaration , Helpers } from 'postcss' ;
3- import valueParser , {
4- ParsedValue ,
5- Node ,
6- FunctionNode ,
7- } from 'postcss-value-parser' ;
3+ import valueParser , { ParsedValue , Node } from 'postcss-value-parser' ;
84
95import { POLARIS_MIGRATOR_COMMENT } from '../../constants' ;
106import {
117 NamespaceOptions ,
128 namespace ,
139 isSassFunction ,
1410 hasNumericOperator ,
11+ isTransformableDuration ,
12+ isPolarisVar ,
1513} from '../../utilities/sass' ;
1614import { isKeyOf } from '../../utilities/type-guards' ;
1715
@@ -58,25 +56,7 @@ function normaliseStringifiedNumber(number: string): string {
5856 return Number ( number ) . toString ( ) ;
5957}
6058
61- function isValidConstantTimeUnit ( value : string ) : boolean {
62- const unit = valueParser . unit ( value ) ;
63- // 1. <time> is a dimension with 's' or 'ms' as the unit
64- // https://w3c.github.io/csswg-drafts/css-values-3/#time-value
65- return (
66- unit &&
67- ( [ 's' , 'ms' ] . includes ( unit . unit ) ||
68- ( ! unit . unit && Number ( unit . number ) === 0 ) )
69- ) ;
70- }
71-
72- function isPolarisCustomProperty ( node : Node ) : boolean {
73- return (
74- isSassFunction ( 'var' , node ) &&
75- ( node . nodes ?. [ 0 ] ?. value ?? '' ) . startsWith ( '--p-' )
76- ) ;
77- }
78-
79- function replaceNodeWithValue ( node : Node , value : string ) : void {
59+ function setNodeValue ( node : Node , value : string ) : void {
8060 const { sourceIndex} = node ;
8161 const parsedValue = valueParser ( value ) . nodes [ 0 ] ;
8262 Object . assign ( node , parsedValue ) ;
@@ -124,24 +104,20 @@ interface PluginOptions extends Options, NamespaceOptions {}
124104const plugin = ( options : PluginOptions = { } ) : Plugin => {
125105 const durationFunc = namespace ( 'duration' , options ) ;
126106
127- function isValidDurationFunction ( node : Node ) : node is FunctionNode {
128- return isSassFunction ( durationFunc , node ) ;
129- }
130-
131107 function mutateTransitionDurationValue (
132108 node : Node ,
133109 decl : ParsedValueDeclaration ,
134110 ) : boolean {
135- if ( isPolarisCustomProperty ( node ) ) {
111+ if ( isPolarisVar ( node ) ) {
136112 return true ;
137113 }
138114
139- if ( isValidDurationFunction ( node ) ) {
115+ if ( isSassFunction ( durationFunc , node ) ) {
140116 const duration = node . nodes [ 0 ] ?. value ?? DEFAULT_DURATION ;
141117
142118 if ( isKeyOf ( durationFuncMap , duration ) ) {
143119 const durationCustomProperty = durationFuncMap [ duration ] ;
144- replaceNodeWithValue ( node , `var(${ durationCustomProperty } )` ) ;
120+ setNodeValue ( node , `var(${ durationCustomProperty } )` ) ;
145121 } else {
146122 decl . before (
147123 postcss . comment ( {
@@ -162,7 +138,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
162138 if ( isKeyOf ( durationConstantsMap , constantDuration ) ) {
163139 const durationCustomProperty = durationConstantsMap [ constantDuration ] ;
164140
165- replaceNodeWithValue ( node , `var(${ durationCustomProperty } )` ) ;
141+ setNodeValue ( node , `var(${ durationCustomProperty } )` ) ;
166142 } else {
167143 decl . before (
168144 postcss . comment ( {
@@ -220,10 +196,8 @@ const plugin = (options: PluginOptions = {}): Plugin => {
220196 const timings : Node [ ] = [ ] ;
221197
222198 nodes . forEach ( ( node ) => {
223- if (
224- isValidConstantTimeUnit ( node . value ) ||
225- isValidDurationFunction ( node )
226- ) {
199+ const unit = valueParser . unit ( node . value ) ;
200+ if ( isTransformableDuration ( unit ) || isSassFunction ( durationFunc , node ) ) {
227201 timings . push ( node ) ;
228202 } else {
229203 // TODO: Try process it as an easing function
0 commit comments