|
| 1 | +import type { NodePath } from '@babel/core'; |
| 2 | +import * as t from '@babel/types'; |
| 3 | + |
| 4 | +import type { Metadata } from '../types'; |
| 5 | +import { buildCodeFrameError } from '../utils/ast'; |
| 6 | +import { buildCss } from '../utils/css-builders'; |
| 7 | +import { transformCssItems } from '../utils/transform-css-items'; |
| 8 | + |
| 9 | +// The messages are exported for testing. |
| 10 | +export enum ErrorMessages { |
| 11 | + NO_TAGGED_TEMPLATE = 'cssMap function cannot be used as a tagged template expression.', |
| 12 | + NUMBER_OF_ARGUMENT = 'cssMap function can only receive one argument.', |
| 13 | + ARGUMENT_TYPE = 'cssMap function can only receive an object.', |
| 14 | + DEFINE_MAP = 'CSS Map must be declared at the top-most scope of the module.', |
| 15 | + NO_SPREAD_ELEMENT = 'Spread element is not supported in CSS Map.', |
| 16 | + NO_OBJECT_METHOD = 'Object method is not supported in CSS Map.', |
| 17 | + STATIC_VARIANT_OBJECT = 'The variant object must be statically defined.', |
| 18 | +} |
| 19 | + |
| 20 | +const createErrorMessage = (message: string): string => { |
| 21 | + return ` |
| 22 | +${message} |
| 23 | +To correctly implement a CSS Map, follow the syntax below: |
| 24 | +
|
| 25 | +\`\`\` |
| 26 | +import { css, cssMap } from '@compiled/react'; |
| 27 | +const borderStyleMap = cssMap({ |
| 28 | + none: { borderStyle: 'none' }, |
| 29 | + solid: { borderStyle: 'solid' }, |
| 30 | +}); |
| 31 | +const Component = ({ borderStyle }) => <div css={css(borderStyleMap[borderStyle])} /> |
| 32 | +\`\`\` |
| 33 | + `; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Takes `cssMap` function expression and then transforms it to a record of class names and sheets. |
| 38 | + * |
| 39 | + * For example: |
| 40 | + * ``` |
| 41 | + * const styles = cssMap({ |
| 42 | + * none: { color: 'red' }, |
| 43 | + * solid: { color: 'green' }, |
| 44 | + * }); |
| 45 | + * ``` |
| 46 | + * gets transformed to |
| 47 | + * ``` |
| 48 | + * const styles = { |
| 49 | + * danger: "_syaz5scu", |
| 50 | + * success: "_syazbf54", |
| 51 | + * }; |
| 52 | + * ``` |
| 53 | + * |
| 54 | + * @param path {NodePath} The path to be evaluated. |
| 55 | + * @param meta {Metadata} Useful metadata that can be used during the transformation |
| 56 | + */ |
| 57 | +export const visitCssMapPath = ( |
| 58 | + path: NodePath<t.CallExpression> | NodePath<t.TaggedTemplateExpression>, |
| 59 | + meta: Metadata |
| 60 | +): void => { |
| 61 | + // We don't support tagged template expressions. |
| 62 | + if (t.isTaggedTemplateExpression(path.node)) { |
| 63 | + throw buildCodeFrameError( |
| 64 | + createErrorMessage(ErrorMessages.DEFINE_MAP), |
| 65 | + path.node, |
| 66 | + meta.parentPath |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // We need to ensure CSS Map is declared at the top-most scope of the module. |
| 71 | + if (!t.isVariableDeclarator(path.parent) || !t.isIdentifier(path.parent.id)) { |
| 72 | + throw buildCodeFrameError( |
| 73 | + createErrorMessage(ErrorMessages.DEFINE_MAP), |
| 74 | + path.node, |
| 75 | + meta.parentPath |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + // We need to ensure cssMap receives only one argument. |
| 80 | + if (path.node.arguments.length !== 1) { |
| 81 | + throw buildCodeFrameError( |
| 82 | + createErrorMessage(ErrorMessages.NUMBER_OF_ARGUMENT), |
| 83 | + path.node, |
| 84 | + meta.parentPath |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + // We need to ensure the argument is an objectExpression. |
| 89 | + if (!t.isObjectExpression(path.node.arguments[0])) { |
| 90 | + throw buildCodeFrameError( |
| 91 | + createErrorMessage(ErrorMessages.ARGUMENT_TYPE), |
| 92 | + path.node, |
| 93 | + meta.parentPath |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + const totalSheets: string[] = []; |
| 98 | + path.replaceWith( |
| 99 | + t.objectExpression( |
| 100 | + path.node.arguments[0].properties.map((property) => { |
| 101 | + if (t.isSpreadElement(property)) { |
| 102 | + throw buildCodeFrameError( |
| 103 | + createErrorMessage(ErrorMessages.NO_SPREAD_ELEMENT), |
| 104 | + property.argument, |
| 105 | + meta.parentPath |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + if (t.isObjectMethod(property)) { |
| 110 | + throw buildCodeFrameError( |
| 111 | + createErrorMessage(ErrorMessages.NO_OBJECT_METHOD), |
| 112 | + property.key, |
| 113 | + meta.parentPath |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + if (!t.isObjectExpression(property.value)) { |
| 118 | + throw buildCodeFrameError( |
| 119 | + createErrorMessage(ErrorMessages.STATIC_VARIANT_OBJECT), |
| 120 | + property.value, |
| 121 | + meta.parentPath |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + const { css, variables } = buildCss(property.value, meta); |
| 126 | + |
| 127 | + if (variables.length) { |
| 128 | + throw buildCodeFrameError( |
| 129 | + createErrorMessage(ErrorMessages.STATIC_VARIANT_OBJECT), |
| 130 | + property.value, |
| 131 | + meta.parentPath |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + const { sheets, classNames } = transformCssItems(css, meta); |
| 136 | + totalSheets.push(...sheets); |
| 137 | + |
| 138 | + if (classNames.length !== 1) { |
| 139 | + throw buildCodeFrameError( |
| 140 | + createErrorMessage(ErrorMessages.STATIC_VARIANT_OBJECT), |
| 141 | + property, |
| 142 | + meta.parentPath |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + return t.objectProperty(property.key, classNames[0]); |
| 147 | + }) |
| 148 | + ) |
| 149 | + ); |
| 150 | + |
| 151 | + // We store sheets in the meta state so that we can use it later to generate Compiled component. |
| 152 | + meta.state.cssMap[path.parent.id.name] = totalSheets; |
| 153 | +}; |
0 commit comments