Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(makeStyles): correctly handle computed keys #20969

Merged
merged 6 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fix: handle computed keys on objects",
"packageName": "@fluentui/babel-make-styles",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { makeStyles } from '@fluentui/react-make-styles';
import { className, color, selector } from './consts';

export const useStyles = makeStyles({
root: {
[selector]: () => ({ [color]: 'red' }),
[`& .${className}`]: () => ({ color: 'blue' }),
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const color = 'color';
export const className = 'component-foo';
export const selector = '& .component-bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { __styles } from '@fluentui/react-make-styles';
import { className, color, selector } from './consts';
export const useStyles = __styles(
{
root: {
B0egftl: 'f1wgwx3x',
qhv8v2: 'fglt6ox',
},
},
{
d: ['.f1wgwx3x .component-bar{color:red;}', '.fglt6ox .component-foo{color:blue;}'],
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { makeStyles } from '@fluentui/react-make-styles';
import { className, selector } from './consts';

export const useStyles = makeStyles({
root: {
[selector]: { color: 'red' },
[`& .${className}`]: { color: 'blue' },
Copy link
Member

@ling1726 ling1726 Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a case where the computation is a function call ?

also what happens if the result of the computation is undefined or null ? should we throw ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a case where the computation is a function call ?

I added fixtures for function, that's fun as #20918 will remove it :)

also what happens if the result of the computation is undefined or null ? should we throw ?

It's tricky as in runtime there will be no errors 🙄

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I actually meant something along the lines of:

{ [generateKey()]: value}

},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const className = 'component-foo';
export const selector = '& .component-bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { __styles } from '@fluentui/react-make-styles';
import { className, selector } from './consts';
export const useStyles = __styles(
{
root: {
B0egftl: 'f1wgwx3x',
qhv8v2: 'fglt6ox',
},
},
{
d: ['.f1wgwx3x .component-bar{color:red;}', '.fglt6ox .component-foo{color:blue;}'],
},
);
14 changes: 13 additions & 1 deletion packages/babel-make-styles/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,20 @@ function processDefinitions(
}

if (propertyPath.isObjectProperty()) {
const keyPath = propertyPath.get('key');
const valuePath = propertyPath.get('value');

/**
* Computed properties may require lazy evaluation.
*
* @example
* makeStyles({ [var]: { color: 'red' } })
* makeStyles({ [`${var}`]: { color: SOME_VARIABLE } })
*/
if (propertyPath.node.computed) {
lazyPaths.push(keyPath);
}

if (valuePath.isStringLiteral() || valuePath.isNullLiteral() || valuePath.isNumericLiteral()) {
return;
}
Expand Down Expand Up @@ -315,7 +327,7 @@ function processDefinitions(
return;
}

// This condition resolves "theme.alias.color.green.foreground1" to CSS variable
// This condition resolves "theme.aliasColorGreenForeground1" to CSS variable
if (valuePath.isMemberExpression()) {
const identifierPath = getMemberExpressionIdentifier(valuePath);
const paramsName = paramsPath?.node.name;
Expand Down