Skip to content

Commit

Permalink
fix: precise css resolved symbol type
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros committed Oct 1, 2023
1 parent 2d2e5b4 commit 88f8be5
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/core/src/stylable-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,21 @@ export type CachedModuleEntity =

export type StylableResolverCache = Map<string, CachedModuleEntity>;

export interface CSSResolveMaybe<
T extends StylableSymbol | STStructure.PartSymbol = StylableSymbol
> {
_kind: 'css';
symbol: T | undefined;
meta: StylableMeta;
}
export interface CSSResolve<T extends StylableSymbol | STStructure.PartSymbol = StylableSymbol> {
_kind: 'css';
symbol: T;
meta: StylableMeta;
}
export function isValidCSSResolve(resolved: CSSResolveMaybe): resolved is CSSResolve {
return resolved.symbol !== undefined;
}
export type CSSResolvePath = Array<CSSResolve<ClassSymbol | ElementSymbol>>;

export interface JSResolve {
Expand All @@ -93,7 +103,7 @@ export interface MetaResolvedSymbols {
}

export type ReportError = (
res: CSSResolve | JSResolve | null,
res: CSSResolveMaybe | JSResolve | null,
extend: ImportSymbol | ClassSymbol | ElementSymbol,
extendPath: Array<CSSResolve<ClassSymbol | ElementSymbol>>,
meta: StylableMeta,
Expand Down Expand Up @@ -194,7 +204,7 @@ export class StylableResolver {
imported: Imported,
name: string,
subtype: 'mappedSymbols' | 'mappedKeyframes' | STSymbol.Namespaces = 'mappedSymbols'
): CSSResolve | JSResolve | null {
): CSSResolveMaybe | JSResolve | null {
const res = this.getModule(imported);
if (res.value === null) {
return null;
Expand Down Expand Up @@ -228,7 +238,7 @@ export class StylableResolver {
const name = importSymbol.type === 'named' ? importSymbol.name : '';
return this.resolveImported(importSymbol.import, name);
}
public resolve(maybeImport: StylableSymbol | undefined): CSSResolve | JSResolve | null {
public resolve(maybeImport: StylableSymbol | undefined): CSSResolveMaybe | JSResolve | null {
if (!maybeImport || maybeImport._kind !== 'import') {
if (
maybeImport &&
Expand Down Expand Up @@ -263,7 +273,7 @@ export class StylableResolver {
public deepResolve(
maybeImport: StylableSymbol | undefined,
path: StylableSymbol[] = []
): CSSResolve | JSResolve | null {
): CSSResolveMaybe | JSResolve | null {
let resolved = this.resolve(maybeImport);
while (
resolved &&
Expand Down Expand Up @@ -336,7 +346,7 @@ export class StylableResolver {
};
// resolve main namespace
for (const [name, symbol] of Object.entries(meta.getAllSymbols())) {
let deepResolved: CSSResolve | JSResolve | null;
let deepResolved: CSSResolveMaybe | JSResolve | null;
if (symbol._kind === `import` || (symbol._kind === `cssVar` && symbol.alias)) {
deepResolved = this.deepResolve(symbol);
if (!deepResolved || !deepResolved.symbol) {
Expand Down Expand Up @@ -368,14 +378,20 @@ export class StylableResolver {
} else {
deepResolved = { _kind: `css`, meta, symbol };
}
switch (deepResolved.symbol._kind) {
// deepResolved symbol is resolved by this point
switch (deepResolved.symbol!._kind) {
case `class`:
resolvedSymbols.class[name] = this.resolveExtends(
meta,
deepResolved.symbol,
deepResolved.symbol!,
false,
undefined,
validateClassResolveExtends(meta, name, diagnostics, deepResolved)
validateClassResolveExtends(
meta,
name,
diagnostics,
deepResolved as CSSResolve
)
);
break;
case `element`:
Expand All @@ -388,7 +404,7 @@ export class StylableResolver {
resolvedSymbols.cssVar[name] = deepResolved as CSSResolve<CSSVarSymbol>;
break;
}
resolvedSymbols.mainNamespace[name] = deepResolved.symbol._kind;
resolvedSymbols.mainNamespace[name] = deepResolved.symbol!._kind;
}
// resolve keyframes
for (const [name, symbol] of Object.entries(CSSKeyframes.getAll(meta))) {
Expand Down

0 comments on commit 88f8be5

Please sign in to comment.