Skip to content

Commit

Permalink
refactor: use flags from meta
Browse files Browse the repository at this point in the history
- instead of piping them everywhere
  • Loading branch information
idoros committed Dec 3, 2023
1 parent 00b0e12 commit 00b5f04
Show file tree
Hide file tree
Showing 12 changed files with 11 additions and 74 deletions.
1 change: 0 additions & 1 deletion packages/core-test-kit/src/generate-test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export function createTransformer(
replaceValueHook,
postProcessor,
mode: config.mode,
flags: { ...defaultFeatureFlags },
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/core/src/features/css-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ export const hooks = createFeature<{
if (selectorContext.originMeta === meta && symbol[`-st-states`]) {
// ToDo: refactor out to transformer validation phase
validateRuleStateDefinition(
context.flags,
selectorContext.selectorStr,
selectorContext.ruleOrAtRule,
context.meta,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/features/css-custom-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function addCSSProperty({
// already defined
return;
// eslint-disable-next-line no-constant-condition
} else if (context.meta.type === 'stylable' && context.flags.strictCustomProperty) {
} else if (context.meta.type === 'stylable' && context.meta.flags.strictCustomProperty) {
// strict mode
context.diagnostics.report(diagnostics.UNDEFINED_CSS_CUSTOM_PROP(name), {
node,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/features/css-pseudo-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const hooks = createFeature({
if (inferredState) {
if (selectorContext.transform) {
STCustomState.transformPseudoClassToCustomState(
context.flags,
inferredState.state,
inferredState.meta,
node.value,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/features/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export type SelectorNodeContext = [
export interface FeatureContext {
meta: StylableMeta;
diagnostics: Diagnostics;
flags: FeatureFlags;
}
export interface FeatureTransformContext extends FeatureContext {
resolver: StylableResolver;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/features/st-symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function addSymbol({
safeRedeclare = false,
localName,
}: {
context: Omit<FeatureContext, 'flags'>;
context: FeatureContext;
symbol: StylableSymbol;
node?: postcss.Node;
safeRedeclare?: boolean;
Expand All @@ -190,7 +190,7 @@ export function addSymbol({
return symbol;
}

export function reportRedeclare(context: Omit<FeatureContext, 'flags'>) {
export function reportRedeclare(context: FeatureContext) {
const { byNS } = plugableRecord.getUnsafe(context.meta.data, dataKey);
for (const symbols of Object.values(byNS)) {
const flat: Record<string, SymbolDeclaration[]> = {};
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/features/st-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export class StylablePublicApi {
const diagnostics = new Diagnostics();
const { outputValue, topLevelType, runtimeValue } = evaluator.evaluateValue(
{
flags: this.stylable.flags,
resolver: this.stylable.resolver,
evaluator,
meta,
Expand Down
9 changes: 1 addition & 8 deletions packages/core/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { replaceValueHook, RuntimeStVar, StylableTransformer } from './styl
import { getFormatterArgs, getStringValue, stringifyFunction } from './helpers/value';
import { unescapeCSS } from './helpers/escape';
import type { ParsedValue } from './types';
import type { FeatureFlags, FeatureTransformContext } from './features/feature';
import type { FeatureTransformContext } from './features/feature';
import { CSSCustomProperty, STVar } from './features';
import { unbox, CustomValueError } from './custom-values';

Expand Down Expand Up @@ -54,7 +54,6 @@ export class StylableEvaluator {
) {
return processDeclarationValue(
context.resolver,
context.flags,
this.getResolvedSymbols,
data.value,
data.meta,
Expand Down Expand Up @@ -99,7 +98,6 @@ export function resolveArgumentsValue(
for (const k in options) {
resolvedArgs[k] = evalDeclarationValue(
transformer.resolver,
transformer.flags,
unescapeCSS(options[k]),
meta,
node,
Expand All @@ -115,7 +113,6 @@ export function resolveArgumentsValue(

export function processDeclarationValue(
resolver: StylableResolver,
flags: FeatureFlags,
getResolvedSymbols: (meta: StylableMeta) => MetaResolvedSymbols,
value: string,
meta: StylableMeta,
Expand Down Expand Up @@ -147,7 +144,6 @@ export function processDeclarationValue(
evaluator,
getResolvedSymbols,
passedThrough,
flags,
},
data: {
value,
Expand Down Expand Up @@ -220,7 +216,6 @@ export function processDeclarationValue(
evaluator,
getResolvedSymbols,
passedThrough,
flags,
},
data: {
value,
Expand Down Expand Up @@ -303,7 +298,6 @@ export function processDeclarationValue(

export function evalDeclarationValue(
resolver: StylableResolver,
flags: FeatureFlags,
value: string,
meta: StylableMeta,
node?: postcss.Node,
Expand All @@ -319,7 +313,6 @@ export function evalDeclarationValue(
): string {
return processDeclarationValue(
resolver,
flags,
getResolvedSymbols,
value,
meta,
Expand Down
53 changes: 5 additions & 48 deletions packages/core/src/helpers/custom-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { CSSClass } from '../features';
import { reservedFunctionalPseudoClasses } from '../native-reserved-lists';
import { BaseAstNode, stringifyCSSValue } from '@tokey/css-value-parser';
import { findCustomIdent, findNextCallNode } from './css-value-seeker';
import type { FeatureFlags } from '../features/feature';

export interface MappedStates {
[s: string]: StateParsedValue | string | TemplateStateParsedValue | null;
Expand Down Expand Up @@ -764,7 +763,6 @@ export const systemValidators: Record<string, StateParamType> = {
};

export function validateRuleStateDefinition(
flags: FeatureFlags,
selector: string,
selectorNode: postcss.Rule | postcss.AtRule,
meta: StylableMeta,
Expand All @@ -787,7 +785,6 @@ export function validateRuleStateDefinition(
if (state && typeof state === 'object') {
const stateParam = isTemplateState(state) ? state.params[0] : state;
const { errors } = validateStateArgument(
flags,
stateParam,
meta,
stateParam.defaultValue || '',
Expand Down Expand Up @@ -825,7 +822,6 @@ export function validateRuleStateDefinition(
}

export function validateStateArgument(
flags: FeatureFlags,
stateAst: StateParsedValue,
meta: StylableMeta,
value: string,
Expand All @@ -837,7 +833,6 @@ export function validateStateArgument(
) {
const resolvedValidations: StateResult = {
res: resolveParam(
flags,
meta,
resolver,
diagnostics,
Expand All @@ -855,7 +850,7 @@ export function validateStateArgument(
const { errors } = validator.validate(
resolvedValidations.res,
stateAst.arguments,
resolveParam.bind(null, flags, meta, resolver, diagnostics, selectorNode),
resolveParam.bind(null, meta, resolver, diagnostics, selectorNode),
!!validateDefinition,
validateValue
);
Expand All @@ -871,7 +866,6 @@ export function validateStateArgument(
// transform

export function transformPseudoClassToCustomState(
flags: FeatureFlags,
stateDef: MappedStates[string],
meta: StylableMeta,
name: string,
Expand Down Expand Up @@ -903,7 +897,6 @@ export function transformPseudoClassToCustomState(
} else if (typeof stateDef === 'object') {
if (isTemplateState(stateDef)) {
convertTemplateState(
flags,
meta,
resolver,
diagnostics,
Expand All @@ -914,7 +907,6 @@ export function transformPseudoClassToCustomState(
);
} else {
resolveStateValue(
flags,
meta,
resolver,
diagnostics,
Expand Down Expand Up @@ -953,7 +945,6 @@ export function resolveStateParam(param: string, escape = false) {
return escape ? cssesc(`s` + result, { isIdentifier: true }).slice(1) : result;
}
function convertTemplateState(
flags: FeatureFlags,
meta: StylableMeta,
resolver: StylableResolver,
diagnostics: Diagnostics,
Expand All @@ -964,7 +955,6 @@ function convertTemplateState(
) {
const paramStateDef = stateParamDef.params[0];
const resolvedParam = getParamInput(
flags,
meta,
resolver,
diagnostics,
Expand All @@ -974,16 +964,7 @@ function convertTemplateState(
name
);

validateParam(
flags,
meta,
resolver,
diagnostics,
selectorNode,
paramStateDef,
resolvedParam,
name
);
validateParam(meta, resolver, diagnostics, selectorNode, paramStateDef, resolvedParam, name);

const strippedParam = stripQuotation(resolvedParam);
transformMappedStateWithParam({
Expand All @@ -996,7 +977,6 @@ function convertTemplateState(
});
}
function getParamInput(
flags: FeatureFlags,
meta: StylableMeta,
resolver: StylableResolver,
diagnostics: Diagnostics,
Expand All @@ -1008,7 +988,6 @@ function getParamInput(
const inputValue =
stateNode.nodes && stateNode.nodes.length ? stringifySelector(stateNode.nodes) : ``;
const resolvedParam = resolveParam(
flags,
meta,
resolver,
diagnostics,
Expand All @@ -1025,7 +1004,6 @@ function getParamInput(
return resolvedParam;
}
function validateParam(
flags: FeatureFlags,
meta: StylableMeta,
resolver: StylableResolver,
diagnostics: Diagnostics,
Expand All @@ -1041,7 +1019,7 @@ function validateParam(
stateParamOutput = validator.validate(
resolvedParam,
stateParamDef.arguments,
resolveParam.bind(null, flags, meta, resolver, diagnostics, selectorNode),
resolveParam.bind(null, meta, resolver, diagnostics, selectorNode),
false,
true
);
Expand Down Expand Up @@ -1070,7 +1048,6 @@ function validateParam(
}
}
function resolveStateValue(
flags: FeatureFlags,
meta: StylableMeta,
resolver: StylableResolver,
diagnostics: Diagnostics,
Expand All @@ -1081,7 +1058,6 @@ function resolveStateValue(
namespace: string
) {
const resolvedParam = getParamInput(
flags,
meta,
resolver,
diagnostics,
Expand All @@ -1091,16 +1067,7 @@ function resolveStateValue(
name
);

validateParam(
flags,
meta,
resolver,
diagnostics,
selectorNode,
stateParamDef,
resolvedParam,
name
);
validateParam(meta, resolver, diagnostics, selectorNode, stateParamDef, resolvedParam, name);

const strippedParam = stripQuotation(resolvedParam);
convertToClass(stateNode).value = createStateWithParamClassName(name, namespace, strippedParam);
Expand Down Expand Up @@ -1209,7 +1176,6 @@ function validateTemplateSelector({
}

function resolveParam(
flags: FeatureFlags,
meta: StylableMeta,
resolver: StylableResolver,
diagnostics: Diagnostics,
Expand All @@ -1218,14 +1184,5 @@ function resolveParam(
) {
const defaultStringValue = '';
const param = nodeContent || defaultStringValue;
return evalDeclarationValue(
resolver,
flags,
param,
meta,
node,
undefined,
undefined,
diagnostics
);
return evalDeclarationValue(resolver, param, meta, node, undefined, undefined, diagnostics);
}
4 changes: 2 additions & 2 deletions packages/core/src/stylable-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export class StylableMeta {
constructor(
public sourceAst: postcss.Root,
public diagnostics: Diagnostics,
flags: FeatureFlags
public flags: FeatureFlags
) {
// initiate features
const context: FeatureContext = { meta: this, diagnostics, flags };
const context: FeatureContext = { meta: this, diagnostics };
for (const { hooks } of features) {
hooks.metaInit(context);
}
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/stylable-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import { validateCustomPropertyName } from './helpers/css-custom-property';
import type { ModuleResolver } from './types';
import { getRuleScopeSelector } from './deprecated/postcss-ast-extension';
import type { MappedStates } from './helpers/custom-state';
import type { FeatureFlags } from './features/feature';

export interface ResolvedElement {
name: string;
Expand Down Expand Up @@ -109,7 +108,6 @@ export interface TransformerOptions {
resolverCache?: StylableResolverCache;
stVarOverride?: Record<string, string>;
experimentalSelectorInference?: boolean;
flags: FeatureFlags;
}

export const transformerDiagnostics = {
Expand All @@ -136,7 +134,6 @@ export class StylableTransformer {
private directiveNodes: postcss.Declaration[] = [];
public experimentalSelectorInference: boolean;
public containerInferredSelectorMap = new Map<PostcssContainer, InferredSelector>();
public flags: FeatureFlags;
constructor(options: TransformerOptions) {
this.diagnostics = options.diagnostics;
this.keepValues = options.keepValues || false;
Expand All @@ -152,7 +149,6 @@ export class StylableTransformer {
);
this.mode = options.mode || 'production';
this.defaultStVarOverride = options.stVarOverride || {};
this.flags = options.flags;
this.getResolvedSymbols = createSymbolResolverWithCache(this.resolver, this.diagnostics);
this.evaluator = new StylableEvaluator({
valueHook: this.replaceValueHook,
Expand All @@ -176,7 +172,6 @@ export class StylableTransformer {
resolver: this.resolver,
evaluator: this.evaluator,
getResolvedSymbols: this.getResolvedSymbols,
flags: this.flags,
};
STImport.hooks.transformInit({ context });
STGlobal.hooks.transformInit({ context });
Expand Down Expand Up @@ -214,7 +209,6 @@ export class StylableTransformer {
getResolvedSymbols: this.getResolvedSymbols,
passedThrough: path.slice(),
inferredSelectorMixin,
flags: this.flags,
};
const transformResolveOptions = {
context: transformContext,
Expand Down Expand Up @@ -533,7 +527,6 @@ export class StylableTransformer {
resolver: this.resolver,
evaluator: this.evaluator,
getResolvedSymbols: this.getResolvedSymbols,
flags: this.flags,
};
if (node.type === 'class') {
CSSClass.hooks.transformSelectorNode({
Expand Down
Loading

0 comments on commit 00b5f04

Please sign in to comment.