-
-
Notifications
You must be signed in to change notification settings - Fork 136
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 variables with tests #300
Merged
lukasoppermann
merged 14 commits into
lukasoppermann:main
from
JackHowa:fix-aliased-variables-with-tests
Jun 26, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
53211d9
fix aliased variables being emmited twice
a3fc702
Run build
JackHowa 18dcbc4
Update manifest.json with new design token name and menu formatting
JackHowa 0226b8a
Try to update id to publish new version
JackHowa 80e0840
Update manifest.json with new name and ID
JackHowa 0d6f382
Merge pull request #1 from JackHowa/issue-293-incorrect-mode-referenc…
JackHowa e20ccc7
style: Fix lint
JackHowa 1c06b79
Add handleVariableAlias utility function and test updated
JackHowa 8616a64
Add processAliasModes utility function and tests
JackHowa ef77190
Fix import path in standardTransformer.ts
JackHowa 252151e
Remove rename
JackHowa 29e63e6
Re-run build
JackHowa 03c8794
Merge remote-tracking branch 'og/main' into fix-aliased-variables-wit…
JackHowa 8028dc2
Fix spacing
JackHowa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { tokenExportKeyType } from '@typings/tokenExportKey' | ||
import { tokenTypes } from '@config/tokenTypes' | ||
|
||
import { getVariableTypeByValue } from '../../src/utilities/getVariableTypeByValue' | ||
import { changeNotation } from '../../src/utilities/changeNotation' | ||
|
||
function handleVariableAlias (variable, value, mode) { | ||
const resolvedAlias = figma.variables.getVariableById(value.id) | ||
const collection = figma.variables.getVariableCollectionById( | ||
resolvedAlias.variableCollectionId | ||
) | ||
return { | ||
description: variable.description || undefined, | ||
exportKey: tokenTypes.variables.key as tokenExportKeyType, | ||
category: getVariableTypeByValue( | ||
Object.values(resolvedAlias.valuesByMode)[0] | ||
), | ||
values: `{${collection.name.toLowerCase()}.${changeNotation( | ||
resolvedAlias.name, | ||
'/', | ||
'.' | ||
)}}`, | ||
|
||
// 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(), | ||
aliasMode: mode | ||
} | ||
} | ||
|
||
export default handleVariableAlias |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const processAliasModes = (variables) => { | ||
return variables.reduce((collector, variable) => { | ||
// only one mode will be passed in if any | ||
if (!variable.aliasMode) { | ||
collector.push(variable) | ||
|
||
return collector | ||
} | ||
|
||
// alias mode singular because only one is shown | ||
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.aliasMode | ||
delete variable.aliasCollectionName | ||
|
||
collector.push({ | ||
...variable, | ||
values: variable.values.replace( | ||
`{${aliasCollectionName}.`, | ||
`{${aliasCollectionName}.${aliasMode.name}.` | ||
) | ||
}) | ||
|
||
return collector | ||
}, []) | ||
} | ||
|
||
export default processAliasModes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import handleVariableAlias from "../../src/utilities/handleVariableAlias"; | ||
|
||
import { tokenExportKeyType } from "@typings/tokenExportKey"; | ||
import { tokenTypes } from "@config/tokenTypes"; | ||
|
||
import { getVariableTypeByValue } from "../../src/utilities/getVariableTypeByValue"; | ||
import { changeNotation } from "../../src/utilities/changeNotation"; | ||
|
||
jest.mock("../../src/utilities/getVariableTypeByValue", () => ({ | ||
getVariableTypeByValue: jest.fn(), | ||
})); | ||
|
||
jest.mock("../../src/utilities/changeNotation", () => ({ | ||
changeNotation: jest.fn(), | ||
})); | ||
|
||
describe("handleVariableAlias", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
beforeAll(() => { | ||
// @ts-ignore | ||
global.figma = { | ||
variables: { | ||
getVariableById: jest.fn(), | ||
getVariableCollectionById: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
it("should return the correct object", () => { | ||
const variable = { description: "test description" }; | ||
const value = { id: "test id" }; | ||
const resolvedAlias = { | ||
variableCollectionId: "test collection id", | ||
name: "test name", | ||
valuesByMode: { mode1: "value1" }, | ||
}; | ||
const collection = { | ||
name: "test collection name", | ||
modes: "test modes", | ||
}; | ||
|
||
// @ts-ignore | ||
global.figma.variables.getVariableById.mockReturnValue(resolvedAlias); | ||
|
||
// @ts-ignore | ||
getVariableTypeByValue.mockImplementation(() => "test category"); | ||
|
||
// @ts-ignore | ||
changeNotation.mockImplementation(() => "test notation"); | ||
|
||
// @ts-ignore | ||
global.figma.variables.getVariableCollectionById.mockReturnValue( | ||
collection | ||
); | ||
|
||
const result = handleVariableAlias(variable, value, 'passedInMode'); | ||
|
||
expect(result).toEqual({ | ||
description: "test description", | ||
exportKey: tokenTypes.variables.key as tokenExportKeyType, | ||
category: "test category", | ||
values: `{test collection name.test notation}`, | ||
aliasCollectionName: "test collection name", | ||
aliasMode: "passedInMode", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import processAliasModes from "../../src/utilities/processAliasModes"; | ||
|
||
describe("processAliasModes", () => { | ||
it("should return the same variables if they have no alias modes", () => { | ||
const variables = [ | ||
{ values: "{color.black}" }, | ||
]; | ||
const result = processAliasModes(variables); | ||
expect(result).toEqual(variables); | ||
}); | ||
|
||
it("should remove aliasModes and aliasCollectionName properties from the variables", () => { | ||
const variables = [ | ||
{ | ||
values: "{collection.}", | ||
aliasMode: "mode1", | ||
aliasCollectionName: "collection", | ||
}, | ||
]; | ||
const result = processAliasModes(variables); | ||
result.forEach((variable) => { | ||
expect(variable).not.toHaveProperty("aliasMode"); | ||
expect(variable).not.toHaveProperty("aliasCollectionName"); | ||
}); | ||
}); | ||
|
||
it("should match aliasCollectionName case-insensitively and return the alias collection name", () => { | ||
const variables = [ | ||
{ | ||
values: "{CollEctIon.}", | ||
aliasMode: "mode1", | ||
aliasCollectionName: "collection", | ||
}, | ||
]; | ||
const result = processAliasModes(variables); | ||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"values": "{CollEctIon.}", | ||
}, | ||
] | ||
`) | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had to update syntax so tests would recognize origin of mapping