Skip to content

Commit

Permalink
refactor: inferred selector data structure
Browse files Browse the repository at this point in the history
- combine resolved selector data into a common type that can later be used to retrieve intersection resolve of multiple selectors
  • Loading branch information
idoros committed Feb 23, 2023
1 parent 7e7ea04 commit 2cf5cca
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 123 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/features/css-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ export const hooks = createFeature<{
// which might not have a definition for the mixed-in class
{ _kind: 'css', meta: originMeta, symbol: { _kind: 'class', name: node.value } },
];
selectorContext.setCurrentAnchor({ name: node.value, type: 'class', resolved });
selectorContext.setNodeResolve(node, resolved);
selectorContext.setNextSelectorScope(resolved, node, node.value);
const { symbol, meta } = getOriginDefinition(resolved);
if (selectorContext.originMeta === meta && symbol[`-st-states`]) {
// ToDo: refactor out to transformer validation phase
Expand Down Expand Up @@ -450,7 +449,7 @@ function handleDirectives(context: FeatureContext, decl: postcss.Declaration) {
const isSimplePerSelector = isSimpleSelector(rule.selector);
const type = isSimplePerSelector.reduce((accType, { type }) => {
return !accType ? type : accType !== type ? `complex` : type;
}, `` as (typeof isSimplePerSelector)[number]['type']);
}, `` as typeof isSimplePerSelector[number]['type']);
const isSimple = type !== `complex`;
if (decl.prop === `-st-states`) {
if (isSimple && type !== 'type') {
Expand Down
15 changes: 10 additions & 5 deletions packages/core/src/features/css-pseudo-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ export const diagnostics = {

export const hooks = createFeature({
transformSelectorNode({ context, selectorContext }) {
const { currentAnchor, node, rule, scopeSelectorAst } = selectorContext;
const { inferredSelector, node, rule, scopeSelectorAst } = selectorContext;
if (node.type !== 'pseudo_class') {
return;
}
// find matching custom state
const resolved = inferredSelector.getSingleResolve();
let foundCustomState = false;
for (const { symbol, meta } of currentAnchor.resolved) {
for (const { symbol, meta } of resolved) {
// Handle node resolve mapping for custom-selector.
// Currently custom selectors cannot get to this point in the process,
// due to them being replaced at the beginning of the transform process.
Expand All @@ -42,8 +43,9 @@ export const hooks = createFeature({
const mappedContext = selectorContext.createNestedContext(mappedSelectorAst);
// ToDo: wrap in :is() to get intersection of selectors
scopeSelectorAst(mappedContext);
if (mappedContext.currentAnchor) {
selectorContext.setNodeResolve(node, mappedContext.currentAnchor.resolved);
if (!mappedContext.inferredSelector.isEmpty()) {
// ToDo: support multi selector with: "selectorContext.multiSelectorScope"
selectorContext.setNextSelectorScope(mappedContext.inferredSelector, node); // doesn't add to the resolved elements
}
return; // this is not a state
}
Expand Down Expand Up @@ -77,7 +79,10 @@ export const hooks = createFeature({
const innerSelectors = (
node.nodes[0] && node.nodes[0].type === `nth` ? node.nodes.slice(1) : node.nodes
) as Selector[];
const nestedContext = selectorContext.createNestedContext(innerSelectors);
const nestedContext = selectorContext.createNestedContext(
innerSelectors,
selectorContext.inferredSelector
);
scopeSelectorAst(nestedContext);
/**
* ToDo: remove once elements is deprecated!
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/features/css-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export const hooks = createFeature<{
];
}
}
selectorContext.setCurrentAnchor({ name: node.value, type: 'element', resolved });
selectorContext.setNodeResolve(node, resolved);
selectorContext.setNextSelectorScope(resolved, node, node.value);
// native node does not resolve e.g. div
if (selectorContext.transform && resolved && resolved.length > 1) {
const { symbol, meta } = getOriginDefinition(resolved);
Expand Down
19 changes: 11 additions & 8 deletions packages/core/src/features/st-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ function handleCSSMixin(
const roots = getCSSMixinRoots(
context.meta,
resolveChain,
({ mixinRoot, resolvedClass, isRootMixin }) => {
({ mixinRoot, resolved, isRootMixin }) => {
const stVarOverride = context.evaluator.stVarOverride || {};
const mixDef = config.mixDef;
const namedArgs = mixDef.data.options as Record<string, string>;
Expand All @@ -462,24 +462,27 @@ function handleCSSMixin(
config.cssPropertyMapping
);
collectOptionalArgs(
{ meta: resolvedClass.meta, resolver: context.resolver },
{ meta: resolved.meta, resolver: context.resolver },
mixinRoot,
optionalArgs
);
// transform mixin
const mixinMeta: StylableMeta = resolvedClass.meta;
const mixinMeta: StylableMeta = resolved.meta;
const symbolName =
isRootMixin && resolvedClass.meta !== context.meta ? 'default' : mixDef.data.type;
isRootMixin && resolved.meta !== context.meta ? 'default' : mixDef.data.type;
config.transformer.transformAst(
mixinRoot,
mixinMeta,
undefined,
resolvedArgs,
config.path.concat(symbolName + ' from ' + context.meta.source),
true,
resolvedClass.symbol.name
config.transformer.createInferredSelector(mixinMeta, {
name: resolved.symbol.name,
type: resolved.symbol._kind,
})
);
fixRelativeUrls(mixinRoot, resolvedClass.meta.source, context.meta.source);
fixRelativeUrls(mixinRoot, resolved.meta.source, context.meta.source);
}
);

Expand Down Expand Up @@ -521,7 +524,7 @@ function getCSSMixinRoots(
resolveChain: CSSResolve<ClassSymbol | ElementSymbol>[],
processMixinRoot: (data: {
mixinRoot: postcss.Root;
resolvedClass: CSSResolve;
resolved: CSSResolve<ClassSymbol | ElementSymbol>;
isRootMixin: boolean;
}) => void
) {
Expand All @@ -535,7 +538,7 @@ function getCSSMixinRoots(
isRootMixin,
(name) => STCustomSelector.getCustomSelector(contextMeta, name)
);
processMixinRoot({ mixinRoot, resolvedClass: resolved, isRootMixin });
processMixinRoot({ mixinRoot, resolved, isRootMixin });
roots.push(mixinRoot);
if (resolved.symbol[`-st-extends`]) {
break;
Expand Down
Loading

0 comments on commit 2cf5cca

Please sign in to comment.