1- import { getCssModuleFileName , isComponentFileName , STYLES_EXPORT_NAME } from '@css-modules-kit/core' ;
1+ import type { CMKConfig } from '@css-modules-kit/core' ;
2+ import { getCssModuleFileName , isComponentFileName , isCSSModuleFile , STYLES_EXPORT_NAME } from '@css-modules-kit/core' ;
23import ts from 'typescript' ;
34
45export function getCompletionsAtPosition (
56 languageService : ts . LanguageService ,
7+ config : CMKConfig ,
68) : ts . LanguageService [ 'getCompletionsAtPosition' ] {
79 return ( fileName , position , options , formattingSettings ) => {
810 const prior = languageService . getCompletionsAtPosition ( fileName , position , options , formattingSettings ) ;
11+ if ( ! prior ) return undefined ;
912
10- if ( isComponentFileName ( fileName ) && prior ) {
13+ if ( isComponentFileName ( fileName ) ) {
1114 const cssModuleFileName = getCssModuleFileName ( fileName ) ;
1215 for ( const entry of prior . entries ) {
1316 if ( isStylesEntryForCSSModuleFile ( entry , cssModuleFileName ) ) {
@@ -20,6 +23,20 @@ export function getCompletionsAtPosition(
2023 }
2124 }
2225 }
26+ if ( config . namedExports ) {
27+ // When namedExports is enabled, you can write code as follows:
28+ // ```ts
29+ // import { button } from './a.module.css';
30+ // const Button = () => <button className={button}>Click me!</button>;
31+ // ```
32+ // However, it is more common to use namespace imports for styles.
33+ // ```ts
34+ // import * as styles from './a.module.css';
35+ // const Button = () => <button className={styles.button}>Click me!</button>;
36+ // ```
37+ // Therefore, completion for tokens like `button` is disabled.
38+ prior . entries = prior . entries . filter ( ( entry ) => ! isTokenEntry ( entry ) ) ;
39+ }
2340 return prior ;
2441 } ;
2542}
@@ -38,6 +55,10 @@ function isStylesEntryForCSSModuleFile(entry: ts.CompletionEntry, cssModuleFileN
3855 ) ;
3956}
4057
58+ function isTokenEntry ( entry : ts . CompletionEntry ) {
59+ return entry . source && isCSSModuleFile ( entry . source ) ;
60+ }
61+
4162function isClassNamePropEntry ( entry : ts . CompletionEntry ) {
4263 return (
4364 entry . name === 'className' &&
0 commit comments