1313// package.json in this directory is not the real package.json. Lint rule not smart enough.
1414import assert from 'assert' ;
1515// eslint-disable-next-line rulesdir/imports
16- import * as tokens from '@adobe/spectrum-tokens/dist/json/variables.json' ;
16+ import * as originalTokens from '@adobe/spectrum-tokens/dist/json/variables.json' ;
1717
18- export function getToken ( name : keyof typeof tokens ) : string {
18+ // This forces TSC to inline the token keys instead of leaving a dependency on it.
19+ function keys < T extends Record < string , any > > ( v : T ) : Record < keyof T , any > {
20+ return v ;
21+ }
22+
23+ const tokens = keys ( originalTokens ) ;
24+ type TokenName = keyof typeof tokens ;
25+
26+ export function getToken ( name : TokenName ) : string {
1927 return ( tokens [ name ] as any ) . value ;
2028}
2129
@@ -26,7 +34,7 @@ export interface ColorToken {
2634 forcedColors ?: string
2735}
2836
29- export function colorToken ( name : keyof typeof tokens ) : ColorToken | ColorRef {
37+ export function colorToken ( name : TokenName ) : ColorToken | ColorRef {
3038 let token = tokens [ name ] as typeof tokens [ 'gray-25' ] | typeof tokens [ 'neutral-content-color-default' ] ;
3139 if ( 'ref' in token ) {
3240 return {
@@ -43,7 +51,7 @@ export function colorToken(name: keyof typeof tokens): ColorToken | ColorRef {
4351 } ;
4452}
4553
46- export function rawColorToken ( name : keyof typeof tokens ) : string {
54+ export function rawColorToken ( name : TokenName ) : string {
4755 let token = tokens [ name ] as typeof tokens [ 'gray-25' ] ;
4856 return `light-dark(${ token . sets . light . value } , ${ token . sets . dark . value } )` ;
4957}
@@ -55,7 +63,7 @@ export interface ColorRef {
5563 forcedColors ?: string
5664}
5765
58- export function weirdColorToken ( name : keyof typeof tokens ) : ColorRef {
66+ export function weirdColorToken ( name : TokenName ) : ColorRef {
5967 let token = tokens [ name ] as typeof tokens [ 'accent-background-color-default' ] ;
6068 return {
6169 type : 'ref' ,
@@ -66,18 +74,18 @@ export function weirdColorToken(name: keyof typeof tokens): ColorRef {
6674
6775type ReplaceColor < S extends string > = S extends `${infer S } -color-${infer N } ` ? `${S } -${N } ` : S ;
6876
69- export function colorScale < S extends string > ( scale : S ) : Record < ReplaceColor < Extract < keyof typeof tokens , `${S } -${number } `> > , ReturnType < typeof colorToken > > {
77+ export function colorScale < S extends string > ( scale : S ) : Record < ReplaceColor < Extract < TokenName , `${S } -${number } `> > , ReturnType < typeof colorToken > > {
7078 let res : any = { } ;
7179 let re = new RegExp ( `^${ scale } -\\d+$` ) ;
7280 for ( let token in tokens ) {
7381 if ( re . test ( token ) ) {
74- res [ token . replace ( '-color' , '' ) ] = colorToken ( token as keyof typeof tokens ) ;
82+ res [ token . replace ( '-color' , '' ) ] = colorToken ( token as TokenName ) ;
7583 }
7684 }
7785 return res ;
7886}
7987
80- export function simpleColorScale < S extends string > ( scale : S ) : Record < Extract < keyof typeof tokens , `${S } -${number } `> , string > {
88+ export function simpleColorScale < S extends string > ( scale : S ) : Record < Extract < TokenName , `${S } -${number } `> , string > {
8189 let res : any = { } ;
8290 let re = new RegExp ( `^${ scale } -\\d+$` ) ;
8391 for ( let token in tokens ) {
@@ -156,10 +164,10 @@ const indexes = {
156164} ;
157165
158166/** Returns the index of a font token relative to font-size-100 (which is index 0). */
159- export function fontSizeToken ( name : keyof typeof tokens ) : number {
167+ export function fontSizeToken ( name : TokenName ) : number {
160168 let token = tokens [ name ] as typeof tokens [ 'font-size-100' ] | typeof tokens [ 'heading-size-m' ] ;
161169 if ( 'ref' in token ) {
162- name = token . ref . slice ( 1 , - 1 ) as keyof typeof tokens ;
170+ name = token . ref . slice ( 1 , - 1 ) as TokenName ;
163171 }
164172
165173 let index = indexes [ name ] ;
0 commit comments