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

babel-make-styles: Correctly handling sequence expressions #21010

Merged
merged 3 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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": "babel-make-styles: Correctly handling sequence expressions.",
"packageName": "@fluentui/babel-make-styles",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { makeStyles, MakeStylesStyle } from '@fluentui/react-make-styles';

const switchClassName = 'fui-Switch';
let _a: Record<string, MakeStylesStyle>;

export const useStyles = makeStyles({
root:
((_a = {}),
(_a[':hover .' + switchClassName] = {
':before': {
backgroundColor: 'red',
},
}),
_a),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { __styles, MakeStylesStyle } from '@fluentui/react-make-styles';
const switchClassName = 'fui-Switch';

let _a: Record<string, MakeStylesStyle>;

export const useStyles = __styles(
{
root: {
ozcac4: 'f1cm6cy7',
},
},
{
h: ['.f1cm6cy7:hover .fui-Switch:before{background-color:red;}'],
},
);
18 changes: 17 additions & 1 deletion packages/babel-make-styles/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type AstStyleNode =
}
| { kind: 'LAZY_FUNCTION'; nodePath: NodePath<t.ArrowFunctionExpression | t.FunctionExpression> }
| { kind: 'LAZY_EXPRESSION_CALL'; nodePath: NodePath<t.CallExpression> }
| { kind: 'LAZY_SEQUENCE_EXPRESSION'; nodePath: NodePath<t.SequenceExpression> }
| { kind: 'LAZY_MEMBER'; nodePath: NodePath<t.MemberExpression> }
| { kind: 'LAZY_IDENTIFIER'; nodePath: NodePath<t.Identifier> }
| { kind: 'SPREAD'; nodePath: NodePath<t.SpreadElement>; spreadPath: NodePath<t.SpreadElement> };
Expand Down Expand Up @@ -434,6 +435,20 @@ function processDefinitions(
state.styleNodes?.push({ kind: 'LAZY_MEMBER', nodePath: stylesPath });
return;
}

/**
* A scenario when slots styles are represented by a sequence expression.
*
* @example
* // ❌ lazy evaluation
* makeStyles({ root: (_a: { color: 'red' }, _a.backgroundColor: 'blue', _a ) })
* makeStyles({ root: (_a: { color: SOME_VARIABLE }, _a.backgroundColor: 'blue', _a) })
* makeStyles({ root: (_a: { color: 'red' }, _a[`. ${some_className}`]: { color: 'blue' }, _a) })
*/
if (stylesPath.isSequenceExpression()) {
state.styleNodes?.push({ kind: 'LAZY_SEQUENCE_EXPRESSION', nodePath: stylesPath });
return;
}
}

throw styleSlotPath.buildCodeFrameError(UNHANDLED_CASE_ERROR);
Expand Down Expand Up @@ -504,7 +519,8 @@ export const plugin = declare<Partial<BabelPluginOptions>, PluginObj<BabelPlugin
styleNode.kind === 'LAZY_IDENTIFIER' ||
styleNode.kind === 'LAZY_FUNCTION' ||
styleNode.kind === 'LAZY_MEMBER' ||
styleNode.kind === 'LAZY_EXPRESSION_CALL'
styleNode.kind === 'LAZY_EXPRESSION_CALL' ||
styleNode.kind === 'LAZY_SEQUENCE_EXPRESSION'
) {
return [...acc, styleNode.nodePath];
}
Expand Down