-
Notifications
You must be signed in to change notification settings - Fork 55
Adding token analyzer logic and tests #371
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
Open
brandonthomas
wants to merge
76
commits into
microsoft:main
Choose a base branch
from
brandonthomas:token-analyzer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
351952d
scaffolding token-analyzer package
brandonthomas a1199bf
adding logic changes
brandonthomas 1309a0b
updating dev dep into root package.json
brandonthomas 55821b2
updating readme
brandonthomas 0a87658
fixing gen issues
brandonthomas 2a5b7c1
updating packages
brandonthomas 921bca4
add script to find the nearest tsconfig
brandonthomas 0266aa4
update todos
brandonthomas cb228f4
updating readme with root cause
brandonthomas f61813d
updating readme
brandonthomas cea1019
tracing root of issue
brandonthomas 771deec
fixed recursive nesting issue for correct data structure
brandonthomas 1cdf978
updated analysis file with more complete path data
brandonthomas 8f23bed
exclude our test-files from dep checks
brandonthomas b9a8943
updated todo
brandonthomas c163aaa
Fixing issue with nested spread calls within call expressions
brandonthomas 26ba585
adding make reset styles tests
brandonthomas a37f71e
makeStyles tests
brandonthomas 1561e6e
fixing additional recursion bug
brandonthomas 34d2230
updating test with new data
brandonthomas bf1476d
expanding tests to cover makeResetStyles
brandonthomas b5391f1
update logic in extractTokensFromText to get manage variable declarat…
brandonthomas 8645435
update todos
brandonthomas ee124cb
remove processNodeForAffectedProperties
brandonthomas 5d87eda
fix typings and update comments
brandonthomas 29f3454
updates to package.json
brandonthomas f0e8d1d
small fixes
brandonthomas b12cb76
remove usage of process.cwd
brandonthomas b4f0228
add small test
brandonthomas 3549e89
partial change
brandonthomas 59ac866
refactor token add logic
brandonthomas b5a4993
start of refactor token analysis path
brandonthomas 28838ca
additional refactors and clean up of logic.
brandonthomas ff6c6c4
move last functionality into token resolver
brandonthomas ad94063
additional clean up
brandonthomas 36f98f8
clean up files
brandonthomas d42e43f
move to nullish coalescing
brandonthomas 3a46640
additional code clean up and refactor
brandonthomas c12bd2c
additional refactor
brandonthomas 9ec8be3
processing module imports and marking as known token modules
brandonthomas ea3caea
mark tokens based on imports and symbol mapping
brandonthomas c195537
additional changes
brandonthomas a5eafea
additional fixes
brandonthomas c5669bf
hoist module check
brandonthomas 8f7c960
add additional tests for nested import/export types
brandonthomas 402c8fe
getting import/export values sorted
brandonthomas 5aed065
ensure default re-exports work
brandonthomas 6859506
Updating to use real semantic-tokens package
brandonthomas 151bb67
Adding template literal processing function
brandonthomas 13958dd
process template expressions properly in resolver
brandonthomas 349fde6
Update comment
brandonthomas 1bffe0d
more notes and tasks in todo
brandonthomas a5f6a40
refactoring import analyzer (partial)
brandonthomas 3bce4c0
moving analyzer flow diagram
brandonthomas 54cb420
clean up of unused types
brandonthomas d1ad55b
update css test
brandonthomas bddb0d2
process template expression imports
brandonthomas 3b2e180
Fixing deeply nested template expression values
brandonthomas 8fe96a8
fixing default export analysis
brandonthomas f20c401
fixing tsc error
brandonthomas a195c30
adding fixes for running as CLI
brandonthomas ac53d2e
match defaults to documentation
brandonthomas a3558e7
comment clean up
brandonthomas 82f944d
add yargs for better CLI experience
brandonthomas c8eb338
removing test output file. This will be generated by tests only now.
brandonthomas aeede41
deduping yargs types
brandonthomas bfa9c54
fixing bad merge.
brandonthomas a1184a2
fixing dedupe issues.
brandonthomas 9a4c583
updates to packages to avoid syncpack issues
brandonthomas 0bd6e9c
formatting files
brandonthomas 8dcf8e5
Merge branch 'main' into token-analyzer
brandonthomas ba6eb15
updating lock
brandonthomas 53b1fc7
Fixing CLI behavior and ensuring it runs correctly even when hitting …
brandonthomas 40598b8
export types.
brandonthomas 642e946
update todos and json output examples.
brandonthomas e022f0e
remove optional from `StyleCondition`
brandonthomas 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
This file contains hidden or 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 hidden or 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,4 @@ | ||
| { | ||
| "singleQuote": true, | ||
| "printWidth": 120 | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,11 +1,304 @@ | ||
| # token-analyzer | ||
| # Design Token Usage Analyzer | ||
|
|
||
brandonthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| This library was generated with [Nx](https://nx.dev). | ||
| A static analysis tool that scans your project's style files to track and analyze design token usage. The analyzer helps identify where and how design tokens are being used across your codebase, making it easier to maintain consistency and track token adoption. The data from this tool can also be used to create other tools like theme designers. | ||
|
|
||
| ## Building | ||
| ### How it works | ||
|
|
||
| Run `nx build token-analyzer` to build the library. | ||
| The tool first scans for the common pattern of `styles` or `style` being in the file name (along with common file extensions). From there it checks all imports to see if there are imports of known tokens. Currently, the list of known tokens and packages are internally maintained but we could easily expose this for extension libraries as well. See [knownTokenImportsAndModules](./src/types.ts#L65) in `types.ts` for the current list. This analysis isn't just done for direct imports but for any re-exports, new variable declarations, template expressions values, etc. This hopefully covers a wide range of scenarios the tool might encounter in code but it's possible there's more edge cases. Please report any issues you find so we can fix them and add new tests. Once this mapping is done, the tool scans for `makeStyles`, `makeResetStyles` and `mergeStyles` to build a comprehensive picture of what styles use which tokens, what meta data is considered when applying the styles and what properties they're applied to. As a result, this tool is targeted towards Griffel based styles for now. Since this tool works off the AST maps the usage of tokens and imports back to their symbols instead of just string analysis which we've found to be quite robust. Once analysis is complete, it outputs a JSON file with the mappings. By default it will produce a single analysis file for a given run. Multiple files are under an object key with their relative path within the JSON file. | ||
|
|
||
| ## Running unit tests | ||
| ## TODO | ||
|
|
||
| Run `nx test token-analyzer` to execute the unit tests via [Jest](https://jestjs.io). | ||
| - add config to point to custom prettier config for file output. | ||
| - add tests for findTsConfigPath | ||
| - Remove `extractTokensFromText` as we're only using it to help with `getPropertiesForShorthand`, we should leverage the existing analysis for this | ||
| - update contributing doc with info about version management | ||
| - Update test to return promise instead of async/await function. | ||
| - Add ability to customize glob used to find style files | ||
| - Add ability to add known tokens | ||
| - Read gitignore from target dir and use that for ignore if we find one. (currently hard coded). | ||
brandonthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - Add 'thorough' or 'complete' mode that doesn't filter files based on `style` or styles` in the name. | ||
| - | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install --save-dev @fluentui-contrib/token-analyzer | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ```bash | ||
| yarn add @fluentui-contrib/token-analyzer -D | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Command Line Interface | ||
|
|
||
| Run the style analysis tool: | ||
|
|
||
| ```bash | ||
| npm run analyze-tokens [options] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| | Option | Alias | Type | Default | Description | | ||
| | ----------- | ----- | ------- | ----------------------- | ------------------------------------- | | ||
| | `--root` | `-r` | string | `./src` | Root directory to analyze | | ||
| | `--output` | `-o` | string | `./token-analysis.json` | Output file path for results | | ||
| | `--debug` | `-d` | boolean | `false` | Enable debug mode for verbose logging | | ||
| | `--perf` | `-p` | boolean | `false` | Enable performance tracking | | ||
| | `--help` | `-h` | - | - | Show help information | | ||
| | `--version` | - | - | - | Show version number | | ||
|
|
||
| ### Examples | ||
|
|
||
| **Basic usage (uses defaults):** | ||
|
|
||
| ```bash | ||
| npm run analyze-tokens | ||
| ``` | ||
|
|
||
| **Custom directory and output:** | ||
|
|
||
| ```bash | ||
| npm run analyze-tokens -- --root ./components --output ./analysis-results.json | ||
| ``` | ||
|
|
||
| **With debugging and performance tracking:** | ||
|
|
||
| ```bash | ||
| npm run analyze-tokens -- --root ./src/components --debug --perf | ||
| ``` | ||
|
|
||
| ### Getting Help | ||
|
|
||
| View all available options and examples: | ||
|
|
||
| ```bash | ||
| npm run analyze-tokens --help | ||
| # or | ||
| npm run analyze-tokens -h | ||
| ``` | ||
|
|
||
| View version information: | ||
|
|
||
| ```bash | ||
| npm run analyze-tokens --version | ||
| ``` | ||
|
|
||
| ### Output | ||
|
|
||
| The tool will display progress information and a summary: | ||
|
|
||
| ``` | ||
| Starting analysis of ./src | ||
| Output will be written to ./token-analysis.json | ||
| Debug mode enabled | ||
| Performance tracking enabled | ||
|
|
||
| Analysis complete! | ||
| Processed 23 files containing styles | ||
| Found 156 token references | ||
| ``` | ||
|
|
||
| Results are saved as JSON to the specified output file, containing detailed analysis of each file's style usage and token references. | ||
|
|
||
| ### Programmatic Usage | ||
|
|
||
| ```typescript | ||
| import { analyzeProjectStyles } from '@fluentui-contrib/token-analyzer'; | ||
|
|
||
| async function analyze() { | ||
| const results = await analyzeProjectStyles('./src', './analysis.json', { | ||
| debug: true, | ||
| perf: true, | ||
| }); | ||
|
|
||
| console.log(`Analyzed ${Object.keys(results).length} files`); | ||
| } | ||
| ``` | ||
|
|
||
| ## Example JSON Output | ||
|
|
||
| Below is a simplification of styles output that the tool might produce. Note that the `assignedVariables` field corresponds to the key name under `styleConditions`. | ||
|
|
||
| ```json | ||
| { | ||
| "useButtonStyles.styles.ts": { | ||
| "styles": { | ||
| "useRootBaseClassName": { | ||
| "resetStyles": { | ||
| "tokens": [ | ||
| { | ||
| "property": "backgroundColor", | ||
| "token": ["tokens.colorNeutralBackground1"], | ||
| "path": ["backgroundColor"] | ||
| }, | ||
| { | ||
| "property": "color", | ||
| "token": ["semanticTokens.cornerFlyoutRest"], | ||
| "path": ["color"] | ||
| }, | ||
| { | ||
| "property": "border", | ||
| "token": ["tokens.strokeWidthThin"], | ||
| "path": ["border"] | ||
| }, | ||
| { | ||
| "property": "border", | ||
| "token": ["tokens.colorNeutralStroke1"], | ||
| "path": ["border"] | ||
| }, | ||
| { | ||
| "property": "fontFamily", | ||
| "token": ["textStyleAiHeaderFontfamily"], | ||
| "path": ["fontFamily"] | ||
| }, | ||
| { | ||
| "property": "padding", | ||
| "token": ["tokens.spacingHorizontalM"], | ||
| "path": ["padding"] | ||
| }, | ||
| { | ||
| "property": "borderRadius", | ||
| "token": ["tokens.borderRadiusMedium"], | ||
| "path": ["borderRadius"] | ||
| }, | ||
| { | ||
| "property": "fontSize", | ||
| "token": ["tokens.fontSizeBase300"], | ||
| "path": ["fontSize"] | ||
| }, | ||
| { | ||
| "property": "fontWeight", | ||
| "token": ["tokens.fontWeightSemibold"], | ||
| "path": ["fontWeight"] | ||
| }, | ||
| { | ||
| "property": "lineHeight", | ||
| "token": ["tokens.lineHeightBase300"], | ||
| "path": ["lineHeight"] | ||
| }, | ||
| { | ||
| "property": "transitionDuration", | ||
| "token": ["tokens.durationFaster"], | ||
| "path": ["transitionDuration"] | ||
| }, | ||
| { | ||
| "property": "transitionTimingFunction", | ||
| "token": ["tokens.curveEasyEase"], | ||
| "path": ["transitionTimingFunction"] | ||
| } | ||
| ], | ||
| "nested": { | ||
| "':hover'": { | ||
| "tokens": [ | ||
| { | ||
| "property": "backgroundColor", | ||
| "token": ["cornerCtrlLgHoverRaw"], | ||
| "path": ["':hover'", "backgroundColor"] | ||
| }, | ||
| { | ||
| "property": "borderColor", | ||
| "token": ["ctrlLinkForegroundBrandHover"], | ||
| "path": ["':hover'", "borderColor"] | ||
| }, | ||
| { | ||
| "property": "color", | ||
| "token": ["tokens.colorNeutralForeground1Hover"], | ||
| "path": ["':hover'", "color"] | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "isResetStyles": true, | ||
| "assignedVariables": ["rootBaseClassName"] | ||
| } | ||
| }, | ||
| "useRootDisabledStyles": { | ||
| "base": { | ||
| "tokens": [ | ||
| { | ||
| "property": "backgroundColor", | ||
| "token": ["tokens.colorNeutralBackgroundDisabled"], | ||
| "path": ["backgroundColor"] | ||
| }, | ||
| { | ||
| "property": "borderTopColor", | ||
| "token": ["tokens.colorNeutralStrokeDisabled"], | ||
| "path": ["borderTopColor"] | ||
| }, | ||
| { | ||
| "property": "borderRightColor", | ||
| "token": ["tokens.colorNeutralStrokeDisabled"], | ||
| "path": ["borderRightColor"] | ||
| }, | ||
| { | ||
| "property": "borderBottomColor", | ||
| "token": ["tokens.colorNeutralStrokeDisabled"], | ||
| "path": ["borderBottomColor"] | ||
| }, | ||
| { | ||
| "property": "borderLeftColor", | ||
| "token": ["tokens.colorNeutralStrokeDisabled"], | ||
| "path": ["borderLeftColor"] | ||
| }, | ||
| { | ||
| "property": "color", | ||
| "token": ["tokens.colorNeutralForegroundDisabled"], | ||
| "path": ["color"] | ||
| } | ||
| ], | ||
| "assignedVariables": ["rootDisabledStyles"] | ||
| } | ||
| } | ||
| }, | ||
| "metadata": { | ||
| "styleConditions": { | ||
| "rootBaseClassName": { | ||
| "isBase": true, | ||
| "slotName": "root" | ||
| }, | ||
| "rootDisabledStyles.base": { | ||
| "conditions": ["(disabled || disabledFocusable)"], | ||
| "slotName": "root" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| The analyzer identifies style files based on naming conventions. By default, it looks for: | ||
|
|
||
| - Files containing `style` or `styles` in the name | ||
brandonthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - Files with extensions: `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs` | ||
|
|
||
| ### Debug Configuration | ||
|
|
||
| Debug and performance tracking can be configured via: | ||
|
|
||
| 1. CLI flags (as shown above) | ||
| 2. Programmatic options when calling `analyzeProjectStyles` | ||
|
|
||
| ## Development | ||
|
|
||
| ### Running Tests | ||
|
|
||
| ```bash | ||
| npm test | ||
| ``` | ||
|
|
||
| ### Building | ||
|
|
||
| ```bash | ||
| npm run build | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under the MIT License - see the LICENSE file for details. | ||
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.