Skip to content

Commit

Permalink
feat!: strict class -st-global mapping (#2910)
Browse files Browse the repository at this point in the history
* feat!: disallow complex global mapping
* feat!: no transform or global for multi selector
  • Loading branch information
idoros authored Oct 1, 2023
1 parent 0a5facd commit 2d2e5b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
15 changes: 15 additions & 0 deletions packages/core/src/features/css-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ export const diagnostics = {
'error',
() => `unsupported multi selector in -st-global`
),
UNSUPPORTED_COMPLEX_SELECTOR: createDiagnosticReporter(
'00010',
'error',
() => `unsupported complex selector`
),
IMPORT_ISNT_EXTENDABLE: createDiagnosticReporter(
'00005',
'error',
Expand Down Expand Up @@ -663,6 +668,16 @@ function parseStGlobal(
context.diagnostics.report(diagnostics.UNSUPPORTED_MULTI_SELECTORS_ST_GLOBAL(), {
node: decl,
});
return;
} else {
for (const node of selector[0].nodes) {
if (node.type === 'combinator') {
context.diagnostics.report(diagnostics.UNSUPPORTED_COMPLEX_SELECTOR(), {
node: decl,
});
return;
}
}
}
return selector[0].nodes;
}
25 changes: 10 additions & 15 deletions packages/core/test/features/css-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ describe(`features/css-class`, () => {
/* @transform-remove */
-st-global: "[attr=val]";
}
/* @rule(complex) .y .z */
.d {
/* @transform-remove */
-st-global: ".y .z";
}
/* @rule(not only classes compound) .yy[attr] */
.e {
Expand All @@ -176,9 +170,6 @@ describe(`features/css-class`, () => {
expect(CSSClass.get(meta, `c`), `c symbol`).to.contain({
name: 'c',
});
expect(CSSClass.get(meta, `d`), `d symbol`).to.contain({
name: 'd',
});
expect(CSSClass.get(meta, `e`), `e symbol`).to.contain({
name: 'e',
});
Expand All @@ -188,7 +179,6 @@ describe(`features/css-class`, () => {
x: true,
z: true,
zz: true,
y: true,
yy: true,
});

Expand All @@ -208,23 +198,28 @@ describe(`features/css-class`, () => {
-st-global: "";
}
/* @rule(empty) .y */
/* @rule(multi) .entry__b */
.b {
/* @analyze-error(multi) ${classDiagnostics.UNSUPPORTED_MULTI_SELECTORS_ST_GLOBAL()} */
-st-global: ".y , .z";
}
/* @rule(complex) .entry__c */
.c {
/* @analyze-error(complex) ${classDiagnostics.UNSUPPORTED_COMPLEX_SELECTOR()} */
-st-global: ".y .z";
}
`);

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

// meta.globals
expect(meta.globals).to.eql({
y: true,
});
expect(meta.globals).to.eql({});

// JS exports
expect(exports.classes.a, `a (empty) JS export`).to.eql(`entry__a`);
expect(exports.classes.b, `b (multi) JS export`).to.eql(`y`);
expect(exports.classes.b, `b (multi) JS export`).to.eql(`entry__b`);
expect(exports.classes.c, `c (complex) JS export`).to.eql(`entry__c`);
});
it(`should escape`, () => {
const { sheets } = testStylableCore(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/stylable-transformer/global.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Stylable postcss transform (Global)', () => {
@st-import [test] from './imported.st.css';
.root {}
.test {}
.x { -st-global: '.a .b'; }
.x { -st-global: '.a.b'; }
:global(.c .d) {}
:global(.e) {}
`,
Expand All @@ -38,7 +38,7 @@ describe('Stylable postcss transform (Global)', () => {
e: true,
});
expect((meta.targetAst!.nodes[1] as postcss.Rule).selector).to.equal('.global-test');
expect((meta.targetAst!.nodes[2] as postcss.Rule).selector).to.equal('.a .b');
expect((meta.targetAst!.nodes[2] as postcss.Rule).selector).to.equal('.a.b');
expect((meta.targetAst!.nodes[3] as postcss.Rule).selector).to.equal('.c .d');
expect((meta.targetAst!.nodes[4] as postcss.Rule).selector).to.equal('.e');
});
Expand Down

0 comments on commit 2d2e5b4

Please sign in to comment.