@@ -10,13 +10,28 @@ export function getDefinitionAndBoundSpan(
1010 const result = languageService . getDefinitionAndBoundSpan ( ...args ) ;
1111 if ( ! result ) return ;
1212 if ( ! result . definitions ) return result ;
13+
14+ // Exclude re-exported tokens from `@value ... from '...'`; the real definition lives in the target file.
1315 result . definitions = result . definitions . filter ( ( def ) => {
1416 const script = language . scripts . get ( def . fileName ) ;
1517 if ( ! isCSSModuleScript ( script ) ) return true ;
16-
1718 const cssModule = script . generated . root [ CMK_DATA_KEY ] ;
19+ const defName = unquote ( def . name ) ;
20+ const importedValue = cssModule . tokenImporters
21+ . flatMap ( ( i ) => ( i . type === 'value' ? i . values : [ ] ) )
22+ . find ( ( v ) => {
23+ const localName = v . localName ?? v . name ;
24+ const localLoc = v . localLoc ?? v . loc ;
25+ return localName === defName && localLoc . start . offset === def . textSpan . start ;
26+ } ) ;
27+ return ! importedValue ;
28+ } ) ;
1829
19- // Search tokens and set `contextSpan`. `contextSpan` is used for Definition Preview in editors.
30+ // Set `contextSpan` for local tokens. `contextSpan` is used for Definition Preview in editors.
31+ for ( const def of result . definitions ) {
32+ const script = language . scripts . get ( def . fileName ) ;
33+ if ( ! isCSSModuleScript ( script ) ) continue ;
34+ const cssModule = script . generated . root [ CMK_DATA_KEY ] ;
2035 const defName = unquote ( def . name ) ;
2136 const localToken = cssModule . localTokens . find (
2237 ( t ) => t . name === defName && t . loc . start . offset === def . textSpan . start ,
@@ -26,20 +41,8 @@ export function getDefinitionAndBoundSpan(
2641 start : localToken . declarationLoc . start . offset ,
2742 length : localToken . declarationLoc . end . offset - localToken . declarationLoc . start . offset ,
2843 } ;
29- return true ;
3044 }
31- // Exclude re-exported tokens from `@value ... from '...'`; the real definition lives in the target file.
32- const importedValue = cssModule . tokenImporters
33- . flatMap ( ( i ) => ( i . type === 'value' ? i . values : [ ] ) )
34- . find ( ( v ) => {
35- const localName = v . localName ?? v . name ;
36- const localLoc = v . localLoc ?? v . loc ;
37- return localName === defName && localLoc . start . offset === def . textSpan . start ;
38- } ) ;
39- if ( importedValue ) return false ;
40-
41- return true ;
42- } ) ;
45+ }
4346 return result ;
4447 } ;
4548}
0 commit comments