Skip to content

Commit 9e808f3

Browse files
committed
Expose reusable utilities outside motion migration
1 parent c154bce commit 9e808f3

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
@@ -279,6 +279,33 @@ export function replaceRemFunction(
279279
);
280280
}
281281

282+
/**
283+
* All transformable duration units. These values are used to determine
284+
* if a decl.value can be mapped to a Polaris custom property.
285+
*
286+
* Note: <time> is a dimension with 's' or 'ms' as the unit:
287+
* https://w3c.github.io/csswg-drafts/css-values-3/#time-value
288+
*/
289+
export const transformableDurationUnits = ['s', 'ms'];
290+
291+
export function isTransformableDuration(
292+
dimension: false | Dimension,
293+
): dimension is Dimension {
294+
if (!dimension) return false;
295+
296+
// Zero is the only unitless dimension our duration transforms support
297+
if (isUnitlessZero(dimension)) return true;
298+
299+
return transformableDurationUnits.includes(dimension.unit);
300+
}
301+
302+
export function isPolarisVar(node: Node): boolean {
303+
return (
304+
isSassFunction('var', node) &&
305+
(node.nodes?.[0]?.value ?? '').startsWith('--p-')
306+
);
307+
}
308+
282309
export function createInlineComment(text: string, options?: {prose?: boolean}) {
283310
const formatted = prettier
284311
.format(text, {

0 commit comments

Comments
 (0)