Skip to content

Commit

Permalink
Fix nested types output from createStrictAPI() being unintentionally …
Browse files Browse the repository at this point in the history
…marked as required (#1604)

* fix: nested types being unintentionally marked as required

* fix: css map throwing invariant when no styles
  • Loading branch information
itsdouges authored Dec 22, 2023
1 parent da0c07e commit 39714ae
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-toys-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compiled/babel-plugin': patch
---

cssMap() no longer throws an invariant if no styles were generated.
5 changes: 5 additions & 0 deletions .changeset/sour-nails-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compiled/react': patch
---

Fix nested types being forcibly required.
4 changes: 2 additions & 2 deletions packages/babel-plugin/src/css-map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ export const visitCssMapPath = (
const { sheets, classNames } = transformCssItems(css, meta);
totalSheets.push(...sheets);

if (classNames.length !== 1) {
if (classNames.length > 1) {
throw buildCodeFrameError(
createErrorMessage(ErrorMessages.STATIC_VARIANT_OBJECT),
property,
meta.parentPath
);
}

return t.objectProperty(property.key, classNames[0]);
return t.objectProperty(property.key, classNames[0] || t.stringLiteral(''));
})
)
);
Expand Down
52 changes: 52 additions & 0 deletions packages/react/src/create-strict-api/__tests__/generics.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,58 @@ import type { XCSSProp } from './__fixtures__/strict-api-recursive';
import { css, cssMap } from './__fixtures__/strict-api-recursive';

describe('createStrictAPI()', () => {
it('should mark all styles as optional in css()', () => {
const styles = css({
'&:hover': {},
'&:active': {},
'&::before': {},
'&::after': {},
});

const { getByTestId } = render(<div css={styles} data-testid="div" />);

expect(getByTestId('div')).toBeDefined();
});

it('should mark all styles as optional in cssMap()', () => {
const styles = cssMap({
nested: {
'&:hover': {},
'&:active': {},
'&::before': {},
'&::after': {},
},
});

const { getByTestId } = render(<div css={styles.nested} data-testid="div" />);

expect(getByTestId('div')).toBeDefined();
});

it('should mark all styles as optional in xcss prop', () => {
function Component({
xcss,
}: {
xcss: ReturnType<
typeof XCSSProp<
'backgroundColor' | 'color',
'&:hover' | '&:active' | '&::before' | '&::after'
>
>;
}) {
return <div data-testid="div" className={xcss} />;
}

const { getByTestId } = render(
<Component
xcss={{ '&:hover': {}, '&:active': {}, '&::before': {}, '&::after': {} }}
data-testid="div"
/>
);

expect(getByTestId('div')).toBeDefined();
});

describe('type violations', () => {
it('should violate types for css()', () => {
const styles = css({
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/create-strict-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ type PseudosDeclarations = {
[Q in CSSPseudos]?: StrictCSSProperties;
};

type EnforceSchema<TObject> = {
[P in keyof TObject]?: P extends keyof CompiledSchema
? TObject[P] extends Record<string, unknown>
? EnforceSchema<TObject[P]>
: TObject[P]
type EnforceSchema<TSchema> = {
[P in keyof TSchema]?: P extends keyof CompiledSchema
? TSchema[P] extends Record<string, any>
? EnforceSchema<TSchema[P]>
: TSchema[P]
: never;
};

Expand Down

0 comments on commit 39714ae

Please sign in to comment.