Skip to content

Commit

Permalink
chore: handle cases where CSS symbols may be resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros committed Oct 1, 2023
1 parent 88f8be5 commit 2379b63
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/features/st-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/index-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions packages/language-service/src/lib/completion-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ export const ExtendCompletionPlugin: LangServicePlugin = {
return (
res &&
res._kind === 'css' &&
res.symbol &&
(res.symbol._kind === 'class' || res.symbol._kind === 'element')
);
})
Expand Down
32 changes: 14 additions & 18 deletions packages/language-service/src/lib/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
ImportSymbol,
Stylable,
StylableMeta,
JSResolve,
Diagnostics,
} from '@stylable/core';
import {
safeParse,
StylableProcessor,
STCustomSelector,
MappedStates,
type StylableResolver,
isValidCSSResolve,
} from '@stylable/core/dist/index-internal';
import type {
Location,
Expand Down Expand Up @@ -202,7 +203,7 @@ export class Provider {
break;
}
case 'import': {
let resolved: CSSResolve | JSResolve | null = null;
let resolved: ReturnType<StylableResolver['resolve']> = null;
try {
resolved = this.stylable.resolver.resolve(symbol);
} catch {
Expand Down Expand Up @@ -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<StylableResolver['resolveImport']> = 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(
Expand Down
5 changes: 3 additions & 2 deletions packages/webpack-extensions/src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,5 +25,5 @@ export type MetadataList = Array<{

export type ResolvedImport = {
stImport: Imported;
resolved: CSSResolve | JSResolve | null;
resolved: CSSResolveMaybe | JSResolve | null;
};

0 comments on commit 2379b63

Please sign in to comment.