Skip to content

Commit 4703188

Browse files
committed
Use getFunctionArgs for color function
1 parent eaabd43 commit 4703188

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
NamespaceOptions,
88
namespace,
99
isSassFunction,
10+
getFunctionArgs,
11+
stripQuotes,
1012
StopWalkingFunctionNodes,
1113
} from '../../utilities/sass';
1214
import {isKeyOf} from '../../utilities/type-guards';
@@ -93,9 +95,9 @@ const plugin = (options: PluginOptions = {}): Plugin => {
9395

9496
// 2. Replace `color()` with variable
9597
if (node.value === namespacedColor) {
96-
const colorFnArgs = node.nodes.filter((node) => node.type !== 'div');
97-
const hue = colorFnArgs[0]?.value ?? '';
98-
const value = colorFnArgs[1]?.value ?? 'base';
98+
const colorFnArgs = getFunctionArgs(node);
99+
const hue = stripQuotes(colorFnArgs[0] ?? '');
100+
const value = stripQuotes(colorFnArgs[1] ?? 'base');
99101
const forBackground = colorFnArgs[2];
100102

101103
// Skip color() with for-background argument

polaris-migrator/src/utilities/sass.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ export function getFunctionArgs(node: FunctionNode): string[] {
179179
return args;
180180
}
181181

182+
/**
183+
* Removes surrounding quotes from a string
184+
* @example
185+
* const string = '"hello"';
186+
* stripQuotes(string); // hello
187+
*/
188+
export function stripQuotes(string: string) {
189+
return string.replace(/^['"]|['"]$/g, '');
190+
}
191+
182192
/**
183193
* All transformable dimension units. These values are used to determine
184194
* if a decl.value can be converted to pixels and mapped to a Polaris custom property.

0 commit comments

Comments
 (0)