Skip to content

feat: Add helpers and parser#67

Open
lebojo wants to merge 1 commit intofeat/ios/build-tokensfrom
feat/ios/helpers-parser
Open

feat: Add helpers and parser#67
lebojo wants to merge 1 commit intofeat/ios/build-tokensfrom
feat/ios/helpers-parser

Conversation

@lebojo
Copy link
Copy Markdown
Member

@lebojo lebojo commented Mar 24, 2026

This PR adds Helpers and parser that Style dictionary needs to work

@lebojo lebojo self-assigned this Mar 24, 2026
@github-actions github-actions bot requested review from lifaon74 and vjo March 24, 2026 12:38
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 24, 2026

✅ Storybook build successful

  • Decision: relevant files changed
  • Changed files inspected: 3
  • Workflow run: View details
  • Artifact: not uploaded (deploy succeeded)
  • Deployment: Open Storybook

Relevant files

  • packages/tokens/scripts/scripts/build-tokens/src/build/outputs/swift/helpers.ts
  • packages/tokens/scripts/scripts/build-tokens/src/build/outputs/swift/parser.ts
  • packages/tokens/scripts/scripts/build-tokens/src/build/outputs/swift/transforms.ts

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 24, 2026

PR Reviewer Guide 🔍

(Review updated until commit f71858c)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing Error Handling

The parser calls JSON.parse(contents) without a try-catch block. If a token file contains malformed JSON, this will throw an unhandled exception and crash the build process. Wrap the parsing logic in a try-catch to provide a meaningful error message indicating which file failed to parse.

const data = JSON.parse(contents);
Incorrect Array Handling

The check typeof val === 'object' returns true for arrays. If a top-level property is an array (e.g., a metadata field), the code will incorrectly add a $type property to the array object. Add !Array.isArray(val) to the condition to ensure only plain objects receive the inherited type.

if (val && typeof val === 'object' && !(val as any)['$type']) {
  (val as any)['$type'] = rootType;

Comment on lines +9 to +10
parser: ({ contents }: { filePath: string; contents: string }) => {
const data = JSON.parse(contents);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: Wrap JSON.parse(contents) in a try-catch block to handle malformed JSON files gracefully. This prevents the build process from crashing with an unhandled exception and allows for a more descriptive error message that includes the filePath. [possible issue, importance: 7]

Suggested change
parser: ({ contents }: { filePath: string; contents: string }) => {
const data = JSON.parse(contents);
parser: ({ contents, filePath }: { filePath: string; contents: string }) => {
let data: any;
try {
data = JSON.parse(contents);
} catch (e) {
throw new Error(`Failed to parse ${filePath}: ${e instanceof Error ? e.message : String(e)}`);
}

@lebojo lebojo force-pushed the feat/ios/helpers-parser branch from 1cf15e3 to f71858c Compare March 24, 2026 12:46
@github-actions
Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit f71858c

@github-actions
Copy link
Copy Markdown
Contributor

Failed to generate code suggestions for PR

Comment on lines +5 to +7
export const T1_DIRECTORY_NAME = 't1-primitive';
export const T2_DIRECTORY_NAME = 't2-semantic';
export const T3_DIRECTORY_NAME = 't3-component';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: these constants are already defined in /packages/tokens/scripts/scripts/build-tokens/src/constants/design-token-tiers.ts, could we re-use them?

@@ -0,0 +1,31 @@
import { AUTO_GENERATED_FILE_HEADER } from '../../constants/auto-generated-file-header.ts';

export const VARIABLE_PREFIX = 'esds';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: could we re-use or defined this constant in the same folder as CSS_VARIABLE_PREFIX, see packages/tokens/scripts/scripts/build-tokens/src/constants/css-variable-prefix.ts

* Name transform that replicates the exact naming logic from the custom DTCG framework.
*/
export const nameTransform: Transform = {
name: 'esds/name',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Question: should esds come from a constant?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think you're right!

import { segmentKebabCase } from './helpers.ts';

/**
* Name transform that replicates the exact naming logic from the custom DTCG framework.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Question: how is DTCG custom? It should be a standard, no?

Do you mean "custom parser" ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not, it's not a parser, it's for the output.
But I think it's an oversight of my part with testing cause actually SD have a Kebabcase transform built-in

* source files define different $type at their root level.
*/
export const fixTypeInheritanceParser = {
name: 'esds/fix-type-inheritance',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Question: should esds come from a constant?

@@ -0,0 +1,23 @@
/**
* Custom parser that moves root-level $type into each top-level group.
* This prevents SD5's merge from losing per-file $type when multiple
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Question: what does SD5 mean here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Style Dictionary 5

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* This prevents SD5's merge from losing per-file $type when multiple
* This prevents Style Dictionary's merge from losing per-file $type when multiple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants