From 2379b63f90ce9ccd89b11634bb27ef2cd944028f Mon Sep 17 00:00:00 2001 From: Ido Rosenthal Date: Sun, 1 Oct 2023 15:19:59 +0300 Subject: [PATCH] chore: handle cases where CSS symbols may be resolved --- packages/core/src/features/st-var.ts | 2 +- packages/core/src/index-internal.ts | 7 +++- .../src/lib/completion-providers.ts | 1 + packages/language-service/src/lib/provider.ts | 32 ++++++++----------- packages/webpack-extensions/src/types.ts | 5 +-- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/core/src/features/st-var.ts b/packages/core/src/features/st-var.ts index dc75f5d7f8..430cbe20a0 100644 --- a/packages/core/src/features/st-var.ts +++ b/packages/core/src/features/st-var.ts @@ -480,7 +480,7 @@ export function resolveReferencedVarNames( break; case 'import': { const resolved = context.resolver.deepResolve(symbol); - if (resolved?._kind === 'css' && resolved.symbol._kind === 'var') { + if (resolved?._kind === 'css' && resolved.symbol?._kind === 'var') { varsToCheck.push({ meta: resolved.meta, name: resolved.symbol.name }); } break; diff --git a/packages/core/src/index-internal.ts b/packages/core/src/index-internal.ts index b8361725ee..c769560520 100644 --- a/packages/core/src/index-internal.ts +++ b/packages/core/src/index-internal.ts @@ -47,7 +47,12 @@ export { EmitDiagnosticsContext, reportDiagnostic, } from './report-diagnostic'; -export { StylableResolver, StylableResolverCache } from './stylable-resolver'; +export { + StylableResolver, + StylableResolverCache, + isValidCSSResolve, + CSSResolveMaybe, +} from './stylable-resolver'; export { CacheItem, FileProcessor, cachedProcessFile, processFn } from './cached-process-file'; export { createStylableFileProcessor } from './create-stylable-processor'; export { packageNamespaceFactory } from './resolve-namespace-factories'; diff --git a/packages/language-service/src/lib/completion-providers.ts b/packages/language-service/src/lib/completion-providers.ts index fc18c8c0c2..94a7dbfee1 100644 --- a/packages/language-service/src/lib/completion-providers.ts +++ b/packages/language-service/src/lib/completion-providers.ts @@ -565,6 +565,7 @@ export const ExtendCompletionPlugin: LangServicePlugin = { return ( res && res._kind === 'css' && + res.symbol && (res.symbol._kind === 'class' || res.symbol._kind === 'element') ); }) diff --git a/packages/language-service/src/lib/provider.ts b/packages/language-service/src/lib/provider.ts index f9ae236d8e..96225a42eb 100644 --- a/packages/language-service/src/lib/provider.ts +++ b/packages/language-service/src/lib/provider.ts @@ -10,7 +10,6 @@ import { ImportSymbol, Stylable, StylableMeta, - JSResolve, Diagnostics, } from '@stylable/core'; import { @@ -18,6 +17,8 @@ import { StylableProcessor, STCustomSelector, MappedStates, + type StylableResolver, + isValidCSSResolve, } from '@stylable/core/dist/index-internal'; import type { Location, @@ -202,7 +203,7 @@ export class Provider { break; } case 'import': { - let resolved: CSSResolve | JSResolve | null = null; + let resolved: ReturnType = null; try { resolved = this.stylable.resolver.resolve(symbol); } catch { @@ -321,29 +322,24 @@ export class Provider { state: string ): CSSResolve | null { const importedSymbol = origMeta.getClass(elementName)![`-st-extends`]; - let res: CSSResolve | JSResolve | null = null; + let res: ReturnType = null; if (importedSymbol && importedSymbol._kind === 'import') { res = this.stylable.resolver.resolveImport(importedSymbol); } const localSymbol = origMeta.getSymbol(elementName)!; - if ( - res && - res._kind === 'css' && - Object.keys((res.symbol as ClassSymbol)[`-st-states`]!).includes(state) - ) { - return res; - } else if ( - res && - res._kind === 'css' && - (localSymbol._kind === 'class' || localSymbol._kind === 'element') && - localSymbol[`-st-extends`] - ) { - return this.findMyState(res.meta, res.symbol.name, state); - } else { - return null; + if (res && res._kind === 'css' && isValidCSSResolve(res)) { + if (Object.keys((res.symbol as ClassSymbol)[`-st-states`]!).includes(state)) { + return res; + } else if ( + (localSymbol._kind === 'class' || localSymbol._kind === 'element') && + localSymbol[`-st-extends`] + ) { + return this.findMyState(res.meta, res.symbol.name, state); + } } + return null; } public getSignatureHelp( diff --git a/packages/webpack-extensions/src/types.ts b/packages/webpack-extensions/src/types.ts index 2742092e5f..b9167ca7d7 100644 --- a/packages/webpack-extensions/src/types.ts +++ b/packages/webpack-extensions/src/types.ts @@ -1,4 +1,5 @@ -import type { CSSResolve, Imported, JSResolve, StylableMeta } from '@stylable/core'; +import type { Imported, JSResolve, StylableMeta } from '@stylable/core'; +import type { CSSResolveMaybe } from '@stylable/core/dist/index-internal'; export interface Metadata { entry: string; @@ -24,5 +25,5 @@ export type MetadataList = Array<{ export type ResolvedImport = { stImport: Imported; - resolved: CSSResolve | JSResolve | null; + resolved: CSSResolveMaybe | JSResolve | null; };