Skip to content

Commit

Permalink
Feature/duplicate tokens (#63)
Browse files Browse the repository at this point in the history
* Add detection for duplicated tokens name

* Changeset

* Lint
  • Loading branch information
Imar Abreu committed Jun 3, 2024
1 parent 34bb856 commit a7f3ff3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-pots-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@runroom/design-tokens': patch
---

Add detection for duplicate tokens name
39 changes: 37 additions & 2 deletions src/functions/getTokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FigmaComponent, FigmaFrame } from '@/types/figma';
import {
DesignTokensGenerator,
Token,
TokenCollection,
TokenPayload,
Tokens,
Expand All @@ -9,6 +10,7 @@ import {
import { snakeCase } from './stringManipulation.ts';
import { DESIGN_TOKENS, DesignPages } from '@/designTokensPages.ts';
import { validateFrameName } from './ensureType.ts';
import { logWarning } from '@/functions/logger.ts';

const treeParser = <T extends FigmaComponent>(frames: (FigmaFrame | FigmaComponent)[]): T[] => {
const components: T[] = [];
Expand Down Expand Up @@ -50,15 +52,48 @@ const buildPayload = <P extends TokenCollection>(payload: object, componentsKey:
} as P;
};

const mergeTokens = (payload: any, token: any): any | undefined => {
const isKeyDuplicated = <T extends TokenCollection, K extends Tokens>(
payload: T,
token: K,
key: string
) => Object.keys(payload).filter(payloadKey => payloadKey === (token[key] as any).name).length;

const renameDuplicatedToken = <T extends TokenCollection, K extends Tokens>(
payload: T,
token: K
) => {
let duplicatedToken = token;
Object.keys(payload).forEach(key => {
const payloadToken = payload[key] as any as Token;

if (payloadToken.name === (duplicatedToken[key] as Token).name) {
const duplicatedKey = `${key}-duplicate`;
(duplicatedToken[key] as Token).name = `${(duplicatedToken[key] as Token).name}-duplicate`;
duplicatedToken = { [duplicatedKey]: duplicatedToken[key] } as K;
}
});

return duplicatedToken;
};

const mergeTokens = <T extends TokenCollection, K extends Tokens>(payload: T, token: K): T => {
const key = Object.keys(token)[0];

if (!payload[key]) {
Object.assign(payload, token);

return payload;
}

const newPayload = mergeTokens(payload[key], token[key]);
if (isKeyDuplicated(payload, token, key)) {
logWarning(`Duplicated token name: ${(token[key] as Token).name}`);
const duplicatedToken = renameDuplicatedToken(payload, token);

Object.assign(payload, duplicatedToken);
return payload;
}

const newPayload: T = mergeTokens(payload[key] as T, token[key] as K);

return {
...payload,
Expand Down
3 changes: 2 additions & 1 deletion src/functions/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const EMOJIS = {
};

const log = (message: string, emoji = '') => {
const reset = '\x1b[0m';
// eslint-disable-next-line no-console
console.log(`\n${message} ${emoji}`);
console.info(`\n${reset}${emoji} ${message}\n`);
};

const logError = (message: string) => {
Expand Down

3 comments on commit a7f3ff3

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.❔
Category Percentage Covered / Total
🟑 Statements 69.73% 523/750
πŸ”΄ Branches 57.26% 138/241
🟑 Functions 66.67% 144/216
🟑 Lines 69.02% 479/694

Test suite run success

85 tests passing in 4 suites.

Report generated by πŸ§ͺjest coverage report action from a7f3ff3

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.❔
Category Percentage Covered / Total
🟑 Statements 69.73% 523/750
πŸ”΄ Branches 57.26% 138/241
🟑 Functions 66.67% 144/216
🟑 Lines 69.02% 479/694

Test suite run success

85 tests passing in 4 suites.

Report generated by πŸ§ͺjest coverage report action from a7f3ff3

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.❔
Category Percentage Covered / Total
🟑 Statements 69.73% 523/750
πŸ”΄ Branches 57.26% 138/241
🟑 Functions 66.67% 144/216
🟑 Lines 69.02% 479/694

Test suite run success

85 tests passing in 4 suites.

Report generated by πŸ§ͺjest coverage report action from a7f3ff3

Please sign in to comment.