Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix aliased variables being emmited twice #289

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/utilities/getVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getVariableTypeByValue } from './getVariableTypeByValue'
import roundWithDecimals from './roundWithDecimals'
import { Settings } from '@typings/settings'

const extractVariable = (variable, value) => {
const extractVariable = (variable, value, mode) => {
let category: tokenCategoryType = 'color'
let values = {}
if (value.type === 'VARIABLE_ALIAS') {
Expand All @@ -25,7 +25,7 @@ const extractVariable = (variable, value) => {
// this is being stored so we can properly update the design tokens later to account for all
// modes when using aliases
aliasCollectionName: collection.name.toLowerCase(),
aliasModes: collection.modes
aliasMode: mode
}
}
switch (variable.resolvedType) {
Expand Down Expand Up @@ -64,24 +64,22 @@ const extractVariable = (variable, value) => {
const processAliasModes = (variables) => {
return variables.reduce((collector, variable) => {
// nothing needs to be done to variables that have no alias modes, or only one mode
if (!variable.aliasModes || variable.aliasModes.length < 2) {
if (!variable.aliasMode) {
collector.push(variable)

return collector
}

const { aliasModes, aliasCollectionName } = variable
const { aliasMode, aliasCollectionName } = variable

// this was only added for this function to process that data so before we return the variables, we can remove it
delete variable.aliasModes
delete variable.aliasMode
delete variable.aliasCollectionName

for (const aliasMode of aliasModes) {
const modeBasedVariable = { ...variable }
modeBasedVariable.values = modeBasedVariable.values.replace(new RegExp(`({${aliasCollectionName}.)`, "i"), `{${aliasCollectionName}.${aliasMode.name}.`)

collector.push(modeBasedVariable)
}
collector.push({
...variable,
values: variable.values.replace(new RegExp(`({${aliasCollectionName}.)`, "i"), `{${aliasCollectionName}.${aliasMode.name}.`)
});

return collector
}, [])
Expand All @@ -101,7 +99,7 @@ export const getVariables = (figma: PluginAPI, settings: Settings) => {
// Only add mode if there's more than one
let addMode = settings.modeReference && modes.length > 1
return {
...extractVariable(variable, value),
...extractVariable(variable, value, modes.find(({ modeId }) => modeId === id)),
// name is contstructed from collection, mode and variable name

name: addMode ? `${collection}/${modes.find(({ modeId }) => modeId === id).name}/${variable.name}` : `${collection}/${variable.name}`,
Expand Down
Loading