Skip to content

Commit

Permalink
feat: strick js resolved type
Browse files Browse the repository at this point in the history
- change from any to unknown
- fix some types
  • Loading branch information
idoros committed Oct 1, 2023
1 parent 2379b63 commit d64bcdc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/features/st-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class StylablePublicApi {
name,
kind: 'js-func',
args: [],
func: resolvedSymbols.js[name].symbol,
func: resolvedSymbols.js[name].symbol as (...args: any[]) => any,
};
for (const arg of Object.values(data.options)) {
mixRef.args.push(arg.value);
Expand Down Expand Up @@ -374,10 +374,10 @@ function appendMixin(context: FeatureTransformContext, config: ApplyMixinContext
handleCSSMixin(context, config, resolveChain);
return;
} else if (resolvedType === `js`) {
const resolvedMixin = resolvedSymbols.js[symbolName];
if (typeof resolvedMixin.symbol === 'function') {
const jsValue = resolvedSymbols.js[symbolName].symbol;
if (typeof jsValue === 'function') {
try {
handleJSMixin(context, config, resolvedMixin.symbol);
handleJSMixin(context, config, jsValue as (...args: any[]) => any);
} catch (e) {
context.diagnostics.report(diagnostics.FAILED_TO_APPLY_MIXIN(String(e)), {
node: config.rule,
Expand Down
15 changes: 5 additions & 10 deletions packages/core/src/features/st-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,16 @@ function evaluateValueCall(
const type = resolvedSymbols.mainNamespace[varName];
if (type === `js`) {
const deepResolve = resolvedSymbols.js[varName];
const importedType = typeof deepResolve.symbol;
if (importedType === 'string') {
const jsValue = deepResolve.symbol;
if (typeof jsValue === 'string') {
parsedNode.resolvedValue = context.evaluator.valueHook
? context.evaluator.valueHook(
deepResolve.symbol,
varName,
false,
passedThrough
)
: deepResolve.symbol;
? context.evaluator.valueHook(jsValue, varName, false, passedThrough)
: jsValue;
} else if (node) {
// unsupported Javascript value
// ToDo: provide actual exported id (default/named as x)
context.diagnostics.report(
diagnostics.CANNOT_USE_JS_AS_VALUE(importedType, varName),
diagnostics.CANNOT_USE_JS_AS_VALUE(typeof jsValue, varName),
{
node,
word: varName,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ export function processDeclarationValue(
const formatter = resolvedSymbols.js[value];
const formatterArgs = getFormatterArgs(parsedNode);
try {
parsedNode.resolvedValue = formatter.symbol.apply(null, formatterArgs);
// ToDo: check if function instead of calling on a non function
parsedNode.resolvedValue = (formatter.symbol as Function)(...formatterArgs);
if (evaluator.valueHook && typeof parsedNode.resolvedValue === 'string') {
parsedNode.resolvedValue = evaluator.valueHook(
parsedNode.resolvedValue,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/stylable-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export type CSSResolvePath = Array<CSSResolve<ClassSymbol | ElementSymbol>>;

export interface JSResolve {
_kind: 'js';
symbol: any;
symbol: unknown;
meta: null;
}

Expand Down Expand Up @@ -390,7 +390,7 @@ export class StylableResolver {
meta,
name,
diagnostics,
deepResolved as CSSResolve
deepResolved as CSSResolve<ClassSymbol>
)
);
break;
Expand Down Expand Up @@ -534,7 +534,7 @@ function validateClassResolveExtends(
meta: StylableMeta,
name: string,
diagnostics: Diagnostics,
deepResolved: CSSResolve<StylableSymbol> | JSResolve | null
deepResolved: CSSResolve<ClassSymbol>
): ReportError | undefined {
return (res, extend) => {
const decl = findRule(meta.sourceAst, '.' + name);
Expand All @@ -557,7 +557,7 @@ function validateClassResolveExtends(
});
}
} else {
if (deepResolved?.symbol.alias) {
if (deepResolved.symbol.alias) {
meta.sourceAst.walkRules(new RegExp('\\.' + name), (rule) => {
diagnostics.report(CSSClass.diagnostics.UNKNOWN_IMPORT_ALIAS(name), {
node: rule,
Expand Down

0 comments on commit d64bcdc

Please sign in to comment.