Skip to content

Commit

Permalink
babel-make-styles: Correctly handling sequence expressions (#21010)
Browse files Browse the repository at this point in the history
* babel-make-styles: Correctly handling sequence expressions.

* Change files

* Update packages/babel-make-styles/src/plugin.ts

Co-authored-by: Oleksandr Fediashov <[email protected]>

Co-authored-by: Oleksandr Fediashov <[email protected]>
  • Loading branch information
khmakoto and layershifter authored Dec 14, 2021
1 parent bd40a6e commit 9498e23
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
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

0 comments on commit 9498e23

Please sign in to comment.