Skip to content

Commit c578d70

Browse files
committed
Expose reusable utilities outside motion migration
1 parent ff614cd commit c578d70

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

polaris-migrator/src/migrations/replace-sass-motion/replace-sass-motion.ts

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import type {FileInfo, API, Options} from 'jscodeshift';
22
import 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

95
import {POLARIS_MIGRATOR_COMMENT} from '../../constants';
106
import {
117
NamespaceOptions,
128
namespace,
139
isSassFunction,
1410
hasNumericOperator,
11+
isTransformableDuration,
12+
isPolarisVar,
1513
} from '../../utilities/sass';
1614
import {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 {}
124104
const 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

polaris-migrator/src/utilities/sass.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,30 @@ export function replaceRemFunction(
208208
},
209209
);
210210
}
211+
212+
/**
213+
* All transformable duration units. These values are used to determine
214+
* if a decl.value can be mapped to a Polaris custom property.
215+
*
216+
* Note: <time> is a dimension with 's' or 'ms' as the unit:
217+
* https://w3c.github.io/csswg-drafts/css-values-3/#time-value
218+
*/
219+
export const transformableDurationUnits = ['s', 'ms'];
220+
221+
export function isTransformableDuration(
222+
dimension: false | Dimension,
223+
): dimension is Dimension {
224+
if (!dimension) return false;
225+
226+
// Zero is the only unitless dimension our duration transforms support
227+
if (isUnitlessZero(dimension)) return true;
228+
229+
return transformableDurationUnits.includes(dimension.unit);
230+
}
231+
232+
export function isPolarisVar(node: Node): boolean {
233+
return (
234+
isSassFunction('var', node) &&
235+
(node.nodes?.[0]?.value ?? '').startsWith('--p-')
236+
);
237+
}

0 commit comments

Comments
 (0)