Skip to content

Commit

Permalink
fix: collection if mixin declaration under rule block + more (#2871)
Browse files Browse the repository at this point in the history
  • Loading branch information
barak007 authored Jun 14, 2023
1 parent e0c6bd9 commit 6039655
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/features/st-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,23 @@ function collectRuleMixins(
const resolvedSymbols = context.getResolvedSymbols(context.meta);
const { mainNamespace } = resolvedSymbols;
const decls: postcss.Declaration[] = [];
rule.walkDecls((decl) => {
if (decl.prop === `-st-mixin` || decl.prop === `-st-partial-mixin`) {
decls.push(decl);
for (const node of rule.nodes) {
if (
node.type === 'decl' &&
(node.prop === `-st-mixin` || node.prop === `-st-partial-mixin`)
) {
decls.push(node);
mixins = collectDeclMixins(
context,
resolvedSymbols,
decl,
node,
(mixinSymbolName) => {
return mainNamespace[mixinSymbolName] === 'js' ? 'args' : 'named';
},
mixins
);
}
});
}
return [decls, mixins];
}

Expand Down
13 changes: 6 additions & 7 deletions packages/core/src/helpers/custom-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,23 +724,22 @@ export function validateRuleStateDefinition(
!!stateParam.defaultValue
);
if (errors) {
selectorNode.walkDecls((decl) => {
if (decl.prop === `-st-states`) {
for (const node of selectorNode.nodes) {
if (node.type === 'decl' && node.prop === `-st-states`) {
diagnostics.report(
stateDiagnostics.DEFAULT_PARAM_FAILS_VALIDATION(
stateName,
stateParam.defaultValue || '',
errors
),
{
node: decl,
word: decl.value,
node: node,
word: node.value,
}
);
return false;
break;
}
return;
});
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/helpers/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,18 @@ function createPseudoImportProps(
function patchDecls(node: Rule, named: string[], pseudoImport: Imported) {
const namedDecls: Declaration[] = [];
const defaultDecls: Declaration[] = [];
node.walkDecls((decl) => {
for (const decl of node.nodes) {
if (decl.type !== 'decl') {
continue;
}
if (decl.prop === '-st-named') {
decl.assign({ value: named.join(', ') });
namedDecls.push(decl);
} else if (decl.prop === '-st-default') {
decl.assign({ value: pseudoImport.defaultExport });
defaultDecls.push(decl);
}
});
}
return { defaultDecls, namedDecls };
}

Expand Down
56 changes: 56 additions & 0 deletions packages/core/test/features/st-mixin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,62 @@ describe(`features/st-mixin`, () => {

shouldReportNoDiagnostics(meta);
});
it(`should append mixin declarations (within nesting)`, () => {
const { sheets } = testStylableCore(`
.mix {
propA: blue;
propB: green;
}
/* @rule .entry__empty {propA: blue; propB: green;} */
.empty {
-st-mixin: mix;
}
.insert {
/* @rule .entry__child {before:1; propA:blue; propB:green; after: 2} */
.child {
before: 1;
-st-mixin: mix;
after: 2;
}
}
`);

const { meta } = sheets['/entry.st.css'];

shouldReportNoDiagnostics(meta);
});
it.skip(`should keep other nested rules`, () => {
const { sheets } = testStylableCore(`
.mix {
propA: blue;
propB: green;
}
/* @rule .entry__empty {propA: blue; propB: green;} */
.empty {
-st-mixin: mix;
}
.insert {
/* @rule .entry__child {before:1; propA:blue; propB:green; after: 2} */
.child {
.grandchild-1 { z-index: 1; }
before: 1;
.grandchild-2 { z-index: 2; }
-st-mixin: mix;
.grandchild-3 { z-index: 3; }
after: 2;
.grandchild-4 { z-index: 4; }
}
}
`);

const { meta } = sheets['/entry.st.css'];

shouldReportNoDiagnostics(meta);
});
it(`should append mixin rules`, () => {
const { sheets } = testStylableCore(`
.mix {
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-extensions/src/create-metadata-stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export function rewriteImports(
);
}
if (rawRule.type === 'rule') {
rawRule.walkDecls((decl) => {
if (decl.prop === `-st-from`) {
rawRule.nodes.forEach((decl) => {
if (decl.type === 'decl' && decl.prop === `-st-from`) {
decl.value = JSON.stringify(
`/${ensureHash(resolved.meta, hashes)}.st.css`
);
Expand Down

0 comments on commit 6039655

Please sign in to comment.